File: tTableIter.cc

package info (click to toggle)
casacore 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,680 kB
  • sloc: cpp: 462,815; fortran: 16,372; ansic: 7,403; yacc: 4,626; lex: 2,340; sh: 1,786; python: 629; perl: 531; sed: 499; csh: 34; makefile: 31
file content (340 lines) | stat: -rw-r--r-- 11,140 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//# tTableIter.cc: Test program for table iterators
//# Copyright (C) 1994,1995,1996,1999,2000,2001,2003
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This program is free software; you can redistribute it and/or modify it
//# under the terms of the GNU General Public License as published by the Free
//# Software Foundation; either version 2 of the License, or (at your option)
//# any later version.
//#
//# This program is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
//# more details.
//#
//# You should have received a copy of the GNU General Public License along
//# with this program; if not, write to the Free Software Foundation, Inc.,
//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id$

#include <casacore/tables/Tables/TableDesc.h>
#include <casacore/tables/Tables/ScaColDesc.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/SetupNewTab.h>
#include <casacore/tables/Tables/TableIter.h>
#include <casacore/tables/Tables/ScalarColumn.h>
#include <casacore/casa/Containers/Block.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/Arrays/ArrayLogical.h>

#include <casacore/casa/iostream.h>
#include <casacore/casa/sstream.h>


#include <casacore/casa/namespace.h>
// <summary> Test program for table iterators </summary>

// This program tests the table iterators..
// It creates a description and a table.
// It reads back the table, sorts it and selects rows.
// The data read is written to stdout to be checked.
// The standard output file tTableIter.out is checked in as a reference.
// The script tTableIter.run executes tTableIter and compares its output
// with tTableIter.out.

void credes();
void cretab(uInt);
void doiter0();
void doiter1();
void doiter2();
void doiter3();
void test_cache_boundaries();

int main (int argc, const char* argv[])
{
    uInt nr = 5000;
    if (argc > 1) {
	istringstream istr(argv[1]);
	istr >> nr;
    }
    credes();                // make description
    cretab(nr);              // create table (and write it)
    doiter0();               // do no column iteration
    doiter1();               // do single column iteration
    doiter2();               // do two column iteration
    doiter3();               // do interval iteration
    test_cache_boundaries(); // test option to cache group boundaries
    return 0;                // successfully executed
}

// Create the description of the table and its subtable.
// Define some defaults (as scalar and array) to test if they
// are filled in correctly when a row is added to a table.
void credes () {
    TableDesc txp("tTableIter_tmp", TableDesc::New);
    txp.addColumn (ScalarColumnDesc<Int> ("col1"));
    txp.addColumn (ScalarColumnDesc<double> ("col2"));
    txp.addColumn (ScalarColumnDesc<float> ("col3"));
    txp.addColumn (ScalarColumnDesc<Complex> ("col4"));
}

// Write data into the table.
void cretab(uInt nr) {
    Int i;
    SetupNewTable newtab ("tTableIter_tmp.data","tTableIter_tmp",Table::New);
    Table tab(newtab, nr);
    ScalarColumn<Int>     col1 (tab, "col1");
    ScalarColumn<double>  col2 (tab, "col2");
    ScalarColumn<float>   col3 (tab, "col3");
    ScalarColumn<Complex> col4 (tab, "col4");
    for (i=0; i<Int(nr); i++) {
	col1.put (i, (100000+i) % 10);
	col2.put (i, (i+1) % 105);
	col3.put (i, (i+2) % 75);
	col4.put (i, i+3);
    }
    cout << "Filling done" << endl;
}

void doiter0()
{
    Table t;
    Table tab ("tTableIter_tmp.data");
    Block<String> iv0;
    TableIterator iter0(tab, iv0);
    Int nr = 0;
    while (!iter0.pastEnd()) {
	t = iter0.table();
        AlwaysAssertExit (t.nrow() == tab.nrow());
	nr++;
	iter0.next();
    }
    cout << "   #iter=" << nr << endl;

    iter0.reset();
    nr = 0;
    while (!iter0.pastEnd()) {
	t = iter0.table();
        AlwaysAssertExit (t.nrow() == tab.nrow());
	nr++;
	iter0.next();
    }
    cout << "   #iter1=" << nr << endl;
}

