casacore
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>
33 #include <casacore/tables/Tables/Table.h>
34 #include <casacore/casa/Utilities/Sort.h>
35 #include <casacore/casa/Utilities/Compare.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 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  // 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  TableIterator (const Table&, const Block<String>& columnNames,
173  const Block<Int>& orders, Option = ParSort);
174  // </group>
175 
176  // Copy constructor (copy semantics).
177  TableIterator (const TableIterator&);
178 
179  ~TableIterator();
180 
181  // Assignment (copy semantics).
183 
184  // Test if the object is null, i.e. does not reference a table yet.
185  // This is the case if the default constructor is used.
186  Bool isNull() const
187  { return (tabIterPtr_p == 0 ? True : False); }
188 
189  // Throw an exception if the object is null, i.e.
190  // if function isNull() is True.
191  void throwIfNull() const;
192 
193  // Reset the iterator (i.e. restart iteration).
194  void reset();
195 
196  // Test if at the end.
197  Bool pastEnd() const;
198 
199  // Go to the next group.
200  // <group>
201  void next();
202  void operator++();
203  void operator++(int);
204  // </group>
205 
206  // Get the current group.
207  Table table() const;
208 
209 protected:
212 };
213 
214 
215 
216 //# Iterator is at the end if the subtable is empty.
218  { return (subTable_p.nrow() == 0 ? True : False); }
219 
221  { return subTable_p; }
222 
224  { next(); }
225 
227  { next(); }
228 
229 
230 
231 
232 
233 } //# NAMESPACE CASACORE - END
234 
235 #endif
TableIterator()
Create a null TableIterator object (i.e.
Main interface class to a read/write table.
Definition: Table.h:149
Iterate through a Table.
Definition: TableIter.h:117
Bool pastEnd() const
Test if at the end.
Definition: TableIter.h:217
Base class for table iterator.
Definition: BaseTabIter.h:83
BaseTableIterator * tabIterPtr_p
Definition: TableIter.h:210
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:86
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
void next()
Go to the next group.
uInt nrow() const
Get the number of rows.
Definition: Table.h:1147
const Bool False
Definition: aipstype.h:41
Bool isNull() const
Test if the object is null, i.e.
Definition: TableIter.h:186
simple 1-D array
Definition: ArrayIO.h:47
Table table() const
Get the current group.
Definition: TableIter.h:220
String: the storage and methods of handling collections of characters.
Definition: String.h:223
TableIterator & operator=(const TableIterator &)
Assignment (copy semantics).
const Bool True
Definition: aipstype.h:40
void throwIfNull() const
Throw an exception if the object is null, i.e.
this file contains all the compiler specific defines
Definition: mainpage.dox:28
void reset()
Reset the iterator (i.e.