ARTS  2.3.1285(git:92a29ea9-dirty)
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);
130  Array& operator=(const Array<base>& A);
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) {
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:
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 
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
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.
Definition: complex.cc:478
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:292
Array(const Array< base > &A)
Definition: array.h:116
const ArrayOfNumeric & values
Definition: array.h:338
Array< ArrayOfMatrix > ArrayOfArrayOfMatrix
Definition: array.h:68
Array< Numeric > ArrayOfNumeric
An array of Numeric.
Definition: array.h:48
base min(const Array< base > &x)
Min function.
Definition: array.h:263
Index nelem() const
Number of elements.
Definition: array.h:195
void push_back_n(const base &elem, const Index n)
Append element n times.
Definition: array.h:221
Array(Array< base > &&A) noexcept
Definition: array.h:118
The Vector class.
Definition: matpackI.h:860
The Sparse class.
Definition: matpackII.h:60
The Tensor4 class.
Definition: matpackIV.h:421
Array< ArrayOfTensor7 > ArrayOfArrayOfTensor7
Definition: array.h:96
cmplx FADDEEVA() w(cmplx z, double relerr)
Definition: Faddeeva.cc:680
The Tensor7 class.
Definition: matpackVII.h:2382
wsv_data push_back(WsvRecord(NAME("mc_z_field_is_1D"), DESCRIPTION("Flag for MCGeneral and associated methods.\ "\" "If fields outside the cloud box are 1D, this flag can be set to 1\" "and the calculations will be more rapid.\" "\" "Usage:Set by the user.\"), GROUP("Index")))
Array(const std::array< base, N > &input)
Definition: array.h:124
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:58
Array()
Definition: array.h:111
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
The Tensor3 class.
Definition: matpackIII.h:339
Index TotalNumberOfElements(const Array< Array< base > > &aa)
Determine total number of elements in an ArrayOfArray.
Definition: array.h:343
Array< ArrayOfTensor6 > ArrayOfArrayOfTensor6
Definition: array.h:91
Array(Index n)
Definition: array.h:113
Array< Sparse > ArrayOfSparse
An array of sparse matrices.
Definition: array.h:71
Array< Tensor4 > ArrayOfTensor4
An array of Tensor4.
Definition: array.h:79
The Matrix class.
Definition: matpackI.h:1193
const base & operator[](const Index n) const
Constant index operator.
Definition: array.h:204
Helper comparison class to sort an array or vector based on an ArrayOfNumeric.
Definition: array.h:331
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:66
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:158
Array(std::initializer_list< base > init)
Definition: array.h:121
This can be used to make arrays out of anything.
Definition: array.h:40
Array< ArrayOfVector > ArrayOfArrayOfVector
Definition: array.h:63
bool operator()(const int &a, const int &b) const
Definition: array.h:334
Workspace & init(Workspace &ws)
Array< ArrayOfTensor5 > ArrayOfArrayOfTensor5
Definition: array.h:86
The Tensor6 class.
Definition: matpackVI.h:1088
CmpArrayOfNumeric(const ArrayOfNumeric &vec)
Definition: array.h:333
Array< ArrayOfIndex > ArrayOfArrayOfIndex
Definition: array.h:45
base max(const Array< base > &x)
Max function.
Definition: array.h:247
Array< Tensor7 > ArrayOfTensor7
An array of Tensor7.
Definition: array.h:94
Array< Tensor3 > ArrayOfTensor3
An array of Tensor3.
Definition: array.h:74
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< 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:374
Array< ArrayOfTensor4 > ArrayOfArrayOfTensor4
Definition: array.h:81
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:314
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
Array< Tensor5 > ArrayOfTensor5
An array of Tensor5.
Definition: array.h:84
Array< ArrayOfTensor3 > ArrayOfArrayOfTensor3
Definition: array.h:76
virtual ~Array()=default
The Tensor5 class.
Definition: matpackV.h:506