casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TSMShape.h
Go to the documentation of this file.
00001 //# TSMShape.h: Expanded IPosition for shapes
00002 //# Copyright (C) 1994,1995,1996,1999
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: TSMShape.h 20859 2010-02-03 13:14:15Z gervandiepen $
00027 
00028 #ifndef TABLES_TSMSHAPE_H
00029 #define TABLES_TSMSHAPE_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/IPosition.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 
00039 
00040 // <summary>
00041 // Expanded IPosition for shapes.
00042 // </summary>
00043 
00044 // <use visibility=local>
00045 
00046 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00047 // </reviewed>
00048 
00049 // <prerequisite>
00050 //# Classes you should understand before using this one.
00051 //   <li> <linkto class=IPosition>IPosition</linkto>
00052 // </prerequisite>
00053 
00054 // <etymology>
00055 // TSMShape handles the shapes for the Tiled Storage Manager.
00056 // </etymology>
00057 
00058 // <synopsis> 
00059 // TSMShape is an extension of class
00060 // <linkto class=IPosition>IPosition</linkto>
00061 // to handle shapes.
00062 // It contains some precalculated values to speed up the calculation
00063 // of an array offset from an array index (and vice-versa).
00064 // </synopsis> 
00065 
00066 // <motivation>
00067 // The Tiled Hypercube Storage Manager is heavily using array shapes
00068 // and determining offsets from array indices. This class makes these
00069 // calculations more efficient.
00070 // </motivation>
00071 
00072 // <todo asof="$DATE:$">
00073 //  <li> Integrate in a class like LatticeLayout.
00074 // </todo>
00075 
00076 
00077 class TSMShape
00078 {
00079 public:
00080     // A zero-length TSMShape.
00081     TSMShape();
00082 
00083     // Construct from a shape and precalculate some values.
00084     TSMShape (const IPosition& shape);
00085 
00086     // Copy constructor (copy semantics).
00087     TSMShape (const TSMShape& that);
00088     
00089     // Assignment (copy semantics).
00090     // "this" and "that" must either be conformant (same size)
00091     // or "this" must be 0-length, in which case it will
00092     // resize itself to be the same length as "that".
00093     TSMShape& operator= (const TSMShape& that);
00094 
00095     ~TSMShape();
00096 
00097     // Index into the TSMShape. Indices are zero-based. If the preprocessor
00098     // symbol AIPS_ARRAY_INDEX_CHECK is defined, "index" will be
00099     // checked to ensure it is not out of bounds. If this check fails, an
00100     // AipsError will be thrown.
00101     Int operator() (uInt index) const;
00102 
00103     // The number of elements in this TSMShape. Since TSMShape
00104     // objects use zero-based indexing, the maximum available index is
00105     // nelements() - 1.
00106     uInt nelements() const;
00107 
00108     // conform returns true if nelements() == other.nelements().
00109     Bool conform (const TSMShape& other) const;
00110 
00111     // Calculate the offset for a given position.
00112     // <group>
00113     size_t offset (const IPosition& position) const;
00114     size_t offset (const IPosition& position, const IPosition& origin) const;
00115     // </group>
00116 
00117     // Calculate the position for a given offset.
00118     // <group>
00119     IPosition position (size_t offset) const;
00120     IPosition position (size_t offset, const IPosition& origin) const;
00121     // </group>
00122 
00123     // Calculate the increments when stepping through an array in
00124     // a linear way. This can be used to update the array offset
00125     // without recalculating it after each step.
00126     // For example:
00127     // <srcblock>
00128     // template<class T>
00129     // Array<T> someFunc (const Array<T>& array,
00130     //                    const IPosition& subArrayShape,
00131     //                    const IPosition& subArrayStart) const
00132     // {
00133     //     TSMShape TSM (array.shape());
00134     //     IPosition offsetIncr = TSM.offsetIncrement (subArrayShape);
00135     //     Array<T> subArray(subArrayShape);
00136     //     Bool deleteMain;
00137     //     const T* mainData = array.getStorage (deleteMain);
00138     //     mainData += TSM.offset (subArrayStart)
00139     //     Bool deleteSub;
00140     //     T* subData = subArray.getStorage (deleteSub);
00141     //     for (uInt i=0; i<subArrayShape(2); i++) {
00142     //         for (uInt j=0; j<subArrayShape(1); j++) {
00143     //             for (uInt k=0; k<subArrayShape(0); k++) {
00144     //                 *subData++ = *mainData++;
00145     //             }
00146     //             mainData += offsetIncr(1);
00147     //         }
00148     //         mainData += offSetIncr(2);
00149     //     }
00150     // }
00151     // </srcblock>
00152     // <group>
00153     IPosition offsetIncrement (const IPosition& subShape) const;
00154     IPosition offsetIncrement (const IPosition& subShape,
00155                                const IPosition& stride) const;
00156     // </group>
00157 
00158 private:
00159     IPosition data_p;
00160     uInt      size_p;     //# Not necessary, but done for speedup
00161 };
00162 
00163 
00164 inline uInt TSMShape::nelements() const
00165 {
00166     return size_p;
00167 }
00168 
00169 inline Int TSMShape::operator()(uInt index) const
00170 {
00171     return data_p(index);
00172 }
00173 
00174 inline Bool TSMShape::conform (const TSMShape& other) const
00175 {
00176     return data_p.conform (other.data_p);
00177 }
00178 
00179 
00180 
00181 } //# NAMESPACE CASA - END
00182 
00183 #endif