ARTS 2.5.4 (git: 31ce4f0e)
array.h
Go to the documentation of this file.
1/* Copyright (C) 2001-2012 Stefan Buehler <sbuehler@ltu.se>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
27#ifndef array_h
28#define array_h
29
30#include <array>
31#include <climits>
32#include <iomanip>
33#include <iostream>
34#include <vector>
35
36#include "matpack.h"
37
47template <class base>
48class Array : public std::vector<base> {
49 public:
50 // Constructors:
51 Array() : std::vector<base>() {}
52
53 explicit Array(Index n) : std::vector<base>(n) {}
54
55 Array(Index n, const base& fillvalue) : std::vector<base>(n) {
56 std::fill(this->begin(), this->end(), fillvalue);
57 }
58
59 Array(const Array& A) : std::vector<base>(A) {}
60
61 Array(Array&& A) noexcept : std::vector<base>(std::move(A)) {}
62
63 Array(std::initializer_list<base> init) : std::vector<base>(init) {}
64
65 template <class base2, size_t N>
66 explicit Array(const std::array<base2, N>& input)
67 : std::vector<base>(input.begin(), input.end()) {
68 static_assert(std::is_convertible<base2, base>::value,
69 "Must be convertible");
70 }
71
72 Array(std::vector<base> x) : std::vector<base>(std::move(x)) {}
73
74 // Assignment operators:
75 Array& operator=(base x) {
76 std::fill(this->begin(), this->end(), x);
77 return *this;
78 }
79
80 Array& operator=(const Array& A) {
81 this->resize(A.size());
82 std::copy(A.begin(), A.end(), this->begin());
83 return *this;
84 }
85
86 Array& operator=(Array&& A) noexcept {
87 std::vector<base>::operator=(std::move(A));
88 return *this;
89 }
90
91 // Number of elements:
92 [[nodiscard]] Index nelem() const ARTS_NOEXCEPT {
93 size_t s = this->size();
94 ARTS_ASSERT(s < LONG_MAX);
95 return static_cast<Index>(s);
96 }
97
98 // Index operators:
99 const base& operator[](const Index n) const {
100 ARTS_ASSERT(0 <= n);
101 ARTS_ASSERT(n < nelem());
102 return std::vector<base>::operator[](n);
103 }
104
105 base& operator[](const Index n) {
106 ARTS_ASSERT(0 <= n);
107 ARTS_ASSERT(n < nelem());
108 return std::vector<base>::operator[](n);
109 }
110
111 // Helper functions
112 void push_back_n(const base& elem, const Index n) {
113 for (Index i = 0; i < n; i++) std::vector<base>::push_back(elem);
114 }
115
116 virtual ~Array() = default;
117
118 friend std::ostream& operator<<(std::ostream& os, const Array& v) {
119 typename Array::const_iterator i = v.begin();
120 const typename Array::const_iterator end = v.end();
121
122 if (i != end) {
123 os << std::setw(3) << *i;
124 ++i;
125 }
126
127 for (; i != end; ++i) {
128 os << " " << std::setw(3) << *i;
129 }
130
131 return os;
132 }
133};
134
137
139
142
144template <class base>
145inline base max(const Array<base>& x) {
146 // Initial value for max:
147 base max = x[0];
148
149 typename Array<base>::const_iterator xi = x.begin();
150 const typename Array<base>::const_iterator xe = x.end();
151
152 for (; xi != xe; ++xi) {
153 if (*xi > max) max = *xi;
154 }
155
156 return max;
157}
158
160template <class base>
161inline base min(const Array<base>& x) {
162 // Initial value for min:
163 base min = x[0];
164
165 typename Array<base>::const_iterator xi = x.begin();
166 const typename Array<base>::const_iterator xe = x.end();
167
168 for (; xi != xe; ++xi) {
169 if (*xi < min) min = *xi;
170 }
171
172 return min;
173}
174
176
189template <class base>
190Index find_first(const Array<base>& x, const base& w) {
191 for (Index i = 0; i < x.nelem(); ++i)
192 if (w == x[i]) return i;
193
194 return -1;
195}
196
198
211template <class base>
212void find_all(ArrayOfIndex& pos, const Array<base>& x, const base& w) {
213 pos.resize(0);
214 for (Index i = 0; i < x.nelem(); ++i)
215 if (w == x[i]) pos.push_back(i);
216}
217
230 public:
232 bool operator()(const int& a, const int& b) const {
233 return values[a] < values[b];
234 }
235
237};
238
240template <class base>
242 Index N_aa = 0;
243 for (Index i = 0; i < aa.nelem(); i++) {
244 N_aa += aa[i].nelem();
245 }
246
247 return N_aa;
248}
249
251template <class base>
253 Index outer,
254 Index inner = 0) {
255 ARTS_ASSERT(outer < aa.nelem());
256 ARTS_ASSERT(inner < aa[outer].nelem());
257
258 Index N = 0;
259 for (Index i = 0; i < outer; i++) {
260 N += aa[i].nelem();
261 }
262
263 return N + inner;
264}
265
266// It is not a good idea to put all the predefined array types in one
267// place. If I do this than a file cannot use one without defining all
268// the others.
269
271template <typename T, typename... Ts>
272constexpr std::array<T, 1 + sizeof...(Ts)> stdarrayify(const T& first,
273 const Ts&... the_rest) {
274 return {first, T(the_rest)...};
275}
276
277template <typename T>
278std::string stringify(const Array<T>& list,
279 const char* const sep = " ",
280 const char* const beg = "") {
281 std::ostringstream os;
282 for (auto& x : list) os << beg << x << sep;
283 return os.str();
284}
285
286#endif // array_h
std::string stringify(const Array< T > &list, const char *const sep=" ", const char *const beg="")
Definition: array.h:278
Index TotalNumberOfElements(const Array< Array< base > > &aa)
Determine total number of elements in an ArrayOfArray.
Definition: array.h:241
Index FlattenedIndex(const Array< Array< base > > &aa, Index outer, Index inner=0)
Determine the index of an element in a flattened version of the array.
Definition: array.h:252
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:190
base max(const Array< base > &x)
Max function.
Definition: array.h:145
base min(const Array< base > &x)
Min function.
Definition: array.h:161
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:212
constexpr std::array< T, 1+sizeof...(Ts)> stdarrayify(const T &first, const Ts &... the_rest)
Make a std::array of a list of variables (must be 1-long at least)
Definition: array.h:272
This can be used to make arrays out of anything.
Definition: array.h:48
Array(const Array &A)
Definition: array.h:59
Array(Index n, const base &fillvalue)
Definition: array.h:55
virtual ~Array()=default
Array(const std::array< base2, N > &input)
Definition: array.h:66
Array & operator=(base x)
Definition: array.h:75
void push_back_n(const base &elem, const Index n)
Definition: array.h:112
Array(std::vector< base > x)
Definition: array.h:72
Array & operator=(const Array &A)
Definition: array.h:80
base & operator[](const Index n)
Definition: array.h:105
Array(Index n)
Definition: array.h:53
friend std::ostream & operator<<(std::ostream &os, const Array &v)
Definition: array.h:118
Array(std::initializer_list< base > init)
Definition: array.h:63
const base & operator[](const Index n) const
Definition: array.h:99
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:92
Array & operator=(Array &&A) noexcept
Definition: array.h:86
Array(Array &&A) noexcept
Definition: array.h:61
Array()
Definition: array.h:51
Helper comparison class to sort an array or vector based on an ArrayOfNumeric.
Definition: array.h:229
CmpArrayOfNumeric(const ArrayOfNumeric &vec)
Definition: array.h:231
const ArrayOfNumeric & values
Definition: array.h:236
bool operator()(const int &a, const int &b) const
Definition: array.h:232
#define ARTS_NOEXCEPT
Definition: debug.h:80
#define ARTS_ASSERT(condition,...)
Definition: debug.h:83
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
void copy(ConstComplexIterator1D origin, const ConstComplexIterator1D &end, ComplexIterator1D target)
Copy data between begin and end to target.
Index nelem(const Lines &l)
Number of lines.
VectorView std(VectorView std, const Vector &y, const ArrayOfVector &ys, const Index start, const Index end_tmp)
Compute the standard deviation of the ranged ys.
Definition: raw.cc:205
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:113
#define N
Definition: rng.cc:164
#define v
#define w
#define a
#define b