netxsimdg
Loading...
Searching...
No Matches
ModelArrayRef.hpp
Go to the documentation of this file.
1
8#ifndef MODELARRAYREF2_HPP
9#define MODELARRAYREF2_HPP
10
11#include "ModelArray.hpp"
12#include <vector>
13
14namespace Nextsim {
15const bool RW = true;
16const bool RO = false;
17
18typedef ModelArray* ModelArrayReference;
19typedef const ModelArray* ModelArrayConstReference;
20typedef std::vector<ModelArrayReference> MARBackingStore;
21typedef std::vector<ModelArrayConstReference> MARConstBackingStore;
22
36template <auto arrayName, typename S, bool access = RO> class ModelArrayRef {
37public:
38 ModelArrayRef(const S& backingStoreIn)
39 : backingStore(backingStoreIn)
40 {
41 }
42
53 const double& operator[](const ModelArray::MultiDim& dims)
54 {
55 checkMaybeThrow();
56 return backingStore[static_cast<size_t>(arrayName)]->operator[](dims);
57 }
67 const double& operator[](size_t index) const
68 {
69 checkMaybeThrow();
70 return backingStore[static_cast<size_t>(arrayName)]->operator[](index);
71 }
75 const double& operator()(size_t i) const
76 {
77 checkMaybeThrow();
78 return backingStore[static_cast<size_t>(arrayName)]->operator()(i);
79 }
83 const double& operator()(size_t i, size_t j) const
84 {
85 checkMaybeThrow();
86 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j);
87 }
91 const double& operator()(size_t i, size_t j, size_t k) const
92 {
93 checkMaybeThrow();
94 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k);
95 }
99 const double& operator()(size_t i, size_t j, size_t k, size_t l) const
100 {
101 checkMaybeThrow();
102 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l);
103 }
107 const double& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
108 {
109 checkMaybeThrow();
110 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m);
111 }
115 const double& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const
116 {
117 checkMaybeThrow();
118 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m, n);
119 }
123 const double& operator()(
124 size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p) const
125 {
126 checkMaybeThrow();
127 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m, n, p);
128 }
132 const double& operator()(
133 size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p, size_t q) const
134 {
135 checkMaybeThrow();
136 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m, n, p, q);
137 }
138
148 const double& zIndexAndLayer(size_t hIndex, size_t layer)
149 {
150 checkMaybeThrow();
151 return backingStore[static_cast<size_t>(arrayName)]->zIndexAndLayer(hIndex, layer);
152 }
153
155 const ModelArray& data() const
156 {
157 checkMaybeThrow();
158 return *backingStore[static_cast<size_t>(arrayName)];
159 }
161 operator const ModelArray&() const { return data(); }
162
165 ModelArray operator+(const ModelArray& addend) const { return data() + addend; }
168 ModelArray operator-(const ModelArray& subtrahend) const { return data() - subtrahend; }
171 ModelArray operator*(const ModelArray& multiplier) const { return data() * multiplier; }
174 ModelArray operator/(const ModelArray& divisor) const { return data() / divisor; }
175
178 ModelArray operator+(double addend) const { return data() + addend; }
181 ModelArray operator-(double subtrahend) const { return data() - subtrahend; }
184 ModelArray operator*(double multiplier) const { return data() * multiplier; }
187 ModelArray operator/(double divisor) const { return data() / divisor; }
188
189private:
190 const S& backingStore;
191
192 void checkMaybeThrow() const
193 {
194#ifdef DEBUG_MODELARRAYREF
195 // To activate the debuggin version of this function, add
196 // target_compile_definitions(target PRIVATE DEBUG_MODELARRAYREF)
197 // to the target in the relevant CMakeLists.txt
198 if (!backingStore[static_cast<size_t>(arrayName)])
199 throw std::invalid_argument(
200 "No registered ModelArray at " + std::to_string(static_cast<size_t>(arrayName)));
201#else
202
203#endif
204 }
205};
206
217template <auto arrayName> class ModelArrayRef<arrayName, MARBackingStore, RW> {
218public:
219 ModelArrayRef(const MARBackingStore& backingStoreIn)
220 : backingStore(backingStoreIn)
221 {
222 }
223
234 double& operator[](const ModelArray::MultiDim& dims)
235 {
236 checkMaybeThrow();
237 return backingStore[static_cast<size_t>(arrayName)]->operator[](dims);
238 }
248 double& operator[](size_t index) const
249 {
250 checkMaybeThrow();
251 return backingStore[static_cast<size_t>(arrayName)]->operator[](index);
252 }
256 double& operator()(size_t i) const
257 {
258 checkMaybeThrow();
259 return backingStore[static_cast<size_t>(arrayName)]->operator()(i);
260 }
264 double& operator()(size_t i, size_t j) const
265 {
266 checkMaybeThrow();
267 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j);
268 }
272 double& operator()(size_t i, size_t j, size_t k) const
273 {
274 checkMaybeThrow();
275 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k);
276 }
280 double& operator()(size_t i, size_t j, size_t k, size_t l) const
281 {
282 checkMaybeThrow();
283 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l);
284 }
288 double& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
289 {
290 checkMaybeThrow();
291 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m);
292 }
296 double& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const
297 {
298 checkMaybeThrow();
299 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m, n);
300 }
304 double& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p) const
305 {
306 checkMaybeThrow();
307 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m, n, p);
308 }
312 double& operator()(
313 size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p, size_t q) const
314 {
315 checkMaybeThrow();
316 return backingStore[static_cast<size_t>(arrayName)]->operator()(i, j, k, l, m, n, p, q);
317 }
318
328 double& zIndexAndLayer(size_t hIndex, size_t layer)
329 {
330 checkMaybeThrow();
331 return backingStore[static_cast<size_t>(arrayName)]->zIndexAndLayer(hIndex, layer);
332 }
333
336 {
337 checkMaybeThrow();
338 return *backingStore[static_cast<size_t>(arrayName)];
339 }
341 operator ModelArray&() const { return data(); }
342
343private:
344 const MARBackingStore& backingStore;
345
346 void checkMaybeThrow() const
347 {
348#ifdef DEBUG_MODELARRAYREF
349 if (!backingStore[static_cast<size_t>(arrayName)])
350 throw std::invalid_argument(
351 "No registered ModelArray at " + std::to_string(static_cast<size_t>(arrayName)));
352#else
353
354#endif
355 }
356};
357}
358#endif /* MODELARRAYREF2_HPP */
A class that holds the array data for the model.
double & zIndexAndLayer(size_t hIndex, size_t layer)
Special access function for ZFields.
double & operator[](const ModelArray::MultiDim &dims)
Returns the data at the indices.
double & operator()(size_t i) const
Returns the specified point from a 1 dimensional ModelArray.
double & operator()(size_t i, size_t j, size_t k) const
Returns the specified point from a 3 dimensional ModelArray.
double & operator()(size_t i, size_t j) const
Returns the specified point from a 2 dimensional ModelArray.
double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p) const
Returns the specified point from a 7 dimensional ModelArray.
double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
Returns the specified point from a 5 dimensional ModelArray.
double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p, size_t q) const
Returns the specified point from a 8 dimensional ModelArray.
double & operator()(size_t i, size_t j, size_t k, size_t l) const
Returns the specified point from a 4 dimensional ModelArray.
double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const
Returns the specified point from a 6 dimensional ModelArray.
double & operator[](size_t index) const
Returns the data at the specified one dimensional index.
ModelArray & data() const
Direct access top the underlying data array.
A class which provides indirect access to ModelArray.
const double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const
Returns the specified point from a 6 dimensional ModelArray.
const double & zIndexAndLayer(size_t hIndex, size_t layer)
Special access function for ZFields.
const double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p, size_t q) const
Returns the specified point from a 8 dimensional ModelArray.
const double & operator[](size_t index) const
Returns the data at the specified one dimensional index.
const double & operator()(size_t i, size_t j) const
Returns the specified point from a 2 dimensional ModelArray.
const double & operator()(size_t i, size_t j, size_t k, size_t l) const
Returns the specified point from a 4 dimensional ModelArray.
const double & operator()(size_t i, size_t j, size_t k) const
Returns the specified point from a 3 dimensional ModelArray.
const double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n, size_t p) const
Returns the specified point from a 7 dimensional ModelArray.
const double & operator()(size_t i) const
Returns the specified point from a 1 dimensional ModelArray.
const double & operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
Returns the specified point from a 5 dimensional ModelArray.
ModelArray operator-(double subtrahend) const
Returns a ModelArray containing the per-element difference between the object and the provided ModelA...
ModelArray operator/(const ModelArray &divisor) const
Returns a ModelArray containing the per-element ratio between the object and the provided ModelArray.
const double & operator[](const ModelArray::MultiDim &dims)
Returns the data at the indices.
ModelArray operator+(double addend) const
Returns a ModelArray containing the per-element sum of the object and the provided ModelArray.
const ModelArray & data() const
Direct access top the underlying data array.
ModelArray operator*(const ModelArray &multiplier) const
Returns a ModelArray containing the per-element product of the object and the provided ModelArray.
ModelArray operator/(double divisor) const
Returns a ModelArray containing the per-element ratio between the object and the provided ModelArray.
ModelArray operator*(double multiplier) const
Returns a ModelArray containing the per-element product of the object and the provided ModelArray.
ModelArray operator+(const ModelArray &addend) const
Returns a ModelArray containing the per-element sum of the object and the provided ModelArray.
ModelArray operator-(const ModelArray &subtrahend) const
Returns a ModelArray containing the per-element difference between the object and the provided ModelA...