#ifndef _ASAP_INDEX_ITERATOR_H_ #define _ASAP_INDEX_ITERATOR_H_ #include #include #include #include #include #include #include #include "Scantable.h" using namespace std ; using namespace casa ; namespace { vector split(const string &str, char delim) { vector result; size_t current = 0; size_t found; while ((found = str.find_first_of(delim, current)) != string::npos) { result.push_back(string(str, current, found - current)); current = found + 1; } result.push_back(string(str, current, str.size() - current)); return result; } } // anonymous namespace namespace asap { class IndexIterator { public: IndexIterator( IPosition &shape ) ; Block current() { return idx_m ; } ; Bool pastEnd() ; void next() ; private: uInt nfield_m ; Block prod_m ; Block idx_m ; uInt niter_m ; uInt maxiter_m ; } ; class ArrayIndexIterator { public: ArrayIndexIterator( Matrix &arr, vector< vector > idlist=vector< vector >() ) ; virtual ~ArrayIndexIterator() ; Vector current() ; Bool pastEnd() ; virtual void next() = 0 ; virtual Vector getRows( StorageInitPolicy policy=COPY ) = 0 ; protected: IndexIterator *iter_m ; uInt nrow_m ; uInt ncol_m ; Block storage_m ; Matrix arr_m ; IPosition pos_m ; Vector current_m ; vector< vector > idxlist_m ; } ; class ArrayIndexIteratorNormal : public ArrayIndexIterator { public: ArrayIndexIteratorNormal( Matrix &arr, vector< vector > idlist=vector< vector >() ) ; void next() ; Vector getRows( StorageInitPolicy policy=COPY ) ; } ; class ArrayIndexIteratorAcc : public ArrayIndexIterator { public: ArrayIndexIteratorAcc( Matrix &arr, vector< vector > idlist=vector< vector >() ) ; void next() ; Vector getRows( StorageInitPolicy policy=COPY ) ; private: Int isChanged( Block &idx ) ; uInt *updateStorage( Int &icol, uInt *base, uInt &v ) ; Block prev_m ; Block len_m ; Block skip_m ; } ; class STIdxIter { public: STIdxIter() ; STIdxIter( const string &name, const vector &cols ) ; STIdxIter( const CountedPtr &s, const vector &cols ) ; virtual ~STIdxIter() ; vector currentSTL() { return tovector( iter_m->current() ) ; } ; Vector current() { return iter_m->current() ; } ; Bool pastEnd() { return iter_m->pastEnd() ; } ; void next() { iter_m->next() ; } ; vector getRowsSTL() { return tovector( iter_m->getRows() ) ; } ; // !!!you should not use policy=TAKE_OVER since it causes problem!!! Vector getRows( StorageInitPolicy policy=COPY ) ; protected: ArrayIndexIterator *iter_m ; virtual void init( Table &t, const vector &cols ) = 0 ; private: vector tovector( Vector v ) ; } ; class STIdxIterNormal : public STIdxIter { public: STIdxIterNormal() ; STIdxIterNormal( const string &name, const vector &cols ) ; STIdxIterNormal( const CountedPtr &s, const vector &cols ) ; ~STIdxIterNormal() ; protected: void init( Table &t, const vector &cols ) ; } ; class STIdxIterAcc : public STIdxIter { public: STIdxIterAcc() ; STIdxIterAcc( const string &name, const vector &cols ) ; STIdxIterAcc( const CountedPtr &s, const vector &cols ) ; ~STIdxIterAcc() ; protected: virtual void init( Table &t, const vector &cols ) ; } ; class STIdxIterExAcc : public STIdxIter { public: STIdxIterExAcc() ; STIdxIterExAcc( const string &name, const vector &cols ) ; STIdxIterExAcc( const CountedPtr &s, const vector &cols ) ; ~STIdxIterExAcc() ; Int getSrcType() ; String getSrcName() ; protected: virtual void init( Table &t, const vector &cols ) ; private: void processIntCol( Vector &in, Vector &out, Block &val ) ; void processStrCol( Vector &in, Vector &out, Block &val ) ; Block srctype_m ; Block srcname_m ; Int srctypeid_m ; Int srcnameid_m ; } ; class STIdxIter2 { public: template static void Iterate(T &processor, const string cols_list) { vector cols = split(cols_list, ','); // for (vector::iterator i = cols.begin(); i != cols.end(); ++i) // cout << *i << endl; STIdxIter2 iter(processor.target(), cols); STSelector sel ; while ( !iter.pastEnd() ) { const Record current = iter.currentValue() ; Vector rows = iter.getRows( SHARE ) ; // any process processor.Process(cols, current, rows); // go next iter.next() ; } } STIdxIter2() ; STIdxIter2( const string &name, const vector &cols ) ; STIdxIter2( const CountedPtr &s, const vector &cols ) ; virtual ~STIdxIter2() ; Record currentValue(); Bool pastEnd() ; void next() ; Vector getRows(StorageInitPolicy policy=COPY) ; vector getRowsSTL() { return tovector( getRows() ) ; } ; virtual void init(); private: vector tovector(Vector v); void addSortKey(const string &name); template void addColumnToKey(const string &name); void addColumnToKeyTpString(const string &name); void deallocate(); vector cols_; Table table_; uInt counter_; uInt num_iter_; uInt num_row_; Sort sorter_; Vector index_; Vector unique_; vector pointer_; vector > string_storage_; } ; } // namespace #endif /* _ASAP_INDEX_ITERATOR_H_ */