casacore
Loading...
Searching...
No Matches
ArrayPosIter.h
Go to the documentation of this file.
1// # ArrayPosIter.h: Iterate an IPosition through the shape of an Array
2// # Copyright (C) 1993,1994,1995,1998,1999,2004
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_ARRAYPOSITER_2_H
27#define CASA_ARRAYPOSITER_2_H
28
29// # Change the following to a forward declare?
30#include "IPosition.h"
31
32namespace casacore { // # NAMESPACE CASACORE - BEGIN
33
34// # Forward Declarations
35class ArrayBase;
36
37// <summary> Iterate an IPosition through the shape of an Array </summary>
38// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
39// </reviewed>
40
41// <synopsis>
42// ArrayPositionIterator manipulates an IPosition "cursor" through some
43// volume defined by an origin and shape. This position can in turn be
44// used to index into, or otherwise define a position in, an Array. Normally
45// users won't use this class directly, rather they will use an ArrayIterator,
46// VectorIterator or MatrixIterator object, which in turn uses this class.
47// ArrayPositionIterator is also used in the implementation of Array.
48//
49// <srcblock>
50// template<class T> void verySlowArrayCopy(Array<T> &to, const Array<T> &from)
51// {
52// if (! to.conform(from)) {
53// // throw some error
54// }
55// ArrayPositionIterator toiter(to.shape(), to.origin(),0);
56// ArrayPositionIterator fromiter(from.shape(), from.origin(),0);
57// // If to.origin() == from.origin() we only need one iterator
58// // or we could offset positions by the difference in origins.
59// // The "0" means we are stepping by scalars.
60// while (! toiter.pastEnd()) { // we know arrays conform
61// to(toiter.pos()) = fromiter(fromiter.pos());
62// toiter.next(); fromiter.next();
63// }
64// }
65// </srcblock>
66//
67// Iteration can be done by any combination of axes, but it can only be
68// done for full axes.
69// <br>The iteration step always "fills up" its dimensionality.
70// E.g., if we are stepping through a cube by matrices, the matrix completely
71// fills up the plane.
72// Casacore class ArrayLattice in the lattices
73// package can be used to iterate with partial volumes.
74//
75// <p>
76// ArrayPositionIterator also serves as the base class of ArrayIterator.
77// Function <src>makeIterator</src> in class ArrayBase can be used to make an
78// ArrayIterator without having to know the template type. Function
79// <src>getArray</src> in this class can be used to obtain the current
80// contents of the cursor as an ArrayBase object.
81// </synopsis>
82
84 public:
85 // Define the shape and origin of the volume the cursor will step
86 // through. Also define the dimensionality of the step. byDim==0 implies
87 // we are stepping by scalars (i.e. every element), byDim==1 implies that
88 // we are stepping by vector, ==2 by matrices, and so on.
89 // If uses the first byDim axes as the cursor volume and it steps
90 // through the remaining axes.
91 // <group>
92 ArrayPositionIterator(const IPosition &shape, const IPosition &origin, size_t byDim);
93 ArrayPositionIterator(const IPosition &shape, size_t byDim);
94 // </group>
95
96 // Step through an array using the given axes.
97 // The axes can be given in two ways:
98 // <ol>
99 // <li>axesAreCursor=true means that the axes form the cursor axes.
100 // The remaining axes will form the iteration axes.
101 // This is the default.
102 // <li>axesAreCursor=false means the opposite.
103 // In this case the iteration axes can be given in any order.
104 // </ol>
105 // E.g. when using iteration axes 2,0 for an array with shape [5,3,7], each
106 // iteration step returns a cursor (containing the data of axis 1).
107 // During the iteration axis 2 will vary most rapidly (as it was
108 // given first).
109 // <br>E.g. for a shape of [3,4,5,6] and cursor axes [2,0], the cursor size
110 // is [3,5] (axes 0 and 2), while the iteration is done over axes 1 and 3
111 // (1 the fastest varying one).
112 ArrayPositionIterator(const IPosition &shape, const IPosition &axes, bool axesAreCursor = true);
113
115
116 // Reset the cursor to the beginning of the volume.
117 // <group>
118 virtual void reset();
119 void origin() { reset(); }
120 // </group>
121
122 // Returns true of the cursor is at the origin.
123 bool atStart() const;
124
125 // Returns true if the cursor has moved past the end of its volume.
126 bool pastEnd() const;
127
128 // Return the position of the cursor.
129 // This include all axes
130 const IPosition &pos() const { return Cursor; }
131
132 // Return the end position of the cursor.
134
135 // Advance the cursor to its next position.
136 virtual void next();
137
138 // Set the cursor to the given position.
139 // The position can only contain the iteration axes or it can be the full
140 // position.
141 // <br>In the first case the position must to be given in the order
142 // of the iteration axes as given in the constructor.
143 // In the latter case the position must be given in natural order
144 // (as given by function <src>pos</src> and only the cursor axes are taken
145 // into account.
146 virtual void set(const IPosition &cursorPos);
147
148 // What is the dimensionality of the volume we are iterating through?
149 size_t ndim() const;
150
151 // Return the iteration axes.
152 const IPosition &iterAxes() const { return iterationAxes; }
153
154 // Return the cursor axes.
155 const IPosition &cursorAxes() const { return cursAxes; }
156
157 // Get the array in the cursor.
158 // This is only implemented in the derived ArrayIterator class.
159 // By default it throws an exception.
160 virtual ArrayBase &getArray();
161
162 protected:
163 // Advance cursor to its next position and tell which dimension stepped.
164 size_t nextStep();
165 // What is the dimensionality of the "step" the cursor takes, i.e.
166 // 0 for scalars, 1 for vector, ....
167 size_t dimIter() const { return cursAxes.nelements(); }
168
169 private:
170 // Setup the object for the constructor.
171 // <group>
172 void setup(size_t byDim);
173 void setup(const IPosition &axes, bool axesAreCursor);
174 // </group>
175
176 // # We should probably have mf's for getting at Start,Shape and End.
180};
181
182// Dimensionality of the array we are iterating through.
183inline size_t ArrayPositionIterator::ndim() const { return Start.nelements(); }
184
185// We are at the "end" if we cannot advance any more.
186inline bool ArrayPositionIterator::pastEnd() const { return atOrBeyondEnd; }
187
188} // namespace casacore
189
190#endif
Non-templated base class for templated Array class.
Definition ArrayBase.h:69
size_t dimIter() const
What is the dimensionality of the "step" the cursor takes, i.e.
void setup(const IPosition &axes, bool axesAreCursor)
const IPosition & pos() const
Return the position of the cursor.
ArrayPositionIterator(const IPosition &shape, const IPosition &axes, bool axesAreCursor=true)
Step through an array using the given axes.
const IPosition & iterAxes() const
Return the iteration axes.
IPosition endPos() const
Return the end position of the cursor.
ArrayPositionIterator(const IPosition &shape, size_t byDim)
ArrayPositionIterator(const IPosition &shape, const IPosition &origin, size_t byDim)
Define the shape and origin of the volume the cursor will step through.
virtual void set(const IPosition &cursorPos)
Set the cursor to the given position.
virtual void reset()
Reset the cursor to the beginning of the volume.
size_t nextStep()
Advance cursor to its next position and tell which dimension stepped.
size_t ndim() const
What is the dimensionality of the volume we are iterating through?
bool atStart() const
Returns true of the cursor is at the origin.
virtual void next()
Advance the cursor to its next position.
bool pastEnd() const
Returns true if the cursor has moved past the end of its volume.
const IPosition & cursorAxes() const
Return the cursor axes.
void setup(size_t byDim)
Setup the object for the constructor.
virtual ArrayBase & getArray()
Get the array in the cursor.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
IPosition shape(const RecordFieldId &) const
Get the actual shape of this field.