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
39class String;
40template <class T>
41class Block;
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
115 public:
116 // Define the possible iteration orders.
118 // Define the possible sorts.
126
127 // Create a null TableIterator object (i.e. no iterator is attached yet).
128 // The sole purpose of this constructor is to allow construction
129 // of an array of TableIterator objects.
130 // The assignment operator can be used to make a null object
131 // reference a column.
132 // Note that sort functions, etc. will cause a segmentation fault
133 // when operating on a null object. It was felt it was too expensive
134 // to test on null over and over again. The user should use the isNull
135 // or throwIfNull function in case of doubt.
137
138 // Create a table iterator on the given column(s) for the given table.
139 // Each iteration step results in a Table containing all
140 // rows in which the values in each given column is equal.
141 // The column vector can be empty, resulting in a single iteration step
142 // giving the entire table.
143 // An iteration order can be given; it defaults to Ascending.
144 // Per column a compare object can be given to use other compare
145 // functions than the standard ones defined in Compare.h.
146 // The compare functions are used for both the sort and the iteration.
147 // The option argument makes it possible to choose from various
148 // sorting algorithms. Usually ParSort is the fastest, but for
149 // a single core machine QuickSort usually performs better.
150 // InsSort (insertion sort) should only be used if the input
151 // is almost in order.
152 // If it is known that the table is already in order, the sort step can be
153 // bypassed by giving the option TableIterator::NoSort.
154 // The default option is ParSort.
155 // <group>
156 TableIterator(const Table&, const String& columnName, Order = Ascending, Option = ParSort);
157 TableIterator(const Table&, const Block<String>& columnNames, Order = Ascending,
158 Option = ParSort);
159 // Give the iteration order per column.
160 // <note>If an interval comparison object like CompareIntervalReal
161 // is used, the data are sorted on the interval, not on the value.
162 // One should consider to do an explicitsort on value and no iteration sort.
163 // </note>
164 TableIterator(const Table&, const Block<String>& columnNames, const Block<Int>& orders,
165 Option = ParSort);
166 // Give the iteration order per column.
167 // Give an optional compare object per column.
168 // A zero pointer means that the default compare function will be used.
169 // If cacheIterationBoundaries is set to true then the iteration
170 // boundaries computed at construction time while sorting the table
171 // are used when advancing with next(). Otherwise, for each next()
172 // call the comparison functions are reevaluated again to get the
173 // iteration boundary. This improves performance in general but will
174 // break existing applications that change the comparison objects
175 // (cmpObjs) between iterations.
176 TableIterator(const Table&, const Block<String>& columnNames,
177 const Block<std::shared_ptr<BaseCompare>>& cmpObjs, const Block<Int>& orders,
178 Option = ParSort, bool cacheIterationBoundaries = false);
179 // </group>
180
181 // Copy constructor (copy semantics).
183
185
186 // Assignment (copy semantics).
188
189 // Test if the object is null, i.e. does not reference a table yet.
190 // This is the case if the default constructor is used.
191 Bool isNull() const { return !tabIterPtr_p; }
192
193 // Throw an exception if the object is null, i.e.
194 // if function isNull() is True.
195 void throwIfNull() const;
196
197 // Reset the iterator (i.e. restart iteration).
198 void reset();
199
200 // Test if at the end.
201 Bool pastEnd() const;
202
203 // Go to the next group.
204 // <group>
205 void next();
206 void operator++();
207 void operator++(int);
208 // </group>
209
211
212 // Report Name of slowest column that changes at end of current iteration
214
215 // Get the current group.
216 Table table() const;
217
218 protected:
221};
222
223// # Iterator is at the end if the subtable is empty.
224inline Bool TableIterator::pastEnd() const { return (subTable_p.nrow() == 0 ? True : False); }
225
226inline Table TableIterator::table() const { return subTable_p; }
227
228inline void TableIterator::operator++() { next(); }
229
230inline void TableIterator::operator++(int) { next(); }
231
232} // namespace casacore
233
234#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
BaseTableIterator * tabIterPtr_p
Definition TableIter.h:219
TableIterator(const TableIterator &)
Copy constructor (copy semantics).
Bool pastEnd() const
Test if at the end.
Definition TableIter.h:224
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:117
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:226
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:119
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:191
For temporary backward namespace compatibility, use casa as alias for casacore.
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