void doiter1()
{
    Table t;
    Table tab ("tTableIter_tmp.data");
    Block<String> iv0(1);
    iv0[0] = "col1";
    TableIterator iter0(tab, iv0);
    Int nr = 0;
    while (!iter0.pastEnd()) {
	t = iter0.table();
	ScalarColumn<Int> col1(t, "col1");
	Vector<Int> vec;
	col1.getColumn (vec);
	cout << t.nrow() << " ";
	if (!allEQ(vec, nr)) {
	    cout << "error in iter. " << nr << " " << vec(0)
		 << " " << vec(vec.nelements()-1) << " " << vec.nelements() << endl;
	}
	cout << "(" << iter0.keyChangeAtLastNext() << ") ";
	if ( (nr<9 && iter0.keyChangeAtLastNext()!=iv0[0]) ||
	     (nr==9 && iter0.keyChangeAtLastNext()!="")  ) {     // last iter is empty string
	  cout << "error in TableIter::keyChangeAtLastNext() " << iter0.keyChangeAtLastNext() << endl;
	}
	nr++;
	iter0.next();
    }
    cout << "   #iter=" << nr << endl;

    iter0.reset();
    nr = 0;
    while (!iter0.pastEnd()) {
	t = iter0.table();
	ScalarColumn<Int> col1(t, "col1");
	Vector<Int> vec;
	col1.getColumn (vec);
	cout << t.nrow() << " ";
	if (!allEQ(vec, nr)) {
	    cout << "error in iter. " << nr << " " << vec(0)
		 << " " << vec(vec.nelements()-1) << " " << vec.nelements() << endl;
	}
	nr++;
	iter0.next();
    }
    cout << "   #iter1=" << nr << endl;
}

void doiter2()
{
    Table tab1 ("tTableIter_tmp.data", Table::Update);
    Table t1;
    Block<String> iv1(2);
    iv1[0] = "col2";
    iv1[1] = "col1";
    TableIterator iter1(tab1, iv1);
    Int nr = 0;
    Int l1 = -1;
    double l2 = -1;
    while (!iter1.pastEnd()) {
	t1 = iter1.table();
	ScalarColumn<Int> col1(t1, "col1");
	ScalarColumn<double> col2(t1, "col2");
	Vector<Int> vec1;
	col1.getColumn (vec1);
	Vector<double> vec2;
	col2.getColumn (vec2);
	if (!(allEQ(vec1, vec1(0))  &&  allEQ(vec2, vec2(0)))) {
	    cout << "error in iter. " << nr << " " << vec1(0) << " "
		 << vec1(vec1.nelements()-1) << " " << vec2(0) << " "
		 << vec2(vec2.nelements()-1) << endl;
	}
	if (vec2(0) < l2  ||  (vec2(0) == l2  &&  vec1(0) <= l1)) {
	    cout << "order error " << vec2(0) << " " << l2 << ", "
		 << vec1(0) << " " << l1 << endl;
	}
	l1 = vec1(0);
	l2 = vec2(0);
	nr++;
	iter1.next();
    }
    cout << "   #iter2=" << nr << endl;
}

