1 | #include <iostream> |
---|
2 | #include <casa/Utilities/GenSort.h> |
---|
3 | #include <casa/Arrays/Vector.h> |
---|
4 | #include <casa/Arrays/Matrix.h> |
---|
5 | #include <casa/Arrays/ArrayMath.h> |
---|
6 | #include <casa/Arrays/ArrayIO.h> |
---|
7 | #include <tables/Tables/ScalarColumn.h> |
---|
8 | #include "STIdxIter.h" |
---|
9 | |
---|
10 | namespace asap { |
---|
11 | |
---|
12 | IndexIterator::IndexIterator( IPosition &shape ) |
---|
13 | : niter_m( 0 ) |
---|
14 | { |
---|
15 | nfield_m = shape.nelements() ; |
---|
16 | prod_m.resize( nfield_m ) ; |
---|
17 | idx_m.resize( nfield_m ) ; |
---|
18 | prod_m[0] = 1 ; |
---|
19 | idx_m[0] = 0 ; |
---|
20 | for ( uInt i = 1 ; i < nfield_m ; i++ ) { |
---|
21 | prod_m[i] = shape[i-1] * prod_m[i-1] ; |
---|
22 | idx_m[i] = 0 ; |
---|
23 | } |
---|
24 | maxiter_m = prod_m[nfield_m-1] * shape[nfield_m-1] ; |
---|
25 | // cout << "maxiter_m=" << maxiter_m << endl ; |
---|
26 | } |
---|
27 | |
---|
28 | Bool IndexIterator::pastEnd() |
---|
29 | { |
---|
30 | return niter_m >= maxiter_m ; |
---|
31 | } |
---|
32 | |
---|
33 | void IndexIterator::next() |
---|
34 | { |
---|
35 | niter_m++ ; |
---|
36 | uInt x = niter_m ; |
---|
37 | for ( Int i = nfield_m-1 ; i >= 0 ; i-- ) { |
---|
38 | idx_m[i] = x / prod_m[i] ; |
---|
39 | //cout << "i=" << i << ": prod=" << prod_m[i] |
---|
40 | // << ", idx=" << idx_m[i] << endl ; |
---|
41 | x %= prod_m[i] ; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | ArrayIndexIterator::ArrayIndexIterator( Matrix<uInt> &arr, |
---|
46 | vector< vector<uInt> > idlist ) |
---|
47 | : arr_m( arr ), |
---|
48 | pos_m( 1 ) |
---|
49 | { |
---|
50 | ncol_m = arr_m.ncolumn() ; |
---|
51 | nrow_m = arr_m.nrow() ; |
---|
52 | vector< vector<uInt> > l = idlist ; |
---|
53 | if ( l.size() != ncol_m ) { |
---|
54 | l.resize( ncol_m ) ; |
---|
55 | for ( uInt i = 0 ; i < ncol_m ; i++ ) { |
---|
56 | Vector<uInt> a( arr_m.column( i ).copy() ) ; |
---|
57 | uInt n = genSort( a, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ; |
---|
58 | a.resize(n,True) ; |
---|
59 | a.tovector( l[i] ) ; |
---|
60 | } |
---|
61 | } |
---|
62 | idxlist_m = l ; |
---|
63 | IPosition shape( ncol_m ) ; |
---|
64 | for ( uInt i = 0 ; i < ncol_m ; i++ ) { |
---|
65 | shape[i] = idxlist_m[i].size() ; |
---|
66 | } |
---|
67 | // cout << "shape=" << shape << endl ; |
---|
68 | iter_m = new IndexIterator( shape ) ; |
---|
69 | current_m.resize( ncol_m ) ; |
---|
70 | } |
---|
71 | |
---|
72 | ArrayIndexIterator::~ArrayIndexIterator() |
---|
73 | { |
---|
74 | delete iter_m ; |
---|
75 | } |
---|
76 | |
---|
77 | Vector<uInt> ArrayIndexIterator::current() |
---|
78 | { |
---|
79 | Block<uInt> idx = iter_m->current() ; |
---|
80 | for ( uInt i = 0 ; i < ncol_m ; i++ ) |
---|
81 | current_m[i] = idxlist_m[i][idx[i]] ; |
---|
82 | return current_m ; |
---|
83 | } |
---|
84 | |
---|
85 | Bool ArrayIndexIterator::pastEnd() |
---|
86 | { |
---|
87 | return iter_m->pastEnd() ; |
---|
88 | } |
---|
89 | |
---|
90 | ArrayIndexIteratorNormal::ArrayIndexIteratorNormal( Matrix<uInt> &arr, |
---|
91 | vector< vector<uInt> > idlist ) |
---|
92 | : ArrayIndexIterator( arr, idlist ) |
---|
93 | { |
---|
94 | storage_m.resize( nrow_m ) ; |
---|
95 | } |
---|
96 | |
---|
97 | void ArrayIndexIteratorNormal::next() |
---|
98 | { |
---|
99 | iter_m->next() ; |
---|
100 | } |
---|
101 | |
---|
102 | Vector<uInt> ArrayIndexIteratorNormal::getRows( StorageInitPolicy policy ) |
---|
103 | { |
---|
104 | Vector<uInt> v = current() ; |
---|
105 | uInt len = 0 ; |
---|
106 | uInt *p = storage_m.storage() ; |
---|
107 | for ( uInt i = 0 ; i < nrow_m ; i++ ) { |
---|
108 | if ( allEQ( v, arr_m.row( i ) ) ) { |
---|
109 | *p = i ; |
---|
110 | len++ ; |
---|
111 | p++ ; |
---|
112 | } |
---|
113 | } |
---|
114 | pos_m[0] = len ; |
---|
115 | p = storage_m.storage() ; |
---|
116 | return Vector<uInt>( pos_m, p, policy ) ; |
---|
117 | } |
---|
118 | |
---|
119 | ArrayIndexIteratorAcc::ArrayIndexIteratorAcc( Matrix<uInt> &arr, |
---|
120 | vector< vector<uInt> > idlist ) |
---|
121 | : ArrayIndexIterator( arr, idlist ) |
---|
122 | { |
---|
123 | storage_m.resize( arr_m.nelements()+nrow_m ) ; |
---|
124 | uInt *p = storage_m.storage() + arr_m.nelements() ; |
---|
125 | for ( uInt i = 0 ; i < nrow_m ; i++ ) { |
---|
126 | *p = i ; |
---|
127 | p++ ; |
---|
128 | } |
---|
129 | len_m.resize( ncol_m+1 ) ; |
---|
130 | p = len_m.storage() ; |
---|
131 | for ( uInt i = 0 ; i < ncol_m+1 ; i++ ) { |
---|
132 | *p = nrow_m ; |
---|
133 | p++ ; |
---|
134 | // cout << "len[" << i << "]=" << len_m[i] << endl ; |
---|
135 | } |
---|
136 | prev_m = iter_m->current() ; |
---|
137 | p = prev_m.storage() ; |
---|
138 | for ( uInt i = 0 ; i < ncol_m ; i++ ) { |
---|
139 | *p = *p - 1 ; |
---|
140 | p++ ; |
---|
141 | } |
---|
142 | // cout << "prev_m=" << Vector<uInt>(IPosition(1,ncol_m),prev_m.storage()) << endl ; |
---|
143 | skip_m.resize( ncol_m ) ; |
---|
144 | for ( uInt i = 0 ; i < ncol_m ; i++ ) { |
---|
145 | skip_m[i] = (Bool)(idxlist_m[i].size()==1) ; |
---|
146 | // cout << "skip_m[" << i << "]=" << skip_m[i] << endl ; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | void ArrayIndexIteratorAcc::next() |
---|
151 | { |
---|
152 | prev_m = iter_m->current() ; |
---|
153 | iter_m->next() ; |
---|
154 | } |
---|
155 | |
---|
156 | Vector<uInt> ArrayIndexIteratorAcc::getRows( StorageInitPolicy policy ) |
---|
157 | { |
---|
158 | Block<uInt> v = iter_m->current() ; |
---|
159 | Int c = isChanged( v ) ; |
---|
160 | // cout << "c=" << c << endl ; |
---|
161 | if ( c < 0 ) { |
---|
162 | pos_m[0] = len_m[0] ; |
---|
163 | uInt offset = 0 ; |
---|
164 | while( offset <= ncol_m && !(skip_m[offset]) ) |
---|
165 | offset++ ; |
---|
166 | // cout << "offset=" << offset << endl ; |
---|
167 | return Vector<uInt>( pos_m, storage_m.storage()+offset*nrow_m, policy ) ; |
---|
168 | } |
---|
169 | uInt *base = storage_m.storage() + (c+1) * nrow_m ; |
---|
170 | // cout << "len_m[c+1]=" << len_m[c+1] << endl ; |
---|
171 | // cout << "base=" << Vector<uInt>(IPosition(1,len_m[c+1]),base,SHARE) << endl ; |
---|
172 | for ( Int i = c ; i >= 0 ; i-- ) { |
---|
173 | base = updateStorage( i, base, idxlist_m[i][v[i]] ) ; |
---|
174 | // cout << "len_m[" << i << "]=" << len_m[i] << endl ; |
---|
175 | // cout << "base=" << Vector<uInt>(IPosition(1,len_m[i]),base,SHARE) << endl ; |
---|
176 | } |
---|
177 | pos_m[0] = len_m[0] ; |
---|
178 | return Vector<uInt>( pos_m, base, policy ) ; |
---|
179 | } |
---|
180 | |
---|
181 | Int ArrayIndexIteratorAcc::isChanged( Block<uInt> &idx ) |
---|
182 | { |
---|
183 | Int i = ncol_m - 1 ; |
---|
184 | while( i >= 0 && idx[i] == prev_m[i] ) |
---|
185 | i-- ; |
---|
186 | return i ; |
---|
187 | } |
---|
188 | |
---|
189 | uInt *ArrayIndexIteratorAcc::updateStorage( Int &icol, |
---|
190 | uInt *base, |
---|
191 | uInt &v ) |
---|
192 | { |
---|
193 | uInt *p ; |
---|
194 | if ( skip_m[icol] ) { |
---|
195 | // skip update, just update len_m[icol] and pass appropriate pointer |
---|
196 | // cout << "skip " << icol << endl ; |
---|
197 | p = base ; |
---|
198 | len_m[icol] = len_m[icol+1] ; |
---|
199 | } |
---|
200 | else { |
---|
201 | uInt len = 0 ; |
---|
202 | p = storage_m.storage() + icol * nrow_m ; |
---|
203 | uInt *work = p ; |
---|
204 | Bool b ; |
---|
205 | const uInt *arr_p = arr_m.getStorage( b ) ; |
---|
206 | long offset = 0 ; |
---|
207 | // warr_p points a first element of (icol)-th column |
---|
208 | const uInt *warr_p = arr_p + icol * nrow_m ; |
---|
209 | for ( uInt i = 0 ; i < len_m[icol+1] ; i++ ) { |
---|
210 | // increment warr_p by (*(base)-*(base-1)) |
---|
211 | warr_p += *base - offset ; |
---|
212 | // check if target element is equal to value specified |
---|
213 | if ( *warr_p == v ) { |
---|
214 | // then, add current index to storage_m |
---|
215 | // cout << "add " << *base << endl ; |
---|
216 | *work = *base ; |
---|
217 | len++ ; |
---|
218 | work++ ; |
---|
219 | } |
---|
220 | // update offset |
---|
221 | offset = *base ; |
---|
222 | // next index |
---|
223 | base++ ; |
---|
224 | } |
---|
225 | arr_m.freeStorage( arr_p, b ) ; |
---|
226 | len_m[icol] = len ; |
---|
227 | } |
---|
228 | return p ; |
---|
229 | } |
---|
230 | |
---|
231 | STIdxIter::STIdxIter() |
---|
232 | { |
---|
233 | iter_m = 0 ; |
---|
234 | } |
---|
235 | |
---|
236 | STIdxIter::STIdxIter( const string &name, |
---|
237 | const vector<string> &cols ) |
---|
238 | { |
---|
239 | } |
---|
240 | |
---|
241 | STIdxIter::STIdxIter( const CountedPtr<Scantable> &s, |
---|
242 | const vector<string> &cols ) |
---|
243 | { |
---|
244 | } |
---|
245 | |
---|
246 | STIdxIter::~STIdxIter() |
---|
247 | { |
---|
248 | if ( iter_m != 0 ) |
---|
249 | delete iter_m ; |
---|
250 | } |
---|
251 | |
---|
252 | vector<uInt> STIdxIter::tovector( Vector<uInt> v ) |
---|
253 | { |
---|
254 | vector<uInt> ret ; |
---|
255 | v.tovector( ret ) ; |
---|
256 | return ret ; |
---|
257 | } |
---|
258 | |
---|
259 | Vector<uInt> STIdxIter::getRows( StorageInitPolicy policy ) |
---|
260 | { |
---|
261 | return iter_m->getRows( policy ) ; |
---|
262 | } |
---|
263 | |
---|
264 | STIdxIterNormal::STIdxIterNormal() |
---|
265 | : STIdxIter() |
---|
266 | { |
---|
267 | } |
---|
268 | |
---|
269 | STIdxIterNormal::STIdxIterNormal( const string &name, |
---|
270 | const vector<string> &cols ) |
---|
271 | : STIdxIter( name, cols ) |
---|
272 | { |
---|
273 | Table t( name, Table::Old ) ; |
---|
274 | init( t, cols ) ; |
---|
275 | } |
---|
276 | |
---|
277 | STIdxIterNormal::STIdxIterNormal( const CountedPtr<Scantable> &s, |
---|
278 | const vector<string> &cols ) |
---|
279 | : STIdxIter( s, cols ) |
---|
280 | { |
---|
281 | init( s->table(), cols ) ; |
---|
282 | } |
---|
283 | |
---|
284 | STIdxIterNormal::~STIdxIterNormal() |
---|
285 | { |
---|
286 | } |
---|
287 | |
---|
288 | void STIdxIterNormal::init( Table &t, |
---|
289 | const vector<string> &cols ) |
---|
290 | { |
---|
291 | uInt ncol = cols.size() ; |
---|
292 | uInt nrow = t.nrow() ; |
---|
293 | Matrix<uInt> arr( nrow, ncol ) ; |
---|
294 | ROScalarColumn<uInt> col ; |
---|
295 | Vector<uInt> v ; |
---|
296 | for ( uInt i = 0 ; i < ncol ; i++ ) { |
---|
297 | col.attach( t, cols[i] ) ; |
---|
298 | v.reference( arr.column( i ) ) ; |
---|
299 | col.getColumn( v ) ; |
---|
300 | } |
---|
301 | iter_m = new ArrayIndexIteratorNormal( arr ) ; |
---|
302 | } |
---|
303 | |
---|
304 | STIdxIterAcc::STIdxIterAcc() |
---|
305 | : STIdxIter() |
---|
306 | { |
---|
307 | } |
---|
308 | |
---|
309 | STIdxIterAcc::STIdxIterAcc( const string &name, |
---|
310 | const vector<string> &cols ) |
---|
311 | : STIdxIter( name, cols ) |
---|
312 | { |
---|
313 | Table t( name, Table::Old ) ; |
---|
314 | init( t, cols ) ; |
---|
315 | } |
---|
316 | |
---|
317 | STIdxIterAcc::STIdxIterAcc( const CountedPtr<Scantable> &s, |
---|
318 | const vector<string> &cols ) |
---|
319 | : STIdxIter( s, cols ) |
---|
320 | { |
---|
321 | init( s->table(), cols ) ; |
---|
322 | } |
---|
323 | |
---|
324 | STIdxIterAcc::~STIdxIterAcc() |
---|
325 | { |
---|
326 | } |
---|
327 | |
---|
328 | void STIdxIterAcc::init( Table &t, |
---|
329 | const vector<string> &cols ) |
---|
330 | { |
---|
331 | uInt ncol = cols.size() ; |
---|
332 | uInt nrow = t.nrow() ; |
---|
333 | // array shape here is as follows if cols=["BEAMNO","POLNO","IFNO"]: |
---|
334 | // [[B0,B1,B2,...,BN], |
---|
335 | // [P0,P1,P2,...,PN], |
---|
336 | // [I0,I1,I2,...,IN]] |
---|
337 | // order of internal storage is |
---|
338 | // [B0,B1,B2,..,BN,P0,P1,P2,...,PN,I0,I1,I2,...,IN] |
---|
339 | Matrix<uInt> arr( nrow, ncol ) ; |
---|
340 | Vector<uInt> v ; |
---|
341 | ROScalarColumn<uInt> col ; |
---|
342 | for ( uInt i = 0 ; i < ncol ; i++ ) { |
---|
343 | col.attach( t, cols[i] ) ; |
---|
344 | v.reference( arr.column( i ) ) ; |
---|
345 | col.getColumn( v ) ; |
---|
346 | } |
---|
347 | iter_m = new ArrayIndexIteratorAcc( arr ) ; |
---|
348 | } |
---|
349 | |
---|
350 | } // namespace |
---|