Changeset 2921
- Timestamp:
- 04/07/14 10:28:37 (11 years ago)
- Location:
- trunk/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/STIdxIter.cpp
r2918 r2921 13 13 14 14 namespace asap { 15 16 IndexIterator::IndexIterator( IPosition &shape )17 : niter_m( 0 )18 {19 nfield_m = shape.nelements() ;20 prod_m.resize( nfield_m ) ;21 idx_m.resize( nfield_m ) ;22 prod_m[nfield_m-1] = 1 ;23 idx_m[nfield_m-1] = 0 ;24 for ( Int i = nfield_m-2 ; i >= 0 ; i-- ) {25 prod_m[i] = shape[i+1] * prod_m[i+1] ;26 idx_m[i] = 0 ;27 }28 maxiter_m = prod_m[0] * shape[0] ;29 // cout << "maxiter_m=" << maxiter_m << endl ;30 }31 32 Bool IndexIterator::pastEnd()33 {34 return niter_m >= maxiter_m ;35 }36 37 void IndexIterator::next()38 {39 niter_m++ ;40 uInt x = niter_m ;41 for ( Int i = 0 ; i < nfield_m ; i++ ) {42 idx_m[i] = x / prod_m[i] ;43 //cout << "i=" << i << ": prod=" << prod_m[i]44 // << ", idx=" << idx_m[i] << endl ;45 x %= prod_m[i] ;46 }47 }48 49 ArrayIndexIterator::ArrayIndexIterator( Matrix<uInt> &arr,50 vector< vector<uInt> > idlist )51 : arr_m( arr ),52 pos_m( 1 )53 {54 ncol_m = arr_m.ncolumn() ;55 nrow_m = arr_m.nrow() ;56 vector< vector<uInt> > l = idlist ;57 if ( l.size() != ncol_m ) {58 l.resize( ncol_m ) ;59 for ( uInt i = 0 ; i < ncol_m ; i++ ) {60 Vector<uInt> a( arr_m.column( i ).copy() ) ;61 uInt n = genSort( a, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ;62 a.resize(n,True) ;63 a.tovector( l[i] ) ;64 }65 }66 idxlist_m = l ;67 IPosition shape( ncol_m ) ;68 for ( uInt i = 0 ; i < ncol_m ; i++ ) {69 shape[i] = idxlist_m[i].size() ;70 }71 // cout << "shape=" << shape << endl ;72 iter_m = new IndexIterator( shape ) ;73 current_m.resize( ncol_m ) ;74 }75 76 ArrayIndexIterator::~ArrayIndexIterator()77 {78 delete iter_m ;79 }80 81 Vector<uInt> ArrayIndexIterator::current()82 {83 Block<uInt> idx = iter_m->current() ;84 for ( uInt i = 0 ; i < ncol_m ; i++ )85 current_m[i] = idxlist_m[i][idx[i]] ;86 return current_m ;87 }88 89 Bool ArrayIndexIterator::pastEnd()90 {91 return iter_m->pastEnd() ;92 }93 94 ArrayIndexIteratorNormal::ArrayIndexIteratorNormal( Matrix<uInt> &arr,95 vector< vector<uInt> > idlist )96 : ArrayIndexIterator( arr, idlist )97 {98 storage_m.resize( nrow_m ) ;99 }100 101 void ArrayIndexIteratorNormal::next()102 {103 iter_m->next() ;104 }105 106 Vector<uInt> ArrayIndexIteratorNormal::getRows( StorageInitPolicy policy )107 {108 Vector<uInt> v = current() ;109 uInt len = 0 ;110 uInt *p = storage_m.storage() ;111 for ( uInt i = 0 ; i < nrow_m ; i++ ) {112 if ( allEQ( v, arr_m.row( i ) ) ) {113 *p = i ;114 len++ ;115 p++ ;116 }117 }118 pos_m[0] = len ;119 p = storage_m.storage() ;120 return Vector<uInt>( pos_m, p, policy ) ;121 }122 123 ArrayIndexIteratorAcc::ArrayIndexIteratorAcc( Matrix<uInt> &arr,124 vector< vector<uInt> > idlist )125 : ArrayIndexIterator( arr, idlist )126 {127 // storage_m layout128 // length: ncol_m * (nrow_m + 1)129 // 0~nrow_m-1: constant temporary storage that indicates whole rows130 // nrow_m~2*nrow_m-1: temporary storage for column 0131 // 2*nrow_m~3*nrow_m-1: temporary storage for column 1132 // ...133 storage_m.resize( arr_m.nelements()+nrow_m ) ;134 // initialize constant temporary storage135 uInt *p = storage_m.storage() ;136 for ( uInt i = 0 ; i < nrow_m ; i++ ) {137 *p = i ;138 p++ ;139 }140 // len_m layout141 // len[0]: length of temporary storage that incidates whole rows (constant)142 // len[1]: number of matched row for column 0 selection143 // len[2]: number of matched row for column 1 selection144 // ...145 len_m.resize( ncol_m+1 ) ;146 p = len_m.storage() ;147 for ( uInt i = 0 ; i < ncol_m+1 ; i++ ) {148 *p = nrow_m ;149 p++ ;150 // cout << "len[" << i << "]=" << len_m[i] << endl ;151 }152 // skip_m layout153 // skip_m[0]: True if column 0 is filled by unique value154 // skip_m[1]: True if column 1 is filled by unique value155 // ...156 skip_m.resize( ncol_m ) ;157 for ( uInt i = 0 ; i < ncol_m ; i++ ) {158 skip_m[i] = (Bool)(idxlist_m[i].size()==1) ;159 // cout << "skip_m[" << i << "]=" << skip_m[i] << endl ;160 }161 prev_m = iter_m->current() ;162 p = prev_m.storage() ;163 for ( uInt i = 0 ; i < ncol_m ; i++ ) {164 *p = *p - 1 ;165 p++ ;166 }167 // cout << "prev_m=" << Vector<uInt>(IPosition(1,ncol_m),prev_m.storage()) << endl ;168 }169 170 void ArrayIndexIteratorAcc::next()171 {172 prev_m = iter_m->current() ;173 iter_m->next() ;174 }175 176 Vector<uInt> ArrayIndexIteratorAcc::getRows( StorageInitPolicy policy )177 {178 Block<uInt> v = iter_m->current() ;179 Int c = isChanged( v ) ;180 // cout << "v=" << Vector<uInt>(IPosition(1,v.nelements()),v.storage(),SHARE) << endl ;181 // cout << "c=" << c << endl ;182 if ( c > ncol_m-1 ) {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 >= 0 && skip_m[offset] )193 offset-- ;194 offset++ ;195 // cout << "offset = " << offset << endl ;196 uInt *base = storage_m.storage() + offset * nrow_m ;197 // cout << "len_m[c+1]=" << len_m[c+1] << endl ;198 // cout << "base=" << Vector<uInt>(IPosition(1,len_m[c+1]),base,SHARE) << endl ;199 for ( Int i = c ; i < ncol_m ; i++ ) {200 base = updateStorage( i, base, idxlist_m[i][v[i]] ) ;201 // cout << "len_m[" << i << "]=" << len_m[i] << endl ;202 // cout << "base=" << Vector<uInt>(IPosition(1,len_m[i]),base,SHARE) << endl ;203 }204 pos_m[0] = len_m[ncol_m] ;205 // cout << "pos_m=" << pos_m << endl ;206 // cout << "ret=" << Vector<uInt>( pos_m, base, policy ) << endl ;207 return Vector<uInt>( pos_m, base, policy ) ;208 }209 210 Int ArrayIndexIteratorAcc::isChanged( Block<uInt> &idx )211 {212 Int i = 0 ;213 while( i < ncol_m && idx[i] == prev_m[i] )214 i++ ;215 return i ;216 }217 218 uInt *ArrayIndexIteratorAcc::updateStorage( Int &icol,219 uInt *base,220 uInt &v )221 {222 // CAUTION:223 // indexes for storage_m and len_m differ from index for skip_m by 1224 // (skip_m[0] corresponds to storage_m[1] and len_m[1]) since there225 // is additional temporary storage at first segment in storage_m226 uInt *p ;227 if ( skip_m[icol] ) {228 // skip update, just update len_m[icol] and pass appropriate pointer229 // cout << "skip " << icol << endl ;230 p = base ;231 len_m[icol+1] = len_m[icol] ;232 }233 else {234 // cout << "update " << icol << endl ;235 uInt len = 0 ;236 p = storage_m.storage() + (icol+1) * nrow_m ;237 uInt *work = p ;238 Bool b ;239 const uInt *arr_p = arr_m.getStorage( b ) ;240 long offset = 0 ;241 // warr_p points a first element of (icol)-th column242 const uInt *warr_p = arr_p + icol * nrow_m ;243 for ( uInt i = 0 ; i < len_m[icol] ; i++ ) {244 // increment warr_p by (*(base)-*(base-1))245 warr_p += *base - offset ;246 // check if target element is equal to value specified247 if ( *warr_p == v ) {248 // then, add current index to storage_m249 // cout << "add " << *base << endl ;250 *work = *base ;251 len++ ;252 work++ ;253 }254 // update offset255 offset = *base ;256 // next index257 base++ ;258 }259 arr_m.freeStorage( arr_p, b ) ;260 len_m[icol+1] = len ;261 }262 return p ;263 }264 265 STIdxIter::STIdxIter()266 {267 iter_m = 0 ;268 }269 270 STIdxIter::STIdxIter( const string &name,271 const vector<string> &cols )272 {273 }274 275 STIdxIter::STIdxIter( const CountedPtr<Scantable> &s,276 const vector<string> &cols )277 {278 }279 280 STIdxIter::~STIdxIter()281 {282 if ( iter_m != 0 )283 delete iter_m ;284 }285 286 vector<uInt> STIdxIter::tovector( Vector<uInt> v )287 {288 vector<uInt> ret ;289 v.tovector( ret ) ;290 return ret ;291 }292 293 Vector<uInt> STIdxIter::getRows( StorageInitPolicy policy )294 {295 return iter_m->getRows( policy ) ;296 }297 298 STIdxIterNormal::STIdxIterNormal()299 : STIdxIter()300 {301 }302 303 STIdxIterNormal::STIdxIterNormal( const string &name,304 const vector<string> &cols )305 : STIdxIter( name, cols )306 {307 Table t( name, Table::Old ) ;308 init( t, cols ) ;309 }310 311 STIdxIterNormal::STIdxIterNormal( const CountedPtr<Scantable> &s,312 const vector<string> &cols )313 : STIdxIter( s, cols )314 {315 init( s->table(), cols ) ;316 }317 318 STIdxIterNormal::~STIdxIterNormal()319 {320 }321 322 void STIdxIterNormal::init( Table &t,323 const vector<string> &cols )324 {325 uInt ncol = cols.size() ;326 uInt nrow = t.nrow() ;327 Matrix<uInt> arr( nrow, ncol ) ;328 ROScalarColumn<uInt> col ;329 Vector<uInt> v ;330 for ( uInt i = 0 ; i < ncol ; i++ ) {331 col.attach( t, cols[i] ) ;332 v.reference( arr.column( i ) ) ;333 col.getColumn( v ) ;334 }335 iter_m = new ArrayIndexIteratorNormal( arr ) ;336 }337 338 STIdxIterAcc::STIdxIterAcc()339 : STIdxIter()340 {341 }342 343 STIdxIterAcc::STIdxIterAcc( const string &name,344 const vector<string> &cols )345 : STIdxIter( name, cols )346 {347 Table t( name, Table::Old ) ;348 init( t, cols ) ;349 }350 351 STIdxIterAcc::STIdxIterAcc( const CountedPtr<Scantable> &s,352 const vector<string> &cols )353 : STIdxIter( s, cols )354 {355 init( s->table(), cols ) ;356 }357 358 STIdxIterAcc::~STIdxIterAcc()359 {360 }361 362 void STIdxIterAcc::init( Table &t,363 const vector<string> &cols )364 {365 uInt ncol = cols.size() ;366 uInt nrow = t.nrow() ;367 // array shape here is as follows if cols=["BEAMNO","POLNO","IFNO"]:368 // [[B0,B1,B2,...,BN],369 // [P0,P1,P2,...,PN],370 // [I0,I1,I2,...,IN]]371 // order of internal storage is372 // [B0,B1,B2,..,BN,P0,P1,P2,...,PN,I0,I1,I2,...,IN]373 Matrix<uInt> arr( nrow, ncol ) ;374 Vector<uInt> v ;375 ROScalarColumn<uInt> col ;376 for ( uInt i = 0 ; i < ncol ; i++ ) {377 col.attach( t, cols[i] ) ;378 v.reference( arr.column( i ) ) ;379 col.getColumn( v ) ;380 }381 iter_m = new ArrayIndexIteratorAcc( arr ) ;382 }383 384 STIdxIterExAcc::STIdxIterExAcc()385 : STIdxIter(),386 srctypeid_m( -1 ),387 srcnameid_m( -1 )388 {389 }390 391 STIdxIterExAcc::STIdxIterExAcc( const string &name,392 const vector<string> &cols )393 : STIdxIter( name, cols ),394 srctypeid_m( -1 ),395 srcnameid_m( -1 )396 {397 Table t( name, Table::Old ) ;398 init( t, cols ) ;399 }400 401 STIdxIterExAcc::STIdxIterExAcc( const CountedPtr<Scantable> &s,402 const vector<string> &cols )403 : STIdxIter( s, cols ),404 srctypeid_m( -1 ),405 srcnameid_m( -1 )406 {407 init( s->table(), cols ) ;408 }409 410 STIdxIterExAcc::~STIdxIterExAcc()411 {412 }413 414 void STIdxIterExAcc::init( Table &t,415 const vector<string> &cols )416 {417 uInt ncol = cols.size() ;418 uInt nrow = t.nrow() ;419 // array shape here is as follows if cols=["BEAMNO","POLNO","IFNO"]:420 // [[B0,B1,B2,...,BN],421 // [P0,P1,P2,...,PN],422 // [I0,I1,I2,...,IN]]423 // order of internal storage is424 // [B0,B1,B2,..,BN,P0,P1,P2,...,PN,I0,I1,I2,...,IN]425 Matrix<uInt> arr( nrow, ncol ) ;426 Vector<uInt> v ;427 ROScalarColumn<uInt> col ;428 ROScalarColumn<String> strCol ;429 ROScalarColumn<Int> intCol ;430 for ( uInt i = 0 ; i < ncol ; i++ ) {431 v.reference( arr.column( i ) ) ;432 if ( cols[i] == "SRCTYPE" ) {433 intCol.attach( t, cols[i] ) ;434 Vector<Int> srctype = intCol.getColumn() ;435 processIntCol( srctype, v, srctype_m ) ;436 srctypeid_m = i ;437 }438 else if ( cols[i] == "SRCNAME" ) {439 strCol.attach( t, cols[i] ) ;440 Vector<String> srcname = strCol.getColumn() ;441 processStrCol( srcname, v, srcname_m ) ;442 srcnameid_m = i ;443 }444 else {445 col.attach( t, cols[i] ) ;446 col.getColumn( v ) ;447 }448 }449 iter_m = new ArrayIndexIteratorAcc( arr ) ;450 }451 452 void STIdxIterExAcc::processIntCol( Vector<Int> &in,453 Vector<uInt> &out,454 Block<Int> &val )455 {456 convertArray( out, in ) ;457 }458 459 void STIdxIterExAcc::processStrCol( Vector<String> &in,460 Vector<uInt> &out,461 Block<String> &val )462 {463 uInt len = in.nelements() ;464 Vector<String> tmp = in.copy() ;465 uInt n = genSort( tmp, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ;466 val.resize( n ) ;467 for ( uInt i = 0 ; i < n ; i++ ) {468 val[i] = tmp[i] ;469 // cout << "val[" << i << "]=" << val[i] << endl ;470 }471 if ( n == 1 ) {472 //cout << "n=1" << endl ;473 out = 0 ;474 }475 else if ( n == 2 ) {476 //cout << "n=2" << endl ;477 for ( uInt i = 0 ; i < len ; i++ ) {478 out[i] = (in[i] == val[0]) ? 0 : 1 ;479 }480 }481 else {482 //cout << "n=" << n << endl ;483 map<String,uInt> m ;484 for ( uInt i = 0 ; i < n ; i++ )485 m[val[i]] = i ;486 for ( uInt i = 0 ; i < len ; i++ ) {487 out[i] = m[in[i]] ;488 }489 }490 }491 492 Int STIdxIterExAcc::getSrcType()493 {494 if ( srctypeid_m >= 0 )495 return (Int)(iter_m->current()[srctypeid_m]) ;496 else497 return -999 ;498 }499 500 String STIdxIterExAcc::getSrcName()501 {502 if ( srcname_m.nelements() > 0 )503 return srcname_m[iter_m->current()[srcnameid_m]] ;504 else505 return "" ;506 }507 508 15 STIdxIter2::STIdxIter2() 509 16 : cols_(), -
trunk/src/STIdxIter.h
r2920 r2921 32 32 33 33 namespace asap { 34 class IndexIterator35 {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 ArrayIndexIterator50 {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 ArrayIndexIterator71 {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 ArrayIndexIterator80 {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 STIdxIter96 {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 STIdxIter120 {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 STIdxIter134 {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 STIdxIter148 {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 34 class STIdxIter2 175 35 { -
trunk/src/python_Iterator.cpp
r2580 r2921 11 11 12 12 void python_Iterator() { 13 class_<STIdxIter Acc>("iterator")13 class_<STIdxIter2>("iterator") 14 14 .def( init < const string &, const vector<string> & > () ) 15 .def("current", &STIdxIterAcc::currentSTL) 16 .def("pastEnd", &STIdxIterAcc::pastEnd) 17 .def("next", &STIdxIterAcc::next) 18 .def("getrows", &STIdxIterAcc::getRowsSTL) 15 #ifdef HAVE_LIBPYRAP 16 .def("current", &STIdxIter2::currentValue) 17 #endif 18 .def("pastEnd", &STIdxIter2::pastEnd) 19 .def("next", &STIdxIter2::next) 20 .def("getrows", &STIdxIter2::getRowsSTL) 19 21 ; 20 22 }; -
trunk/src/python_asap.cpp
r2916 r2921 88 88 asap::python::python_SrcType(); 89 89 asap::python::python_STGrid(); 90 //asap::python::python_Iterator();90 asap::python::python_Iterator(); 91 91 asap::python::python_EdgeMarker(); 92 92 asap::python::python_PlotHelper(); -
trunk/src/python_asap.h
r2916 r2921 55 55 void python_SrcType(); 56 56 void python_STGrid(); 57 //void python_Iterator();57 void python_Iterator(); 58 58 void python_EdgeMarker(); 59 59 void python_PlotHelper();
Note:
See TracChangeset
for help on using the changeset viewer.