C++ mpi module for stochmagnet_main Package
CORE_ValArray.h
1 #ifndef CORE_ValArray_H
2 #define CORE_ValArray_H
3 
4 #include "CORE_Array.h"
5 
6 
7 //numerics functin header
8 #include "functions_numeric.h"
9 
10 #include <vector>
11 #include <span>
12 
69 template <typename T,class I>
70 class CORE_ValArray : public CORE_Array<T,I> {
71 
72 
73 private:
74 
75  std::valarray<T> mValues;
76 
77  // CONSTRUCTORS
78 public:
82  }
83 
87  explicit CORE_ValArray(const tIndex& n) : mValues(n) {
88  }
89 
90 
91 
92  // DESTRUCTORS
95  virtual ~CORE_ValArray() {
96  }
97 
98 
99 private:
100 
101 
102 
103 
104  //MEMORY
105  //=====
106 public:
114  virtual tMemSize getMemorySize() const override {
115  return sizeof(*this)+this->getContentsMemorySize();
116  }
117 
126  virtual tMemSize getContentsMemorySize() const override {
128  mem+=mValues.size()*sizeof(T);
129  return mem;
130  }
131 
132  //allocation methods
133  //===================
134 public:
140  inline void setSize(const tIndex& n) {
141  //avoid no need re-allocation
142  if (n!=mValues.size()) mValues.resize(n);
143  };
147  inline tIndex getSize() const {
148  return mValues.size();
149  };
150 
153  inline void resize(const tIndex& n) {
154  std::valarray<T> newValues(n);
155  //copy the p values of old values withinh newValues
156  tIndex p=functions_numeric::min(n,this->getSize());
157  Copy(p,mValues,newValues);
158  mValues.swap(newValues);
159 
160  }
161 
162  //accessor operators
163  //====================
164 public:
168  inline const T& operator[](const tIndex& i) const {
169 #ifdef DEBUG
170  if (i>=mValues.size()) {
171  throw CORE_Exception("core",
172  "CORE_ValArray[]",
173  "CORE_ValArray["+std::to_string(i)+"] is out of bounds [0,"+std::to_string(mValues.size())+"[ for reading");
174  }
175 #endif
176  return mValues[i];
177  };
181  inline T& operator[](const tIndex& i) {
182 #ifdef DEBUG
183  if (i>=mValues.size()) {
184  throw CORE_Exception("core",
185  "CORE_ValArray[]",
186  "CORE_ValArray["+std::to_string(i)+"] is out of bounds [0,"+std::to_string(mValues.size())+"[ for writing ");
187  }
188 #endif
189  return mValues[i];
190  };
191 
192 
193  //iterator accessor
194  //=================
195 public:
199  inline constexpr auto cbegin() const {
200  return std::cbegin(this->mValues);
201  }
205  inline constexpr auto cend() const {
206  return std::cend(this->mValues);
207  }
211  inline auto begin() {
212  return std::begin(this->mValues);
213  }
217  inline auto end() {
218  return std::end(this->mValues);
219  }
220 
224  inline auto rbegin() {
225  //return std::ranges::rbegin(this->mValues);
226  return rbegin(this->mValues);
227  }
231  inline auto rend() {
232  //return std::ranges::rend(this->mValues);
233  return rend(this->mValues);
234  }
235 
239  inline constexpr auto crbegin() const {
240  //return std::ranges::crbegin(this->mValues);
241  return crbegin(this->mValues);
242  }
246  inline constexpr auto crend() const {
247  //return std::ranges::crend(this->mValues);
248  return crend(this->mValues);
249  }
250 
251  //accessor methods
252  //================
253 public:
257  inline std::valarray<T>& getArray() {
258  return mValues;
259  }
263  inline const std::valarray<T>& getArray() const {
264  return mValues;
265  }
266 
270  inline const T* getValues() const {
271  return &mValues[0];
272  }
276  inline T* getValues() {
277  return &mValues[0];
278  }
279 
280 
281 
282  //assignment operators
283  //====================
284 
285 
286  //copy methods
287  //============
288 
289  //initializers
290  //============
291 
292 
293  //compound assignement operator
294  //=============================
295 
296 
297 
298 
299  //transform methods
300  //==================
301 
302 
306  inline void swap(CORE_ValArray<T,I>& a) {
307  mValues.swap(a.getArray());
308  }
313  inline void swap(const tIndex& i,const tIndex& j) {
314  std::swap(mValues[i],mValues[j]);
315  }
316 
317 
318  //array algebric operators
319  //==========================
320 
321 
322 
323 
324  //Data consistency
325  //=================
326 
327 
328 
329 
330 
331  //algebric methods
332  //================
333 
334 
335 
336 
337 };
338 
339 
340 #endif
this class describes an array of values T of dynamical size with algebrical operators and I is an imp...
Definition: CORE_Array.h:91
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
this class describes an arithmetic array type implemented with a std::valarray object with implementa...
Definition: CORE_ValArray.h:70
constexpr auto cend() const
return end iterator for reading
Definition: CORE_ValArray.h:205
CORE_ValArray(const tIndex &n)
build an array of size n
Definition: CORE_ValArray.h:87
constexpr auto cbegin() const
return begin iterator for reading
Definition: CORE_ValArray.h:199
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_ValArray.h:126
T & operator[](const tIndex &i)
get the i-th element for writting. Do not verify the bounds
Definition: CORE_ValArray.h:181
constexpr auto crbegin() const
return reverse begin iterator for reading
Definition: CORE_ValArray.h:239
tIndex getSize() const
return the size of the array for writing
Definition: CORE_ValArray.h:147
auto rbegin()
return reverse begin iterator for writing
Definition: CORE_ValArray.h:224
const std::valarray< T > & getArray() const
get the array for reading
Definition: CORE_ValArray.h:263
auto rend()
return reverse end iterator for writing
Definition: CORE_ValArray.h:231
auto end()
return end iterator for writing
Definition: CORE_ValArray.h:217
constexpr auto crend() const
return reverse end iterator for reading
Definition: CORE_ValArray.h:246
void resize(const tIndex &n)
modify the size of the array and keep its old values
Definition: CORE_ValArray.h:153
void swap(const tIndex &i, const tIndex &j)
swap the 2 elements of the array
Definition: CORE_ValArray.h:313
auto begin()
return begin iterator for writing
Definition: CORE_ValArray.h:211
const T & operator[](const tIndex &i) const
get the i-th element for reading. Do not verify the bounds
Definition: CORE_ValArray.h:168
std::valarray< T > & getArray()
get the array
Definition: CORE_ValArray.h:257
void setSize(const tIndex &n)
set the number of values
Definition: CORE_ValArray.h:140
const T * getValues() const
get the values of the array for reading
Definition: CORE_ValArray.h:270
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_ValArray.h:114
virtual ~CORE_ValArray()
destroy an instance of this
Definition: CORE_ValArray.h:95
void swap(CORE_ValArray< T, I > &a)
swap the contents of the array
Definition: CORE_ValArray.h:306
T * getValues()
get the values of the array
Definition: CORE_ValArray.h:276
CORE_ValArray()
build an array of T*
Definition: CORE_ValArray.h:81