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