casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: 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: MArrayMathBase.h 21262 2012-09-07 12:38:36Z gervandiepen $
27 
28 #ifndef CASA_MARRAYMATHBASE_H
29 #define CASA_MARRAYMATHBASE_H
30 
31 #include <casacore/casa/aips.h>
33 
34 namespace casacore {
35 
36  //# Forward declarations.
37  template<typename T> class MArray;
38 
39 
40  // <summary>
41  // Basic functions and classes for math on MArray objects
42  // </summary>
43  //
44  // <reviewed reviewer="UNKNOWN" date="" tests="tMArrayMath">
45  //
46  // <prerequisite>
47  // <li> <linkto class=MArray>MArray</linkto>
48  // </prerequisite>
49  //
50  // <synopsis>
51  // This header file defines several STL-like functions to work on
52  // iterators with a mask.
53  //
54  // Furthermore, abstract base classes are defined for functors to be used
55  // in functions like slidingXXX.
56  // Virtual functions instead of templated functions are used to avoid
57  // code bloat when used in functions like partialArrayMath. Because a
58  // reduction operation usually takes much more time than the call, using
59  // virtual functions hardly imposes a performance penalty.
60  // </synopsis>
61 
62 
63  //
64  // <group name="Array basic functions">
65 
66  // Define STL-like accumulate function operating on arrays with masks.
67  // A mask value True means masked-off, thus is not taken into account.
68  // <group>
69  // <br>The first function initializes the accumulator to the first
70  // unmasked value. This is useful if it is not possible to initialize
71  // it externally (e.g. for a function like min).
72  template<typename T, typename ARRAYITER, typename MASKITER, typename OPER>
73  T accumulateMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
74  OPER oper)
75  {
76  T accum = T();
77  for (; abegin!=aend; ++abegin, ++mbegin) {
78  if (!*mbegin) { accum = *abegin; ++abegin; ++mbegin; break; }
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).
88  template<typename T, typename ARRAYITER, typename MASKITER, typename OPER>
89  T accumulateMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
90  T accum, OPER oper)
91  {
92  for (; abegin!=aend; ++abegin, ++mbegin) {
93  if (!*mbegin) accum = oper(accum, *abegin);
94  }
95  return accum;
96  }
97  // </group>
98 
99  // Count the number of unmasked values matching the given value.
100  // It is similar to std::count, but a mask is applied.
101  template<typename T, typename ARRAYITER, typename MASKITER>
102  size_t countMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
103  const T& value)
104  {
105  size_t n = 0;
106  for (; abegin!=aend; ++abegin, ++mbegin) {
107  if (!*mbegin && *abegin == value) ++n;
108  }
109  return n;
110  }
111 
112  // Count the number of unmasked values not matching the given value.
113  // It is similar to std::count, but a mask is applied.
114  template<typename T, typename ARRAYITER, typename MASKITER>
115  size_t countNEMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
116  const T& value)
117  {
118  size_t n = 0;
119  for (; abegin!=aend; ++abegin, ++mbegin) {
120  if (!*mbegin && *abegin != value) ++n;
121  }
122  return n;
123  }
124 
125  // Define a function to compare the unmasked elements of two sequences.
126  // It returns true if all unmasked elements compare true or if there are
127  // no unmasked elements.
128  // An example compare operator is <src>std::equal_to</src>.
129  // <group>
130  template<typename InputIterator1, typename InputIterator2,
131  typename MaskIterator, typename CompareOperator>
132  inline bool compareAllMasked (InputIterator1 first1, InputIterator1 last1,
133  InputIterator2 first2,
134  MaskIterator mask1, MaskIterator mask2,
135  CompareOperator op)
136  {
137  for (; first1!=last1; ++first1, ++first2, ++mask1, ++mask2) {
138  if (!*mask1 && !*mask2) {
139  if (!op(*first1, *first2)) return False;
140  }
141  }
142  return true;
143  }
144  template<typename InputIterator1, typename InputIterator2,
145  typename MaskIterator, typename CompareOperator>
146  inline bool compareAllMasked (InputIterator1 first1, InputIterator1 last1,
147  InputIterator2 first2,
148  MaskIterator mask1,
149  CompareOperator op)
150  {
151  for (; first1!=last1; ++first1, ++first2, ++mask1) {
152  if (!*mask1) {
153  if (!op(*first1, *first2)) return False;
154  }
155  }
156  return true;
157  }
158  // For use with a constant left value.
159  // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
160  // (see ArrayMath.h).
161  template<typename InputIterator1, typename T,
162  typename MaskIterator, typename CompareOperator>
163  inline bool compareAllLeftMasked (InputIterator1 first1, InputIterator1 last1,
164  T left, MaskIterator mask1,
165  CompareOperator op)
166  {
167  for (; first1!=last1; ++first1, ++mask1) {
168  if (!*mask1) {
169  if (!op(left, *first1)) return False;
170  }
171  }
172  return true;
173  }
174  // For use with a constant right value.
175  // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
176  // (see ArrayMath.h).
177  template<typename InputIterator1, typename T,
178  typename MaskIterator, typename CompareOperator>
179  inline bool compareAllRightMasked(InputIterator1 first1, InputIterator1 last1,
180  T right, MaskIterator mask1,
181  CompareOperator op)
182  {
183  for (; first1!=last1; ++first1, ++mask1) {
184  if (!*mask1) {
185  if (!op(*first1, right)) return False;
186  }
187  }
188  return true;
189  }
190  // </group>
191 
192  // Define a function to compare the unmasked elements of two sequences.
193  // It returns true if any element compares true.
194  // If there are no unmasked elements, it returns False.
195  // An example compare operator is <src>std::equal_to</src>.
196  // <group>
197  template<typename InputIterator1, typename InputIterator2,
198  typename MaskIterator, typename CompareOperator>
199  inline bool compareAnyMasked (InputIterator1 first1, InputIterator1 last1,
200  InputIterator2 first2,
201  MaskIterator mask1, MaskIterator mask2,
202  CompareOperator op)
203  {
204  for (; first1!=last1; ++first1, ++first2, ++mask1, ++mask2) {
205  if (!*mask1 && !*mask2) {
206  if (op(*first1, *first2)) return true;
207  }
208  }
209  return False;
210  }
211  template<typename InputIterator1, typename InputIterator2,
212  typename MaskIterator, typename CompareOperator>
213  inline bool compareAnyMasked (InputIterator1 first1, InputIterator1 last1,
214  InputIterator2 first2,
215  MaskIterator mask1,
216  CompareOperator op)
217  {
218  for (; first1!=last1; ++first1, ++first2, ++mask1) {
219  if (!*mask1) {
220  if (op(*first1, *first2)) return true;
221  }
222  }
223  return False;
224  }
225  // For use with a constant left value.
226  // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
227  // (see ArrayMath.h).
228  template<typename InputIterator1, typename T,
229  typename MaskIterator, typename CompareOperator>
230  inline bool compareAnyLeftMasked (InputIterator1 first1, InputIterator1 last1,
231  T left, MaskIterator mask1,
232  CompareOperator op)
233  {
234  for (; first1!=last1; ++first1, ++mask1) {
235  if (!*mask1) {
236  if (op(left, *first1)) return true;
237  }
238  }
239  return False;
240  }
241  // For use with a constant right value.
242  // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
243  // (see ArrayMath.h).
244  template<typename InputIterator1, typename T,
245  typename MaskIterator, typename CompareOperator>
246  inline bool compareAnyRightMasked(InputIterator1 first1, InputIterator1 last1,
247  T right, MaskIterator mask1,
248  CompareOperator op)
249  {
250  for (; first1!=last1; ++first1, ++mask1) {
251  if (!*mask1) {
252  if (op(*first1, right)) return true;
253  }
254  }
255  return False;
256  }
257  // </group>
258 
259 
260 
261  // Define the base class for functors to perform a reduction function on an
262  // MArray object. The functors themselves are defined elsewhere.
263  template<typename T, typename RES=T> class MArrayFunctorBase {
264  public:
265  virtual ~MArrayFunctorBase() {}
266  virtual RES operator() (const MArray<T>&) const = 0;
267  };
268 
269  // </group>
270 
271 } //# end namespace
272 
273 #endif
Class to handle an Array with an optional mask.
bool compareAllMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, MaskIterator mask1, CompareOperator op)
T accumulateMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, OPER oper)
Define STL-like accumulate function operating on arrays with masks.
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.
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.
bool compareAllLeftMasked(InputIterator1 first1, InputIterator1 last1, T left, MaskIterator mask1, CompareOperator op)
For use with a constant left value.
T accumulateMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, T accum, OPER oper)
The second function uses an externally initialized accumulator (e.g.
bool compareAnyMasked(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, MaskIterator mask1, CompareOperator op)
Define the base class for functors to perform a reduction function on an MArray object.
const Bool False
Definition: aipstype.h:44
virtual casacore::Bool operator()(Flux< casacore::Double > &value, Flux< casacore::Double > &error, const casacore::MFrequency &mfreq, const casacore::Bool updatecoeffs)=0
size_t countNEMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, const T &value)
Count the number of unmasked values not matching the given value.
size_t countMasked(ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin, const T &value)
Count the number of unmasked values matching the given value.
bool compareAnyRightMasked(InputIterator1 first1, InputIterator1 last1, T right, MaskIterator mask1, CompareOperator op)
For use with a constant right value.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42