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.
35 template<typename T> class MArray;
36
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 //
62 // <group name="Array basic functions">
64 // Define STL-like accumulate function operating on arrays with masks.
65 // A mask value True means masked-off, thus is not taken into account.
66 // <group>
67 // <br>The first function initializes the accumulator to the first
68 // unmasked value. This is useful if it is not possible to initialize
69 // it externally (e.g. for a function like min).
70 template<typename T, typename ARRAYITER, typename MASKITER, typename OPER>
71 T accumulateMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
72 OPER oper)
73 {
74 T accum = T();
75 for (; abegin!=aend; ++abegin, ++mbegin) {
76 if (!*mbegin) { accum = *abegin; ++abegin; ++mbegin; break; }
77 }
78 for (; abegin!=aend; ++abegin, ++mbegin) {
79 if (!*mbegin) accum = oper(accum, *abegin);
80 }
81 return accum;
82 }
83
84 // The second function uses an externally initialized accumulator
85 // (e.g. needed for sum).
86 template<typename T, typename ARRAYITER, typename MASKITER, typename OPER>
87 T accumulateMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
88 T accum, OPER oper)
89 {
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.
99 template<typename T, typename ARRAYITER, typename MASKITER>
100 size_t countMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
101 const T& value)
102 {
103 size_t n = 0;
104 for (; abegin!=aend; ++abegin, ++mbegin) {
105 if (!*mbegin && *abegin == value) ++n;
106 }
107 return n;
108 }
109
110 // Count the number of unmasked values not matching the given value.
111 // It is similar to std::count, but a mask is applied.
112 template<typename T, typename ARRAYITER, typename MASKITER>
113 size_t countNEMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
114 const T& value)
115 {
116 size_t n = 0;
117 for (; abegin!=aend; ++abegin, ++mbegin) {
118 if (!*mbegin && *abegin != value) ++n;
119 }
120 return n;
121 }
122
123 // Define a function to compare the unmasked elements of two sequences.
124 // It returns true if all unmasked elements compare true or if there are
125 // no unmasked elements.
126 // An example compare operator is <src>std::equal_to</src>.
127 // <group>
128 template<typename InputIterator1, typename InputIterator2,
129 typename MaskIterator, typename CompareOperator>
130 inline bool compareAllMasked (InputIterator1 first1, InputIterator1 last1,
131 InputIterator2 first2,
132 MaskIterator mask1, MaskIterator mask2,
133 CompareOperator op)
134 {
135 for (; first1!=last1; ++first1, ++first2, ++mask1, ++mask2) {
136 if (!*mask1 && !*mask2) {
137 if (!op(*first1, *first2)) return False;
138 }
139 }
140 return true;
141 }
142 template<typename InputIterator1, typename InputIterator2,
143 typename MaskIterator, typename CompareOperator>
144 inline bool compareAllMasked (InputIterator1 first1, InputIterator1 last1,
145 InputIterator2 first2,
146 MaskIterator mask1,
147 CompareOperator op)
148 {
149 for (; first1!=last1; ++first1, ++first2, ++mask1) {
150 if (!*mask1) {
151 if (!op(*first1, *first2)) return False;
152 }
153 }
154 return true;
155 }
156 // For use with a constant left value.
157 // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
158 // (see ArrayMath.h).
159 template<typename InputIterator1, typename T,
160 typename MaskIterator, typename CompareOperator>
161 inline bool compareAllLeftMasked (InputIterator1 first1, InputIterator1 last1,
162 T left, MaskIterator mask1,
163 CompareOperator op)
164 {
165 for (; first1!=last1; ++first1, ++mask1) {
166 if (!*mask1) {
167 if (!op(left, *first1)) return False;
168 }
169 }
170 return true;
171 }
172 // For use with a constant right value.
173 // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
174 // (see ArrayMath.h).
175 template<typename InputIterator1, typename T,
176 typename MaskIterator, typename CompareOperator>
177 inline bool compareAllRightMasked(InputIterator1 first1, InputIterator1 last1,
178 T right, MaskIterator mask1,
179 CompareOperator op)
180 {
181 for (; first1!=last1; ++first1, ++mask1) {
182 if (!*mask1) {
183 if (!op(*first1, right)) return False;
184 }
185 }
186 return true;
187 }
188 // </group>
189
190 // Define a function to compare the unmasked elements of two sequences.
191 // It returns true if any element compares true.
192 // If there are no unmasked elements, it returns False.
193 // An example compare operator is <src>std::equal_to</src>.
194 // <group>
195 template<typename InputIterator1, typename InputIterator2,
196 typename MaskIterator, typename CompareOperator>
197 inline bool compareAnyMasked (InputIterator1 first1, InputIterator1 last1,
198 InputIterator2 first2,
199 MaskIterator mask1, MaskIterator mask2,
200 CompareOperator op)
201 {
202 for (; first1!=last1; ++first1, ++first2, ++mask1, ++mask2) {
203 if (!*mask1 && !*mask2) {
204 if (op(*first1, *first2)) return true;
205 }
206 }
207 return False;
208 }
209 template<typename InputIterator1, typename InputIterator2,
210 typename MaskIterator, typename CompareOperator>
211 inline bool compareAnyMasked (InputIterator1 first1, InputIterator1 last1,
212 InputIterator2 first2,
213 MaskIterator mask1,
214 CompareOperator op)
215 {
216 for (; first1!=last1; ++first1, ++first2, ++mask1) {
217 if (!*mask1) {
218 if (op(*first1, *first2)) return true;
219 }
220 }
221 return False;
222 }
223 // For use with a constant left value.
224 // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
225 // (see ArrayMath.h).
226 template<typename InputIterator1, typename T,
227 typename MaskIterator, typename CompareOperator>
228 inline bool compareAnyLeftMasked (InputIterator1 first1, InputIterator1 last1,
229 T left, MaskIterator mask1,
230 CompareOperator op)
231 {
232 for (; first1!=last1; ++first1, ++mask1) {
233 if (!*mask1) {
234 if (op(left, *first1)) return true;
235 }
236 }
237 return False;
238 }
239 // For use with a constant right value.
240 // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
241 // (see ArrayMath.h).
242 template<typename InputIterator1, typename T,
243 typename MaskIterator, typename CompareOperator>
244 inline bool compareAnyRightMasked(InputIterator1 first1, InputIterator1 last1,
245 T right, MaskIterator mask1,
246 CompareOperator op)
247 {
248 for (; first1!=last1; ++first1, ++mask1) {
249 if (!*mask1) {
250 if (op(*first1, right)) return true;
251 }
252 }
253 return False;
254 }
255 // </group>
256
257
258
259 // Define the base class for functors to perform a reduction function on an
260 // MArray object. The functors themselves are defined elsewhere.
261 template<typename T, typename RES=T> class MArrayFunctorBase {
262 public:
264 virtual RES operator() (const MArray<T>&) const = 0;
265 };
266
267 // </group>
268
269} //# end namespace
270
271#endif
Define the base class for functors to perform a reduction function on an MArray object.
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
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.