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$ 00027 00028 #ifndef CASA_ARRAYITER_H 00029 #define CASA_ARRAYITER_H 00030 00031 #include <casacore/casa/aips.h> 00032 #include <casacore/casa/Arrays/ArrayPosIter.h> 00033 #include <casacore/casa/Arrays/Array.h> 00034 00035 00036 namespace casacore { //# NAMESPACE CASACORE - 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(const 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(const 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(const 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 IPosition offset_p; 00136 T* dataPtr_p; 00137 00138 //# Presently the following are not defined. 00139 ArrayIterator(const ArrayIterator<T> &); 00140 ArrayIterator<T> &operator=(const ArrayIterator<T> &); 00141 }; 00142 00143 // 00144 // <summary> Iterate a const Array cursor through a const Array. </summary> 00145 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""> 00146 // </reviewed> 00147 // 00148 // This class behaves exactly like an ArrayIterator, only it iterates through 00149 // const Arrays. 00150 // 00151 // <srcblock> 00152 // void CopyArray(Array<Float> &to, const Array<Float> &from) 00153 // { 00154 // //... check that they are conformant 00155 // ArrayIterator toiter(to,1); 00156 // ReadOnlyArrayIterator fromiter(from,1); 00157 // while (! toiter.pastEnd() ) { 00158 // toiter.array() = fromiter.array(); // copy vector by vector 00159 // toiter.next(); fromiter.next(); 00160 // } 00161 // } 00162 // </srcblock> 00163 // <note role=tip> This class is not derived from ArrayPositionIterator. For simplicity 00164 // it merely contains an ArrayIterator to which it forwards requests 00165 // and returns (const) results. The iterator classes should be 00166 // rethought and reimplemented. 00167 // </note> 00168 // 00169 // <linkfrom anchor=ReadOnlyArrayIterator classes="Array Vector Matrix Cube"> 00170 // <here>ReadOnlyArrayIterator</here> -- Iterate a const Array cursor through 00171 // a const Array. 00172 // </linkfrom> 00173 // 00174 template<class T> class ReadOnlyArrayIterator 00175 { 00176 public: 00177 // Step through array "arr" using a cursor of dimensionality "byDim". 00178 explicit ReadOnlyArrayIterator(const Array<T> &arr, uInt byDim=1) 00179 : ai(const_cast<Array<T>&>(arr),byDim) {} 00180 00181 // Step through an array for the given iteration axes. 00182 ReadOnlyArrayIterator(const Array<T> &arr, const IPosition &axes, 00183 Bool axesAreCursor = True) 00184 : ai(const_cast<Array<T>&>(arr),axes,axesAreCursor) {} 00185 00186 // Move the cursor to the next position. 00187 void next() {ai.next();} 00188 00189 // Set the cursor to the given position. 00190 // The position can only contain the iteration axes or it can be the full 00191 // position. 00192 // <br>In the first case the position must to be given in the order 00193 // of the iteration axes as given in the constructor. 00194 // In the latter case the position must be given in natural order 00195 // (as given by function <src>pos</src> and only the cursor axes are taken 00196 // into account. 00197 void set (const IPosition& cursorPos) {ai.set(cursorPos);} 00198 00199 // Reset the cursor to the beginning. 00200 // <group> 00201 void reset() {ai.origin();} 00202 void origin() {ai.origin();} 00203 // </group> 00204 00205 // Return the cursor. (Perhaps we should have a fn() that returns a 00206 // reference to the original array as well?) 00207 const Array<T> &array() {return ai.array();} 00208 00209 // The same as the functions in ArrayPositionIterator. 00210 // <group> 00211 Bool atStart() const {return ai.atStart();} 00212 Bool pastEnd() const {return ai.pastEnd();} 00213 const IPosition &pos() const {return ai.pos();} 00214 IPosition endPos() const {return ai.endPos();} 00215 uInt ndim() const {return ai.ndim();} 00216 // </group> 00217 private: 00218 // Not implemented. 00219 // <group> 00220 ReadOnlyArrayIterator (const ReadOnlyArrayIterator<T> &); 00221 ReadOnlyArrayIterator<T> &operator=(const ReadOnlyArrayIterator<T> &); 00222 // </group> 00223 00224 ArrayIterator<T> ai; 00225 }; 00226 00227 00228 00229 } //# NAMESPACE CASACORE - END 00230 00231 #ifndef CASACORE_NO_AUTO_TEMPLATES 00232 #include <casacore/casa/Arrays/ArrayIter.tcc> 00233 #endif //# CASACORE_NO_AUTO_TEMPLATES 00234 #endif