STLMath.h

Go to the documentation of this file.
00001 //# STLMath.h: Math operations on STL-like containers
00002 //# Copyright (C) 2014
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: STLMath.h 21331 2013-03-26 15:08:06Z gervandiepen $
00027 
00028 #ifndef CASA_STLMATH_H
00029 #define CASA_STLMATH_H
00030 
00031 //# Includes
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/casa/BasicMath/Functors.h>
00034 #include <vector>
00035 #include <algorithm>
00036 
00037 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00038 
00039   // <summary>
00040   //    Math operations on STL-like containers
00041   // </summary>
00042 
00043   // <use visibility=export>
00044 
00045   // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
00046   // </reviewed>
00047 
00048   // <prerequisite>
00049   //   <li> STL container concept
00050   // </prerequisite>
00051 
00052   // <synopsis> 
00053   // This file defines a few functions with a math operation on a vector.
00054   // Others can be added later.
00055   // </synopsis>
00056 
00057   // <group name="Container Math">
00058 
00059   // Throw an exception that two container sizes mismatch.
00060   void throwContainerSizes (const char* name, size_t l1, size_t l2);
00061 
00062   // Check if the sizes of both containers are the same.
00063   template<typename CONTAINER>
00064   inline void checkContainerSizes (const CONTAINER& left, const CONTAINER& right,
00065                                    const char* name)
00066   {
00067     if (left.size() != right.size()) {
00068       throwContainerSizes (name, left.size(), right.size());
00069     }
00070   }
00071 
00072   // Reverse a Casacore container like IPosition, Block, or Vector.
00073   template<typename CONTAINER>
00074   inline CONTAINER reversedCasaContainer (const CONTAINER& in)
00075   {
00076     size_t sz = in.size();
00077     CONTAINER out(sz);
00078     for (size_t i=0; i<sz; ++i) {
00079       out[i] = in[sz-i-1];
00080     }
00081     return out;
00082   }
00083 
00084   // Add two std::vector objects.
00085   template<class T>
00086   std::vector<T> operator+ (const std::vector<T> &left,
00087                             const std::vector<T> &right)
00088   {
00089     checkContainerSizes(left, right, "+");
00090     std::vector<T> result(left.size());
00091     std::transform (left.begin(), left.end(), right.begin(),
00092                     result.begin(), std::plus<T>());
00093     return result;
00094   }
00095 
00096   // Divide a vector by a scalar.
00097   template<class T>
00098   std::vector<T> operator/ (const std::vector<T> &left, const T &right)
00099   {
00100     std::vector<T> result(left.size());
00101     std::transform (left.begin(), left.end(), result.begin(),
00102                     std::bind2nd(std::divides<T>(), right));
00103     return result;
00104   }
00105 
00106   // </group>
00107 
00108 } //# NAMESPACE CASACORE - END
00109 
00110 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1