casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ConcatRows.h
Go to the documentation of this file.
00001 //# ConcatRows.h: Class holding the row numbers in a ConcatTable
00002 //# Copyright (C) 2008
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: ConcatRows.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef TABLES_CONCATROWS_H
00029 #define TABLES_CONCATROWS_H
00030 
00031 //# Includes
00032 #include <tables/Tables/RefRows.h>
00033 #include <casa/Containers/Block.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037   // <summary>
00038   // Class holding the row numbers in a ConcatTable
00039   // </summary>
00040 
00041   // <use visibility=local>
00042 
00043   // <reviewed reviewer="UNKNOWN" date="" tests="tConcatRows.cc">
00044   // </reviewed>
00045 
00046   // <prerequisite>
00047   //# Classes you should understand before using this one.
00048   //   <li> <linkto class=Block>Block</linkto>
00049   // </prerequisite>
00050 
00051   // <synopsis>
00052   // ConcatRows is used to hold the row numbers forming the concatenation
00053   // of oher tables.
00054   // table. It contains a vector which can hold the row numbers in 2 ways:
00055   // <ol>
00056   // <li> As a normal series of row numbers. This is used by e.g. class
00057   //  <linkto class=ConcatTable>ConcatTable</linkto> 
00058   // <li> As a series of Slices. In this case 3 subsequent entries
00059   //  in the vector are used to represent start, end, and increment.
00060   //  This is used by a function like <src>ScalarColumn::getColumnRange</src>.
00061   // </ol>
00062   // Class <linkto class=ConcatRowsIter>ConcatRowsIter</linkto> can be
00063   // used to iterate through a ConcatRows object. Each step in the iteration
00064   // goes to the next slice. If the ConcatRows object contains a simple series
00065   // of row numbers, each slice contains only one row number.
00066   // This can degrade performance, so it is possible to use shortcuts by
00067   // testing if the object contains slices (using <src>isSliced()</src>)
00068   // and getting the row number vector directly (using <src>rowVector()</src>).
00069   // </synopsis>
00070 
00071   // <motivation>
00072   // ConcatRows is meant to have one class representing the various ways
00073   // of picking row numbers. This simplifies the interface of the table
00074   // and data manager classes dealing with getting/putting the data.
00075   // </motivation>
00076 
00077   //# <todo asof="$DATE:$">
00078   //# A List of bugs, limitations, extensions or planned concatinements.
00079   //# </todo>
00080 
00081 
00082   class ConcatRows
00083   {
00084   public:
00085     // Construct an empty block.
00086     ConcatRows()
00087       : itsRows       (1,0),
00088         itsNTable     (0),
00089         itsLastStRow  (1),
00090         itsLastEndRow (0)
00091     {}
00092 
00093     // Reserve the block for the given nr of tables.
00094     void reserve (uInt ntable)
00095       { itsRows.resize (ntable+1); }
00096 
00097     // Add a table with the given nr of rows.
00098     void add (uInt nrow);
00099 
00100     // Give the nr of tables.
00101     uInt ntable() const
00102       { return itsNTable; }
00103 
00104     // Get the total nr of rows.
00105     uInt nrow() const
00106       { return itsRows[itsNTable]; }
00107 
00108     // Give the nr of rows for the i-th table.
00109     uInt operator[] (uInt i) const
00110       { return itsRows[i+1]; }
00111 
00112     // Give the offset for the i-th table.
00113     uInt offset (uInt i) const
00114       { return itsRows[i]; }
00115 
00116     // Map an overall row number to a table and row number.
00117     void mapRownr (uInt& tableNr, uInt& tabRownr, uInt rownr) const
00118     {
00119       if (rownr < itsLastStRow  ||  rownr >= itsLastEndRow) {
00120         findRownr (rownr);
00121       }
00122       tableNr = itsLastTableNr;
00123       tabRownr = rownr - itsLastStRow;
00124     }
00125 
00126   private:
00127     // Find the row number and fill in the lastXX_p values.
00128     void findRownr (uInt rownr) const;
00129 
00130     //# Data members.
00131     Block<uInt>  itsRows;
00132     uInt         itsNTable;
00133     mutable uInt itsLastStRow;         //# Cached variables to spped up
00134     mutable uInt itsLastEndRow;        //# function mapRownr().
00135     mutable uInt itsLastTableNr;
00136   };
00137 
00138 
00139 
00140   // <summary>
00141   // Class to iterate through a ConcatRows object.
00142   // </summary>
00143 
00144   // <use visibility=local>
00145 
00146   // <reviewed reviewer="UNKNOWN" date="" tests="tConcatRows.cc">
00147   // </reviewed>
00148 
00149   // <prerequisite>
00150   //# Classes you should understand before using this one.
00151   //   <li> <linkto class=ConcatRows>ConcatRows</linkto>
00152   // </prerequisite>
00153 
00154   // <synopsis>
00155   // ConcatRowsSliceIter is useful to iterate through a
00156   // <linkto class=ConcatRows>ConcatRows</linkto> object.
00157   // It is possible to define for which row
00158 // especially if the ConcatRows object contains slices.
00159 // Each step in the iteration returns a Slice object containing
00160 // the next slice in the ConcatRows object.
00161 // <br>
00162 // It is used in Table and data manager classes (e.g. StManColumn).
00163 // </synopsis>
00164 
00165 // <example>
00166 // This example shows how to iterate through a ConcatRows object
00167 // (giving a slice) and through each of the slices.
00168 // <srcblock>
00169 // void somefunc (const ConcatRows& rownrs)
00170 //   // Iterate through all slices.
00171 //   ConcatRowsSliceIter rowiter(rownrs);
00172 //   while (! rowiter.pastEnd()) {
00173 //     // Get start, end, and increment for this slice.
00174 //     uInt rownr = rowiter.sliceStart();
00175 //     uInt end = rowiter.sliceEnd();
00176 //     uInt incr = rowiter.sliceIncr();
00177 //     // Iterate through the row numbers in the slice.
00178 //     while (rownr <= end) {
00179 //       rownr += incr;
00180 //     }
00181 //     // Go to next slice.
00182 //     rowiter++;
00183 //   }
00184 // }
00185 // </srcblock>
00186 // </example>
00187 
00188 //# <todo asof="$DATE:$">
00189 //# A List of bugs, limitations, extensions or planned concatinements.
00190 //# </todo>
00191 
00192 
00193   class ConcatRowsIter
00194   {
00195   public:
00196     // Construct the iterator on a ConcatRows object.
00197     // It is set to the full range.
00198     explicit ConcatRowsIter (const ConcatRows&);
00199 
00200     // Construct the iterator on a ConcatRows object for the given row range.
00201     ConcatRowsIter (const ConcatRows&, uInt start, uInt end, uInt incr=1);
00202 
00203     // Is the iterator past the end?
00204     Bool pastEnd() const
00205       { return itsPastEnd; }
00206 
00207     // Go the next chunk.
00208     // <group>
00209     void operator++()
00210       { next(); }
00211     void operator++(int)
00212       { next(); }
00213     void next();
00214     // </group>
00215 
00216     // Get the current chunk.
00217     RefRows getChunk() const
00218       { return RefRows(itsChunk, True); }
00219 
00220     // Get the nr of the table the current chunk is in.
00221     uInt tableNr() const
00222       { return itsPos; }
00223 
00224   private:
00225     const ConcatRows* itsRows;
00226     Vector<uInt>      itsChunk;
00227     uInt              itsStart;
00228     uInt              itsEnd;
00229     uInt              itsIncr;
00230     uInt              itsPos;
00231     Bool              itsPastEnd;
00232   };
00233 
00234 
00235 } //# NAMESPACE CASA - END
00236 
00237 #endif