casacore
Loading...
Searching...
No Matches
MArrayMathBase.h
Go to the documentation of this file.
1// # MArrayMathBase.h: Basic functions and classes for math on MArray objects
2// # Copyright (C) 2012
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_MARRAYMATHBASE_H
27#define CASA_MARRAYMATHBASE_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Arrays/ArrayMathBase.h>
31
32namespace casacore {
33
34// # Forward declarations.
35template <typename T>
36class MArray;
37
38// <summary>
39// Basic functions and classes for math on MArray objects
40// </summary>
41//
42// <reviewed reviewer="UNKNOWN" date="" tests="tMArrayMath">
43//
44// <prerequisite>
45// <li> <linkto class=MArray>MArray</linkto>
46// </prerequisite>
47//
48// <synopsis>
49// This header file defines several STL-like functions to work on
50// iterators with a mask.
51//
52// Furthermore, abstract base classes are defined for functors to be used
53// in functions like slidingXXX.
54// Virtual functions instead of templated functions are used to avoid
55// code bloat when used in functions like partialArrayMath. Because a
56// reduction operation usually takes much more time than the call, using
57// virtual functions hardly imposes a performance penalty.
58// </synopsis>
59
60//
61// <group name="Array basic functions">
63// Define STL-like accumulate function operating on arrays with masks.
64// A mask value True means masked-off, thus is not taken into account.
65// <group>
66// <br>The first function initializes the accumulator to the first
67// unmasked value. This is useful if it is not possible to initialize
68// it externally (e.g. for a function like min).
69template <typename T, typename ARRAYITER, typename MASKITER, typename OPER>
70T accumulateMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, OPER oper) {
71 T accum = T();
72 for (; abegin != aend; ++abegin, ++mbegin) {
73 if (!*mbegin) {
74 accum = *abegin;
75 ++abegin;
76 ++mbegin;
77 break;
78 }
79 }
80 for (; abegin != aend; ++abegin, ++mbegin) {
81 if (!*mbegin) accum = oper(accum, *abegin);
82 }
83 return accum;
84}
85
86// The second function uses an externally initialized accumulator
87// (e.g. needed for sum).
88template <typename T, typename ARRAYITER, typename MASKITER, typename OPER>
89T accumulateMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, T accum, OPER oper) {
90 for (; abegin != aend; ++abegin, ++mbegin) {
91 if (!*mbegin) accum = oper(accum, *abegin);
92 }
93 return accum;
94}
95// </group>
96
97// Count the number of unmasked values matching the given value.
98// It is similar to std::count, but a mask is applied.
99template <typename T, typename ARRAYITER, typename MASKITER>
100size_t countMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, const T& value) {
101 size_t n = 0;
102 for (; abegin != aend; ++abegin, ++mbegin) {
103 if (!*mbegin && *abegin == value) ++n;
104 }
105 return n;
106}
107
108// Count the number of unmasked values not matching the given value.
109// It is similar to std::count, but a mask is applied.
110template <typename T, typename ARRAYITER, typename MASKITER>
111size_t countNEMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, const T& value) {
112 size_t n = 0;
113 for (; abegin != aend; ++abegin, ++mbegin) {
114 if (!*mbegin && *abegin != value) ++n;
115 }
116 return n;
117}
118
119// Define a function to compare the unmasked elements of two sequences.
120// It returns true if all unmasked elements compare true or if there are
121// no unmasked elements.
122// An example compare operator is <src>std::equal_to</src>.
123// <group>
124template <typename InputIterator1, typename InputIterator2, typename MaskIterator,
125 typename CompareOperator>
126inline bool compareAllMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
127 MaskIterator mask1, MaskIterator mask2, CompareOperator op) {
128 for (; first1 != last1; ++first1, ++first2, ++mask1, ++mask2) {
129 if (!*mask1 && !*mask2) {
130 if (!op(*first1, *first2)) return False;
131 }
132 }
133 return true;
134}
135template <typename InputIterator1, typename InputIterator2, typename MaskIterator,
136 typename CompareOperator>
137inline bool compareAllMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
138 MaskIterator mask1, CompareOperator op) {
139 for (; first1 != last1; ++first1, ++first2, ++mask1) {
140 if (!*mask1) {
141 if (!op(*first1, *first2)) return False;
142 }
143 }
144 return true;
145}
146// For use with a constant left value.
147// This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
148// (see ArrayMath.h).
149template <typename InputIterator1, typename T, typename MaskIterator, typename CompareOperator>
150inline bool compareAllLeftMasked(InputIterator1 first1, InputIterator1 last1, T left,
151 MaskIterator mask1, CompareOperator op) {
152 for (; first1 != last1; ++first1, ++mask1) {
153 if (!*mask1) {
154 if (!op(left, *first1)) return False;
155 }
156 }
157 return true;
158}
159// For use with a constant right value.
160// This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
161// (see ArrayMath.h).
162template <typename InputIterator1, typename T, typename MaskIterator, typename CompareOperator>
163inline bool compareAllRightMasked(InputIterator1 first1, InputIterator1 last1, T right,
164 MaskIterator mask1, CompareOperator op) {
165 for (; first1 != last1; ++first1, ++mask1) {
166 if (!*mask1) {
167 if (!op(*first1, right)) return False;
168 }
169 }
170 return true;
171}
172// </group>
173
174// Define a function to compare the unmasked elements of two sequences.
175// It returns true if any element compares true.
176// If there are no unmasked elements, it returns False.
177// An example compare operator is <src>std::equal_to</src>.
178// <group>
179template <typename InputIterator1, typename InputIterator2, typename MaskIterator,
180 typename CompareOperator>
181inline bool compareAnyMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
182 MaskIterator mask1, MaskIterator mask2, CompareOperator op) {
183 for (; first1 != last1; ++first1, ++first2, ++mask1, ++mask2) {
184 if (!*mask1 && !*mask2) {
185 if (op(*first1, *first2)) return true;
186 }
187 }
188 return False;
189}
190template <typename InputIterator1, typename InputIterator2, typename MaskIterator,
191 typename CompareOperator>
192inline bool compareAnyMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
193 MaskIterator mask1, CompareOperator op) {
194 for (; first1 != last1; ++first1, ++first2, ++mask1) {
195 if (!*mask1) {
196 if (op(*first1, *first2)) return true;
197 }
198 }
199 return False;
200}
201// For use with a constant left value.
202// This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
203// (see ArrayMath.h).
204template <typename InputIterator1, typename T, typename MaskIterator, typename CompareOperator>
205inline bool compareAnyLeftMasked(InputIterator1 first1, InputIterator1 last1, T left,
206 MaskIterator mask1, CompareOperator op) {
207 for (; first1 != last1; ++first1, ++mask1) {
208 if (!*mask1) {
209 if (op(left, *first1)) return true;
210 }
211 }
212 return False;
213}
214// For use with a constant right value.
215// This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
216// (see ArrayMath.h).
217template <typename InputIterator1, typename T, typename MaskIterator, typename CompareOperator>
218inline bool compareAnyRightMasked(InputIterator1 first1, InputIterator1 last1, T right,
219 MaskIterator mask1, CompareOperator op) {
220 for (; first1 != last1; ++first1, ++mask1) {
221 if (!*mask1) {
222 if (op(*first1, right)) return true;
223 }
224 }
225 return False;
226}
227// </group>
228
229// Define the base class for functors to perform a reduction function on an
230// MArray object. The functors themselves are defined elsewhere.
231template <typename T, typename RES = T>
233 public:
235 virtual RES operator()(const MArray<T>&) const = 0;
236};
237
238// </group>
239
240} // namespace casacore
241
242#endif
Define the base class for functors to perform a reduction function on an MArray object.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
T accumulateMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, OPER oper)
Define STL-like accumulate function operating on arrays with masks.
bool compareAnyRightMasked(InputIterator1 first1, InputIterator1 last1, T right, MaskIterator mask1, CompareOperator op)
For use with a constant right value.
bool compareAnyLeftMasked(InputIterator1 first1, InputIterator1 last1, T left, MaskIterator mask1, CompareOperator op)
For use with a constant left value.
bool compareAllRightMasked(InputIterator1 first1, InputIterator1 last1, T right, MaskIterator mask1, CompareOperator op)
For use with a constant right value.
size_t countNEMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, const T &value)
Count the number of unmasked values not matching the given value.
bool compareAllMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, MaskIterator mask1, MaskIterator mask2, CompareOperator op)
Define a function to compare the unmasked elements of two sequences.
bool compareAnyMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, MaskIterator mask1, MaskIterator mask2, CompareOperator op)
Define a function to compare the unmasked elements of two sequences.
T accumulateMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, T accum, OPER oper)
The second function uses an externally initialized accumulator (e.g.
bool compareAllMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, MaskIterator mask1, CompareOperator op)
bool compareAllLeftMasked(InputIterator1 first1, InputIterator1 last1, T left, MaskIterator mask1, CompareOperator op)
For use with a constant left value.
bool compareAnyMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, MaskIterator mask1, CompareOperator op)
size_t countMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, const T &value)
Count the number of unmasked values matching the given value.