void doiter3()
{
    Table tab1 ("tTableIter_tmp.data");
    Table t1;
    Block<String> iv1(1);
    iv1[0] = "col3";
    Block<CountedPtr<BaseCompare> > compObj(1);
    CompareIntervalReal<Float> xx(10., 0.);
    CountedPtr<BaseCompare> xxx (new CompareIntervalReal<Float>(10., 0.));
    compObj[0] = xxx;
    Block<Int> orders(1);
    orders[0] = TableIterator::Ascending;
    TableIterator iter1(tab1, iv1, compObj, orders);

    TableIterator iter2;
    iter2 = iter1;
    AlwaysAssertExit(iter2.table().nrow() == iter1.table().nrow());

    // Test that copied new iterator gives the same number of rows
    int iter1count = 0;
    while(!iter1.pastEnd()) { iter1.next(); iter1count++; }
    int iter2count = 0;
    while(!iter2.pastEnd()) { iter2.next(); iter2count++; }
    AlwaysAssertExit(iter1count == iter2count);
    iter1.reset();
    iter2.reset();

    // Test that copied new iterator does not copy the state
    iter1.next();
    iter1.next(); // Advance iter1 by two, so state changes
    iter1count = 0;
    iter2 = iter1; // iter2 should be 'clean' and iterate 2 more than iter1
    TableIterator iter3 = iter1;
    iter3.copyState(iter1); // iter3 should iterate as much as iter1
    while(!iter1.pastEnd()) { iter1.next(); iter1count++; }
    iter2count = 0;
    while(!iter2.pastEnd()) { iter2.next(); iter2count++; }
    AlwaysAssertExit(iter2count == iter1count + 2);
    int iter3count = 0;
    while(!iter3.pastEnd()) { iter3.next(); iter3count++; }
    AlwaysAssertExit(iter3count == iter1count);

    iter1.reset();

    Int nr = 0;
    float l3 = -1;
    while (!iter1.pastEnd()) {
	t1 = iter1.table();
        cout << t1.nrow() << " ";
	ScalarColumn<Float> col3(t1, "col3");
	Vector<Float> vec3;
        col3.getColumn (vec3);
        if (max(vec3) - min(vec3) > 9.5) {
          cout << "Interval order error" << endl;
        }
	if (vec3(0) < l3) {
          cout << "order error " << vec3(0) << " " << l3 << endl;
	}
	l3 = vec3(0);
	nr++;
	iter1.next();
    }
    cout << "   #iter3=" << nr << endl;
}

void test_cache_boundaries()
{
    // Create two iterators, one that cached the boundaries between iterations
    // and one that does not.
    Table tab1 ("tTableIter_tmp.data");
    Table t1;
    Block<String> sortCols(2);
    sortCols[0] = "col2";
    sortCols[1] = "col1";
    Block<CountedPtr<BaseCompare> > compObj(2);
    compObj[0] = nullptr;
    compObj[1] = nullptr;
    Block<Int> orders(2);
    orders[0] = TableIterator::Ascending;
    orders[1] = TableIterator::Ascending;
    TableIterator iter1(tab1, sortCols, compObj, orders);
    TableIterator iter2(tab1, sortCols, compObj, orders, TableIterator::ParSort, true);

    iter1.reset();
    iter2.reset();

    while (!iter1.pastEnd()) {
        auto iter1Table = iter1.table();
        auto iter2Table = iter2.table();
        AlwaysAssertExit(iter2Table.nrow() == iter1Table.nrow());
        ScalarColumn<Int>     iter1Col1 (iter1Table, "col1");
        ScalarColumn<double>  iter1Col2 (iter1Table, "col2");
        ScalarColumn<float>   iter1Col3 (iter1Table, "col3");
        ScalarColumn<Complex> iter1Col4 (iter1Table, "col4");
        ScalarColumn<Int>     iter2Col1 (iter2Table, "col1");
        ScalarColumn<double>  iter2Col2 (iter2Table, "col2");
        ScalarColumn<float>   iter2Col3 (iter2Table, "col3");
        ScalarColumn<Complex> iter2Col4 (iter2Table, "col4");
        Vector<int>     iter1Vec1;
        Vector<double>  iter1Vec2;
        Vector<float>   iter1Vec3;
        Vector<Complex> iter1Vec4;
        Vector<int>     iter2Vec1;
        Vector<double>  iter2Vec2;
        Vector<float>   iter2Vec3;
        Vector<Complex> iter2Vec4;
        iter1Col1.getColumn (iter1Vec1);
        iter1Col2.getColumn (iter1Vec2);
        iter1Col3.getColumn (iter1Vec3);
        iter1Col4.getColumn (iter1Vec4);
        iter2Col1.getColumn (iter2Vec1);
        iter2Col2.getColumn (iter2Vec2);
        iter2Col3.getColumn (iter2Vec3);
        iter2Col4.getColumn (iter2Vec4);
        // Check that all the columns for this iteration have the same 
        // content in both iterators.
        AlwaysAssertExit(allEQ(iter1Vec1, iter2Vec1));
        AlwaysAssertExit(allEQ(iter1Vec2, iter2Vec2));
        AlwaysAssertExit(allEQ(iter1Vec3, iter2Vec3));
        AlwaysAssertExit(allEQ(iter1Vec4, iter2Vec4));
        // Check that the changing key is the same in both iterators.
        AlwaysAssertExit(iter1.keyChangeAtLastNext() == iter2.keyChangeAtLastNext());
        iter1.next();
        iter2.next();
    }
}