File: mbl_data_wrapper_mixer.txx

package info (click to toggle)
vxl 1.17.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 153,280 kB
  • ctags: 105,123
  • sloc: cpp: 747,420; ansic: 209,130; fortran: 34,230; lisp: 14,915; sh: 6,187; python: 5,856; makefile: 340; perl: 294; xml: 160
file content (163 lines) | stat: -rw-r--r-- 4,038 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// This is mul/mbl/mbl_data_wrapper_mixer.txx
#ifndef mbl_data_wrapper_mixer_txx_
#define mbl_data_wrapper_mixer_txx_
//:
// \file

#include "mbl_data_wrapper_mixer.h"

#include <vcl_iostream.h>
#include <vcl_cstdlib.h>
#include <vcl_cassert.h>

//: Default constructor
template<class T>
mbl_data_wrapper_mixer<T>::mbl_data_wrapper_mixer()
  : n_(0),index_(0),current_wrapper_(0)
{
}

//: Constructor
template<class T>
mbl_data_wrapper_mixer<T>::mbl_data_wrapper_mixer(mbl_data_wrapper<T> **wrapper, unsigned long n_wrappers)
{
  set(wrapper,n_wrappers);
}

//: Constructor
template<class T>
mbl_data_wrapper_mixer<T>::mbl_data_wrapper_mixer(vcl_vector<mbl_data_wrapper<T> *> wrapper)
{
  set(&wrapper[0],wrapper.size());
}

//: Constructor
template<class T>
mbl_data_wrapper_mixer<T>::mbl_data_wrapper_mixer(mbl_data_wrapper<T>& wrapper1,
                                                  mbl_data_wrapper<T>& wrapper2)
{
  vcl_vector<mbl_data_wrapper<T>*> w(2);
  w[0] = &wrapper1;
  w[1] = &wrapper2;
  set(&w[0],w.size());
}

//: Initialise to return elements from data[i]
template<class T>
void mbl_data_wrapper_mixer<T>::set(mbl_data_wrapper<T> **wrapper, unsigned long n_wrappers)
{
  assert (n_wrappers != ((unsigned long)-1)); // a common mistake
  wrapper_.resize(n_wrappers);
  n_       = 0;
  for (unsigned long i=0;i<n_wrappers;++i)
  {
    wrapper_[i] = wrapper[i];
    n_ += wrapper[i]->size();
  }
  current_wrapper_ = 0;
  wrapper_[0]->reset();
  set_index(0);
}

//: Default destructor
template<class T>
mbl_data_wrapper_mixer<T>::~mbl_data_wrapper_mixer()
{
}

//: Number of objects available
template<class T>
unsigned long mbl_data_wrapper_mixer<T>::size() const
{
  return n_;
}

//: Reset so that current() returns first object
template<class T>
void mbl_data_wrapper_mixer<T>::reset()
{
  index_=0;
  current_wrapper_=0;
  if (wrapper_.size()>0) wrapper_[0]->reset();
}

//: Return current object
template<class T>
const T& mbl_data_wrapper_mixer<T>::current()
{
  return wrapper_[current_wrapper_]->current();
}

//: Move to next object, returning true if is valid
template<class T>
bool mbl_data_wrapper_mixer<T>::next()
{
  index_++;

  // Increment current and check for end of data in current wrapper
  if (index_<n_ && !wrapper_[current_wrapper_]->next())
  {
    // Find next non-empty wrapper
    if (current_wrapper_<(wrapper_.size()-1)) current_wrapper_++;

    while (current_wrapper_<(wrapper_.size()-1) && wrapper_[current_wrapper_]->size()==0)
      current_wrapper_++;

    wrapper_[current_wrapper_]->reset();
  }

  return index_<n_;
}

//: Return current index
template<class T>
unsigned long mbl_data_wrapper_mixer<T>::index() const
{
  return index_;
}

//: Create copy on heap and return base pointer
template<class T>
mbl_data_wrapper< T >* mbl_data_wrapper_mixer<T>::clone() const
{
  return new mbl_data_wrapper_mixer<T>(*this);
}

//: Move to element n
//  First example has index 0
template<class T>
void mbl_data_wrapper_mixer<T>::set_index(unsigned long n)
{
  assert(n != ((unsigned)-1));
  if (n>=n_)
  {
    vcl_cerr<<"mbl_data_wrapper_mixer<T>::set_index(n) :\n"
            <<"  n = "<<n<<" not in range 0<=n<"<<size()<<vcl_endl;
    vcl_abort();
  }

  index_=n;

  // Determine which wrapper to use
  current_wrapper_=0;
  unsigned long sum_n = wrapper_[0]->size();
  while (index_>=sum_n)
    { current_wrapper_++; sum_n += wrapper_[current_wrapper_]->size(); }
  unsigned long offset = index_ - (sum_n - wrapper_[current_wrapper_]->size());
  wrapper_[current_wrapper_]->set_index(offset);
}

template <class T>
bool mbl_data_wrapper_mixer<T>::is_class(vcl_string const& s) const
{
  return s==is_a(); // no ref to parent's is_class() since that is pure virtual
}


#undef MBL_DATA_WRAPPER_MIXER_INSTANTIATE
#define MBL_DATA_WRAPPER_MIXER_INSTANTIATE(T) \
VCL_DEFINE_SPECIALIZATION vcl_string mbl_data_wrapper_mixer<T >::is_a() const \
{ return vcl_string("mbl_data_wrapper_mixer<" #T ">"); } \
template class mbl_data_wrapper_mixer< T >

#endif // mbl_data_wrapper_mixer_txx_