ARTS 2.5.11 (git: 725533f0)
array.h
Go to the documentation of this file.
1
10#ifndef array_h
11#define array_h
12
13#include <array>
14#include <climits>
15#include <iomanip>
16#include <iostream>
17#include <vector>
18
19#include "matpack_concepts.h"
20
30template <class base>
31class Array : public std::vector<base> {
32 public:
33 // Constructors:
34 Array() : std::vector<base>() {}
35
36 explicit Array(Index n) : std::vector<base>(n) {}
37
38 Array(Index n, const base& fillvalue) : std::vector<base>(n) {
39 std::fill(this->begin(), this->end(), fillvalue);
40 }
41
42 Array(const Array& A) : std::vector<base>(A) {}
43
44 Array(Array&& A) noexcept : std::vector<base>(std::move(A)) {}
45
46 Array(std::initializer_list<base> init) : std::vector<base>(init) {}
47
48 template <class base2, size_t N>
49 explicit Array(const std::array<base2, N>& input)
50 : std::vector<base>(input.begin(), input.end()) {
51 static_assert(std::is_convertible<base2, base>::value,
52 "Must be convertible");
53 }
54
55 Array(std::vector<base> x) : std::vector<base>(std::move(x)) {}
56
57 // Assignment operators:
58 Array& operator=(base x) {
59 std::fill(this->begin(), this->end(), x);
60 return *this;
61 }
62
63 Array& operator=(const Array& A) {
64 this->resize(A.size());
65 std::copy(A.begin(), A.end(), this->begin());
66 return *this;
67 }
68
69 Array& operator=(Array&& A) noexcept {
70 std::vector<base>::operator=(std::move(A));
71 return *this;
72 }
73
74 // Number of elements:
75 [[nodiscard]] Index nelem() const ARTS_NOEXCEPT {
76 size_t s = this->size();
77 ARTS_ASSERT(s < LONG_MAX);
78 return static_cast<Index>(s);
79 }
80
81 // Index operators:
82 const base& operator[](const Index n) const {
83 ARTS_ASSERT(0 <= n);
84 ARTS_ASSERT(n < nelem());
85 return std::vector<base>::operator[](n);
86 }
87
88 base& operator[](const Index n) {
89 ARTS_ASSERT(0 <= n);
90 ARTS_ASSERT(n < nelem());
91 return std::vector<base>::operator[](n);
92 }
93
94 // Helper functions
95 void push_back_n(const base& elem, const Index n) {
96 for (Index i = 0; i < n; i++) std::vector<base>::push_back(elem);
97 }
98
99 virtual ~Array() = default;
100
101 friend std::ostream& operator<<(std::ostream& os, const Array& v) {
102 typename Array::const_iterator i = v.begin();
103 const typename Array::const_iterator end = v.end();
104
105 if (i != end) {
106 os << std::setw(3) << *i;
107 ++i;
108 }
109
110 for (; i != end; ++i) {
111 os << " " << std::setw(3) << *i;
112 }
113
114 return os;
115 }
116};
117
120
122
125
127template <class base>
128inline base max(const Array<base>& x) {
129 // Initial value for max:
130 base max = x[0];
131
132 typename Array<base>::const_iterator xi = x.begin();
133 const typename Array<base>::const_iterator xe = x.end();
134
135 for (; xi != xe; ++xi) {
136 if (*xi > max) max = *xi;
137 }
138
139 return max;
140}
141
143template <class base>
144inline base min(const Array<base>& x) {
145 // Initial value for min:
146 base min = x[0];
147
148 typename Array<base>::const_iterator xi = x.begin();
149 const typename Array<base>::const_iterator xe = x.end();
150
151 for (; xi != xe; ++xi) {
152 if (*xi < min) min = *xi;
153 }
154
155 return min;
156}
157
159
172template <class base>
173Index find_first(const Array<base>& x, const base& w) {
174 for (Index i = 0; i < x.nelem(); ++i)
175 if (w == x[i]) return i;
176
177 return -1;
178}
179
181
194template <class base>
195void find_all(ArrayOfIndex& pos, const Array<base>& x, const base& w) {
196 pos.resize(0);
197 for (Index i = 0; i < x.nelem(); ++i)
198 if (w == x[i]) pos.push_back(i);
199}
200
213 public:
215 bool operator()(const int& a, const int& b) const {
216 return values[a] < values[b];
217 }
218
220};
221
223template <class base>
225 Index N_aa = 0;
226 for (Index i = 0; i < aa.nelem(); i++) {
227 N_aa += aa[i].nelem();
228 }
229
230 return N_aa;
231}
232
234template <class base>
236 Index outer,
237 Index inner = 0) {
238 ARTS_ASSERT(outer < aa.nelem());
239 ARTS_ASSERT(inner < aa[outer].nelem());
240
241 Index N = 0;
242 for (Index i = 0; i < outer; i++) {
243 N += aa[i].nelem();
244 }
245
246 return N + inner;
247}
248
249// It is not a good idea to put all the predefined array types in one
250// place. If I do this than a file cannot use one without defining all
251// the others.
252
254template <typename T, typename... Ts>
255constexpr std::array<T, 1 + sizeof...(Ts)> stdarrayify(const T& first,
256 const Ts&... the_rest) {
257 return {first, T(the_rest)...};
258}
259
260template <typename T>
261std::string stringify(const Array<T>& list,
262 const char* const sep = " ",
263 const char* const beg = "") {
264 std::ostringstream os;
265 for (auto& x : list) os << beg << x << sep;
266 return os.str();
267}
268
269#endif // array_h
std::string stringify(const Array< T > &list, const char *const sep=" ", const char *const beg="")
Definition array.h:261
Index TotalNumberOfElements(const Array< Array< base > > &aa)
Determine total number of elements in an ArrayOfArray.
Definition array.h:224
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:235
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition array.h:173
base max(const Array< base > &x)
Max function.
Definition array.h:128
base min(const Array< base > &x)
Min function.
Definition array.h:144
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition array.h:195
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:255
This can be used to make arrays out of anything.
Definition array.h:31
Array(const Array &A)
Definition array.h:42
Array(Index n, const base &fillvalue)
Definition array.h:38
virtual ~Array()=default
Array(const std::array< base2, N > &input)
Definition array.h:49
Array & operator=(base x)
Definition array.h:58
void push_back_n(const base &elem, const Index n)
Definition array.h:95
Array(std::vector< base > x)
Definition array.h:55
Array & operator=(const Array &A)
Definition array.h:63
base & operator[](const Index n)
Definition array.h:88
Array(Index n)
Definition array.h:36
friend std::ostream & operator<<(std::ostream &os, const Array &v)
Definition array.h:101
Array(std::initializer_list< base > init)
Definition array.h:46
const base & operator[](const Index n) const
Definition array.h:82
Index nelem() const ARTS_NOEXCEPT
Definition array.h:75
Array & operator=(Array &&A) noexcept
Definition array.h:69
Array(Array &&A) noexcept
Definition array.h:44
Array()
Definition array.h:34
Helper comparison class to sort an array or vector based on an ArrayOfNumeric.
Definition array.h:212
CmpArrayOfNumeric(const ArrayOfNumeric &vec)
Definition array.h:214
const ArrayOfNumeric & values
Definition array.h:219
bool operator()(const int &a, const int &b) const
Definition array.h:215
#define ARTS_NOEXCEPT
Definition debug.h:83
#define ARTS_ASSERT(condition,...)
Definition debug.h:86
#define v
#define w
#define a
#define b