[2519] | 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> |
---|
[2533] | 8 | #include <casa/Arrays/IPosition.h> |
---|
[2537] | 9 | #include <casa/BasicSL/String.h> |
---|
[2519] | 10 | |
---|
[2911] | 11 | #include <casa/Utilities/Sort.h> |
---|
| 12 | |
---|
[2519] | 13 | #include "Scantable.h" |
---|
| 14 | |
---|
| 15 | using namespace std ; |
---|
| 16 | using namespace casa ; |
---|
| 17 | |
---|
[2920] | 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 | |
---|
[2519] | 33 | namespace asap { |
---|
| 34 | class IndexIterator |
---|
| 35 | { |
---|
| 36 | public: |
---|
[2533] | 37 | IndexIterator( IPosition &shape ) ; |
---|
| 38 | Block<uInt> current() { return idx_m ; } ; |
---|
[2519] | 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 ; |
---|
[2524] | 58 | virtual Vector<uInt> getRows( StorageInitPolicy policy=COPY ) = 0 ; |
---|
[2519] | 59 | protected: |
---|
| 60 | IndexIterator *iter_m ; |
---|
| 61 | uInt nrow_m ; |
---|
| 62 | uInt ncol_m ; |
---|
[2533] | 63 | Block<uInt> storage_m ; |
---|
[2519] | 64 | Matrix<uInt> arr_m ; |
---|
[2533] | 65 | IPosition pos_m ; |
---|
| 66 | Vector<uInt> current_m ; |
---|
| 67 | vector< vector<uInt> > idxlist_m ; |
---|
[2519] | 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() ; |
---|
[2524] | 76 | Vector<uInt> getRows( StorageInitPolicy policy=COPY ) ; |
---|
[2519] | 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() ; |
---|
[2524] | 85 | Vector<uInt> getRows( StorageInitPolicy policy=COPY ) ; |
---|
[2519] | 86 | private: |
---|
[2536] | 87 | Int isChanged( Block<uInt> &idx ) ; |
---|
[2519] | 88 | uInt *updateStorage( Int &icol, uInt *base, uInt &v ) ; |
---|
| 89 | |
---|
[2536] | 90 | Block<uInt> prev_m ; |
---|
[2519] | 91 | Block<uInt> len_m ; |
---|
[2536] | 92 | Block<Bool> skip_m ; |
---|
[2519] | 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() ) ; } ; |
---|
[2536] | 109 | // !!!you should not use policy=TAKE_OVER since it causes problem!!! |
---|
[2533] | 110 | Vector<uInt> getRows( StorageInitPolicy policy=COPY ) ; |
---|
[2519] | 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, |
---|
[2536] | 124 | const vector<string> &cols ) ; |
---|
[2519] | 125 | STIdxIterNormal( const CountedPtr<Scantable> &s, |
---|
[2536] | 126 | const vector<string> &cols ) ; |
---|
[2519] | 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, |
---|
[2536] | 138 | const vector<string> &cols ) ; |
---|
[2519] | 139 | STIdxIterAcc( const CountedPtr<Scantable> &s, |
---|
[2536] | 140 | const vector<string> &cols ) ; |
---|
[2519] | 141 | ~STIdxIterAcc() ; |
---|
| 142 | protected: |
---|
[2536] | 143 | virtual void init( Table &t, |
---|
| 144 | const vector<string> &cols ) ; |
---|
[2519] | 145 | } ; |
---|
| 146 | |
---|
[2537] | 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 ; |
---|
[2547] | 170 | Int srctypeid_m ; |
---|
| 171 | Int srcnameid_m ; |
---|
[2537] | 172 | } ; |
---|
[2536] | 173 | |
---|
[2911] | 174 | class STIdxIter2 |
---|
| 175 | { |
---|
| 176 | public: |
---|
[2920] | 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 | } |
---|
[2911] | 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); |
---|
[2918] | 210 | void addColumnToKeyTpString(const string &name); |
---|
[2911] | 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_; |
---|
[2918] | 221 | vector<Vector<String> > string_storage_; |
---|
[2911] | 222 | } ; |
---|
| 223 | |
---|
[2519] | 224 | } // namespace |
---|
| 225 | #endif /* _ASAP_INDEX_ITERATOR_H_ */ |
---|