ARTS 2.5.0 (git: 9ee3ac6c)
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 <climits>
31#include <iomanip>
32#include <iostream>
33#include <array>
34#include <vector>
35#include "matpack.h"
36
37// Declare the existance of class Array:
38template <class base>
39class Array;
40
43
45
48
49// Declare the existance of Vector/Matrix/Tensor classes:
50class Vector;
51class Matrix;
52class Sparse;
53class Tensor3;
54class Tensor4;
55class Tensor5;
56class Tensor6;
57class Tensor7;
58
61
63
66
68
71
74
76
79
81
84
86
89
91
94
96
106template <class base>
107class Array : public std::vector<base> {
108 public:
109 // Constructors:
110 Array() : std::vector<base>() { /* Nothing to do here. */
111 }
112 explicit Array(Index n) : std::vector<base>(n) { /* Nothing to do here. */
113 }
114 Array(Index n, const base& fillvalue);
115 Array(const Array<base>& A) : std::vector<base>(A) { /* Nothing to do here. */
116 }
117 Array(Array<base>&& A) noexcept
118 : std::vector<base>(std::move(A)) { /* Nothing to do here. */
119 }
120 Array(std::initializer_list<base> init)
121 : std::vector<base>(init) { /* Nothing to do here. */
122 }
123 template <class base2, size_t N> explicit Array(const std::array<base2, N>& input)
124 : std::vector<base>(input.begin(), input.end()) {
125 static_assert(std::is_convertible<base, base2>::value, "Must be convertible");
126 }
127
128 // Assignment operators:
129 Array& operator=(base x);
132
133 // Number of elements:
135
136 // Index operators:
137 const base& operator[](const Index n) const;
138 base& operator[](const Index n);
139
140 // Helper functions
141 void push_back_n(const base& elem, const Index n);
142
143 virtual ~Array() = default;
144};
145
146// Member functions for Array:
147
149template <class base>
150inline Array<base>::Array(Index n, const base& fillvalue)
151 : std::vector<base>(n) {
152 // Use fill to fill.
153 std::fill(this->begin(), this->end(), fillvalue);
154}
155
157template <class base>
159 std::fill(this->begin(), this->end(), x);
160 return *this;
161}
162
164
179template <class base>
181 // cout << "size this / A = " << size() << " / " << A.size() << "\n";
182 this->resize(A.size());
183 std::copy(A.begin(), A.end(), this->begin());
184 return *this;
185}
186
187template <class base>
189 std::vector<base>::operator=(std::move(A));
190 return *this;
191}
192
194template <class base>
196 size_t s = this->size();
197 ARTS_ASSERT(s < LONG_MAX);
198 return static_cast<Index>(s);
199}
200
203template <class base>
204inline const base& Array<base>::operator[](const Index n) const {
205 ARTS_ASSERT(0 <= n);
206 ARTS_ASSERT(n < nelem());
207 return std::vector<base>::operator[](n);
208}
209
212template <class base>
213inline base& Array<base>::operator[](const Index n) {
214 ARTS_ASSERT(0 <= n);
215 ARTS_ASSERT(n < nelem());
216 return std::vector<base>::operator[](n);
217}
218
220template <class base>
221inline void Array<base>::push_back_n(const base& elem, const Index n) {
222 for (Index i = 0; i < n; i++) std::vector<base>::push_back(elem);
223}
224
225// Non-member functions:
226
228template <class base>
229inline std::ostream& operator<<(std::ostream& os, const Array<base>& v) {
230 typename Array<base>::const_iterator i = v.begin();
231 const typename Array<base>::const_iterator end = v.end();
232
233 if (i != end) {
234 os << std::setw(3) << *i;
235 ++i;
236 }
237
238 for (; i != end; ++i) {
239 os << " " << std::setw(3) << *i;
240 }
241
242 return os;
243}
244
246template <class base>
247inline base max(const Array<base>& x) {
248 // Initial value for max:
249 base max = x[0];
250
251 typename Array<base>::const_iterator xi = x.begin();
252 const typename Array<base>::const_iterator xe = x.end();
253
254 for (; xi != xe; ++xi) {
255 if (*xi > max) max = *xi;
256 }
257
258 return max;
259}
260
262template <class base>
263inline base min(const Array<base>& x) {
264 // Initial value for min:
265 base min = x[0];
266
267 typename Array<base>::const_iterator xi = x.begin();
268 const typename Array<base>::const_iterator xe = x.end();
269
270 for (; xi != xe; ++xi) {
271 if (*xi < min) min = *xi;
272 }
273
274 return min;
275}
276
278
291template <class base>
292Index find_first(const Array<base>& x, const base& w) {
293 for (Index i = 0; i < x.nelem(); ++i)
294 if (w == x[i]) return i;
295
296 return -1;
297}
298
300
313template <class base>
314void find_all(ArrayOfIndex& pos, const Array<base>& x, const base& w) {
315 pos.resize(0);
316 for (Index i = 0; i < x.nelem(); ++i)
317 if (w == x[i]) pos.push_back(i);
318}
319
332 public:
333 CmpArrayOfNumeric(const ArrayOfNumeric& vec) : values(vec) {}
334 bool operator()(const int& a, const int& b) const {
335 return values[a] < values[b];
336 }
337
339};
340
342template <class base>
344 Index N_aa = 0;
345 for (Index i = 0; i < aa.nelem(); i++) {
346 N_aa += aa[i].nelem();
347 }
348
349 return N_aa;
350}
351
353template <class base>
355 Index outer,
356 Index inner = 0) {
357 ARTS_ASSERT(outer < aa.nelem());
358 ARTS_ASSERT(inner < aa[outer].nelem());
359
360 Index N = 0;
361 for (Index i = 0; i < outer; i++) {
362 N += aa[i].nelem();
363 }
364
365 return N + inner;
366}
367
368// It is not a good idea to put all the predefined array types in one
369// place. If I do this than a file cannot use one without defining all
370// the others.
371
373template <typename T, typename ... Ts>
374constexpr std::array<T, 1 + sizeof...(Ts)> stdarrayify(const T& first, const Ts&... the_rest)
375{
376 return {first, T(the_rest)...};
377}
378
379#endif // array_h
std::ostream & operator<<(std::ostream &os, const Array< base > &v)
Output operator.
Definition: array.h:229
Array< ArrayOfTensor5 > ArrayOfArrayOfTensor5
Definition: array.h:85
Array< ArrayOfTensor6 > ArrayOfArrayOfTensor6
Definition: array.h:90
Array< ArrayOfIndex > ArrayOfArrayOfIndex
Definition: array.h:44
Array< ArrayOfMatrix > ArrayOfArrayOfMatrix
Definition: array.h:67
Array< Tensor7 > ArrayOfTensor7
An array of Tensor7.
Definition: array.h:93
Array< ArrayOfTensor7 > ArrayOfArrayOfTensor7
Definition: array.h:95
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:65
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:39
Index TotalNumberOfElements(const Array< Array< base > > &aa)
Determine total number of elements in an ArrayOfArray.
Definition: array.h:343
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:354
Array< Sparse > ArrayOfSparse
An array of sparse matrices.
Definition: array.h:70
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:292
base max(const Array< base > &x)
Max function.
Definition: array.h:247
base min(const Array< base > &x)
Min function.
Definition: array.h:263
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:57
Array< ArrayOfTensor3 > ArrayOfArrayOfTensor3
Definition: array.h:75
Array< Numeric > ArrayOfNumeric
An array of Numeric.
Definition: array.h:47
Array< Tensor4 > ArrayOfTensor4
An array of Tensor4.
Definition: array.h:78
Array< Tensor3 > ArrayOfTensor3
An array of Tensor3.
Definition: array.h:73
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:314
Array< Tensor5 > ArrayOfTensor5
An array of Tensor5.
Definition: array.h:83
Array< Tensor6 > ArrayOfTensor6
An array of Tensor6.
Definition: array.h:88
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:374
Array< ArrayOfVector > ArrayOfArrayOfVector
Definition: array.h:62
Array< ArrayOfTensor4 > ArrayOfArrayOfTensor4
Definition: array.h:80
This can be used to make arrays out of anything.
Definition: array.h:107
Array & operator=(const Array< base > &A)
Assignment from another Array.
Definition: array.h:180
Array(Index n, const base &fillvalue)
Constructor filling with constant value.
Definition: array.h:150
Array(const std::array< base2, N > &input)
Definition: array.h:123
void push_back_n(const base &elem, const Index n)
Append element n times.
Definition: array.h:221
Array(Index n)
Definition: array.h:112
Array(std::initializer_list< base > init)
Definition: array.h:120
const base & operator[](const Index n) const
Constant index operator.
Definition: array.h:204
Index nelem() const ARTS_NOEXCEPT
Number of elements.
Definition: array.h:195
Array(const Array< base > &A)
Definition: array.h:115
Array & operator=(Array< base > &&A) noexcept
Definition: array.h:188
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:158
Array(Array< base > &&A) noexcept
Definition: array.h:117
Array()
Definition: array.h:110
Helper comparison class to sort an array or vector based on an ArrayOfNumeric.
Definition: array.h:331
CmpArrayOfNumeric(const ArrayOfNumeric &vec)
Definition: array.h:333
const ArrayOfNumeric & values
Definition: array.h:338
bool operator()(const int &a, const int &b) const
Definition: array.h:334
The Matrix class.
Definition: matpackI.h:1225
The Sparse class.
Definition: matpackII.h:67
The Tensor3 class.
Definition: matpackIII.h:339
The Tensor4 class.
Definition: matpackIV.h:421
The Tensor5 class.
Definition: matpackV.h:506
The Tensor6 class.
Definition: matpackVI.h:1088
The Tensor7 class.
Definition: matpackVII.h:2382
The Vector class.
Definition: matpackI.h:876
#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.
Workspace & init(Workspace &ws)
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:109
#define v
#define w
#define a
#define b
#define N
Definition: rng.cc:164