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

Last change on this file since 2527 was 2524, 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...

Minor speedup of the code. Share memory between arrays as much as possible.


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