1 | #ifndef _ASAP_INDEX_ITERATOR_H_ |
---|
2 | #define _ASAP_INDEX_ITERATOR_H_ |
---|
3 | |
---|
4 | #include <vector> |
---|
5 | #include <casa/Containers/Block.h> |
---|
6 | #include <casa/Arrays/Vector.h> |
---|
7 | #include <casa/Arrays/Matrix.h> |
---|
8 | #include <casa/Arrays/IPosition.h> |
---|
9 | #include <casa/BasicSL/String.h> |
---|
10 | |
---|
11 | #include <casa/Utilities/Sort.h> |
---|
12 | |
---|
13 | #include "Scantable.h" |
---|
14 | |
---|
15 | using namespace std ; |
---|
16 | using namespace casa ; |
---|
17 | |
---|
18 | namespace { |
---|
19 | vector<string> split(const string &str, char delim) |
---|
20 | { |
---|
21 | vector<string> result; |
---|
22 | size_t current = 0; |
---|
23 | size_t found; |
---|
24 | while ((found = str.find_first_of(delim, current)) != string::npos) { |
---|
25 | result.push_back(string(str, current, found - current)); |
---|
26 | current = found + 1; |
---|
27 | } |
---|
28 | result.push_back(string(str, current, str.size() - current)); |
---|
29 | return result; |
---|
30 | } |
---|
31 | } // anonymous namespace |
---|
32 | |
---|
33 | namespace asap { |
---|
34 | class IndexIterator |
---|
35 | { |
---|
36 | public: |
---|
37 | IndexIterator( IPosition &shape ) ; |
---|
38 | Block<uInt> current() { return idx_m ; } ; |
---|
39 | Bool pastEnd() ; |
---|
40 | void next() ; |
---|
41 | private: |
---|
42 | uInt nfield_m ; |
---|
43 | Block<uInt> prod_m ; |
---|
44 | Block<uInt> idx_m ; |
---|
45 | uInt niter_m ; |
---|
46 | uInt maxiter_m ; |
---|
47 | } ; |
---|
48 | |
---|
49 | class ArrayIndexIterator |
---|
50 | { |
---|
51 | public: |
---|
52 | ArrayIndexIterator( Matrix<uInt> &arr, |
---|
53 | vector< vector<uInt> > idlist=vector< vector<uInt> >() ) ; |
---|
54 | virtual ~ArrayIndexIterator() ; |
---|
55 | Vector<uInt> current() ; |
---|
56 | Bool pastEnd() ; |
---|
57 | virtual void next() = 0 ; |
---|
58 | virtual Vector<uInt> getRows( StorageInitPolicy policy=COPY ) = 0 ; |
---|
59 | protected: |
---|
60 | IndexIterator *iter_m ; |
---|
61 | uInt nrow_m ; |
---|
62 | uInt ncol_m ; |
---|
63 | Block<uInt> storage_m ; |
---|
64 | Matrix<uInt> arr_m ; |
---|
65 | IPosition pos_m ; |
---|
66 | Vector<uInt> current_m ; |
---|
67 | vector< vector<uInt> > idxlist_m ; |
---|
68 | } ; |
---|
69 | |
---|
70 | class ArrayIndexIteratorNormal : public ArrayIndexIterator |
---|
71 | { |
---|
72 | public: |
---|
73 | ArrayIndexIteratorNormal( Matrix<uInt> &arr, |
---|
74 | vector< vector<uInt> > idlist=vector< vector<uInt> >() ) ; |
---|
75 | void next() ; |
---|
76 | Vector<uInt> getRows( StorageInitPolicy policy=COPY ) ; |
---|
77 | } ; |
---|
78 | |
---|
79 | class ArrayIndexIteratorAcc : public ArrayIndexIterator |
---|
80 | { |
---|
81 | public: |
---|
82 | ArrayIndexIteratorAcc( Matrix<uInt> &arr, |
---|
83 | vector< vector<uInt> > idlist=vector< vector<uInt> >() ) ; |
---|
84 | void next() ; |
---|
85 | Vector<uInt> getRows( StorageInitPolicy policy=COPY ) ; |
---|
86 | private: |
---|
87 | Int isChanged( Block<uInt> &idx ) ; |
---|
88 | uInt *updateStorage( Int &icol, uInt *base, uInt &v ) ; |
---|
89 | |
---|
90 | Block<uInt> prev_m ; |
---|
91 | Block<uInt> len_m ; |
---|
92 | Block<Bool> skip_m ; |
---|
93 | } ; |
---|
94 | |
---|
95 | class STIdxIter |
---|
96 | { |
---|
97 | public: |
---|
98 | STIdxIter() ; |
---|
99 | STIdxIter( const string &name, |
---|
100 | const vector<string> &cols ) ; |
---|
101 | STIdxIter( const CountedPtr<Scantable> &s, |
---|
102 | const vector<string> &cols ) ; |
---|
103 | virtual ~STIdxIter() ; |
---|
104 | vector<uInt> currentSTL() { return tovector( iter_m->current() ) ; } ; |
---|
105 | Vector<uInt> current() { return iter_m->current() ; } ; |
---|
106 | Bool pastEnd() { return iter_m->pastEnd() ; } ; |
---|
107 | void next() { iter_m->next() ; } ; |
---|
108 | vector<uInt> getRowsSTL() { return tovector( iter_m->getRows() ) ; } ; |
---|
109 | // !!!you should not use policy=TAKE_OVER since it causes problem!!! |
---|
110 | Vector<uInt> getRows( StorageInitPolicy policy=COPY ) ; |
---|
111 | protected: |
---|
112 | ArrayIndexIterator *iter_m ; |
---|
113 | virtual void init( Table &t, |
---|
114 | const vector<string> &cols ) = 0 ; |
---|
115 | private: |
---|
116 | vector<uInt> tovector( Vector<uInt> v ) ; |
---|
117 | } ; |
---|
118 | |
---|
119 | class STIdxIterNormal : public STIdxIter |
---|
120 | { |
---|
121 | public: |
---|
122 | STIdxIterNormal() ; |
---|
123 | STIdxIterNormal( const string &name, |
---|
124 | const vector<string> &cols ) ; |
---|
125 | STIdxIterNormal( const CountedPtr<Scantable> &s, |
---|
126 | const vector<string> &cols ) ; |
---|
127 | ~STIdxIterNormal() ; |
---|
128 | protected: |
---|
129 | void init( Table &t, |
---|
130 | const vector<string> &cols ) ; |
---|
131 | } ; |
---|
132 | |
---|
133 | class STIdxIterAcc : public STIdxIter |
---|
134 | { |
---|
135 | public: |
---|
136 | STIdxIterAcc() ; |
---|
137 | STIdxIterAcc( const string &name, |
---|
138 | const vector<string> &cols ) ; |
---|
139 | STIdxIterAcc( const CountedPtr<Scantable> &s, |
---|
140 | const vector<string> &cols ) ; |
---|
141 | ~STIdxIterAcc() ; |
---|
142 | protected: |
---|
143 | virtual void init( Table &t, |
---|
144 | const vector<string> &cols ) ; |
---|
145 | } ; |
---|
146 | |
---|
147 | class STIdxIterExAcc : public STIdxIter |
---|
148 | { |
---|
149 | public: |
---|
150 | STIdxIterExAcc() ; |
---|
151 | STIdxIterExAcc( const string &name, |
---|
152 | const vector<string> &cols ) ; |
---|
153 | STIdxIterExAcc( const CountedPtr<Scantable> &s, |
---|
154 | const vector<string> &cols ) ; |
---|
155 | ~STIdxIterExAcc() ; |
---|
156 | Int getSrcType() ; |
---|
157 | String getSrcName() ; |
---|
158 | protected: |
---|
159 | virtual void init( Table &t, |
---|
160 | const vector<string> &cols ) ; |
---|
161 | private: |
---|
162 | void processIntCol( Vector<Int> &in, |
---|
163 | Vector<uInt> &out, |
---|
164 | Block<Int> &val ) ; |
---|
165 | void processStrCol( Vector<String> &in, |
---|
166 | Vector<uInt> &out, |
---|
167 | Block<String> &val ) ; |
---|
168 | Block<Int> srctype_m ; |
---|
169 | Block<String> srcname_m ; |
---|
170 | Int srctypeid_m ; |
---|
171 | Int srcnameid_m ; |
---|
172 | } ; |
---|
173 | |
---|
174 | class STIdxIter2 |
---|
175 | { |
---|
176 | public: |
---|
177 | template<class T> |
---|
178 | static void Iterate(T &processor, const string cols_list) |
---|
179 | { |
---|
180 | vector<string> cols = split(cols_list, ','); |
---|
181 | // for (vector<string>::iterator i = cols.begin(); i != cols.end(); ++i) |
---|
182 | // cout << *i << endl; |
---|
183 | STIdxIter2 iter(processor.target(), cols); |
---|
184 | STSelector sel ; |
---|
185 | while ( !iter.pastEnd() ) { |
---|
186 | const Record current = iter.currentValue() ; |
---|
187 | Vector<uInt> rows = iter.getRows( SHARE ) ; |
---|
188 | // any process |
---|
189 | processor.Process(cols, current, rows); |
---|
190 | // go next |
---|
191 | iter.next() ; |
---|
192 | } |
---|
193 | } |
---|
194 | STIdxIter2() ; |
---|
195 | STIdxIter2( const string &name, |
---|
196 | const vector<string> &cols ) ; |
---|
197 | STIdxIter2( const CountedPtr<Scantable> &s, |
---|
198 | const vector<string> &cols ) ; |
---|
199 | virtual ~STIdxIter2() ; |
---|
200 | Record currentValue(); |
---|
201 | Bool pastEnd() ; |
---|
202 | void next() ; |
---|
203 | Vector<uInt> getRows(StorageInitPolicy policy=COPY) ; |
---|
204 | vector<uInt> getRowsSTL() { return tovector( getRows() ) ; } ; |
---|
205 | virtual void init(); |
---|
206 | private: |
---|
207 | vector<uInt> tovector(Vector<uInt> v); |
---|
208 | void addSortKey(const string &name); |
---|
209 | template<class T, DataType U> void addColumnToKey(const string &name); |
---|
210 | void addColumnToKeyTpString(const string &name); |
---|
211 | void deallocate(); |
---|
212 | vector<string> cols_; |
---|
213 | Table table_; |
---|
214 | uInt counter_; |
---|
215 | uInt num_iter_; |
---|
216 | uInt num_row_; |
---|
217 | Sort sorter_; |
---|
218 | Vector<uInt> index_; |
---|
219 | Vector<uInt> unique_; |
---|
220 | vector<void*> pointer_; |
---|
221 | vector<Vector<String> > string_storage_; |
---|
222 | } ; |
---|
223 | |
---|
224 | } // namespace |
---|
225 | #endif /* _ASAP_INDEX_ITERATOR_H_ */ |
---|