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 = Block<uInt>( nfield_m, 1 ) ; |
---|
17 | idx_m = Block<uInt>( nfield_m, 0 ) ; |
---|
18 | for ( uInt i = 1 ; i < nfield_m ; i++ ) { |
---|
19 | prod_m[i] = shape[i-1] * prod_m[i-1] ; |
---|
20 | } |
---|
21 | maxiter_m = prod_m[nfield_m-1] * shape[nfield_m-1] ; |
---|
22 | // cout << "maxiter_m=" << maxiter_m << endl ; |
---|
23 | } |
---|
24 | |
---|
25 | Bool IndexIterator::pastEnd() |
---|
26 | { |
---|
27 | return niter_m >= maxiter_m ; |
---|
28 | } |
---|
29 | |
---|
30 | void IndexIterator::next() |
---|
31 | { |
---|
32 | niter_m++ ; |
---|
33 | uInt x = niter_m ; |
---|
34 | for ( Int i = nfield_m-1 ; i >= 0 ; i-- ) { |
---|
35 | idx_m[i] = x / prod_m[i] ; |
---|
36 | //cout << "i=" << i << ": prod=" << prod_m[i] |
---|
37 | // << ", idx=" << idx_m[i] << endl ; |
---|
38 | x %= prod_m[i] ; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | ArrayIndexIterator::ArrayIndexIterator( Matrix<uInt> &arr, |
---|
43 | vector< vector<uInt> > idlist ) |
---|
44 | : arr_m( arr ), |
---|
45 | pos_m( IPosition(1,0) ) |
---|
46 | { |
---|
47 | ncol_m = arr_m.ncolumn() ; |
---|
48 | nrow_m = arr_m.nrow() ; |
---|
49 | vector< vector<uInt> > l = idlist ; |
---|
50 | if ( l.size() != ncol_m ) { |
---|
51 | l.resize( ncol_m ) ; |
---|
52 | for ( uInt i = 0 ; i < ncol_m ; i++ ) { |
---|
53 | Vector<uInt> a( arr_m.column( i ).copy() ) ; |
---|
54 | uInt n = genSort( a, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates ) ; |
---|
55 | a.resize(n,True) ; |
---|
56 | a.tovector( l[i] ) ; |
---|
57 | } |
---|
58 | } |
---|
59 | idxlist_m = l ; |
---|
60 | IPosition shape( ncol_m ) ; |
---|
61 | for ( uInt i = 0 ; i < ncol_m ; i++ ) { |
---|
62 | shape[i] = idxlist_m[i].size() ; |
---|
63 | } |
---|
64 | iter_m = new IndexIterator( shape ) ; |
---|
65 | current_m.resize( ncol_m ) ; |
---|
66 | } |
---|
67 | |
---|
68 | ArrayIndexIterator::~ArrayIndexIterator() |
---|
69 | { |
---|
70 | delete iter_m ; |
---|
71 | } |
---|
72 | |
---|
73 | Vector<uInt> ArrayIndexIterator::current() |
---|
74 | { |
---|
75 | Block<uInt> idx = iter_m->current() ; |
---|
76 | for ( uInt i = 0 ; i < ncol_m ; i++ ) |
---|
77 | current_m[i] = idxlist_m[i][idx[i]] ; |
---|
78 | return current_m ; |
---|
79 | } |
---|
80 | |
---|
81 | Bool ArrayIndexIterator::pastEnd() |
---|
82 | { |
---|
83 | return iter_m->pastEnd() ; |
---|
84 | } |
---|
85 | |
---|
86 | ArrayIndexIteratorNormal::ArrayIndexIteratorNormal( Matrix<uInt> &arr, |
---|
87 | vector< vector<uInt> > idlist ) |
---|
88 | : ArrayIndexIterator( arr, idlist ) |
---|
89 | { |
---|
90 | storage_m.resize( nrow_m ) ; |
---|
91 | } |
---|
92 | |
---|
93 | void ArrayIndexIteratorNormal::next() |
---|
94 | { |
---|
95 | iter_m->next() ; |
---|
96 | } |
---|
97 | |
---|
98 | Vector<uInt> ArrayIndexIteratorNormal::getRows( StorageInitPolicy policy ) |
---|
99 | { |
---|
100 | Vector<uInt> v = current() ; |
---|
101 | uInt len = 0 ; |
---|
102 | uInt *p = storage_m.storage() ; |
---|
103 | for ( uInt i = 0 ; i < nrow_m ; i++ ) { |
---|
104 | if ( allEQ( v, arr_m.row( i ) ) ) { |
---|
105 | *p = i ; |
---|
106 | len++ ; |
---|
107 | p++ ; |
---|
108 | } |
---|
109 | } |
---|
110 | pos_m[0] = len ; |
---|
111 | p = storage_m.storage() ; |
---|
112 | return Vector<uInt>( pos_m, p, policy ) ; |
---|
113 | } |
---|
114 | |
---|
115 | ArrayIndexIteratorAcc::ArrayIndexIteratorAcc( Matrix<uInt> &arr, |
---|
116 | vector< vector<uInt> > idlist ) |
---|
117 | : ArrayIndexIterator( arr, idlist ) |
---|
118 | { |
---|
119 | storage_m.resize( arr_m.nelements()+nrow_m ) ; |
---|
120 | for ( uInt i = 0 ; i < nrow_m ; i++ ) { |
---|
121 | storage_m[i+arr_m.nelements()] = i ; |
---|
122 | } |
---|
123 | len_m.resize( ncol_m+1 ) ; |
---|
124 | for ( uInt i = 0 ; i < ncol_m+1 ; i++ ) { |
---|
125 | len_m[i] = nrow_m ; |
---|
126 | // cout << "len[" << i << "]=" << len_m[i] << endl ; |
---|
127 | } |
---|
128 | prev_m = current() - (uInt)1 ; |
---|
129 | } |
---|
130 | |
---|
131 | void ArrayIndexIteratorAcc::next() |
---|
132 | { |
---|
133 | prev_m = current() ; |
---|
134 | iter_m->next() ; |
---|
135 | } |
---|
136 | |
---|
137 | Vector<uInt> ArrayIndexIteratorAcc::getRows( StorageInitPolicy policy ) |
---|
138 | { |
---|
139 | Vector<uInt> v = current() ; |
---|
140 | Int c = isChanged( v ) ; |
---|
141 | // cout << "c=" << c << endl ; |
---|
142 | if ( c < 0 ) { |
---|
143 | pos_m[0] = len_m[0] ; |
---|
144 | return Vector<uInt>( pos_m, storage_m.storage(), policy ) ; |
---|
145 | } |
---|
146 | uInt *base = storage_m.storage() + (c+1) * nrow_m ; |
---|
147 | // cout << "len_m[c+1]=" << len_m[c+1] << endl ; |
---|
148 | // cout << "base=" << Vector<uInt>(IPosition(1,len_m[c+1]),base,SHARE) << endl ; |
---|
149 | for ( Int i = c ; i >= 0 ; i-- ) { |
---|
150 | base = updateStorage( i, base, v[i] ) ; |
---|
151 | // cout << "len_m[" << i << "]=" << len_m[i] << endl ; |
---|
152 | // cout << "base=" << Vector<uInt>(IPosition(1,len_m[i]),base,SHARE) << endl ; |
---|
153 | } |
---|
154 | pos_m[0] = len_m[0] ; |
---|
155 | return Vector<uInt>( pos_m, storage_m.storage(), policy ) ; |
---|
156 | } |
---|
157 | |
---|
158 | Int ArrayIndexIteratorAcc::isChanged( Vector<uInt> &idx ) |
---|
159 | { |
---|
160 | Int i = ncol_m - 1 ; |
---|
161 | while( i >= 0 && idx[i] == prev_m[i] ) |
---|
162 | i-- ; |
---|
163 | return i ; |
---|
164 | } |
---|
165 | |
---|
166 | uInt *ArrayIndexIteratorAcc::updateStorage( Int &icol, |
---|
167 | uInt *base, |
---|
168 | uInt &v ) |
---|
169 | { |
---|
170 | uInt len = 0 ; |
---|
171 | uInt *p = base - nrow_m ; |
---|
172 | uInt *work = p ; |
---|
173 | for ( uInt i = 0 ; i < len_m[icol+1] ; i++ ) { |
---|
174 | if ( arr_m( *base, icol ) == v ) { |
---|
175 | // cout << "add " << *base << endl ; |
---|
176 | *work = *base ; |
---|
177 | len++ ; |
---|
178 | work++ ; |
---|
179 | } |
---|
180 | base++ ; |
---|
181 | } |
---|
182 | len_m[icol] = len ; |
---|
183 | return p ; |
---|
184 | } |
---|
185 | |
---|
186 | STIdxIter::STIdxIter() |
---|
187 | { |
---|
188 | iter_m = 0 ; |
---|
189 | } |
---|
190 | |
---|
191 | STIdxIter::STIdxIter( const string &name, |
---|
192 | const vector<string> &cols ) |
---|
193 | { |
---|
194 | } |
---|
195 | |
---|
196 | STIdxIter::STIdxIter( const CountedPtr<Scantable> &s, |
---|
197 | const vector<string> &cols ) |
---|
198 | { |
---|
199 | } |
---|
200 | |
---|
201 | STIdxIter::~STIdxIter() |
---|
202 | { |
---|
203 | if ( iter_m != 0 ) |
---|
204 | delete iter_m ; |
---|
205 | } |
---|
206 | |
---|
207 | vector<uInt> STIdxIter::tovector( Vector<uInt> v ) |
---|
208 | { |
---|
209 | vector<uInt> ret ; |
---|
210 | v.tovector( ret ) ; |
---|
211 | return ret ; |
---|
212 | } |
---|
213 | |
---|
214 | Vector<uInt> STIdxIter::getRows( StorageInitPolicy policy ) |
---|
215 | { |
---|
216 | return iter_m->getRows( policy ) ; |
---|
217 | } |
---|
218 | |
---|
219 | STIdxIterNormal::STIdxIterNormal() |
---|
220 | : STIdxIter() |
---|
221 | { |
---|
222 | } |
---|
223 | |
---|
224 | STIdxIterNormal::STIdxIterNormal( const string &name, |
---|
225 | const vector<string> &cols ) |
---|
226 | : STIdxIter( name, cols ) |
---|
227 | { |
---|
228 | Table t( name, Table::Old ) ; |
---|
229 | init( t, cols ) ; |
---|
230 | } |
---|
231 | |
---|
232 | STIdxIterNormal::STIdxIterNormal( const CountedPtr<Scantable> &s, |
---|
233 | const vector<string> &cols ) |
---|
234 | : STIdxIter( s, cols ) |
---|
235 | { |
---|
236 | init( s->table(), cols ) ; |
---|
237 | } |
---|
238 | |
---|
239 | STIdxIterNormal::~STIdxIterNormal() |
---|
240 | { |
---|
241 | } |
---|
242 | |
---|
243 | void STIdxIterNormal::init( Table &t, |
---|
244 | const vector<string> &cols ) |
---|
245 | { |
---|
246 | uInt ncol = cols.size() ; |
---|
247 | uInt nrow = t.nrow() ; |
---|
248 | Matrix<uInt> arr( nrow, ncol ) ; |
---|
249 | ROScalarColumn<uInt> col ; |
---|
250 | for ( uInt i = 0 ; i < ncol ; i++ ) { |
---|
251 | col.attach( t, cols[i] ) ; |
---|
252 | Vector<uInt> v( arr.column( i ) ) ; |
---|
253 | col.getColumn( v ) ; |
---|
254 | } |
---|
255 | iter_m = new ArrayIndexIteratorNormal( arr ) ; |
---|
256 | } |
---|
257 | |
---|
258 | STIdxIterAcc::STIdxIterAcc() |
---|
259 | : STIdxIter() |
---|
260 | { |
---|
261 | } |
---|
262 | |
---|
263 | STIdxIterAcc::STIdxIterAcc( const string &name, |
---|
264 | const vector<string> &cols ) |
---|
265 | : STIdxIter( name, cols ) |
---|
266 | { |
---|
267 | Table t( name, Table::Old ) ; |
---|
268 | init( t, cols ) ; |
---|
269 | } |
---|
270 | |
---|
271 | STIdxIterAcc::STIdxIterAcc( const CountedPtr<Scantable> &s, |
---|
272 | const vector<string> &cols ) |
---|
273 | : STIdxIter( s, cols ) |
---|
274 | { |
---|
275 | init( s->table(), cols ) ; |
---|
276 | } |
---|
277 | |
---|
278 | STIdxIterAcc::~STIdxIterAcc() |
---|
279 | { |
---|
280 | } |
---|
281 | |
---|
282 | void STIdxIterAcc::init( Table &t, |
---|
283 | const vector<string> &cols ) |
---|
284 | { |
---|
285 | uInt ncol = cols.size() ; |
---|
286 | uInt nrow = t.nrow() ; |
---|
287 | Matrix<uInt> arr( nrow, ncol ) ; |
---|
288 | ROScalarColumn<uInt> col ; |
---|
289 | for ( uInt i = 0 ; i < ncol ; i++ ) { |
---|
290 | col.attach( t, cols[i] ) ; |
---|
291 | Vector<uInt> v( arr.column( i ) ) ; |
---|
292 | col.getColumn( v ) ; |
---|
293 | } |
---|
294 | iter_m = new ArrayIndexIteratorAcc( arr ) ; |
---|
295 | } |
---|
296 | |
---|
297 | } // namespace |
---|