casa
$Rev:20696$
|
00001 //# TableIter.h: Iterate through a Table 00002 //# Copyright (C) 1994,1995,1996,1997,1999,2000 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: TableIter.h 20997 2010-11-17 07:05:29Z gervandiepen $ 00027 00028 #ifndef TABLES_TABLEITER_H 00029 #define TABLES_TABLEITER_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <tables/Tables/Table.h> 00034 #include <casa/Utilities/Compare.h> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 //# Forward Declarations 00039 class BaseTableIterator; 00040 class String; 00041 template<class T> class Block; 00042 00043 00044 // <summary> 00045 // Iterate through a Table 00046 // </summary> 00047 00048 // <use visibility=export> 00049 00050 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests=""> 00051 // </reviewed> 00052 00053 // <prerequisite> 00054 //# Classes you should understand before using this one. 00055 // <li> Table 00056 // <li> Sort 00057 // </prerequisite> 00058 00059 // <synopsis> 00060 // TableIterator is a class allowing one to iterate in an arbitrary 00061 // way through a table. Each iteration step returns a Table 00062 // containing the result of the iteration step. 00063 // It is possible to have more than one iterator on a table. 00064 // 00065 // An iteration is defined by giving the columns over which to iterate. 00066 // For example, take a UV data set with "axes" frequency, baseline and 00067 // time. Getting all frequencies per time and baseline can be done 00068 // by iterating over columns time and baseline (as shown in the example). 00069 // The main iteration column must be given first. 00070 // It is possible to define an iteration order per column. 00071 // It is also possible to define a compare object per column. 00072 // This can, for example, be used to iterate in intervals by treating 00073 // a range of values as equal (e.g. iterate in 60 seconds time intervals). 00074 // 00075 // Currently the table is sorted before doing the iteration. 00076 // This may change in the future when, for example, indices are supported. 00077 // </synopsis> 00078 00079 // <example> 00080 // <srcblock> 00081 // // Iterate over time and baseline (by default in ascending order). 00082 // // Time is the main iteration order. 00083 // Table t; 00084 // Table tab ("UV_Table.data"); 00085 // Block<String> iv0(2); 00086 // iv0[0] = "time"; 00087 // iv0[1] = "baseline"; 00088 // // Create the iterator. This will prepare the first subtable. 00089 // TableIterator iter(tab, iv0); 00090 // Int nr = 0; 00091 // while (!iter.pastEnd()) { 00092 // // Get the first subtable. 00093 // // This will contain rows with equal time and baseline. 00094 // t = iter.table(); 00095 // cout << t.nrow() << " "; 00096 // nr++; 00097 // // Prepare the next subtable with the next time,baseline value. 00098 // iter.next(); 00099 // } 00100 // cout << endl << nr << " iteration steps" << endl; 00101 // </srcblock> 00102 // </example> 00103 00104 // <motivation> 00105 // It is sometimes needed to access all data in a table in a grouped 00106 // way; for example, all frequencies per time and baseline. 00107 // This can perfectly be done with an iterator. 00108 // </motivation> 00109 00110 //# <todo asof="$DATE:$"> 00111 //# A List of bugs, limitations, extensions or planned refinements. 00112 //# </todo> 00113 00114 00115 class TableIterator 00116 { 00117 public: 00118 00119 // Define the possible iteration orders. 00120 enum Order {Ascending=-1, Descending=1}; 00121 // Define the possible sorts. 00122 enum Option {QuickSort, HeapSort, InsSort, NoSort}; 00123 00124 // Create a null TableIterator object (i.e. no iterator is attached yet). 00125 // The sole purpose of this constructor is to allow construction 00126 // of an array of TableIterator objects. 00127 // The assignment operator can be used to make a null object 00128 // reference a column. 00129 // Note that sort functions, etc. will cause a segmentation fault 00130 // when operating on a null object. It was felt it was too expensive 00131 // to test on null over and over again. The user should use the isNull 00132 // or throwIfNull function in case of doubt. 00133 TableIterator(); 00134 00135 // Create a table iterator for the given table. 00136 // Each iteration step results in a Table containing all 00137 // rows in which the values in each given column is equal. 00138 // An iteration order can be given; it defaults to Ascending. 00139 // Per column a compare object can be given to use other compare 00140 // functions than the standard ones defined in Compare.h. 00141 // The compare functions are used for both the sort and the iteration. 00142 // The option argument makes it possible to choose from various 00143 // sorting algorithms. Usually QuickSort is the fastest, but for 00144 // some ill-conditioned input HeapSort performs much better. 00145 // InsSort (insertion sort) should only be used when the input 00146 // is almost in order. 00147 // When the table is already in order, the sort step can be bypassed 00148 // by giving the option TableIterator::NoSort. 00149 // The default option is HeapSort. 00150 // <group> 00151 TableIterator (const Table&, const String& columnName, 00152 Order = Ascending, Option = HeapSort); 00153 TableIterator (const Table&, const Block<String>& columnNames, 00154 Order = Ascending, Option = HeapSort); 00155 // Give the iteration order per column. 00156 TableIterator (const Table&, const Block<String>& columnNames, 00157 const Block<Int>& orders, Option = HeapSort); 00158 // Give the iteration order per column. 00159 // Give an optional compare object per column. 00160 // A zero pointer means that the default compare function will be used. 00161 TableIterator (const Table&, const Block<String>& columnNames, 00162 const Block<CountedPtr<BaseCompare> >&, 00163 const Block<Int>& orders, Option = HeapSort); 00164 // </group> 00165 00166 // Copy constructor (copy semantics). 00167 TableIterator (const TableIterator&); 00168 00169 ~TableIterator(); 00170 00171 // Assignment (copy semantics). 00172 TableIterator& operator= (const TableIterator&); 00173 00174 // Test if the object is null, i.e. does not reference a table yet. 00175 // This is the case if the default constructor is used. 00176 Bool isNull() const 00177 { return (tabIterPtr_p == 0 ? True : False); } 00178 00179 // Throw an exception if the object is null, i.e. 00180 // if function isNull() is True. 00181 void throwIfNull() const; 00182 00183 // Reset the iterator (i.e. restart iteration). 00184 void reset(); 00185 00186 // Test if at the end. 00187 Bool pastEnd() const; 00188 00189 // Go to the next group. 00190 // <group> 00191 void next(); 00192 void operator++(); 00193 void operator++(int); 00194 // </group> 00195 00196 // Get the current group. 00197 Table table() const; 00198 00199 protected: 00200 BaseTableIterator* tabIterPtr_p; 00201 Table subTable_p; 00202 }; 00203 00204 00205 00206 //# Iterator is at the end if the subtable is empty. 00207 inline Bool TableIterator::pastEnd() const 00208 { return (subTable_p.nrow() == 0 ? True : False); } 00209 00210 inline Table TableIterator::table() const 00211 { return subTable_p; } 00212 00213 inline void TableIterator::operator++() 00214 { next(); } 00215 00216 inline void TableIterator::operator++(int) 00217 { next(); } 00218 00219 00220 00221 00222 } //# NAMESPACE CASA - END 00223 00224 #endif