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