MArrayMathBase.h

Go to the documentation of this file.
00001 //# MArrayMathBase.h: Basic functions and classes for math on MArray objects
00002 //# Copyright (C) 2012
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: MArrayMathBase.h 21262 2012-09-07 12:38:36Z gervandiepen $
00027 
00028 #ifndef CASA_MARRAYMATHBASE_H
00029 #define CASA_MARRAYMATHBASE_H
00030 
00031 #include <casacore/casa/aips.h>
00032 #include <casacore/casa/Arrays/ArrayMathBase.h>
00033 
00034 namespace casacore {
00035 
00036   //# Forward declarations.
00037   template<typename T> class MArray;
00038 
00039 
00040   // <summary>
00041   // Basic functions and classes for math on MArray objects
00042   // </summary>
00043   //
00044   // <reviewed reviewer="UNKNOWN" date="" tests="tMArrayMath">
00045   //
00046   // <prerequisite>
00047   //   <li> <linkto class=MArray>MArray</linkto>
00048   // </prerequisite>
00049   //
00050   // <synopsis>
00051   // This header file defines several STL-like functions to work on
00052   // iterators with a mask.
00053   //
00054   // Furthermore, abstract base classes are defined for functors to be used
00055   // in functions like slidingXXX.
00056   // Virtual functions instead of templated functions are used to avoid
00057   // code bloat when used in functions like partialArrayMath. Because a
00058   // reduction operation usually takes much more time than the call, using
00059   // virtual functions hardly imposes a performance penalty.
00060   // </synopsis>
00061 
00062 
00063   //
00064   // <group name="Array basic functions">
00065 
00066   // Define STL-like accumulate function operating on arrays with masks.
00067   // A mask value True means masked-off, thus is not taken into account.
00068   // <group>
00069   // <br>The first function initializes the accumulator to the first
00070   // unmasked value. This is useful if it is not possible to initialize
00071   // it externally (e.g. for a function like min).
00072   template<typename T, typename ARRAYITER, typename MASKITER, typename OPER>
00073   T accumulateMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
00074                       OPER oper)
00075   {
00076     T accum = T();
00077     for (; abegin!=aend; ++abegin, ++mbegin) {
00078       if (!*mbegin) { accum = *abegin; ++abegin; ++mbegin; break; }
00079     }
00080     for (; abegin!=aend; ++abegin, ++mbegin) {
00081       if (!*mbegin) accum = oper(accum, *abegin);
00082     }
00083     return accum;
00084   }
00085 
00086   // The second function uses an externally initialized accumulator
00087   // (e.g. needed for sum).
00088   template<typename T, typename ARRAYITER, typename MASKITER, typename OPER>
00089   T accumulateMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
00090                       T accum, OPER oper)
00091   {
00092     for (; abegin!=aend; ++abegin, ++mbegin) {
00093       if (!*mbegin) accum = oper(accum, *abegin);
00094     }
00095     return accum;
00096   }
00097   // </group>
00098 
00099   // Count the number of unmasked values matching the given value.
00100   // It is similar to std::count, but a mask is applied.
00101   template<typename T, typename ARRAYITER, typename MASKITER>
00102     size_t countMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
00103                         const T& value)
00104     {
00105       size_t n = 0;
00106       for (; abegin!=aend; ++abegin, ++mbegin) {
00107         if (!*mbegin  &&  *abegin == value) ++n;
00108       }
00109       return n;
00110     }
00111 
00112   // Count the number of unmasked values not matching the given value.
00113   // It is similar to std::count, but a mask is applied.
00114   template<typename T, typename ARRAYITER, typename MASKITER>
00115     size_t countNEMasked (ARRAYITER abegin, ARRAYITER aend, MASKITER mbegin,
00116                         const T& value)
00117     {
00118       size_t n = 0;
00119       for (; abegin!=aend; ++abegin, ++mbegin) {
00120         if (!*mbegin  &&  *abegin != value) ++n;
00121       }
00122       return n;
00123     }
00124 
00125   // Define a function to compare the unmasked elements of two sequences.
00126   // It returns true if all unmasked elements compare true or if there are
00127   // no unmasked elements.
00128   // An example compare operator is <src>std::equal_to</src>.
00129   // <group>
00130   template<typename InputIterator1, typename InputIterator2,
00131            typename MaskIterator, typename CompareOperator>
00132   inline bool compareAllMasked (InputIterator1 first1, InputIterator1 last1,
00133                                 InputIterator2 first2, 
00134                                 MaskIterator mask1, MaskIterator mask2,
00135                                 CompareOperator op)
00136   {
00137     for (; first1!=last1; ++first1, ++first2, ++mask1, ++mask2) {
00138       if (!*mask1 && !*mask2) {
00139         if (!op(*first1, *first2)) return False;
00140       }
00141     }
00142     return true;
00143   }
00144   template<typename InputIterator1, typename InputIterator2,
00145            typename MaskIterator, typename CompareOperator>
00146   inline bool compareAllMasked (InputIterator1 first1, InputIterator1 last1,
00147                                 InputIterator2 first2, 
00148                                 MaskIterator mask1,
00149                                 CompareOperator op)
00150   {
00151     for (; first1!=last1; ++first1, ++first2, ++mask1) {
00152       if (!*mask1) {
00153         if (!op(*first1, *first2)) return False;
00154       }
00155     }
00156     return true;
00157   }
00158   // For use with a constant left value.
00159   // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
00160   // (see ArrayMath.h).
00161   template<typename InputIterator1, typename T,
00162            typename MaskIterator, typename CompareOperator>
00163   inline bool compareAllLeftMasked (InputIterator1 first1, InputIterator1 last1,
00164                                     T left, MaskIterator mask1,
00165                                     CompareOperator op)
00166   {
00167     for (; first1!=last1; ++first1, ++mask1) {
00168       if (!*mask1) {
00169         if (!op(left, *first1)) return False;
00170       }
00171     }
00172     return true;
00173   }
00174   // For use with a constant right value.
00175   // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
00176   // (see ArrayMath.h).
00177   template<typename InputIterator1, typename T,
00178            typename MaskIterator, typename CompareOperator>
00179   inline bool compareAllRightMasked(InputIterator1 first1, InputIterator1 last1,
00180                                     T right, MaskIterator mask1,
00181                                     CompareOperator op)
00182   {
00183     for (; first1!=last1; ++first1, ++mask1) {
00184       if (!*mask1) {
00185         if (!op(*first1, right)) return False;
00186       }
00187     }
00188     return true;
00189   }
00190   // </group>
00191 
00192   // Define a function to compare the unmasked elements of two sequences.
00193   // It returns true if any element compares true.
00194   // If there are no unmasked elements, it returns False.
00195   // An example compare operator is <src>std::equal_to</src>.
00196   // <group>
00197   template<typename InputIterator1, typename InputIterator2,
00198            typename MaskIterator, typename CompareOperator>
00199   inline bool compareAnyMasked (InputIterator1 first1, InputIterator1 last1,
00200                                 InputIterator2 first2, 
00201                                 MaskIterator mask1, MaskIterator mask2,
00202                                 CompareOperator op)
00203   {
00204     for (; first1!=last1; ++first1, ++first2, ++mask1, ++mask2) {
00205       if (!*mask1 && !*mask2) {
00206         if (op(*first1, *first2)) return true;
00207       }
00208     }
00209     return False;
00210   }
00211   template<typename InputIterator1, typename InputIterator2,
00212            typename MaskIterator, typename CompareOperator>
00213   inline bool compareAnyMasked (InputIterator1 first1, InputIterator1 last1,
00214                                 InputIterator2 first2, 
00215                                 MaskIterator mask1,
00216                                 CompareOperator op)
00217   {
00218     for (; first1!=last1; ++first1, ++first2, ++mask1) {
00219       if (!*mask1) {
00220         if (op(*first1, *first2)) return true;
00221       }
00222     }
00223     return False;
00224   }
00225   // For use with a constant left value.
00226   // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
00227   // (see ArrayMath.h).
00228   template<typename InputIterator1, typename T,
00229            typename MaskIterator, typename CompareOperator>
00230   inline bool compareAnyLeftMasked (InputIterator1 first1, InputIterator1 last1,
00231                                     T left, MaskIterator mask1,
00232                                     CompareOperator op)
00233   {
00234     for (; first1!=last1; ++first1, ++mask1) {
00235       if (!*mask1) {
00236         if (op(left, *first1)) return true;
00237       }
00238     }
00239     return False;
00240   }
00241   // For use with a constant right value.
00242   // This avoids use of bind1st or bind2nd which can fail for gcc-4.3.
00243   // (see ArrayMath.h).
00244   template<typename InputIterator1, typename T,
00245            typename MaskIterator, typename CompareOperator>
00246   inline bool compareAnyRightMasked(InputIterator1 first1, InputIterator1 last1,
00247                                     T right, MaskIterator mask1,
00248                                     CompareOperator op)
00249   {
00250     for (; first1!=last1; ++first1, ++mask1) {
00251       if (!*mask1) {
00252         if (op(*first1, right)) return true;
00253       }
00254     }
00255     return False;
00256   }
00257   // </group>
00258 
00259 
00260 
00261   // Define the base class for functors to perform a reduction function on an
00262   // MArray object. The functors themselves are defined elsewhere.
00263   template<typename T, typename RES=T> class MArrayFunctorBase {
00264   public:
00265     virtual ~MArrayFunctorBase() {}
00266     virtual RES operator() (const MArray<T>&) const = 0;
00267   };
00268 
00269   // </group>
00270 
00271 } //# end namespace
00272 
00273 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1