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