ARTS 2.5.4 (git: 4c0d3b4d)
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
38// Declare the existance of class Array:
39template <class base>
40class Array;
41
44
46
49
50// Declare the existance of Vector/Matrix/Tensor classes:
51class Vector;
52class Matrix;
53struct Sparse;
54class Tensor3;
55class Tensor4;
56class Tensor5;
57class Tensor6;
58class Tensor7;
59
62
64
67
69
72
75
77
80
82
85
87
90
92
95
97
107template <class base>
108class Array : public std::vector<base> {
109 public:
110 // Constructors:
111 Array() : std::vector<base>() { /* Nothing to do here. */
112 }
113 explicit Array(Index n) : std::vector<base>(n) { /* Nothing to do here. */
114 }
115 Array(Index n, const base& fillvalue);
116 Array(const Array<base>& A) : std::vector<base>(A) { /* Nothing to do here. */
117 }
118 Array(Array<base>&& A) noexcept
119 : std::vector<base>(std::move(A)) { /* Nothing to do here. */
120 }
121 Array(std::initializer_list<base> init)
122 : std::vector<base>(init) { /* Nothing to do here. */
123 }
124 template <class base2, size_t N> explicit Array(const std::array<base2, N>& input)
125 : std::vector<base>(input.begin(), input.end()) {
126 static_assert(std::is_convertible<base, base2>::value, "Must be convertible");
127 }
128 Array(std::vector<base> x) : std::vector<base>(std::move(x)) {}
129
130 // Assignment operators:
131 Array& operator=(base x);
134
135 // Number of elements:
136 [[nodiscard]] Index nelem() const ARTS_NOEXCEPT;
137
138 // Index operators:
139 const base& operator[](const Index n) const;
140 base& operator[](const Index n);
141
142 // Helper functions
143 void push_back_n(const base& elem, const Index n);
144
145 virtual ~Array() = default;
146};
147
148// Member functions for Array:
149
151template <class base>
152inline Array<base>::Array(Index n, const base& fillvalue)
153 : std::vector<base>(n) {
154 // Use fill to fill.
155 std::fill(this->begin(), this->end(), fillvalue);
156}
157
159template <class base>
161 std::fill(this->begin(), this->end(), x);
162 return *this;
163}
164
166
181template <class base>
183 // cout << "size this / A = " << size() << " / " << A.size() << "\n";
184 this->resize(A.size());
185 std::copy(A.begin(), A.end(), this->begin());
186 return *this;
187}
188
189template <class base>
191 std::vector<base>::operator=(std::move(A));
192 return *this;
193}
194
196template <class base>
198 size_t s = this->size();
199 ARTS_ASSERT(s < LONG_MAX);
200 return static_cast<Index>(s);
201}
202
205template <class base>
206inline const base& Array<base>::operator[](const Index n) const {
207 ARTS_ASSERT(0 <= n);
208 ARTS_ASSERT(n < nelem());
209 return std::vector<base>::operator[](n);
210}
211
214template <class base>
215inline base& Array<base>::operator[](const Index n) {
216 ARTS_ASSERT(0 <= n);
217 ARTS_ASSERT(n < nelem());
218 return std::vector<base>::operator[](n);
219}
220
222template <class base>
223inline void Array<base>::push_back_n(const base& elem, const Index n) {
224 for (Index i = 0; i < n; i++) std::vector<base>::push_back(elem);
225}
226
227// Non-member functions:
228
230template <class base>
231inline std::ostream& operator<<(std::ostream& os, const Array<base>& v) {
232 typename Array<base>::const_iterator i = v.begin();
233 const typename Array<base>::const_iterator end = v.end();
234
235 if (i != end) {
236 os << std::setw(3) << *i;
237 ++i;
238 }
239
240 for (; i != end; ++i) {
241 os << " " << std::setw(3) << *i;
242 }
243
244 return os;
245}
246
248template <class base>
249inline base max(const Array<base>& x) {
250 // Initial value for max:
251 base max = x[0];
252
253 typename Array<base>::const_iterator xi = x.begin();
254 const typename Array<base>::const_iterator xe = x.end();
255
256 for (; xi != xe; ++xi) {
257 if (*xi > max) max = *xi;
258 }
259
260 return max;
261}
262
264template <class base>
265inline base min(const Array<base>& x) {
266 // Initial value for min:
267 base min = x[0];
268
269 typename Array<base>::const_iterator xi = x.begin();
270 const typename Array<base>::const_iterator xe = x.end();
271
272 for (; xi != xe; ++xi) {
273 if (*xi < min) min = *xi;
274 }
275
276 return min;
277}
278
280
293template <class base>
294Index find_first(const Array<base>& x, const base& w) {
295 for (Index i = 0; i < x.nelem(); ++i)
296 if (w == x[i]) return i;
297
298 return -1;
299}
300
302
315template <class base>
316void find_all(ArrayOfIndex& pos, const Array<base>& x, const base& w) {
317 pos.resize(0);
318 for (Index i = 0; i < x.nelem(); ++i)
319 if (w == x[i]) pos.push_back(i);
320}
321
334 public:
335 CmpArrayOfNumeric(const ArrayOfNumeric& vec) : values(vec) {}
336 bool operator()(const int& a, const int& b) const {
337 return values[a] < values[b];
338 }
339
341};
342
344template <class base>
346 Index N_aa = 0;
347 for (Index i = 0; i < aa.nelem(); i++) {
348 N_aa += aa[i].nelem();
349 }
350
351 return N_aa;
352}
353
355template <class base>
357 Index outer,
358 Index inner = 0) {
359 ARTS_ASSERT(outer < aa.nelem());
360 ARTS_ASSERT(inner < aa[outer].nelem());
361
362 Index N = 0;
363 for (Index i = 0; i < outer; i++) {
364 N += aa[i].nelem();
365 }
366
367 return N + inner;
368}
369
370// It is not a good idea to put all the predefined array types in one
371// place. If I do this than a file cannot use one without defining all
372// the others.
373
375template <typename T, typename ... Ts>
376constexpr std::array<T, 1 + sizeof...(Ts)> stdarrayify(const T& first, const Ts&... the_rest)
377{
378 return {first, T(the_rest)...};
379}
380
381template <typename T>
382std::string stringify(const Array<T>& list, const char * const sep=" ", const char * const beg="") {
383 std::ostringstream os;
384 for (auto& x: list) os << beg << x << sep;
385 return os.str();
386}
387
388#endif // array_h
std::ostream & operator<<(std::ostream &os, const Array< base > &v)
Output operator.
Definition: array.h:231
Array< ArrayOfTensor5 > ArrayOfArrayOfTensor5
Definition: array.h:86
Array< ArrayOfTensor6 > ArrayOfArrayOfTensor6
Definition: array.h:91
Array< ArrayOfIndex > ArrayOfArrayOfIndex
Definition: array.h:45
Array< ArrayOfMatrix > ArrayOfArrayOfMatrix
Definition: array.h:68
Array< Tensor7 > ArrayOfTensor7
An array of Tensor7.
Definition: array.h:94
Array< ArrayOfTensor7 > ArrayOfArrayOfTensor7
Definition: array.h:96
std::string stringify(const Array< T > &list, const char *const sep=" ", const char *const beg="")
Definition: array.h:382
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:66
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
Index TotalNumberOfElements(const Array< Array< base > > &aa)
Determine total number of elements in an ArrayOfArray.
Definition: array.h:345
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:356
Array< Sparse > ArrayOfSparse
An array of sparse matrices.
Definition: array.h:71
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:294
base max(const Array< base > &x)
Max function.
Definition: array.h:249
base min(const Array< base > &x)
Min function.
Definition: array.h:265
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:58
Array< ArrayOfTensor3 > ArrayOfArrayOfTensor3
Definition: array.h:76
Array< Numeric > ArrayOfNumeric
An array of Numeric.
Definition: array.h:48
Array< Tensor4 > ArrayOfTensor4
An array of Tensor4.
Definition: array.h:79
Array< Tensor3 > ArrayOfTensor3
An array of Tensor3.
Definition: array.h:74
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:316
Array< Tensor5 > ArrayOfTensor5
An array of Tensor5.
Definition: array.h:84
Array< Tensor6 > ArrayOfTensor6
An array of Tensor6.
Definition: array.h:89
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:376
Array< ArrayOfVector > ArrayOfArrayOfVector
Definition: array.h:63
Array< ArrayOfTensor4 > ArrayOfArrayOfTensor4
Definition: array.h:81
This can be used to make arrays out of anything.
Definition: array.h:108
Array & operator=(const Array< base > &A)
Assignment from another Array.
Definition: array.h:182
Array(Index n, const base &fillvalue)
Constructor filling with constant value.
Definition: array.h:152
Array(const std::array< base2, N > &input)
Definition: array.h:124
void push_back_n(const base &elem, const Index n)
Append element n times.
Definition: array.h:223
Array(std::vector< base > x)
Definition: array.h:128
Array(Index n)
Definition: array.h:113
Array(std::initializer_list< base > init)
Definition: array.h:121
const base & operator[](const Index n) const
Constant index operator.
Definition: array.h:206
Index nelem() const ARTS_NOEXCEPT
Number of elements.
Definition: array.h:197
Array(const Array< base > &A)
Definition: array.h:116
Array & operator=(Array< base > &&A) noexcept
Definition: array.h:190
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:160
Array(Array< base > &&A) noexcept
Definition: array.h:118
Array()
Definition: array.h:111
Helper comparison class to sort an array or vector based on an ArrayOfNumeric.
Definition: array.h:333
CmpArrayOfNumeric(const ArrayOfNumeric &vec)
Definition: array.h:335
const ArrayOfNumeric & values
Definition: array.h:340
bool operator()(const int &a, const int &b) const
Definition: array.h:336
The Matrix class.
Definition: matpackI.h:1270
The Tensor3 class.
Definition: matpackIII.h:344
The Tensor4 class.
Definition: matpackIV.h:427
The Tensor5 class.
Definition: matpackV.h:514
The Tensor6 class.
Definition: matpackVI.h:1097
The Tensor7 class.
Definition: matpackVII.h:2397
The Vector class.
Definition: matpackI.h:908
#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:113
#define v
#define w
#define a
#define b
#define N
Definition: rng.cc:164
The Sparse class.
Definition: matpackII.h:67