casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@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 //# $Id$
27 
28 #ifndef TABLES_TABLEITER_H
29 #define TABLES_TABLEITER_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward Declarations
40 class BaseTableIterator;
41 class String;
42 template<class T> class Block;
43 
44 
45 // <summary>
46 // Iterate through a Table
47 // </summary>
48 
49 // <use visibility=export>
50 
51 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
52 // </reviewed>
53 
54 // <prerequisite>
55 //# Classes you should understand before using this one.
56 // <li> Table
57 // <li> Sort
58 // </prerequisite>
59 
60 // <synopsis>
61 // TableIterator is a class allowing one to iterate in an arbitrary
62 // way through a table. Each iteration step returns a Table
63 // containing the result of the iteration step.
64 // It is possible to have more than one iterator on a table.
65 //
66 // An iteration is defined by giving the columns over which to iterate.
67 // For example, take a UV data set with "axes" frequency, baseline and
68 // time. Getting all frequencies per time and baseline can be done
69 // by iterating over columns time and baseline (as shown in the example).
70 // The main iteration column must be given first.
71 // It is possible to define an iteration order per column.
72 // <br>It is also possible to define a compare object per column.
73 // For example, CompareIntervalReal can be used to iterate in intervals
74 // over, say, the TIME column by treating a range of values as equal
75 // (e.g. iterate in 60 seconds time intervals).
76 //
77 // The table is sorted before doing the iteration unless TableIterator::NoSort
78 // is given.
79 // </synopsis>
80 
81 // <example>
82 // <srcblock>
83 // // Iterate over time and baseline (by default in ascending order).
84 // // Time is the main iteration order.
85 // Table t;
86 // Table tab ("UV_Table.data");
87 // Block<String> iv0(2);
88 // iv0[0] = "time";
89 // iv0[1] = "baseline";
90 // // Create the iterator. This will prepare the first subtable.
91 // TableIterator iter(tab, iv0);
92 // Int nr = 0;
93 // while (!iter.pastEnd()) {
94 // // Get the first subtable.
95 // // This will contain rows with equal time and baseline.
96 // t = iter.table();
97 // cout << t.nrow() << " ";
98 // nr++;
99 // // Prepare the next subtable with the next time,baseline value.
100 // iter.next();
101 // }
102 // cout << endl << nr << " iteration steps" << endl;
103 // </srcblock>
104 // </example>
105 
106 // <motivation>
107 // It is sometimes needed to access all data in a table in a grouped
108 // way; for example, all frequencies per time and baseline.
109 // This can perfectly be done with an iterator.
110 // </motivation>
111 
112 //# <todo asof="$DATE:$">
113 //# A List of bugs, limitations, extensions or planned refinements.
114 //# </todo>
115 
116 
118 {
119 public:
120 
121  // Define the possible iteration orders.
123  // Define the possible sorts.
128  NoSort = 64};
129 
130  // Create a null TableIterator object (i.e. no iterator is attached yet).
131  // The sole purpose of this constructor is to allow construction
132  // of an array of TableIterator objects.
133  // The assignment operator can be used to make a null object
134  // reference a column.
135  // Note that sort functions, etc. will cause a segmentation fault
136  // when operating on a null object. It was felt it was too expensive
137  // to test on null over and over again. The user should use the isNull
138  // or throwIfNull function in case of doubt.
139  TableIterator();
140 
141  // Create a table iterator on the given column(s) for the given table.
142  // Each iteration step results in a Table containing all
143  // rows in which the values in each given column is equal.
144  // The column vector can be empty, resulting in a single iteration step
145  // giving the entire table.
146  // An iteration order can be given; it defaults to Ascending.
147  // Per column a compare object can be given to use other compare
148  // functions than the standard ones defined in Compare.h.
149  // The compare functions are used for both the sort and the iteration.
150  // The option argument makes it possible to choose from various
151  // sorting algorithms. Usually ParSort is the fastest, but for
152  // a single core machine QuickSort usually performs better.
153  // InsSort (insertion sort) should only be used if the input
154  // is almost in order.
155  // If it is known that the table is already in order, the sort step can be
156  // bypassed by giving the option TableIterator::NoSort.
157  // The default option is ParSort.
158  // <group>
159  TableIterator (const Table&, const String& columnName,
161  TableIterator (const Table&, const Block<String>& columnNames,
163  // Give the iteration order per column.
164  // <note>If an interval comparison object like CompareIntervalReal
165  // is used, the data are sorted on the interval, not on the value.
166  // One should consider to do an explicitsort on value and no iteration sort.
167  // </note>
168  TableIterator (const Table&, const Block<String>& columnNames,
169  const Block<Int>& orders, Option = ParSort);
170  // Give the iteration order per column.
171  // Give an optional compare object per column.
172  // A zero pointer means that the default compare function will be used.
173  // If cacheIterationBoundaries is set to true then the iteration
174  // boundaries computed at construction time while sorting the table
175  // are used when advancing with next(). Otherwise, for each next()
176  // call the comparison functions are reevaluated again to get the
177  // iteration boundary. This improves performance in general but will
178  // break existing applications that change the comparison objects
179  // (cmpObjs) between iterations.
180  TableIterator (const Table&, const Block<String>& columnNames,
181  const Block<CountedPtr<BaseCompare> >& cmpObjs,
182  const Block<Int>& orders, Option = ParSort,
183  bool cacheIterationBoundaries = false);
184  // </group>
185 
186  // Copy constructor (copy semantics).
187  TableIterator (const TableIterator&);
188 
189  ~TableIterator();
190 
191  // Assignment (copy semantics).
193 
194  // Test if the object is null, i.e. does not reference a table yet.
195  // This is the case if the default constructor is used.
196  Bool isNull() const
197  { return (tabIterPtr_p == 0 ? True : False); }
198 
199  // Throw an exception if the object is null, i.e.
200  // if function isNull() is True.
201  void throwIfNull() const;
202 
203  // Reset the iterator (i.e. restart iteration).
204  void reset();
205 
206  // Test if at the end.
207  Bool pastEnd() const;
208 
209  // Go to the next group.
210  // <group>
211  void next();
212  void operator++();
213  void operator++(int);
214  // </group>
215 
216  void copyState(const TableIterator &);
217 
218  // Report Name of slowest column that changes at end of current iteration
219  const String& keyChangeAtLastNext() const;
220 
221  // Get the current group.
222  Table table() const;
223 
224 protected:
227 };
228 
229 
230 
231 //# Iterator is at the end if the subtable is empty.
233  { return (subTable_p.nrow() == 0 ? True : False); }
234 
236  { return subTable_p; }
237 
239  { next(); }
240 
242  { next(); }
243 
244 
245 
246 
247 
248 } //# NAMESPACE CASACORE - END
249 
250 #endif
TableIterator()
Create a null TableIterator object (i.e.
void copyState(const TableIterator &)
Main interface class to a read/write table.
Definition: Table.h:157
rownr_t nrow() const
Get the number of rows.
Definition: Table.h:1171
Iterate through a Table.
Definition: TableIter.h:117
Bool pastEnd() const
Test if at the end.
Definition: TableIter.h:232
Base class for table iterator.
Definition: BaseTabIter.h:83
const String & keyChangeAtLastNext() const
Report Name of slowest column that changes at end of current iteration.
BaseTableIterator * tabIterPtr_p
Definition: TableIter.h:225
Order
Define the possible iteration orders.
Definition: TableIter.h:122
Option
Define the possible sorts.
Definition: TableIter.h:124
Referenced counted pointer for constant data.
Definition: CountedPtr.h:80
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
void next()
Go to the next group.
const Bool False
Definition: aipstype.h:44
Bool isNull() const
Test if the object is null, i.e.
Definition: TableIter.h:196
simple 1-D array
Definition: Allocator.h:210
Table table() const
Get the current group.
Definition: TableIter.h:235
String: the storage and methods of handling collections of characters.
Definition: String.h:225
TableIterator & operator=(const TableIterator &)
Assignment (copy semantics).
const Bool True
Definition: aipstype.h:43
void throwIfNull() const
Throw an exception if the object is null, i.e.
void reset()
Reset the iterator (i.e.