ARTS  2.4.0(git:4fb77825)
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 <cassert>
31 #include <climits>
32 #include <iomanip>
33 #include <iostream>
34 #include <array>
35 #include <vector>
36 #include "matpack.h"
37 
38 // Declare the existance of class Array:
39 template <class base>
40 class Array;
41 
44 
46 
49 
50 // Declare the existance of Vector/Matrix/Tensor classes:
51 class Vector;
52 class Matrix;
53 class Sparse;
54 class Tensor3;
55 class Tensor4;
56 class Tensor5;
57 class Tensor6;
58 class Tensor7;
59 
62 
64 
67 
69 
72 
75 
77 
80 
82 
85 
87 
90 
92 
95 
97 
107 template <class base>
108 class 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 <size_t N> explicit Array(const std::array<base, N>& input)
125  : std::vector<base>(input.begin(), input.end()) { /* Nothing to do here. */
126  }
127 
128  // Assignment operators:
129  Array& operator=(base x);
131  Array& operator=(Array<base>&& A) noexcept;
132 
133  // Number of elements:
134  Index nelem() const;
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 
149 template <class base>
150 inline 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 
157 template <class base>
159  std::fill(this->begin(), this->end(), x);
160  return *this;
161 }
162 
164 
179 template <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 
187 template <class base>
189  std::vector<base>::operator=(std::move(A));
190  return *this;
191 }
192 
194 template <class base>
195 inline Index Array<base>::nelem() const {
196  size_t s = this->size();
197  assert(s < LONG_MAX);
198  return static_cast<long>(s);
199 }
200 
203 template <class base>
204 inline const base& Array<base>::operator[](const Index n) const {
205  assert(0 <= n);
206  assert(n < nelem());
207  return std::vector<base>::operator[](n);
208 }
209 
212 template <class base>
213 inline base& Array<base>::operator[](const Index n) {
214  assert(0 <= n);
215  assert(n < nelem());
216  return std::vector<base>::operator[](n);
217 }
218 
220 template <class base>
221 inline 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 
228 template <class base>
229 inline 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 
246 template <class base>
247 inline 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 
262 template <class base>
263 inline 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 
291 template <class base>
292 Index 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 
313 template <class base>
314 void 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:
334  bool operator()(const int& a, const int& b) const {
335  return values[a] < values[b];
336  }
337 
339 };
340 
342 template <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 
353 template <class base>
355  Index outer,
356  Index inner = 0) {
357  assert(outer < aa.nelem());
358  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 
373 template <typename T, typename ... Ts>
374 constexpr 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
Matrix
The Matrix class.
Definition: matpackI.h:1193
ArrayOfArrayOfTensor5
Array< ArrayOfTensor5 > ArrayOfArrayOfTensor5
Definition: array.h:86
matpack.h
w
Complex w(Complex z) noexcept
The Faddeeva function.
Definition: linefunctions.cc:42
Array::operator=
Array & operator=(Array< base > &&A) noexcept
Definition: array.h:188
ArrayOfNumeric
Array< Numeric > ArrayOfNumeric
An array of Numeric.
Definition: array.h:48
Tensor3
The Tensor3 class.
Definition: matpackIII.h:339
ArrayOfTensor4
Array< Tensor4 > ArrayOfTensor4
An array of Tensor4.
Definition: array.h:79
Array::~Array
virtual ~Array()=default
ArrayOfTensor3
Array< Tensor3 > ArrayOfTensor3
An array of Tensor3.
Definition: array.h:74
linalg::std
void std(VectorView std, const Vector &y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the standard deviation of the ranged ys.
Definition: raw.cc:66
Sparse
The Sparse class.
Definition: matpackII.h:60
Array::push_back_n
void push_back_n(const base &elem, const Index n)
Append element n times.
Definition: array.h:221
Array::Array
Array(const std::array< base, N > &input)
Definition: array.h:124
Array::Array
Array()
Definition: array.h:111
TotalNumberOfElements
Index TotalNumberOfElements(const Array< Array< base > > &aa)
Determine total number of elements in an ArrayOfArray.
Definition: array.h:343
Tensor4
The Tensor4 class.
Definition: matpackIV.h:421
stdarrayify
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::Array
Array(Index n)
Definition: array.h:113
ArrayOfVector
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:58
ARTS::Continua::init
Workspace & init(Workspace &ws)
Definition: test_cpp_api.cc:145
Array
This can be used to make arrays out of anything.
Definition: array.h:108
Absorption::nelem
Index nelem(const Lines &l)
Number of lines.
Definition: absorptionlines.h:1820
Array::Array
Array(Index n, const base &fillvalue)
Constructor filling with constant value.
Definition: array.h:150
ArrayOfArrayOfTensor3
Array< ArrayOfTensor3 > ArrayOfArrayOfTensor3
Definition: array.h:76
ArrayOfArrayOfIndex
Array< ArrayOfIndex > ArrayOfArrayOfIndex
Definition: array.h:45
ArrayOfIndex
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
ArrayOfMatrix
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:66
Array::operator[]
base & operator[](const Index n)
Non-constant index operator.
Definition: array.h:213
Zeeman::end
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:108
ArrayOfTensor7
Array< Tensor7 > ArrayOfTensor7
An array of Tensor7.
Definition: array.h:94
FlattenedIndex
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
Tensor5
The Tensor5 class.
Definition: matpackV.h:506
ArrayOfSparse
Array< Sparse > ArrayOfSparse
An array of sparse matrices.
Definition: array.h:71
N
#define N
Definition: rng.cc:164
Array::operator=
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:158
CmpArrayOfNumeric::operator()
bool operator()(const int &a, const int &b) const
Definition: array.h:334
CmpArrayOfNumeric::CmpArrayOfNumeric
CmpArrayOfNumeric(const ArrayOfNumeric &vec)
Definition: array.h:333
ArrayOfArrayOfTensor4
Array< ArrayOfTensor4 > ArrayOfArrayOfTensor4
Definition: array.h:81
Array::Array
Array(std::initializer_list< base > init)
Definition: array.h:121
ArrayOfTensor5
Array< Tensor5 > ArrayOfTensor5
An array of Tensor5.
Definition: array.h:84
find_all
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:314
Array::operator[]
const base & operator[](const Index n) const
Constant index operator.
Definition: array.h:204
copy
void copy(ConstComplexIterator1D origin, const ConstComplexIterator1D &end, ComplexIterator1D target)
Copy data between begin and end to target.
Definition: complex.cc:478
max
base max(const Array< base > &x)
Max function.
Definition: array.h:247
ArrayOfArrayOfVector
Array< ArrayOfVector > ArrayOfArrayOfVector
Definition: array.h:63
operator<<
std::ostream & operator<<(std::ostream &os, const Array< base > &v)
Output operator.
Definition: array.h:229
ArrayOfArrayOfTensor7
Array< ArrayOfTensor7 > ArrayOfArrayOfTensor7
Definition: array.h:96
CmpArrayOfNumeric
Helper comparison class to sort an array or vector based on an ArrayOfNumeric.
Definition: array.h:331
Array::operator=
Array & operator=(const Array< base > &A)
Assignment from another Array.
Definition: array.h:180
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Array::Array
Array(const Array< base > &A)
Definition: array.h:116
find_first
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:292
Tensor6
The Tensor6 class.
Definition: matpackVI.h:1088
CmpArrayOfNumeric::values
const ArrayOfNumeric & values
Definition: array.h:338
ArrayOfArrayOfMatrix
Array< ArrayOfMatrix > ArrayOfArrayOfMatrix
Definition: array.h:68
Vector
The Vector class.
Definition: matpackI.h:860
ArrayOfTensor6
Array< Tensor6 > ArrayOfTensor6
An array of Tensor6.
Definition: array.h:89
min
base min(const Array< base > &x)
Min function.
Definition: array.h:263
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195
Tensor7
The Tensor7 class.
Definition: matpackVII.h:2382
Array::Array
Array(Array< base > &&A) noexcept
Definition: array.h:118
ArrayOfArrayOfTensor6
Array< ArrayOfTensor6 > ArrayOfArrayOfTensor6
Definition: array.h:91