casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MatrixIter.h
Go to the documentation of this file.
1 //# MatrixIter.h: Iterate a matrix cursor through another array
2 //# Copyright (C) 1993,1994,1995,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_MATRIXITER_2_H
29 #define CASA_MATRIXITER_2_H
30 
31 #include "ArrayIter.h"
32 #include "Matrix.h"
33 
34 namespace casacore { //# NAMESPACE CASACORE - BEGIN
35 
36 //
37 // <summary> Iterate a Matrix cursor through another Array. </summary>
38 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
39 // </reviewed>
40 //
41 // MatrixIterator steps a Matrix (the "cursor") through an array.
42 // The cursor "refers" to storage in the array, so that changing the
43 // values in the cursor changes values in the original array.
44 //
45 // This class is derived from ArrayIterator; basically it only adds the
46 // matrix() member function which allows you to access the cursor as a Matrix.
47 //
48 // <note role=tip>
49 // The origin of the cursor, i.e. the subarray that moves through the
50 // larger array, is always zero.
51 // </note>
52 //
53 // In this example we want to make a "moment" map of a cube, i.e. collapse
54 // the "Z" axis by averaging it.
55 // <srcblock>
56 // Cube<float> cube;
57 // MatrixIterator planeIter(cube);
58 // Matrix<float> average(planeIter.matrix().copy()); // init with first plane
59 // planeIter.next(); // advance the iterator
60 // while (! planeIter.pastEnd()) {
61 // average += planeIter.matrix(); // Sum the next plane
62 // planeIter.next();
63 // }
64 // average /= float(cube.shape()(2)); // divide by the number of planes
65 // </srcblock>
66 
67 template<typename T, typename Alloc=std::allocator<T>>
68 class MatrixIterator : public ArrayIterator<T, Alloc>
69 {
70 public:
71  // Iterate by matrices through array "a".
72  // The first 2 axes form the cursor axes.
73  explicit MatrixIterator(Array<T, Alloc> &a);
74 
75  // Iterate by matrices through array "a".
76  // The given axes form the cursor axes.
77  MatrixIterator(Array<T, Alloc> &a, size_t cursorAxis1, size_t cursorAxis2);
78 
79  // Return the matrix at the current position.
80  Matrix<T, Alloc> &matrix() {return *(Matrix<T, Alloc> *)(this->ap_p.get());}
81 
82 private:
83  // Not implemented.
84  MatrixIterator(const MatrixIterator<T, Alloc> &) = delete;
85  // Not implemented.
87 };
88 
89 //
90 // <summary> Iterate a Matrix cursor through a R/O Array. </summary>
91 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
92 // </reviewed>
93 //
94 // ReadOnlyMatrixIterator behaves exactly like MatrixIterator (cf.) only
95 // it should be used on const Arrays.
96 //
97 // <note role=tip> Note that the R/O MatrixIterator is not derived from R/O
98 // ArrayIterator.
99 // </note>
100 //
101 template<class T> class ReadOnlyMatrixIterator
102 {
103 public:
104  // <group>
106  mi(const_cast<Array<T>&>(a)) {}
107 
109  size_t cursorAxis1, size_t cursorAxis2)
110  : mi(const_cast<Array<T>&>(a), cursorAxis1, cursorAxis2) {}
111 
112  void next() {mi.next();}
113  void reset() {mi.origin();}
114  void origin() {mi.origin();}
115 
116  const Array<T> &array() {return mi.array();}
117  const Matrix<T> &matrix() {return mi.matrix();}
118 
119  bool atStart() const {return mi.atStart();}
120  bool pastEnd() const {return mi.pastEnd();}
121  const IPosition &pos() const {return mi.pos();}
122  IPosition endPos() const {return mi.endPos();}
123  size_t ndim() const {return mi.ndim();}
124  // </group>
125 private:
126  // Not implemented.
128  // Not implemented.
130 
132 };
133 
134 
135 } //# NAMESPACE CASACORE - END
136 
137 #include "MatrixIter.tcc"
138 
139 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:118
MatrixIterator(Array< T, Alloc > &a)
Iterate by matrices through array &quot;a&quot;.
const IPosition & pos() const
Definition: MatrixIter.h:121
ReadOnlyMatrixIterator< T > & operator=(const ReadOnlyMatrixIterator< T > &)=delete
Not implemented.
const Matrix< T > & matrix()
Definition: MatrixIter.h:117
Iterate a Matrix cursor through another Array.
Definition: MatrixIter.h:68
MatrixIterator< T, Alloc > & operator=(const MatrixIterator< T, Alloc > &)=delete
Not implemented.
ReadOnlyMatrixIterator(const Array< T > &a, size_t cursorAxis1, size_t cursorAxis2)
Definition: MatrixIter.h:108
Iterate a Matrix cursor through a R/O Array.
Definition: MatrixIter.h:101
A 2-D Specialization of the Array class.
Definition: ArrayFwd.h:10
Matrix< T, Alloc > & matrix()
Return the matrix at the current position.
Definition: MatrixIter.h:80
Iterate an Array cursor through another Array.
Definition: ArrayFwd.h:18
virtual void next() override
Move the cursor to the next position.
const Array< T > & array()
Definition: MatrixIter.h:116
std::unique_ptr< Array< T, Alloc > > ap_p
The cursor.
Definition: ArrayIter.h:122
size_t ndim() const
What is the dimensionality of the volume we are iterating through?
Definition: ArrayPosIter.h:192
IPosition endPos() const
Return the end position of the cursor.
bool pastEnd() const
Returns true if the cursor has moved past the end of its volume.
Definition: ArrayPosIter.h:198
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
bool atStart() const
Returns true of the cursor is at the origin.
Array< T, Alloc > & array()
Return the cursor.
Definition: ArrayIter.h:115
ReadOnlyMatrixIterator(const Array< T > &a)
Definition: MatrixIter.h:105
const IPosition & pos() const
Return the position of the cursor.
Definition: ArrayPosIter.h:139