casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MSTableIndex.h
Go to the documentation of this file.
00001  //# MSTableIndex: index into a MeasurementSet sub-table
00002 //# Copyright (C) 2000,2001,2002
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 //#
00027 //# $Id: MSTableIndex.h 20652 2009-07-06 05:04:32Z Malte.Marquarding $
00028 
00029 #ifndef MS_MSTABLEINDEX_H
00030 #define MS_MSTABLEINDEX_H
00031 
00032 #include <casa/Arrays/Vector.h>
00033 #include <casa/Containers/Block.h>
00034 #include <casa/Containers/RecordField.h>
00035 #include <tables/Tables/Table.h>
00036 #include <tables/Tables/ScalarColumn.h>
00037 #include <tables/Tables/ColumnsIndex.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 //# Forward declarations
00042 class Record;
00043 // class ColumnsIndex;
00044 class String;
00045 
00046 // <summary>
00047 // </summary>
00048 
00049 // <use visibility=export>
00050 
00051 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00052 // </reviewed>
00053 
00054 // <prerequisite>
00055 //   <li> MeasurementSet
00056 //   <li> ColumnsIndex
00057 // </prerequisite>
00058 //
00059 // <etymology>
00060 // </etymology>
00061 //
00062 // <synopsis>
00063 // </synopsis>
00064 //
00065 // <example>
00066 // </example>
00067 //
00068 // <motivation>
00069 // </motivation>
00070 //
00071 // <thrown>
00072 //    <li>
00073 //    <li>
00074 // </thrown>
00075 //
00076 // <todo>
00077 //    <li> Make the searches smarter - for TIME sorted tables, if the time to search is
00078 //         past the last seach, there's no need to search any earlier times.
00079 //    <li> Need to handle the INTERVAL=-1 case fully
00080 // </todo>
00081 class MSTableIndex
00082 {
00083 public:
00084     // no index attached, use the attach function or assignment operator to change that
00085     MSTableIndex();
00086 
00087     // construct one using the indicated subtable which is part of the parent MS
00088     // using the indicated index columns.  All index columns must be scalar integer
00089     // columns.  TIME and INTERVAL will be used when present.  A compare function
00090     // can be provided to over-ride literal matching of column values.
00091     MSTableIndex(const Table &subTable, const Vector<String> &indexCols, 
00092                  ColumnsIndex::Compare *compareFunction = 0);
00093 
00094     // construct one from another
00095     MSTableIndex(const MSTableIndex &other);
00096 
00097     virtual ~MSTableIndex();
00098 
00099     // assignment operator, refernce semantics
00100     MSTableIndex &operator=(const MSTableIndex &other);
00101 
00102     // attach this to a subtable using indexCols
00103     void attach(const Table &subTable, const Vector<String> &indexCols,
00104                         ColumnsIndex::Compare *compareFunction = 0);
00105 
00106     // Call this when an index in an existing row has changed.  There is no need to
00107     // call this when new rows are added to the table
00108     virtual void setChanged();
00109 
00110     // access the record of index (integer) keys
00111     virtual Record &accessKey() {return *key_p;}
00112 
00113     // access the TIME to use in the search (seconds)
00114     virtual Double &time() {return time_p;}
00115 
00116     // access the INTERVAL to use in the search (seconds), must be >= 0
00117     virtual Double &interval() {return interval_p;}
00118 
00119     // get all of the rows in the subTable which have data during the indicated time and
00120     // interval values.  For now, this code will miss the case where the subtable has
00121     // interval = -1 and the start time is outside of the time range implied by the time
00122     // and interval.  If the table has changed and the time is > 
00123     virtual Vector<uInt> getRowNumbers();
00124 
00125     // get the row number which falls in the interval and has the time nearest to the
00126     // center of the interval (time()).  This also has the same problem as the previous function.
00127     virtual uInt getNearestRow(Bool &found);
00128 
00129     // is this attached to a null table
00130     virtual Bool isNull() { return tab_p.isNull();}
00131 
00132     // return the subtable being indexed
00133     virtual Table &table() {return tab_p;}
00134 private:
00135     // the subtable
00136     Table tab_p;
00137 
00138     ROScalarColumn<Double> timeColumn_p, intervalColumn_p;
00139     Vector<Double> timeVec_p, intervalVec_p;
00140     const Double *timeVals_p, *intervalVals_p;
00141     Bool deleteItTime_p, deleteItInterval_p;
00142 
00143     // Internal keys - set by user
00144     Record *key_p;
00145     Block<RecordFieldPtr<Int> > intKeys_p;
00146     Double time_p, interval_p;
00147 
00148     // last known integer key values
00149     Vector<Int> lastKeys_p;
00150     // last known time and interval
00151     Double lastTime_p, lastInterval_p;
00152 
00153     // last search result - matching integer keys
00154     Vector<uInt> lastSearch_p;
00155 
00156     // last nearest
00157     Int lastNearest_p;
00158     Bool nearestFound_p, nearestReady_p;
00159 
00160     // last known sub-table size
00161     uInt nrows_p;
00162 
00163     Bool hasChanged_p;
00164 
00165     ColumnsIndex *index_p;
00166     Block<RecordFieldPtr<Int> > indexKeys_p;
00167     Bool hasTime_p, hasInterval_p;
00168 
00169     void clear();
00170     void makeKeys();
00171     Bool keysChanged();
00172     void getInternals();
00173     void nearestTime();
00174 };
00175 
00176 
00177 } //# NAMESPACE CASA - END
00178 
00179 #endif
00180