casacore
 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_ARRAYITER2_H
29 #define CASA_ARRAYITER2_H
30 
31 #include "ArrayPosIter.h"
32 #include "Array.h"
33 
34 
35 namespace casacore { //# NAMESPACE CASACORE - BEGIN
36 
37 //
38 // <summary> Iterate an Array cursor through another Array. </summary>
39 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
40 // </reviewed>
41 //
42 // ArrayIterator steps an array section (the "cursor") through an array.
43 // The cursor "refers" to storage in the array, so that changing the
44 // values in the cursor changes values in the original array. Like with
45 // ArrayPositionIterator, the cursor presently only moves through the array from
46 // bottom to top in the obvious way; however one may of course iterate
47 // through a slice ("array section"). This class is derived from
48 // ArrayPositionIterator since it also has a position (the blc of the cursor)
49 // which moves through the array volume.
50 //
51 // <note role=tip> The origin of the cursor, i.e. the subarray that moves
52 // through the larger array, is always zero.
53 // </note>
54 //
55 // <srcblock>
56 // Array<float> to, from;
57 // //... set to and from, check that they are conformant
58 // ArrayIterator toiter(to,1);
59 // ArrayIterator fromiter(from,1);
60 // while (! toiter.pastEnd() ) {
61 // toiter.array() = fromiter.array(); // copy vector by vector
62 // toiter.next(); fromiter.next();
63 // }
64 //
65 // </srcblock>
66 //
67 // <linkfrom anchor=ArrayIterator classes="Array Vector Matrix Cube">
68 // <here>ArrayIterator</here> -- Iterate an Array cursor through another Array.
69 // </linkfrom>
70 //
71 template<typename T, typename Alloc> class ArrayIterator : public ArrayPositionIterator
72 {
73 public:
74  // Step through array "arr" over the first byDim axes
75  // (using a cursor of dimensionality "byDim").
76  explicit ArrayIterator(const Array<T, Alloc> &arr, size_t byDim=1);
77 
78  // Step through an array using the given axes.
79  // The axes can be given in two ways:
80  // <ol>
81  // <li>axesAreCursor=true means that the axes form the cursor axes.
82  // The remaining axes will form the iteration axes.
83  // This is the default.
84  // <li>axesAreCursor=false means the opposite.
85  // In this case the iteration axes can be given in any order.
86  // </ol>
87  // E.g. when using iteration axes 2,0 for an array with shape [5,3,7], each
88  // iteration step returns a cursor (containing the data of axis 1).
89  // During the iteration axis 2 will vary most rapidly (as it was
90  // given first).
91  ArrayIterator(const Array<T, Alloc> &arr, const IPosition &axes,
92  bool axesAreCursor = true);
93 
94  // Move the cursor to the next position.
95  virtual void next() override;
96 
97  // Set the cursor to the given position.
98  // The position can only contain the iteration axes or it can be the full
99  // position.
100  // <br>In the first case the position must to be given in the order
101  // of the iteration axes as given in the constructor.
102  // In the latter case the position must be given in natural order
103  // (as given by function <src>pos</src> and only the cursor axes are taken
104  // into account.
105  virtual void set (const IPosition& cursorPos) override;
106 
107  // Reset the cursor to the beginning.
108  // <group>
109  virtual void reset() override;
110  // </group>
111 
112  // Return the cursor. (Perhaps we should have a fn() that returns a
113  // reference to the original array as well?)
114  // <group>
115  Array<T, Alloc> &array() {return *ap_p;}
116  virtual ArrayBase& getArray() override;
117  // </group>
118 
119 
120 protected:
121  // The cursor
122  std::unique_ptr<Array<T, Alloc>> ap_p;
123 
124 private:
125  // helper function to centralize construction work
126  void init(const Array<T, Alloc> &);
127  // helper function to set the pointer to the new data position in ap
128  // after a step in the given dimension. -1 resets it to the beginning.
129  void apSetPointer(int stepDim);
130 
134 
135  //# Presently the following are not defined.
138 };
139 
140 //
141 // <summary> Iterate a const Array cursor through a const Array. </summary>
142 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
143 // </reviewed>
144 //
145 // This class behaves exactly like an ArrayIterator, only it iterates through
146 // const Arrays.
147 //
148 // <srcblock>
149 // void CopyArray(Array<float> &to, const Array<float> &from)
150 // {
151 // //... check that they are conformant
152 // ArrayIterator toiter(to,1);
153 // ReadOnlyArrayIterator fromiter(from,1);
154 // while (! toiter.pastEnd() ) {
155 // toiter.array() = fromiter.array(); // copy vector by vector
156 // toiter.next(); fromiter.next();
157 // }
158 // }
159 // </srcblock>
160 // <note role=tip> This class is not derived from ArrayPositionIterator. For simplicity
161 // it merely contains an ArrayIterator to which it forwards requests
162 // and returns (const) results. The iterator classes should be
163 // rethought and reimplemented.
164 // </note>
165 //
166 // <linkfrom anchor=ReadOnlyArrayIterator classes="Array Vector Matrix Cube">
167 // <here>ReadOnlyArrayIterator</here> -- Iterate a const Array cursor through
168 // a const Array.
169 // </linkfrom>
170 //
171 template<typename T, typename Alloc=std::allocator<T>> class ReadOnlyArrayIterator
172 {
173 public:
174  // Step through array "arr" using a cursor of dimensionality "byDim".
175  explicit ReadOnlyArrayIterator(const Array<T, Alloc> &arr, size_t byDim=1)
176  : ai(const_cast<Array<T, Alloc>&>(arr),byDim) {}
177 
178  // Step through an array for the given iteration axes.
180  bool axesAreCursor = true)
181  : ai(const_cast<Array<T, Alloc>&>(arr),axes,axesAreCursor) {}
182 
183  // Move the cursor to the next position.
184  void next() {ai.next();}
185 
186  // Set the cursor to the given position.
187  // The position can only contain the iteration axes or it can be the full
188  // position.
189  // <br>In the first case the position must to be given in the order
190  // of the iteration axes as given in the constructor.
191  // In the latter case the position must be given in natural order
192  // (as given by function <src>pos</src> and only the cursor axes are taken
193  // into account.
194  void set (const IPosition& cursorPos) {ai.set(cursorPos);}
195 
196  // Reset the cursor to the beginning.
197  // <group>
198  void reset() {ai.origin();}
199  void origin() {ai.origin();}
200  // </group>
201 
202  // Return the cursor. (Perhaps we should have a fn() that returns a
203  // reference to the original array as well?)
204  const Array<T, Alloc> &array() {return ai.array();}
205 
206  // The same as the functions in ArrayPositionIterator.
207  // <group>
208  bool atStart() const {return ai.atStart();}
209  bool pastEnd() const {return ai.pastEnd();}
210  const IPosition &pos() const {return ai.pos();}
211  IPosition endPos() const {return ai.endPos();}
212  size_t ndim() const {return ai.ndim();}
213  // </group>
214 private:
215  // Not implemented.
216  // <group>
219  // </group>
220 
222 };
223 
224 
225 
226 } //# NAMESPACE CASACORE - END
227 
228 #include "ArrayIter.tcc"
229 
230 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:118
const IPosition & pos() const
Definition: ArrayIter.h:210
Non-templated base class for templated Array class.
Definition: ArrayBase.h:72
ReadOnlyArrayIterator< T, Alloc > & operator=(const ReadOnlyArrayIterator< T, Alloc > &)
bool atStart() const
The same as the functions in ArrayPositionIterator.
Definition: ArrayIter.h:208
IPosition endPos() const
Definition: ArrayIter.h:211
ArrayIterator(const Array< T, Alloc > &arr, size_t byDim=1)
Step through array &quot;arr&quot; over the first byDim axes (using a cursor of dimensionality &quot;byDim&quot;)...
virtual void reset() override
Reset the cursor to the beginning.
const Array< T, Alloc > & array()
Return the cursor.
Definition: ArrayIter.h:204
void apSetPointer(int stepDim)
helper function to set the pointer to the new data position in ap after a step in the given dimension...
void reset()
Reset the cursor to the beginning.
Definition: ArrayIter.h:198
virtual ArrayBase & getArray() override
Get the array in the cursor.
Iterate an Array cursor through another Array.
Definition: ArrayFwd.h:18
void next()
Move the cursor to the next position.
Definition: ArrayIter.h:184
virtual void next() override
Move the cursor to the next position.
void init(const Array< T, Alloc > &)
helper function to centralize construction work
virtual void set(const IPosition &cursorPos) override
Set the cursor to the given position.
ArrayIterator< T, Alloc > ai
Definition: ArrayIter.h:221
std::unique_ptr< Array< T, Alloc > > ap_p
The cursor.
Definition: ArrayIter.h:122
ReadOnlyArrayIterator(const Array< T, Alloc > &arr, size_t byDim=1)
Step through array &quot;arr&quot; using a cursor of dimensionality &quot;byDim&quot;.
Definition: ArrayIter.h:175
Iterate a const Array cursor through a const Array.
Definition: ArrayIter.h:171
A templated N-D Array class with zero origin. Array&lt;T, Alloc&gt; is a templated, N-dimensional, Array class. The origin is zero, but by default indices are zero-based. This Array class is the base class for the Vector, Matrix, and Cube subclasses.
Definition: Array.h:156
Array< T, Alloc > pOriginalArray_p
Definition: ArrayIter.h:131
ArrayIterator< T, Alloc > & operator=(const ArrayIterator< T, Alloc > &)
void set(const IPosition &cursorPos)
Set the cursor to the given position.
Definition: ArrayIter.h:194
Array< T, Alloc > & array()
Return the cursor.
Definition: ArrayIter.h:115
ReadOnlyArrayIterator(const Array< T, Alloc > &arr, const IPosition &axes, bool axesAreCursor=true)
Step through an array for the given iteration axes.
Definition: ArrayIter.h:179