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.
58void throwContainerSizes(const char* name, size_t l1, size_t l2);
59
60// Check if the sizes of both containers are the same.
61template <typename CONTAINER>
62inline void checkContainerSizes(const CONTAINER& left, const CONTAINER& right, const char* name) {
63 if (left.size() != right.size()) {
64 throwContainerSizes(name, left.size(), right.size());
65 }
66}
67
68// Reverse a Casacore container like IPosition, Block, or Vector.
69template <typename CONTAINER>
70inline CONTAINER reversedCasaContainer(const CONTAINER& in) {
71 size_t sz = in.size();
72 CONTAINER out(sz);
73 for (size_t i = 0; i < sz; ++i) {
74 out[i] = in[sz - i - 1];
75 }
76 return out;
77}
78
79// Add two std::vector objects.
80template <class T>
81std::vector<T> operator+(const std::vector<T>& left, const std::vector<T>& right) {
82 checkContainerSizes(left, right, "+");
83 std::vector<T> result(left.size());
84 std::transform(left.begin(), left.end(), right.begin(), result.begin(), std::plus<T>());
85 return result;
86}
87
88// Divide a vector by a scalar.
89template <class T>
90std::vector<T> operator/(const std::vector<T>& left, const T& right) {
91 std::vector<T> result(left.size());
92 std::transform(left.begin(), left.end(), result.begin(), [right](T x) { return x / right; });
93 return result;
94}
95
96// </group>
97
98} // namespace casacore
99
100#endif
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
String name() const
Return the name of the field.
std::vector< T > operator/(const std::vector< T > &left, const T &right)
Divide a vector by a scalar.
Definition STLMath.h:90
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:70
std::vector< T > operator+(const std::vector< T > &left, const std::vector< T > &right)
Add two std::vector objects.
Definition STLMath.h:81