casacore
Loading...
Searching...
No Matches
MaskedLatticeIterator.h
Go to the documentation of this file.
1// # MaskedLatticeIterator.h: Iterators for Masked Lattices: readonly
2// # Copyright (C) 2003
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 LATTICES_MASKEDLATTICEITERATOR_H
27#define LATTICES_MASKEDLATTICEITERATOR_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/lattices/Lattices/MaskedLattice.h>
32#include <casacore/lattices/Lattices/LatticeIterator.h>
33#include <memory>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// A readonly iterator for masked Lattices.
39// </summary>
40
41// <use visibility=export>
42
43// <reviewed reviewer="" date="" tests="tMaskedLatticeIterator.cc">
44// </reviewed>
45
46// <prerequisite>
47// <li> <linkto class="MaskedLattice">MaskedLattice</linkto>
48// <li> <linkto class="RO_LatticeIterator">RO_LatticeIterator</linkto>
49// </prerequisite>
50
51// <etymology>
52// The leading "RO" is shorthand for "readonly", which indicates that an
53// RO_MaskedLatticeIterator is used for traversing a masked lattice,
54// examining and possibly extracting its contents, but not for modifying it.
55// </etymology>
56
57// <synopsis>
58// This class provides a convenient way to traverse any class derived from
59// MaskedLattice. It is derived from class
60// <linkto class=RO_LatticeIterator>RO_LatticeIterator</linkto>, so it
61// provides the same iterator capabilities.
62// On top of that it offers the function <src>getMask</src> to get the
63// contents of the mask at the current iterator position.
64//
65// In principle, iteration through a MaskedLattice can be done as:
66// <srcblock>
67// void someFunc (const MaskedLattice<Float>& lattice)
68// {
69// RO_LatticeIterator<Float> iter(lattice);
70// Array<Bool> mask;
71// while (! iter.atEnd()) {
72// const Array<Float>& array = iter.cursor();
73// lattice.getMaskSlice (mask, iter.position(), array.shape());
74// iter++;
75// }
76// }
77// </srcblock>
78// Using a MaskedLatticeIterator makes getting the mask slightly more
79// convenient.
80// <srcblock>
81// void someFunc (const MaskedLattice<Float>& lattice)
82// {
83// RO_MaskedLatticeIterator<Float> iter(lattice);
84// Array<Bool> mask;
85// while (! iter.atEnd()) {
86// const Array<Float>& array = iter.cursor();
87// iter.getMask (mask);
88// iter++;
89// }
90// }
91// </srcblock>
92// However, the most important reason to use MaskedLatticeIterator is
93// performance. If the underlying lattice is a LatticeExpr object,
94// the expression will be evaluated twice if a LatticeIterator object
95// is used. The reason is that the lattice in the LatticeIterator is
96// a different object from the lattice object used to get the mask.
97// Hence, the optimization put in LatticeExpr is not used.
98// When using a MaskedLatticeIterator the same lattice object is used
99// to get data and mask.
100// </synopsis>
101
102// <motivation>
103// The performance gain for LatticeExpr was the most important reason
104// to develop this class.
105// </motivation>
106
107// # <todo asof="2003/11/10">
108// # <li>
109// # </todo>
110
111template <class T>
113 // # Make members of parent class known.
114 public:
119
120 public:
121 // The default constructor creates an empty object which is practically
122 // unusable.
123 // It can only be used as the source or target of an assignment. It can
124 // also be used as the source for the copy constructor and the copy function.
125 // Other functions do not check if the object is empty and will usually
126 // give a segmentation fault.
127 // The function isNull() can be used to test if the object is empty.
129
130 // Construct the Iterator with the supplied data.
131 // It uses a TileStepper as the default iteration strategy.
132 // useRef=True means that if possible the cursor arrays returned
133 // reference the data in the underlying lattice. This is only possible
134 // for ArrayLattice objects (or e.g. a SubLattice using it).
135 explicit RO_MaskedLatticeIterator(const MaskedLattice<T>& data, Bool useRef = True);
136
137 // Construct the Iterator with the supplied data, and iteration strategy
139 Bool useRef = True);
140
141 // Construct the Iterator with the supplied data.
142 // It uses a LatticeStepper with the supplied cursor shape as the
143 // iteration strategy.
145 Bool useRef = True);
146
147 // The copy constructor uses reference semantics (ie. NO real copy is made).
148 // The function <src>copy</src> can be used to make a true copy.
150
151 // Destructor (cleans up dangling references and releases memory)
153
154 // Assignment uses reference semantics (ie. NO real copy is made).
155 // The function <src>copy</src> can be used to make a true copy.
157
158 // Make a copy of the iterator object.
159 // This means that an independent navigator object is created to
160 // be able to iterate independently through the same MaskedLattice.
161 // The position in the copied navigator is the same as the original.
162 // The reset function has to be used to start at the beginning.
163 // <br>Note that if the MaskedLattice uses a cache (e.g. PagedArray), the
164 // cache is shared by the iterators.
166
167 // Return the underlying MaskedLattice object.
168 MaskedLattice<T>& lattice() const { return const_cast<MaskedLattice<T>&>(*itsMaskLattPtr); }
169
170 // Is the underlying MaskedLattice really masked?
171 Bool isMasked() const { return itsMaskLattPtr->isMasked(); }
172
173 // Get the mask for the current position.
174 // It returns the same flag as
175 // <linkto class=MaskedLattice>MaskedLattice::getMaskSlice</linkto>.
176 // <group>
177 Bool getMask(COWPtr<Array<Bool>>&, Bool removeDegenerateAxes = False) const;
178 Bool getMask(Array<Bool>&, Bool removeDegenerateAxes = False) const;
179 Array<Bool> getMask(Bool removeDegenerateAxes = False) const;
180 // </group>
181
182 private:
183 // Construct from a LatticeIterator (for copy function).
185
186 // Fill the pointer with a pointer to the masked lattice.
187 // This pointer is a casted copy of the lattice pointer in the base class.
188 // In this way they share the same MaskedLattice object, which is needed
189 // for optimal performance of e.g. LatticeExpr.
190 // Otherwise getting data from the lattice and from the mask would
191 // result in 2 evaluations of the expression.
192 // However, the lattice can be a PagedArray (for example, for PagedImage).
193 // In that case a clone of the original MaskedLattice is used.
194 void fillPtr(const MaskedLattice<T>& mlattice);
195
196 // The shared pointer is used for automatic deletion.
197 // If not null, it is the same as the normal pointer below.
198 std::shared_ptr<MaskedLattice<T>> itsMaskLattShrPtr;
199 // Pointer to the MaskedLattice.
200 // Deletion (if needed) is done by the shared pointer above.
202};
203
204} // namespace casacore
205
206#ifndef CASACORE_NO_AUTO_TEMPLATES
207#include <casacore/lattices/Lattices/MaskedLatticeIterator.tcc>
208#endif // # CASACORE_NO_AUTO_TEMPLATES
209#endif
Bool isNull() const
Is the iterator object empty?
IPosition endPosition() const
Function which returns the current position of the end of the cursor.
IPosition position() const
Function which returns the current position of the beginning of the cursor within the Lattice.
RO_LatticeIterator()
The default constructor creates an empty object which is practically unusable.
IPosition cursorShape() const
Function which returns the shape of the cursor which is iterating through the Lattice.
RO_MaskedLatticeIterator(const MaskedLattice< T > &data, Bool useRef=True)
Construct the Iterator with the supplied data.
RO_MaskedLatticeIterator(const MaskedLattice< T > &data, const IPosition &cursorShape, Bool useRef=True)
Construct the Iterator with the supplied data.
void fillPtr(const MaskedLattice< T > &mlattice)
Fill the pointer with a pointer to the masked lattice.
Bool getMask(Array< Bool > &, Bool removeDegenerateAxes=False) const
MaskedLattice< T > * itsMaskLattPtr
Pointer to the MaskedLattice.
RO_MaskedLatticeIterator(const RO_LatticeIterator< T > &, const RO_MaskedLatticeIterator< T > &)
Construct from a LatticeIterator (for copy function).
RO_MaskedLatticeIterator< T > copy() const
Make a copy of the iterator object.
MaskedLattice< T > & lattice() const
Return the underlying MaskedLattice object.
~RO_MaskedLatticeIterator()
Destructor (cleans up dangling references and releases memory).
RO_MaskedLatticeIterator(const MaskedLattice< T > &data, const LatticeNavigator &method, Bool useRef=True)
Construct the Iterator with the supplied data, and iteration strategy.
RO_MaskedLatticeIterator()
The default constructor creates an empty object which is practically unusable.
Bool isMasked() const
Is the underlying MaskedLattice really masked?
RO_MaskedLatticeIterator(const RO_MaskedLatticeIterator< T > &other)
The copy constructor uses reference semantics (ie.
RO_MaskedLatticeIterator< T > & operator=(const RO_MaskedLatticeIterator< T > &)
Assignment uses reference semantics (ie.
Array< Bool > getMask(Bool removeDegenerateAxes=False) const
std::shared_ptr< MaskedLattice< T > > itsMaskLattShrPtr
The shared pointer is used for automatic deletion.
Bool getMask(COWPtr< Array< Bool > > &, Bool removeDegenerateAxes=False) const
Get the mask for the current position.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41