casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArrayIter.h
Go to the documentation of this file.
1 //# ArrayIter.h: Iterate an Array cursor through another Array.
2 //# Copyright (C) 1993,1994,1995,1996,1999
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_ARRAYITER_H
29 #define CASA_ARRAYITER_H
30 
31 #include <casacore/casa/aips.h>
34 
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 //
39 // <summary> Iterate an Array cursor through another Array. </summary>
40 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
41 // </reviewed>
42 //
43 // ArrayIterator steps an array section (the "cursor") through an array.
44 // The cursor "refers" to storage in the array, so that changing the
45 // values in the cursor changes values in the original array. Like with
46 // ArrayPositionIterator, the cursor presently only moves through the array from
47 // bottom to top in the obvious way; however one may of course iterate
48 // through a slice ("array section"). This class is derived from
49 // ArrayPositionIterator since it also has a position (the blc of the cursor)
50 // which moves through the array volume.
51 //
52 // <note role=tip> The origin of the cursor, i.e. the subarray that moves
53 // through the larger array, is always zero.
54 // </note>
55 //
56 // <srcblock>
57 // Array<Float> to, from;
58 // //... set to and from, check that they are conformant
59 // ArrayIterator toiter(to,1);
60 // ArrayIterator fromiter(from,1);
61 // while (! toiter.pastEnd() ) {
62 // toiter.array() = fromiter.array(); // copy vector by vector
63 // toiter.next(); fromiter.next();
64 // }
65 //
66 // </srcblock>
67 //
68 // <linkfrom anchor=ArrayIterator classes="Array Vector Matrix Cube">
69 // <here>ArrayIterator</here> -- Iterate an Array cursor through another Array.
70 // </linkfrom>
71 //
72 template<class T> class ArrayIterator : public ArrayPositionIterator
73 {
74 public:
75  // Step through array "arr" over the first byDim axes
76  // (using a cursor of dimensionality "byDim").
77  explicit ArrayIterator(const Array<T> &arr, uInt byDim=1);
78 
79  // Step through an array using the given axes.
80  // The axes can be given in two ways:
81  // <ol>
82  // <li>axesAreCursor=True means that the axes form the cursor axes.
83  // The remaining axes will form the iteration axes.
84  // This is the default.
85  // <li>axesAreCursor=False means the opposite.
86  // In this case the iteration axes can be given in any order.
87  // </ol>
88  // E.g. when using iteration axes 2,0 for an array with shape [5,3,7], each
89  // iteration step returns a cursor (containing the data of axis 1).
90  // During the iteration axis 2 will vary most rapidly (as it was
91  // given first).
92  ArrayIterator(const Array<T> &arr, const IPosition &axes,
93  Bool axesAreCursor = True);
94 
95  virtual ~ArrayIterator();
96 
97  // Move the cursor to the next position.
98  virtual void next();
99 
100  // Set the cursor to the given position.
101  // The position can only contain the iteration axes or it can be the full
102  // position.
103  // <br>In the first case the position must to be given in the order
104  // of the iteration axes as given in the constructor.
105  // In the latter case the position must be given in natural order
106  // (as given by function <src>pos</src> and only the cursor axes are taken
107  // into account.
108  virtual void set (const IPosition& cursorPos);
109 
110  // Reset the cursor to the beginning.
111  // <group>
112  virtual void reset();
113  // </group>
114 
115  // Return the cursor. (Perhaps we should have a fn() that returns a
116  // reference to the original array as well?)
117  // <group>
118  Array<T> &array() {return *ap_p;}
119  virtual ArrayBase& getArray();
120  // </group>
121 
122 
123 protected:
124  // A pointer to the cursor.
126 
127 private:
128  // helper function to centralize construction work
129  void init(const Array<T> &);
130  // helper function to set the pointer to the new data position in ap
131  // after a step in the given dimension. -1 resets it to the beginning.
132  void apSetPointer(Int stepDim);
133 
137 
138  //# Presently the following are not defined.
141 };
142 
143 //
144 // <summary> Iterate a const Array cursor through a const Array. </summary>
145 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
146 // </reviewed>
147 //
148 // This class behaves exactly like an ArrayIterator, only it iterates through
149 // const Arrays.
150 //
151 // <srcblock>
152 // void CopyArray(Array<Float> &to, const Array<Float> &from)
153 // {
154 // //... check that they are conformant
155 // ArrayIterator toiter(to,1);
156 // ReadOnlyArrayIterator fromiter(from,1);
157 // while (! toiter.pastEnd() ) {
158 // toiter.array() = fromiter.array(); // copy vector by vector
159 // toiter.next(); fromiter.next();
160 // }
161 // }
162 // </srcblock>
163 // <note role=tip> This class is not derived from ArrayPositionIterator. For simplicity
164 // it merely contains an ArrayIterator to which it forwards requests
165 // and returns (const) results. The iterator classes should be
166 // rethought and reimplemented.
167 // </note>
168 //
169 // <linkfrom anchor=ReadOnlyArrayIterator classes="Array Vector Matrix Cube">
170 // <here>ReadOnlyArrayIterator</here> -- Iterate a const Array cursor through
171 // a const Array.
172 // </linkfrom>
173 //
174 template<class T> class ReadOnlyArrayIterator
175 {
176 public:
177  // Step through array "arr" using a cursor of dimensionality "byDim".
178  explicit ReadOnlyArrayIterator(const Array<T> &arr, uInt byDim=1)
179  : ai(const_cast<Array<T>&>(arr),byDim) {}
180 
181  // Step through an array for the given iteration axes.
182  ReadOnlyArrayIterator(const Array<T> &arr, const IPosition &axes,
183  Bool axesAreCursor = True)
184  : ai(const_cast<Array<T>&>(arr),axes,axesAreCursor) {}
185 
186  // Move the cursor to the next position.
187  void next() {ai.next();}
188 
189  // Set the cursor to the given position.
190  // The position can only contain the iteration axes or it can be the full
191  // position.
192  // <br>In the first case the position must to be given in the order
193  // of the iteration axes as given in the constructor.
194  // In the latter case the position must be given in natural order
195  // (as given by function <src>pos</src> and only the cursor axes are taken
196  // into account.
197  void set (const IPosition& cursorPos) {ai.set(cursorPos);}
198 
199  // Reset the cursor to the beginning.
200  // <group>
201  void reset() {ai.origin();}
202  void origin() {ai.origin();}
203  // </group>
204 
205  // Return the cursor. (Perhaps we should have a fn() that returns a
206  // reference to the original array as well?)
207  const Array<T> &array() {return ai.array();}
208 
209  // The same as the functions in ArrayPositionIterator.
210  // <group>
211  Bool atStart() const {return ai.atStart();}
212  Bool pastEnd() const {return ai.pastEnd();}
213  const IPosition &pos() const {return ai.pos();}
214  IPosition endPos() const {return ai.endPos();}
215  uInt ndim() const {return ai.ndim();}
216  // </group>
217 private:
218  // Not implemented.
219  // <group>
222  // </group>
223 
225 };
226 
227 
228 
229 } //# NAMESPACE CASACORE - END
230 
231 #ifndef CASACORE_NO_AUTO_TEMPLATES
232 #include <casacore/casa/Arrays/ArrayIter.tcc>
233 #endif //# CASACORE_NO_AUTO_TEMPLATES
234 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
int Int
Definition: aipstype.h:50
Non-templated base class for templated Array class.
Definition: ArrayBase.h:74
ReadOnlyArrayIterator< T > & operator=(const ReadOnlyArrayIterator< T > &)
virtual void reset()
Reset the cursor to the beginning.
void next()
Move the cursor to the next position.
Definition: ArrayIter.h:187
const Array< T > & array()
Return the cursor.
Definition: ArrayIter.h:207
Iterate an Array cursor through another Array.
Definition: Array.h:51
Array< T > * ap_p
A pointer to the cursor.
Definition: ArrayIter.h:125
IPosition endPos() const
Definition: ArrayIter.h:214
void init(const Array< T > &)
helper function to centralize construction work
ReadOnlyArrayIterator(const Array< T > &arr, uInt byDim=1)
Step through array &quot;arr&quot; using a cursor of dimensionality &quot;byDim&quot;.
Definition: ArrayIter.h:178
ReadOnlyArrayIterator(const Array< T > &arr, const IPosition &axes, Bool axesAreCursor=True)
Step through an array for the given iteration axes.
Definition: ArrayIter.h:182
virtual void set(const IPosition &cursorPos)
Set the cursor to the given position.
Array< T > pOriginalArray_p
Definition: ArrayIter.h:134
const IPosition & pos() const
Definition: ArrayIter.h:213
virtual void next()
Move the cursor to the next position.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
void set(const IPosition &cursorPos)
Set the cursor to the given position.
Definition: ArrayIter.h:197
Iterate a const Array cursor through a const Array.
Definition: ArrayIter.h:174
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
ArrayIterator< T > & operator=(const ArrayIterator< T > &)
virtual ArrayBase & getArray()
Get the array in the cursor.
ArrayIterator(const Array< T > &arr, uInt byDim=1)
Step through array &quot;arr&quot; over the first byDim axes (using a cursor of dimensionality &quot;byDim&quot;)...
Array< T > & array()
Return the cursor.
Definition: ArrayIter.h:118
void reset()
Reset the cursor to the beginning.
Definition: ArrayIter.h:201
void apSetPointer(Int stepDim)
helper function to set the pointer to the new data position in ap after a step in the given dimension...
Bool atStart() const
The same as the functions in ArrayPositionIterator.
Definition: ArrayIter.h:211
const Bool True
Definition: aipstype.h:43
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42