casacore
Loading...
Searching...
No Matches
TableIter.h
Go to the documentation of this file.
1//# TableIter.h: Iterate through a Table
2//# Copyright (C) 1994,1995,1996,1997,1999,2000
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef TABLES_TABLEITER_H
27#define TABLES_TABLEITER_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/Tables/Table.h>
32#include <casacore/casa/Utilities/Sort.h>
33#include <casacore/casa/Utilities/Compare.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37//# Forward Declarations
38class BaseTableIterator;
39class String;
40template<class T> class Block;
41
42
43// <summary>
44// Iterate through a Table
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
50// </reviewed>
51
52// <prerequisite>
53//# Classes you should understand before using this one.
54// <li> Table
55// <li> Sort
56// </prerequisite>
57
58// <synopsis>
59// TableIterator is a class allowing one to iterate in an arbitrary
60// way through a table. Each iteration step returns a Table
61// containing the result of the iteration step.
62// It is possible to have more than one iterator on a table.
63//
64// An iteration is defined by giving the columns over which to iterate.
65// For example, take a UV data set with "axes" frequency, baseline and
66// time. Getting all frequencies per time and baseline can be done
67// by iterating over columns time and baseline (as shown in the example).
68// The main iteration column must be given first.
69// It is possible to define an iteration order per column.
70// <br>It is also possible to define a compare object per column.
71// For example, CompareIntervalReal can be used to iterate in intervals
72// over, say, the TIME column by treating a range of values as equal
73// (e.g. iterate in 60 seconds time intervals).
74//
75// The table is sorted before doing the iteration unless TableIterator::NoSort
76// is given.
77// </synopsis>
78
79// <example>
80// <srcblock>
81// // Iterate over time and baseline (by default in ascending order).
82// // Time is the main iteration order.
83// Table t;
84// Table tab ("UV_Table.data");
85// Block<String> iv0(2);
86// iv0[0] = "time";
87// iv0[1] = "baseline";
88// // Create the iterator. This will prepare the first subtable.
89// TableIterator iter(tab, iv0);
90// Int nr = 0;
91// while (!iter.pastEnd()) {
92// // Get the first subtable.
93// // This will contain rows with equal time and baseline.
94// t = iter.table();
95// cout << t.nrow() << " ";
96// nr++;
97// // Prepare the next subtable with the next time,baseline value.
98// iter.next();
99// }
100// cout << endl << nr << " iteration steps" << endl;
101// </srcblock>
102// </example>
103
104// <motivation>
105// It is sometimes needed to access all data in a table in a grouped
106// way; for example, all frequencies per time and baseline.
107// This can perfectly be done with an iterator.
108// </motivation>
109
110//# <todo asof="$DATE:$">
111//# A List of bugs, limitations, extensions or planned refinements.
112//# </todo>
113
114
116{
117public:
118
119 // Define the possible iteration orders.
121 // Define the possible sorts.
127
128 // Create a null TableIterator object (i.e. no iterator is attached yet).
129 // The sole purpose of this constructor is to allow construction
130 // of an array of TableIterator objects.
131 // The assignment operator can be used to make a null object
132 // reference a column.
133 // Note that sort functions, etc. will cause a segmentation fault
134 // when operating on a null object. It was felt it was too expensive
135 // to test on null over and over again. The user should use the isNull
136 // or throwIfNull function in case of doubt.
138
139 // Create a table iterator on the given column(s) for the given table.
140 // Each iteration step results in a Table containing all
141 // rows in which the values in each given column is equal.
142 // The column vector can be empty, resulting in a single iteration step
143 // giving the entire table.
144 // An iteration order can be given; it defaults to Ascending.
145 // Per column a compare object can be given to use other compare
146 // functions than the standard ones defined in Compare.h.
147 // The compare functions are used for both the sort and the iteration.
148 // The option argument makes it possible to choose from various
149 // sorting algorithms. Usually ParSort is the fastest, but for
150 // a single core machine QuickSort usually performs better.
151 // InsSort (insertion sort) should only be used if the input
152 // is almost in order.
153 // If it is known that the table is already in order, the sort step can be
154 // bypassed by giving the option TableIterator::NoSort.
155 // The default option is ParSort.
156 // <group>
157 TableIterator (const Table&, const String& columnName,
159 TableIterator (const Table&, const Block<String>& columnNames,
161 // Give the iteration order per column.
162 // <note>If an interval comparison object like CompareIntervalReal
163 // is used, the data are sorted on the interval, not on the value.
164 // One should consider to do an explicitsort on value and no iteration sort.
165 // </note>
166 TableIterator (const Table&, const Block<String>& columnNames,
167 const Block<Int>& orders, Option = ParSort);
168 // Give the iteration order per column.
169 // Give an optional compare object per column.
170 // A zero pointer means that the default compare function will be used.
171 // If cacheIterationBoundaries is set to true then the iteration
172 // boundaries computed at construction time while sorting the table
173 // are used when advancing with next(). Otherwise, for each next()
174 // call the comparison functions are reevaluated again to get the
175 // iteration boundary. This improves performance in general but will
176 // break existing applications that change the comparison objects
177 // (cmpObjs) between iterations.
178 TableIterator (const Table&, const Block<String>& columnNames,
179 const Block<std::shared_ptr<BaseCompare>>& cmpObjs,
180 const Block<Int>& orders, Option = ParSort,
181 bool cacheIterationBoundaries = false);
182 // </group>
183
184 // Copy constructor (copy semantics).
186
188
189 // Assignment (copy semantics).
191
192 // Test if the object is null, i.e. does not reference a table yet.
193 // This is the case if the default constructor is used.
194 Bool isNull() const
195 { return !tabIterPtr_p; }
196
197 // Throw an exception if the object is null, i.e.
198 // if function isNull() is True.
199 void throwIfNull() const;
200
201 // Reset the iterator (i.e. restart iteration).
202 void reset();
203
204 // Test if at the end.
205 Bool pastEnd() const;
206
207 // Go to the next group.
208 // <group>
209 void next();
210 void operator++();
211 void operator++(int);
212 // </group>
213
215
216 // Report Name of slowest column that changes at end of current iteration
218
219 // Get the current group.
220 Table table() const;
221
222protected:
225};
226
227
228
229//# Iterator is at the end if the subtable is empty.
231 { return (subTable_p.nrow() == 0 ? True : False); }
232
234 { return subTable_p; }
235
237 { next(); }
238
240 { next(); }
241
242
243
244
245
246} //# NAMESPACE CASACORE - END
247
248#endif
simple 1-D array
Definition Block.h:198
String: the storage and methods of handling collections of characters.
Definition String.h:223
BaseTableIterator * tabIterPtr_p
Definition TableIter.h:223
TableIterator(const TableIterator &)
Copy constructor (copy semantics).
Bool pastEnd() const
Test if at the end.
Definition TableIter.h:230
void copyState(const TableIterator &)
TableIterator(const Table &, const String &columnName, Order=Ascending, Option=ParSort)
Create a table iterator on the given column(s) for the given table.
void throwIfNull() const
Throw an exception if the object is null, i.e.
Order
Define the possible iteration orders.
Definition TableIter.h:120
const String & keyChangeAtLastNext() const
Report Name of slowest column that changes at end of current iteration.
TableIterator & operator=(const TableIterator &)
Assignment (copy semantics).
void next()
Go to the next group.
TableIterator(const Table &, const Block< String > &columnNames, const Block< Int > &orders, Option=ParSort)
Give the iteration order per column.
Table table() const
Get the current group.
Definition TableIter.h:233
TableIterator(const Table &, const Block< String > &columnNames, const Block< std::shared_ptr< BaseCompare > > &cmpObjs, const Block< Int > &orders, Option=ParSort, bool cacheIterationBoundaries=false)
Give the iteration order per column.
Option
Define the possible sorts.
Definition TableIter.h:122
TableIterator(const Table &, const Block< String > &columnNames, Order=Ascending, Option=ParSort)
void reset()
Reset the iterator (i.e.
TableIterator()
Create a null TableIterator object (i.e.
Bool isNull() const
Test if the object is null, i.e.
Definition TableIter.h:194
rownr_t nrow() const
Get the number of rows.
Definition Table.h:1176
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41