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