casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TableVector.h
Go to the documentation of this file.
00001 //# TableVector.h: Templated read/write table column vectors
00002 //# Copyright (C) 1994,1995,1996,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: TableVector.h 21298 2012-12-07 14:53:03Z gervandiepen $
00027 
00028 #ifndef TABLES_TABLEVECTOR_H
00029 #define TABLES_TABLEVECTOR_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <tables/Tables/TVec.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 class Table;
00039 class TableColumn;
00040 template<class T> class TableVectorHelper;
00041 class String;
00042 
00043 
00044 // <summary>
00045 // Templated readonly table column vectors
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> Vector
00056 //   <li> Table
00057 // </prerequisite>
00058 
00059 // <etymology>
00060 // TableVector allows to operate on a column in a readonly table as a vector.
00061 // </etymology>
00062 
00063 // <synopsis> 
00064 // A TableVector object is a read/write view of data in a Table.
00065 // This means that the vector data can be changed if the underlying column
00066 // is writable.
00067 //
00068 // Table vectors can be used in the same way as the normal vectors.
00069 // They allow to handle a column in a table as a vector.
00070 // Many mathematical and logical operations are defined for them
00071 // in TabVecMath.h and TabVecLogic.h. In fact, constructors exist
00072 // to convert a TableColumn or a Vector object to a TableVector,
00073 // so they can often directly be used in a table vector expression.
00074 // There are 2 kinds of table vectors:
00075 // <ul>
00076 //  <li> A table vector representing a scalar column in a table.
00077 //         The data types of the vector and the column must conform.
00078 // </li> A temporary vector, which is held in memory.
00079 //         These are usually the result of operations on table vectors.
00080 // </ul>
00081 //
00082 // TableVector is implemented by referencing the counted TabVecRep object.
00083 // A default constructor is defined to allow construction of an array
00084 // of TableVector objects. However, it constructs an object not
00085 // referencing anything. Functions like operator() will fail (i.e. result
00086 // in a segmentation fault) when used on such objects. The functions
00087 // isNull and throwIfNull can be used to test on this.
00088 // </synopsis> 
00089 
00090 // <example>
00091 // <srcblock>
00092 //    // Create a table vector for column COL1.
00093 //    Table tab ("Table.data");
00094 //    TableVector<Int> tabvec(tab, "COL1");
00095 //    // Multiply it by a constant.
00096 //    // The result has to be stored in a TableVector,
00097 //    // since a TableVector cannot be written.
00098 //    TableVector<Int> temp = 2 * tabvec;
00099 // </srcblock>
00100 // </example>
00101 
00102 // <motivation>
00103 // It is very useful to be able to handle a column as a vector.
00104 // To handle a column in a readonly table, a TableVector class
00105 // is needed, otherwise output operations could not be forbidden.
00106 // </motivation>
00107 
00108 // <todo asof="$DATE:$">
00109 //# A List of bugs, limitations, extensions or planned refinements.
00110 //   <li> derive from Lattice one day
00111 //   <li> support slicing
00112 //   <li> support table array columns
00113 //   <li> do we ever need Row vectors?
00114 // </todo>
00115 
00116 
00117 template<class T>
00118 class TableVector
00119 {
00120 public:
00121     // The default constructor creates a null table vector.
00122     // This does not contain an actual vector and cannot be used until
00123     // it references an actual vector (using function reference).
00124     // Its sole purpose is to be able to construct an array of TableVectors.
00125     // Note that operator(), etc. will cause a segmentation fault
00126     // when operating on a null object. It was felt it was too expensive
00127     // to test on null over and over again. The user should use the isNull
00128     // or throwIfNull function in case of doubt.
00129     TableVector();
00130 
00131     // Create a read/write table vector from the given table column name.
00132     // Only scalar columns are supported.
00133     TableVector (const Table&, const String& columnName);
00134 
00135     // Create a read/write table vector from the given table column.
00136     // Only scalar columns are supported.
00137     // This constructor converts a TableColumn to a TableVector and
00138     // allows the use of TableColumn objects in table vector expressions.
00139     TableVector (const TableColumn& column);
00140 
00141     // Create a table vector from another one (reference semantics)
00142     TableVector (const TableVector<T>&);
00143 
00144     // Create a table vector containing the given Vector (reference semantics).
00145     // This constructor converts a Vector to a TableVector and
00146     // allows the use of Vector objects in table vector expressions.
00147     TableVector (const Vector<T>&);
00148 
00149     // Create a table vector containing a Vector with the given length.
00150     TableVector (uInt leng);
00151 
00152     // Destruct the object.
00153     ~TableVector();
00154 
00155     // Assign a table vector to another one (copy semantics).
00156     // The vectors must have equal length.
00157     TableVector<T>& operator= (const TableVector<T>&);
00158 
00159     // Test if the table vector is null, i.e. has no actual vector.
00160     // This is the case if the default constructor has been used.
00161     Bool isNull() const;
00162 
00163     // Throw an exception if the table vector is null, i.e.
00164     // if function isNull() is true.
00165     void throwIfNull() const;
00166 
00167     // Make a reference to the table vector of the other TableVector.
00168     // It will replace an already existing reference.
00169     // It handles null objects correctly.
00170     void reference (const TableVector<T>&);
00171 
00172     // Make a (normal) Vector from a TableVector (copy semantics).
00173     Vector<T> makeVector() const;
00174 
00175     // Get the value of a single pixel.
00176     T operator() (uInt index) const;
00177 
00178     //# Get a slice.
00179 //#    TableVector<T> operator() (const NSlice&) const;
00180 
00181     // Set all elements to a value.
00182     // <group>
00183     TableVector<T>& operator= (const T&);
00184     void set (const T& value);
00185     // </group>
00186 
00187     // Put a value into a single pixel.
00188     // <br><src> tabvec(i) = value; </src>
00189     void set (uInt index, const T& value);
00190 
00191     // Get nr of dimensions (is always 1).
00192     uInt ndim() const;
00193 
00194     // Get nr of elements (ie. vector length).
00195     uInt nelements() const;
00196 
00197     // Test if the shape of the given table vector conforms.
00198     Bool conform (const TableVector<T>&) const;
00199 
00200     // Test if the shape of the given vector conforms.
00201     Bool conform (const Vector<T>&) const;
00202 
00203     // Test if internal state is correct.
00204     Bool ok() const;
00205 
00206 protected:
00207     TabVecRep<T>* tabVecPtr_p;
00208 
00209     // Destruct the object. It decreases the reference count in the
00210     // underlying object.
00211     void destruct();
00212 
00213 public:
00214     // Return the TabVecRep reference.
00215     TabVecRep<T>& tabVec();
00216     const TabVecRep<T>& tabVec() const;
00217 
00218     // Create a TableVector from a TabVecRep as result of an operation.
00219     TableVector (TabVecRep<T>&);
00220 };
00221 
00222 
00223 template<class T>
00224 inline Bool TableVector<T>::isNull() const
00225     { return (tabVecPtr_p == 0  ?  True : False); }
00226 
00227 template<class T>
00228 inline uInt TableVector<T>::ndim () const
00229     { return tabVecPtr_p->ndim(); }
00230 
00231 template<class T>
00232 inline uInt TableVector<T>::nelements() const
00233     { return tabVecPtr_p->nelements(); }
00234 
00235 //# Check if 2 table vectors are conformant.
00236 template<class T>
00237 inline Bool TableVector<T>::conform (const TableVector<T>& vec) const
00238     { return tabVecPtr_p->conform (*vec.tabVecPtr_p); }
00239 template<class T>
00240 inline Bool TableVector<T>::conform (const Vector<T>& vec) const
00241     { return tabVecPtr_p->conform (vec); }
00242 
00243 //# Get the ith pixel.
00244 template<class T>
00245 inline T TableVector<T>::operator() (uInt index) const
00246     { return tabVecPtr_p->value (index); }
00247 
00248 //# Return the TabVecRep (for TabVecMath and Logic).
00249 template<class T>
00250 inline const TabVecRep<T>& TableVector<T>::tabVec() const
00251     { return *tabVecPtr_p; }
00252 template<class T>
00253 inline TabVecRep<T>& TableVector<T>::tabVec()
00254     { return *tabVecPtr_p; }
00255 
00256 
00257 //# Create a new object as a result of an addition, etc..
00258 template<class T>
00259 inline TableVector<T>::TableVector (TabVecRep<T>& vec)
00260     { tabVecPtr_p = vec.link(); }
00261 
00262 //# Assign a table vector to this one.
00263 template<class T>
00264 inline TableVector<T>& TableVector<T>::operator= (const TableVector<T>& that)
00265 {
00266     tabVecPtr_p->assign (that.tabVec());
00267     return *this;
00268 }
00269 
00270 template<class T>
00271 inline void TableVector<T>::set (uInt index, const T& value)
00272 {
00273     tabVecPtr_p->putVal (index, value);
00274 }
00275 template<class T>
00276 inline void TableVector<T>::set (const T& value)
00277 {
00278     tabVecPtr_p->set (value);
00279 }
00280 template<class T>
00281 inline TableVector<T>& TableVector<T>::operator= (const T& value)
00282 {
00283     tabVecPtr_p->set (value);
00284     return *this;
00285 }
00286 
00287 
00288 } //# NAMESPACE CASA - END
00289 
00290 
00291 //# Make old name ROTableVector still available.
00292 #define ROTableVector TableVector
00293 
00294 
00295 #ifndef CASACORE_NO_AUTO_TEMPLATES
00296 #include <tables/Tables/TableVector.tcc>
00297 #endif //# CASACORE_NO_AUTO_TEMPLATES
00298 #endif