casacore
STLMath.h
Go to the documentation of this file.
1//# STLMath.h: Math operations on STL-like containers
2//# Copyright (C) 2014
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: STLMath.h 21331 2013-03-26 15:08:06Z gervandiepen $
27
28#ifndef CASA_STLMATH_H
29#define CASA_STLMATH_H
30
31//# Includes
32#include <casacore/casa/aips.h>
33#include <casacore/casa/BasicMath/Functors.h>
34#include <vector>
35#include <algorithm>
36
37namespace casacore { //# NAMESPACE CASACORE - BEGIN
38
39 // <summary>
40 // Math operations on STL-like containers
41 // </summary>
42
43 // <use visibility=export>
44
45 // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
46 // </reviewed>
47
48 // <prerequisite>
49 // <li> STL container concept
50 // </prerequisite>
51
52 // <synopsis>
53 // This file defines a few functions with a math operation on a vector.
54 // Others can be added later.
55 // </synopsis>
56
57 // <group name="Container Math">
59 // Throw an exception that two container sizes mismatch.
60 void throwContainerSizes (const char* name, size_t l1, size_t l2);
61
62 // Check if the sizes of both containers are the same.
63 template<typename CONTAINER>
64 inline void checkContainerSizes (const CONTAINER& left, const CONTAINER& right,
65 const char* name)
66 {
67 if (left.size() != right.size()) {
68 throwContainerSizes (name, left.size(), right.size());
69 }
70 }
71
72 // Reverse a Casacore container like IPosition, Block, or Vector.
73 template<typename CONTAINER>
74 inline CONTAINER reversedCasaContainer (const CONTAINER& in)
75 {
76 size_t sz = in.size();
77 CONTAINER out(sz);
78 for (size_t i=0; i<sz; ++i) {
79 out[i] = in[sz-i-1];
80 }
81 return out;
82 }
83
84 // Add two std::vector objects.
85 template<class T>
86 std::vector<T> operator+ (const std::vector<T> &left,
87 const std::vector<T> &right)
88 {
89 checkContainerSizes(left, right, "+");
90 std::vector<T> result(left.size());
91 std::transform (left.begin(), left.end(), right.begin(),
92 result.begin(), std::plus<T>());
93 return result;
94 }
95
96 // Divide a vector by a scalar.
97 template<class T>
98 std::vector<T> operator/ (const std::vector<T> &left, const T &right)
99 {
100 std::vector<T> result(left.size());
101 std::transform (left.begin(), left.end(), result.begin(),
102 [right](T x) { return x / right; });
103 return result;
104 }
105
106 // </group>
107
108} //# NAMESPACE CASACORE - END
109
110#endif
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode operator+(const LatticeExprNode &expr)
Global functions operating on a LatticeExprNode.
LatticeExprNode operator/(const LatticeExprNode &left, const LatticeExprNode &right)
std::vector< T > operator/(const std::vector< T > &left, const T &right)
Divide a vector by a scalar.
Definition: STLMath.h:98
void checkContainerSizes(const CONTAINER &left, const CONTAINER &right, const char *name)
Check if the sizes of both containers are the same.
Definition: STLMath.h:64
void throwContainerSizes(const char *name, size_t l1, size_t l2)
Throw an exception that two container sizes mismatch.
CONTAINER reversedCasaContainer(const CONTAINER &in)
Reverse a Casacore container like IPosition, Block, or Vector.
Definition: STLMath.h:74
std::vector< T > operator+(const std::vector< T > &left, const std::vector< T > &right)
Add two std::vector objects.
Definition: STLMath.h:86