source: branches/hpc33/src/STIdxIter.cpp@ 2547

Last change on this file since 2547 was 2547, checked in by Takeshi Nakazato, 12 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Changed an order of iteration for STIdxIter. Now the order is same as
TableIterator (in specified column list for iteration, iterate subsequent
column first).


File size: 14.4 KB
Line 
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
10namespace asap {
11
12IndexIterator::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 prod_m[nfield_m-1] = 1 ;
26 idx_m[nfield_m-1] = 0 ;
27 for ( Int i = nfield_m-2 ; i >= 0 ; i-- ) {
28 prod_m[i] = shape[i+1] * prod_m[i+1] ;
29 idx_m[i] = 0 ;
30 }
31 maxiter_m = prod_m[0] * shape[0] ;
32// cout << "maxiter_m=" << maxiter_m << endl ;
33}
34
35Bool IndexIterator::pastEnd()
36{
37 return niter_m >= maxiter_m ;
38}
39
40void IndexIterator::next()
41{
42 niter_m++ ;
43 uInt x = niter_m ;
44// for ( Int i = nfield_m-1 ; i >= 0 ; i-- ) {
45 for ( Int i = 0 ; i < nfield_m ; i++ ) {
46 idx_m[i] = x / prod_m[i] ;
47 //cout << "i=" << i << ": prod=" << prod_m[i]
48 // << ", idx=" << idx_m[i] << endl ;
49 x %= prod_m[i] ;
50 }
51}
52
53ArrayIndexIterator::ArrayIndexIterator( Matrix<uInt> &arr,
54 vector< vector<uInt> > idlist )
55 : arr_m( arr ),
56 pos_m( 1 )
57{
58 ncol_m = arr_m.ncolumn() ;
59 nrow_m = arr_m.nrow() ;
60 vector< vector<uInt> > l = idlist ;
61 if ( l.size() != ncol_m ) {
62 l.resize( ncol_m ) ;
63 for ( uInt i = 0 ; i < ncol_m ; i++ ) {
64 Vector<uInt> a( arr_m.column( i ).copy() ) ;
65 uInt n = genSort( a, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ;
66 a.resize(n,True) ;
67 a.tovector( l[i] ) ;
68 }
69 }
70 idxlist_m = l ;
71 IPosition shape( ncol_m ) ;
72 for ( uInt i = 0 ; i < ncol_m ; i++ ) {
73 shape[i] = idxlist_m[i].size() ;
74 }
75// cout << "shape=" << shape << endl ;
76 iter_m = new IndexIterator( shape ) ;
77 current_m.resize( ncol_m ) ;
78}
79
80ArrayIndexIterator::~ArrayIndexIterator()
81{
82 delete iter_m ;
83}
84
85Vector<uInt> ArrayIndexIterator::current()
86{
87 Block<uInt> idx = iter_m->current() ;
88 for ( uInt i = 0 ; i < ncol_m ; i++ )
89 current_m[i] = idxlist_m[i][idx[i]] ;
90 return current_m ;
91}
92
93Bool ArrayIndexIterator::pastEnd()
94{
95 return iter_m->pastEnd() ;
96}
97
98ArrayIndexIteratorNormal::ArrayIndexIteratorNormal( Matrix<uInt> &arr,
99 vector< vector<uInt> > idlist )
100 : ArrayIndexIterator( arr, idlist )
101{
102 storage_m.resize( nrow_m ) ;
103}
104
105void ArrayIndexIteratorNormal::next()
106{
107 iter_m->next() ;
108}
109
110Vector<uInt> ArrayIndexIteratorNormal::getRows( StorageInitPolicy policy )
111{
112 Vector<uInt> v = current() ;
113 uInt len = 0 ;
114 uInt *p = storage_m.storage() ;
115 for ( uInt i = 0 ; i < nrow_m ; i++ ) {
116 if ( allEQ( v, arr_m.row( i ) ) ) {
117 *p = i ;
118 len++ ;
119 p++ ;
120 }
121 }
122 pos_m[0] = len ;
123 p = storage_m.storage() ;
124 return Vector<uInt>( pos_m, p, policy ) ;
125}
126
127ArrayIndexIteratorAcc::ArrayIndexIteratorAcc( Matrix<uInt> &arr,
128 vector< vector<uInt> > idlist )
129 : ArrayIndexIterator( arr, idlist )
130{
131 // storage_m layout
132 // length: ncol_m * (nrow_m + 1)
133 // 0~nrow_m-1: constant temporary storage that indicates whole rows
134 // nrow_m~2*nrow_m-1: temporary storage for column 0
135 // 2*nrow_m~3*nrow_m-1: temporary storage for column 1
136 // ...
137 storage_m.resize( arr_m.nelements()+nrow_m ) ;
138// uInt *p = storage_m.storage() + arr_m.nelements() ;
139 uInt *p = storage_m.storage() ;
140 for ( uInt i = 0 ; i < nrow_m ; i++ ) {
141 *p = i ;
142 p++ ;
143 }
144 len_m.resize( ncol_m+1 ) ;
145 p = len_m.storage() ;
146 for ( uInt i = 0 ; i < ncol_m+1 ; i++ ) {
147 *p = nrow_m ;
148 p++ ;
149// cout << "len[" << i << "]=" << len_m[i] << endl ;
150 }
151 prev_m = iter_m->current() ;
152 p = prev_m.storage() ;
153 for ( uInt i = 0 ; i < ncol_m ; i++ ) {
154 *p = *p - 1 ;
155 p++ ;
156 }
157// cout << "prev_m=" << Vector<uInt>(IPosition(1,ncol_m),prev_m.storage()) << endl ;
158 skip_m.resize( ncol_m ) ;
159 for ( uInt i = 0 ; i < ncol_m ; i++ ) {
160 skip_m[i] = (Bool)(idxlist_m[i].size()==1) ;
161// cout << "skip_m[" << i << "]=" << skip_m[i] << endl ;
162 }
163}
164
165void ArrayIndexIteratorAcc::next()
166{
167 prev_m = iter_m->current() ;
168 iter_m->next() ;
169}
170
171Vector<uInt> ArrayIndexIteratorAcc::getRows( StorageInitPolicy policy )
172{
173 Block<uInt> v = iter_m->current() ;
174 Int c = isChanged( v ) ;
175 cout << "v=" << Vector<uInt>(IPosition(1,v.nelements()),v.storage(),SHARE) << endl ;
176 cout << "c=" << c << endl ;
177// if ( c < 0 ) {
178 if ( c > ncol_m-1 ) {
179// pos_m[0] = len_m[0] ;
180// uInt offset = 0 ;
181// while( offset < ncol_m && skip_m[offset] )
182// offset++ ;
183 pos_m[0] = len_m[ncol_m] ;
184 Int offset = ncol_m - 1 ;
185 while( offset >= 0 && skip_m[offset] )
186 offset-- ;
187 offset++ ;
188// cout << "offset=" << offset << endl ;
189 return Vector<uInt>( pos_m, storage_m.storage()+offset*nrow_m, policy ) ;
190 }
191// Int offset = c + 1 ;
192// while( offset < ncol_m && skip_m[offset] )
193// offset++ ;
194 Int offset = c - 1 ;
195 while( offset >= 0 && skip_m[offset] )
196 offset-- ;
197 offset++ ;
198 cout << "offset = " << offset << endl ;
199 uInt *base = storage_m.storage() + offset * nrow_m ;
200// cout << "len_m[c+1]=" << len_m[c+1] << endl ;
201// cout << "base=" << Vector<uInt>(IPosition(1,len_m[c+1]),base,SHARE) << endl ;
202// for ( Int i = c ; i >= 0 ; i-- ) {
203// base = updateStorage( i, base, idxlist_m[i][v[i]] ) ;
204 for ( Int i = c+1 ; i <= ncol_m ; i++ ) {
205 base = updateStorage( i, base, idxlist_m[i-1][v[i-1]] ) ;
206 cout << "len_m[" << i << "]=" << len_m[i] << endl ;
207// cout << "base=" << Vector<uInt>(IPosition(1,len_m[i]),base,SHARE) << endl ;
208 }
209// pos_m[0] = len_m[0] ;
210 pos_m[0] = len_m[ncol_m] ;
211// cout << "pos_m=" << pos_m << endl ;
212 cout << "ret=" << Vector<uInt>( pos_m, base, policy ) << endl ;
213 return Vector<uInt>( pos_m, base, policy ) ;
214}
215
216Int ArrayIndexIteratorAcc::isChanged( Block<uInt> &idx )
217{
218// Int i = ncol_m - 1 ;
219// while( i >= 0 && idx[i] == prev_m[i] )
220// i-- ;
221 Int i = 0 ;
222 while( i < ncol_m && idx[i] == prev_m[i] )
223 i++ ;
224 return i ;
225}
226
227uInt *ArrayIndexIteratorAcc::updateStorage( Int &icol,
228 uInt *base,
229 uInt &v )
230{
231 uInt *p ;
232// if ( skip_m[icol] ) {
233 if ( skip_m[icol-1] ) {
234 // skip update, just update len_m[icol] and pass appropriate pointer
235// cout << "skip " << icol << endl ;
236 p = base ;
237// len_m[icol] = len_m[icol+1] ;
238 len_m[icol] = len_m[icol-1] ;
239 }
240 else {
241// cout << "update " << icol << endl ;
242 uInt len = 0 ;
243 p = storage_m.storage() + icol * nrow_m ;
244 uInt *work = p ;
245 Bool b ;
246 const uInt *arr_p = arr_m.getStorage( b ) ;
247 long offset = 0 ;
248 // warr_p points a first element of (icol)-th column
249// const uInt *warr_p = arr_p + icol * nrow_m ;
250 const uInt *warr_p = arr_p + (icol-1) * nrow_m ;
251// for ( uInt i = 0 ; i < len_m[icol+1] ; i++ ) {
252 for ( uInt i = 0 ; i < len_m[icol-1] ; i++ ) {
253 // increment warr_p by (*(base)-*(base-1))
254 warr_p += *base - offset ;
255 // check if target element is equal to value specified
256 if ( *warr_p == v ) {
257 // then, add current index to storage_m
258 // cout << "add " << *base << endl ;
259 *work = *base ;
260 len++ ;
261 work++ ;
262 }
263 // update offset
264 offset = *base ;
265 // next index
266 base++ ;
267 }
268 arr_m.freeStorage( arr_p, b ) ;
269 len_m[icol] = len ;
270 }
271 return p ;
272}
273
274STIdxIter::STIdxIter()
275{
276 iter_m = 0 ;
277}
278
279STIdxIter::STIdxIter( const string &name,
280 const vector<string> &cols )
281{
282}
283
284STIdxIter::STIdxIter( const CountedPtr<Scantable> &s,
285 const vector<string> &cols )
286{
287}
288
289STIdxIter::~STIdxIter()
290{
291 if ( iter_m != 0 )
292 delete iter_m ;
293}
294
295vector<uInt> STIdxIter::tovector( Vector<uInt> v )
296{
297 vector<uInt> ret ;
298 v.tovector( ret ) ;
299 return ret ;
300}
301
302Vector<uInt> STIdxIter::getRows( StorageInitPolicy policy )
303{
304 return iter_m->getRows( policy ) ;
305}
306
307STIdxIterNormal::STIdxIterNormal()
308 : STIdxIter()
309{
310}
311
312STIdxIterNormal::STIdxIterNormal( const string &name,
313 const vector<string> &cols )
314 : STIdxIter( name, cols )
315{
316 Table t( name, Table::Old ) ;
317 init( t, cols ) ;
318}
319
320STIdxIterNormal::STIdxIterNormal( const CountedPtr<Scantable> &s,
321 const vector<string> &cols )
322 : STIdxIter( s, cols )
323{
324 init( s->table(), cols ) ;
325}
326
327STIdxIterNormal::~STIdxIterNormal()
328{
329}
330
331void STIdxIterNormal::init( Table &t,
332 const vector<string> &cols )
333{
334 uInt ncol = cols.size() ;
335 uInt nrow = t.nrow() ;
336 Matrix<uInt> arr( nrow, ncol ) ;
337 ROScalarColumn<uInt> col ;
338 Vector<uInt> v ;
339 for ( uInt i = 0 ; i < ncol ; i++ ) {
340 col.attach( t, cols[i] ) ;
341 v.reference( arr.column( i ) ) ;
342 col.getColumn( v ) ;
343 }
344 iter_m = new ArrayIndexIteratorNormal( arr ) ;
345}
346
347STIdxIterAcc::STIdxIterAcc()
348 : STIdxIter()
349{
350}
351
352STIdxIterAcc::STIdxIterAcc( const string &name,
353 const vector<string> &cols )
354 : STIdxIter( name, cols )
355{
356 Table t( name, Table::Old ) ;
357 init( t, cols ) ;
358}
359
360STIdxIterAcc::STIdxIterAcc( const CountedPtr<Scantable> &s,
361 const vector<string> &cols )
362 : STIdxIter( s, cols )
363{
364 init( s->table(), cols ) ;
365}
366
367STIdxIterAcc::~STIdxIterAcc()
368{
369}
370
371void STIdxIterAcc::init( Table &t,
372 const vector<string> &cols )
373{
374 uInt ncol = cols.size() ;
375 uInt nrow = t.nrow() ;
376 // array shape here is as follows if cols=["BEAMNO","POLNO","IFNO"]:
377 // [[B0,B1,B2,...,BN],
378 // [P0,P1,P2,...,PN],
379 // [I0,I1,I2,...,IN]]
380 // order of internal storage is
381 // [B0,B1,B2,..,BN,P0,P1,P2,...,PN,I0,I1,I2,...,IN]
382 Matrix<uInt> arr( nrow, ncol ) ;
383 Vector<uInt> v ;
384 ROScalarColumn<uInt> col ;
385 for ( uInt i = 0 ; i < ncol ; i++ ) {
386 col.attach( t, cols[i] ) ;
387 v.reference( arr.column( i ) ) ;
388 col.getColumn( v ) ;
389 }
390 iter_m = new ArrayIndexIteratorAcc( arr ) ;
391}
392
393STIdxIterExAcc::STIdxIterExAcc()
394 : STIdxIter(),
395 srctypeid_m( -1 ),
396 srcnameid_m( -1 )
397{
398}
399
400STIdxIterExAcc::STIdxIterExAcc( const string &name,
401 const vector<string> &cols )
402 : STIdxIter( name, cols ),
403 srctypeid_m( -1 ),
404 srcnameid_m( -1 )
405{
406 Table t( name, Table::Old ) ;
407 init( t, cols ) ;
408}
409
410STIdxIterExAcc::STIdxIterExAcc( const CountedPtr<Scantable> &s,
411 const vector<string> &cols )
412 : STIdxIter( s, cols ),
413 srctypeid_m( -1 ),
414 srcnameid_m( -1 )
415{
416 init( s->table(), cols ) ;
417}
418
419STIdxIterExAcc::~STIdxIterExAcc()
420{
421}
422
423void STIdxIterExAcc::init( Table &t,
424 const vector<string> &cols )
425{
426 uInt ncol = cols.size() ;
427 uInt nrow = t.nrow() ;
428 // array shape here is as follows if cols=["BEAMNO","POLNO","IFNO"]:
429 // [[B0,B1,B2,...,BN],
430 // [P0,P1,P2,...,PN],
431 // [I0,I1,I2,...,IN]]
432 // order of internal storage is
433 // [B0,B1,B2,..,BN,P0,P1,P2,...,PN,I0,I1,I2,...,IN]
434 Matrix<uInt> arr( nrow, ncol ) ;
435 Vector<uInt> v ;
436 ROScalarColumn<uInt> col ;
437 ROScalarColumn<String> strCol ;
438 ROScalarColumn<Int> intCol ;
439 for ( uInt i = 0 ; i < ncol ; i++ ) {
440 v.reference( arr.column( i ) ) ;
441 if ( cols[i] == "SRCTYPE" ) {
442 intCol.attach( t, cols[i] ) ;
443 Vector<Int> srctype = intCol.getColumn() ;
444 processIntCol( srctype, v, srctype_m ) ;
445 srctypeid_m = i ;
446 }
447 else if ( cols[i] == "SRCNAME" ) {
448 strCol.attach( t, cols[i] ) ;
449 Vector<String> srcname = strCol.getColumn() ;
450 processStrCol( srcname, v, srcname_m ) ;
451 srcnameid_m = i ;
452 }
453 else {
454 col.attach( t, cols[i] ) ;
455 col.getColumn( v ) ;
456 }
457 }
458 iter_m = new ArrayIndexIteratorAcc( arr ) ;
459}
460
461void STIdxIterExAcc::processIntCol( Vector<Int> &in,
462 Vector<uInt> &out,
463 Block<Int> &val )
464{
465 convertArray( out, in ) ;
466 //uInt len = in.nelements() ;
467 //Vector<Int> tmp = in.copy() ;
468 //uInt n = genSort( tmp, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ;
469 //val.resize( n ) ;
470 //for ( uInt i = 0 ; i < n ; i++ ) {
471 // val[i] = tmp[i] ;
472// cout << "val[" << i << "]=" << val[i] << endl ;
473 //}
474 //out.resize( len ) ;
475 //uInt idx = -1 ;
476 //cout << "idx=" << idx << endl ;
477 //int idx2 = (Int)idx ;
478 //cout << "idx2=" << idx2 << endl ;
479 //idx = (uInt) idx2 ;
480 //cout << "idx=" << idx << endl ;
481 //if ( n == 1 ) {
482 // cout << "n=1" << endl ;
483 // out = 0 ;
484 //}
485 //else if ( n == 2 ) {
486 // cout << "n=2" << endl ;
487 // for ( uInt i = 0 ; i < len ; i++ ) {
488 // out[i] = (in[i] == val[0]) ? 0 : 1 ;
489 // }
490 //}
491 //else {
492 // cout << "n=" << n << endl ;
493 // map<Int,uInt> m ;
494 // for( uInt i = 0 ; i < n ; i++ )
495 // m[val[i]] = i ;
496 // for ( uInt i = 0 ; i < len ; i++ ) {
497 //for ( uInt j = 0 ; j < n ; j++ ) {
498 // if ( in[i] == val[j] ) {
499 // out[i] = j ;
500 // break ;
501 // }
502 //}
503 // out[i] = m[in[i]] ;
504 // }
505 // }
506}
507
508void STIdxIterExAcc::processStrCol( Vector<String> &in,
509 Vector<uInt> &out,
510 Block<String> &val )
511{
512 uInt len = in.nelements() ;
513 Vector<String> tmp = in.copy() ;
514 uInt n = genSort( tmp, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ;
515 val.resize( n ) ;
516 for ( uInt i = 0 ; i < n ; i++ ) {
517 val[i] = tmp[i] ;
518// cout << "val[" << i << "]=" << val[i] << endl ;
519 }
520 //out.resize( len ) ;
521 if ( n == 1 ) {
522 //cout << "n=1" << endl ;
523 out = 0 ;
524 }
525 else if ( n == 2 ) {
526 //cout << "n=2" << endl ;
527 for ( uInt i = 0 ; i < len ; i++ ) {
528 out[i] = (in[i] == val[0]) ? 0 : 1 ;
529 }
530 }
531 else {
532 //cout << "n=" << n << endl ;
533 map<String,uInt> m ;
534 for ( uInt i = 0 ; i < n ; i++ )
535 m[val[i]] = i ;
536 for ( uInt i = 0 ; i < len ; i++ ) {
537 //for ( uInt j = 0 ; j < n ; j++ ) {
538 // if ( in[i] == val[j] ) {
539 // out[i] = j ;
540 // break ;
541 // }
542 //}
543 out[i] = m[in[i]] ;
544 }
545 }
546}
547
548Int STIdxIterExAcc::getSrcType()
549{
550 //if ( srctype_m.nelements() > 0 )
551 if ( srctypeid_m >= 0 )
552 //return srctype_m[iter_m->current()[srctypeid_m]] ;
553 return (Int)(iter_m->current()[srctypeid_m]) ;
554 else
555 return -999 ;
556}
557
558String STIdxIterExAcc::getSrcName()
559{
560 if ( srcname_m.nelements() > 0 )
561 return srcname_m[iter_m->current()[srcnameid_m]] ;
562 else
563 return "" ;
564}
565
566} // namespace
Note: See TracBrowser for help on using the repository browser.