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