casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ArrayIter.h
Go to the documentation of this file.
00001 //# ArrayIter.h: Iterate an Array cursor through another Array.
00002 //# Copyright (C) 1993,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: ArrayIter.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_ARRAYITER_H
00029 #define CASA_ARRAYITER_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/Arrays/ArrayPosIter.h>
00033 #include <casa/Arrays/Array.h>
00034 
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // 
00039 // <summary> Iterate an Array cursor through another Array. </summary>
00040 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00041 // </reviewed>
00042 //
00043 // ArrayIterator steps an array section (the "cursor") through an array.
00044 // The cursor "refers" to storage in the array, so that changing the
00045 // values in the cursor changes values in the original array. Like with
00046 // ArrayPositionIterator, the cursor presently only moves through the array from
00047 // bottom to top in the obvious way; however one may of course iterate
00048 // through a slice ("array section"). This class is derived from
00049 // ArrayPositionIterator since it also has a position (the blc of the cursor)
00050 // which moves through the array volume.
00051 //
00052 // <note role=tip> The origin of the cursor, i.e. the subarray that moves
00053 //   through the larger array, is always zero.
00054 // </note>
00055 //
00056 // <srcblock>
00057 // Array<Float> to, from;
00058 // //... set to and from, check that they are conformant
00059 // ArrayIterator toiter(to,1);
00060 // ArrayIterator fromiter(from,1);
00061 // while (! toiter.pastEnd() ) {
00062 //     toiter.array() = fromiter.array();  // copy vector by vector
00063 //     toiter.next(); fromiter.next();
00064 // }
00065 // 
00066 // </srcblock>
00067 //
00068 // <linkfrom anchor=ArrayIterator classes="Array Vector Matrix Cube">
00069 //    <here>ArrayIterator</here> -- Iterate an Array cursor through another Array.
00070 // </linkfrom>
00071 //
00072 template<class T> class ArrayIterator : public ArrayPositionIterator
00073 {
00074 public:
00075     // Step through array "arr" over the first byDim axes
00076     // (using a cursor of dimensionality "byDim").
00077     explicit ArrayIterator(Array<T> &arr, uInt byDim=1);
00078 
00079     // Step through an array using the given axes.
00080     // The axes can be given in two ways:
00081     // <ol>
00082     // <li>axesAreCursor=True means that the axes form the cursor axes.
00083     //     The remaining axes will form the iteration axes.
00084     //     This is the default.
00085     // <li>axesAreCursor=False means the opposite.
00086     //     In this case the iteration axes can be given in any order.
00087     // </ol>
00088     // E.g. when using iteration axes 2,0 for an array with shape [5,3,7], each
00089     // iteration step returns a cursor (containing the data of axis 1).
00090     // During the iteration axis 2 will vary most rapidly (as it was
00091     // given first).
00092     ArrayIterator(Array<T> &arr, const IPosition &axes,
00093                   Bool axesAreCursor = True);
00094 
00095     virtual ~ArrayIterator();
00096 
00097     // Move the cursor to the next position.
00098     virtual void next();
00099 
00100     // Set the cursor to the given position.
00101     // The position can only contain the iteration axes or it can be the full
00102     // position.
00103     // <br>In the first case the position must to be given in the order
00104     // of the iteration axes as given in the constructor.
00105     // In the latter case the position must be given in natural order
00106     // (as given by function <src>pos</src> and only the cursor axes are taken
00107     // into account.
00108     virtual void set (const IPosition& cursorPos);
00109 
00110     // Reset the cursor to the beginning.
00111     // <group>
00112     virtual void reset();
00113     // </group>
00114 
00115     // Return the cursor. (Perhaps we should have a fn() that returns a
00116     // reference to the original array as well?)
00117     // <group>
00118     Array<T> &array() {return *ap_p;}
00119     virtual ArrayBase& getArray();
00120     // </group>
00121 
00122 
00123 protected:
00124     // A pointer to the cursor.
00125     Array<T> *ap_p;
00126 
00127 private:
00128     // helper function to centralize construction work
00129     void init(Array<T> &);
00130     // helper function to set the pointer to the new data position in ap
00131     // after a step in the given dimension. -1 resets it to the beginning.
00132     void apSetPointer(Int stepDim);
00133 
00134     Array<T> *pOriginalArray_p;
00135     Bool readOnly_p;
00136     IPosition offset_p;
00137     T* dataPtr_p;
00138 
00139     //# Presently the following are not defined.
00140     ArrayIterator(const ArrayIterator<T> &);
00141     ArrayIterator<T> &operator=(const ArrayIterator<T> &);
00142 };
00143 
00144 // 
00145 // <summary> Iterate a const Array cursor through a const Array. </summary>
00146 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00147 // </reviewed>
00148 //
00149 // This class behaves exactly like an ArrayIterator, only it iterates through
00150 // const Arrays.
00151 //
00152 // <srcblock>
00153 // void CopyArray(Array<Float> &to, const Array<Float> &from)
00154 // {
00155 //     //... check that they are conformant
00156 //     ArrayIterator toiter(to,1);
00157 //     ReadOnlyArrayIterator fromiter(from,1);
00158 //     while (! toiter.pastEnd() ) {
00159 //         toiter.array() = fromiter.array();  // copy vector by vector
00160 //         toiter.next(); fromiter.next();
00161 //     }
00162 // }
00163 // </srcblock>
00164 // <note role=tip> This class is not derived from ArrayPositionIterator. For simplicity
00165 //        it merely contains an ArrayIterator to which it forwards requests
00166 //        and returns (const) results. The iterator classes should be 
00167 //        rethought and reimplemented.
00168 // </note>
00169 //
00170 // <linkfrom anchor=ReadOnlyArrayIterator classes="Array Vector Matrix Cube">
00171 //    <here>ReadOnlyArrayIterator</here> -- Iterate a const Array cursor through
00172 //     a const Array.
00173 // </linkfrom>
00174 //
00175 template<class T> class ReadOnlyArrayIterator
00176 {
00177 public:
00178     // Step through array "arr" using a cursor of dimensionality "byDim".
00179     explicit ReadOnlyArrayIterator(const Array<T> &arr, uInt byDim=1) 
00180         : ai(const_cast<Array<T>&>(arr),byDim) {}
00181 
00182     // Step through an array for the given iteration axes.
00183   ReadOnlyArrayIterator(const Array<T> &arr, const IPosition &axes,
00184                         Bool axesAreCursor = True)
00185         : ai(const_cast<Array<T>&>(arr),axes,axesAreCursor) {}
00186 
00187     // Move the cursor to the next position.
00188     void next() {ai.next();}
00189 
00190     // Set the cursor to the given position.
00191     // The position can only contain the iteration axes or it can be the full
00192     // position.
00193     // <br>In the first case the position must to be given in the order
00194     // of the iteration axes as given in the constructor.
00195     // In the latter case the position must be given in natural order
00196     // (as given by function <src>pos</src> and only the cursor axes are taken
00197     // into account.
00198     void set (const IPosition& cursorPos) {ai.set(cursorPos);}
00199 
00200     // Reset the cursor to the beginning.
00201     // <group>
00202     void reset() {ai.origin();}
00203     void origin() {ai.origin();}
00204     // </group>
00205     
00206     // Return the cursor. (Perhaps we should have a fn() that returns a
00207     // reference to the original array as well?)
00208     const Array<T> &array() {return ai.array();}
00209         
00210     // The same as the functions in ArrayPositionIterator.
00211     // <group>
00212     Bool atStart() const {return ai.atStart();}
00213     Bool pastEnd() const {return ai.pastEnd();}
00214     const IPosition &pos() const {return ai.pos();}
00215     IPosition endPos() const {return ai.endPos();}
00216     uInt ndim() const {return ai.ndim();}
00217     // </group>
00218 private:
00219     // Not implemented.
00220     // <group>
00221     ReadOnlyArrayIterator (const ReadOnlyArrayIterator<T> &);
00222     ReadOnlyArrayIterator<T> &operator=(const ReadOnlyArrayIterator<T> &);
00223     // </group>
00224     
00225     ArrayIterator<T> ai;
00226 };
00227 
00228 
00229 
00230 } //# NAMESPACE CASA - END
00231 
00232 #ifndef CASACORE_NO_AUTO_TEMPLATES
00233 #include <casa/Arrays/ArrayIter.tcc>
00234 #endif //# CASACORE_NO_AUTO_TEMPLATES
00235 #endif