C++ main module for stochmagnet Package  1.0
CORE_Array.h
Go to the documentation of this file.
1 #ifndef CORE_Array_H
2 #define CORE_Array_H
3 
4 //inherited class header
5 #include "CORE_Collection.h"
6 
7 //constant stride iterator
9 
10 //stride iterator
11 #include "CORE_StrideIterator.h"
12 
13 
88 template <typename T,class I>
89 class CORE_Array : public CORE_Collection<T,I> {
90 
91 
92 private:
93  //shorter class name
95 
96 
97  // CONSTRUCTORS
98 protected:
102 
103  }
104 
105 
106  // DESTRUCTORS
109  virtual ~CORE_Array() {
110  }
111 
112 
113 public:
114 
115  //memory method
116  //=============
117 
125  virtual tMemSize getMemorySize() const override {
126  return sizeof(*this)+this->getContentsMemorySize();
127  }
128 
129 
130 
131 
132 
133 public:
134 
135 
136  //allocation methods
137  //===================
138 
139 
140 
141  //accessors operator
142  //==================
143 public:
144 
145 
146 
147  //accessors iterator
148  //===================
149 public:
150 
154  template<int N>
155  inline auto sbegin() {
156  return CORE_StrideIterator<T*,N>(&(*this)[0]);
157  }
161  template<int N>
162  inline auto send() {
163  return CORE_StrideIterator<T*,N>(&(*this)[this->getSize()]);
164  }
165 
169  template<int N>
170  inline constexpr auto csbegin() const {
171  return CORE_ConstantStrideIterator<const T*,N>(&(*this)[0]);
172  }
176  template<int N>
177  inline constexpr auto csend() const {
178  return CORE_ConstantStrideIterator<const T*,N>(&(*this)[this->getSize()]);
179  }
180 
181 
182 
183  //accessor methods
184  //================
185 public:
189  inline const T& get(const tIndex& i) const {
190  return (*this)[i];
191  };
195  inline T& get(const tIndex& i) {
196  return (*this)[i];
197  }
198 
199 
203  inline const T* getValues() const {
204  return static_cast<const I*>(this)->getValues();
205  }
209  inline T* getValues() {
210  return static_cast<I*>(this)->getValues();
211  }
212 
213 
214  //assignment operators
215  //====================
216 public:
220  inline Self& operator=(const T& v) {
221  initialize(v);
222  return *this;
223  }
227  inline Self& operator=(std::initializer_list<T> values) {
228  copy(values.size(),values);
229  return *this;
230  }
231 
235  template<size_t N,typename Q>
236  inline Self& operator=(const std::array<Q,N>& values) {
237  copy(N,values);
238  return *this;
239 
240  }
241 
245  template<typename Q>
246  inline Self& operator=(const std::valarray<Q>& values) {
247  copy(values.size(),values);
248  return *this;
249  }
250 
254  template<typename Q>
255  inline Self& operator=(const std::vector<Q>& values) {
256  copy(values.size(),values);
257  return *this;
258  }
262  template<typename Q,class I1>
263  inline Self& operator=(const CORE_Array<Q,I1>& cpy) {
264  copy(cpy);
265  return *this;
266  }
270  inline Self& operator=(const Self& cpy) {
271  copy(cpy);
272  return *this;
273  }
277  template<typename Q,class I1>
278  inline Self& operator=(CORE_Array<Q,I1>&& cpy) {
279  copy(cpy);
280  return *this;
281  }
285  inline Self& operator=(Self&& cpy) {
286  copy(cpy);
287  return *this;
288  }
289 
290  //copy methods
291  //============
292 public:
293 
294 
295 
300  template<typename Q>
301  inline void copy(const tIndex& n , const Q* vs) {
302  static_cast<I*>(this)->copy(n,vs);
303  }
304 
305 
309  template<typename Q,size_t N>
310  inline void copy(const tIndex& n,const std::array<Q,N>& vs) {
311  static_cast<I*>(this)->copy(n,vs);
312  }
313 
317  template<typename Q>
318  inline void copy(const tIndex& n, const std::valarray<Q>& vs) {
319  static_cast<I*>(this)->copy(n,vs);
320  }
321 
322 
326  template<typename Q>
327  inline void copy(const tIndex& n,const std::vector<Q>& vs) {
328  static_cast<I*>(this)->copy(n,vs);
329  }
330 
331 
335  inline void copy(const tIndex& n,const std::initializer_list<T>& vs) {
336  static_cast<I*>(this)->copy(n,vs);
337  }
341  inline void copy(const tIndex& n,std::initializer_list<T>&& vs) {
342  static_cast<I*>(this)->copy(n,vs);
343  }
344 
345 
349  template<typename Q,class I1>
350  inline void copy(const CORE_Array<Q,I1>& cpy) {
351  static_cast<I*>(this)->copy(cpy);
352  }
353 
357  template<typename Q,class I1>
358  inline void copy(CORE_Array<Q,I1>&& cpy) {
359  static_cast<I>(*this)->copy(cpy);
360  }
361 
362 
363 
364 
365  //initializers methods
366  //====================
367 public:
370  inline void initialize(const T& v) {
371  static_cast<I*>(this)->initialize(v);
372  }
373 
374 
377  inline void uniformRandomize() {
378  if (std::is_floating_point_v<T>)
379  this->uniformRandomize(0,1);
380  else {
381  this->uniformRandomize(0,RAND_MAX);
382  }
383  }
384 
387  inline void uniformRandomize(const T& min,const T& max) {
388  static_cast<I*>(this)->uniformRandomize(min,max);
389  }
390 
391  //compound assignement operators (+=,-=,*=,/=,^=...)
392  //=================================================
393 public:
394  //T type compound assigement operators
395  //-------------------------------------
396 
400  inline I& operator+=(const T& v) {
401  return (*static_cast<I*>(this))+=v;
402  }
403 
407  inline I& operator-=(const T& v) {
408  return (*static_cast<I*>(this))-=v;
409  }
410 
414  inline I& operator*=(const T& v) {
415  return (*static_cast<I*>(this))*=v;
416 
417  }
421  inline I& operator/=(const T& v) {
422  return (*static_cast<I*>(this))/=v;
423  }
424 
425 
426  //integer type compound assignement operators
427  //---------------------------------------------
431  inline I& operator%=(const T& v) requires core_functions::isIntegerType<T> {
432  return (*static_cast<I>(this))%=v;
433  }
434 
438  inline I& operator&=(const T& v) requires core_functions::isIntegerType<T> {
439  return (*static_cast<I>(this))&=v;
440  }
441 
445  inline I& operator|=(const T& v)requires core_functions::isIntegerType<T> {
446  return (*static_cast<I>(this))|=v;
447  }
448 
452  inline I& operator^=(const T& v) requires core_functions::isIntegerType<T> {
453  return (*static_cast<I>(this))^=v;
454  }
458  inline I& operator<<=(const T& v) requires core_functions::isIntegerType<T> {
459  return (*static_cast<I>(this))<<=v;
460  }
461 
465  inline I& operator>>=(const T& v) requires core_functions::isIntegerType<T> {
466  return (*static_cast<I>(this))>>=v;
467  }
468 
469 
470  //class type compound operators
471  //-----------------------------
472 
476  template<typename Q,class I1>
477  inline I& operator+=(const CORE_Array<Q,I1>& v) {
478  return (*static_cast<I*>(this))+=v;
479  }
483  template<typename Q,class I1>
484  inline I& operator-=(const CORE_Array<Q,I1>& v) {
485  return (*static_cast<I*>(this))-=v;
486  }
490  template<typename Q,class I1>
491  inline I& operator*=(const CORE_Array<Q,I1>& v) {
492  return (*static_cast<I*>(this))*=v;
493  }
497  template<typename Q,class I1>
498  inline I& operator/=(const CORE_Array<Q,I1>& v) {
499  return (*static_cast<I*>(this))/=v;
500  }
501 
502 
503 
504 
505  //transformation method
506  //=====================
507 
511  template<typename LambdaFct>
512  inline void transform(LambdaFct&& F) {
513  static_cast<I*>(this)->transform(F);
514  }
515 
520  template<typename LambdaFct>
521  inline void transform(LambdaFct&& F,
522  const Self& X) {
523  static_cast<I*>(this)->transform(F,X);
524  }
525 
531  template<typename LambdaFct>
532  inline void transform(LambdaFct&& F,
533  const Self& X,
534  const Self& Y) {
535  static_cast<I*>(this)->transform(F,X,Y);
536  }
537 
538 
539 
540 
541 
545  inline void swap(Self& a) {
546  static_cast<I*>(this)->swap(a);
547  }
552  inline void swap(const tIndex& i,const tIndex& j) {
553  static_cast<I*>(this)->swap(i,j);
554  }
555 
556 
559  inline void normalize() {
560  static_cast<I*>(this)->normalize();
561  }
562 
563 
564 
565 
566 
572  template<typename Q,class I1>
573  inline void axpy(const Q& alpha, const CORE_Array<Q,I1>& X,const T& beta) {
574  static_cast<I*>(this)->axpy(alpha,X,beta);
575  }
576 
577 
578 
579  //Data consistency
580  //=================
581 
582 public:
587  inline tBoolean isNANContained() const {
588  return static_cast<const I*>(this)->isNANContained();
589  }
590 
591 
592 
593  //algebric methods
594  //================
595 public:
596 
597 
598 
602  inline void sum(T& s) const {
603  return static_cast<const I*>(this)->sum(s);
604  }
608  inline void prod(T& p) const {
609  static_cast<const I*>(this)->prod(p);
610  }
611 
616  template<typename Q,class I1>
617  inline T& scalarProduct(const CORE_Array<Q,I1>& X,T& s) const {
618  return static_cast<const I*>(this)->scalarProduct(X,s);
619  }
620 
621 
625  inline tReal l2Norm2() const {
626  return static_cast<const I*>(this)->l2Norm2();
627  }
632  inline tReal l2Norm() const {
633  return sqrt(l2Norm2());
634  }
639  template<typename Q,class I1>
640  inline tReal l2Distance2(const CORE_Array<Q,I1>& X) const {
641  return static_cast<const I*>(this)->l2Distance2(X);
642  }
647  template<typename Q,class I1>
648  inline tReal l2Distance(const CORE_Array<Q,I1>& X) const {
649  return sqrt(l2Distance2(X));
650  }
651 
656  inline tReal linfNorm(tIndex& i) const {
657  return static_cast<const I*>(this)->linfNorm(i);
658  }
663  template<typename Q,class I1>
664  inline tReal linfDistance(const CORE_Array<Q,I1>& X,tIndex& i) const {
665  return static_cast<const I*>(this)->linfDistance(X,i);
666  }
667 
668  //ordored type methods
669  //====================
670 
671 public:
675  inline void min(T& m) const requires core_functions::isOrderedType<T>{
676  static_cast<const I*>(this)->min(m);
677 
678  }
682  inline void max(T& m) const requires core_functions::isOrderedType<T> {
683  return static_cast<const I*>(this)->min(m);
684  }
685 
686 
689  inline void sort() requires core_functions::isOrderedType<T> {
690  directionalSort('i');
691  }
692 
696  inline void directionalSort(const tUChar& order) requires core_functions::isOrderedType<T> {
697  static_cast<I*>(this)->directionalSort(order);
698 
699  }
700 
701 
702 };
703 
704 
705 #endif
this class describes an array of values T of dynamical size with algebrical operators and I is an imp...
Definition: CORE_Array.h:89
I & operator-=(const T &v)
sub operator
Definition: CORE_Array.h:407
auto sbegin()
return begin stride iterator for writing
Definition: CORE_Array.h:155
I & operator*=(const T &v)
multiplicator operator
Definition: CORE_Array.h:414
Self & operator=(const CORE_Array< Q, I1 > &cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:263
void swap(const tIndex &i, const tIndex &j)
swap the two elements of the array
Definition: CORE_Array.h:552
Self & operator=(const T &v)
fill the values of the array with v
Definition: CORE_Array.h:220
Self & operator=(const std::vector< Q > &values)
build an array by a copy of c
Definition: CORE_Array.h:255
Self & operator=(const std::array< Q, N > &values)
build an array by a copy of c
Definition: CORE_Array.h:236
Self & operator=(CORE_Array< Q, I1 > &&cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:278
I & operator*=(const CORE_Array< Q, I1 > &v)
array multiply operator
Definition: CORE_Array.h:491
virtual tMemSize getMemorySize() const override
return the memory size of the class and the memory size of all its attributes/associations
Definition: CORE_Array.h:125
CORE_Array()
instanciation method of a class
Definition: CORE_Array.h:101
void sum(T &s) const
Computes the sum of all the elements.
Definition: CORE_Array.h:602
const T & get(const tIndex &i) const
get the i-th element for reading
Definition: CORE_Array.h:189
I & operator/=(const CORE_Array< Q, I1 > &v)
array divisor operator
Definition: CORE_Array.h:498
void transform(LambdaFct &&F)
transform the transform element with the lambda function Ti = F(Ti)
Definition: CORE_Array.h:512
void initialize(const T &v)
randomize the field
Definition: CORE_Array.h:370
tReal linfDistance(const CORE_Array< Q, I1 > &X, tIndex &i) const
compute the Linfinitty norm
Definition: CORE_Array.h:664
auto send()
return end N-stride iterator for writing
Definition: CORE_Array.h:162
CORE_Array< T, I > Self
Definition: CORE_Array.h:94
T * getValues()
get the values of the array
Definition: CORE_Array.h:209
T & get(const tIndex &i)
get the i-th element for writting
Definition: CORE_Array.h:195
void copy(const CORE_Array< Q, I1 > &cpy)
copy the container
Definition: CORE_Array.h:350
void copy(CORE_Array< Q, I1 > &&cpy)
copy the conatiner : mv is destroyed after this
Definition: CORE_Array.h:358
tReal l2Norm() const
compute the L2 norm
Definition: CORE_Array.h:632
tReal l2Norm2() const
compute the L2 norm squared
Definition: CORE_Array.h:625
void transform(LambdaFct &&F, const Self &X, const Self &Y)
transform the transform element with the lambda function Ti = F(Xi,Yi)
Definition: CORE_Array.h:532
void copy(const tIndex &n, const std::valarray< Q > &vs)
initialize the array to the values of val array
Definition: CORE_Array.h:318
void uniformRandomize(const T &min, const T &max)
randomize the field
Definition: CORE_Array.h:387
void prod(T &p) const
return the produc of all the elements
Definition: CORE_Array.h:608
void copy(const tIndex &n, const std::vector< Q > &vs)
initialize the array to the values of vector
Definition: CORE_Array.h:327
tBoolean isNANContained() const
return true if one value is Not A Number
Definition: CORE_Array.h:587
void copy(const tIndex &n, const Q *vs)
initialize the array to the values of pointer of size n
Definition: CORE_Array.h:301
void normalize()
normalize the array
Definition: CORE_Array.h:559
I & operator%=(const T &v) requires core_functions
modulo operator
Definition: CORE_Array.h:431
Self & operator=(Self &&cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:285
const T * getValues() const
get the values of the array for reading
Definition: CORE_Array.h:203
constexpr auto csbegin() const
return begin N-stride const iterator for writing
Definition: CORE_Array.h:170
void transform(LambdaFct &&F, const Self &X)
transform the transform element with the lambda function Ti = F(Xi)
Definition: CORE_Array.h:521
I & operator-=(const CORE_Array< Q, I1 > &v)
array sub operator
Definition: CORE_Array.h:484
constexpr auto csend() const
return end N-stride const iterator for writing
Definition: CORE_Array.h:177
Self & operator=(const std::valarray< Q > &values)
build an array by a copy of c
Definition: CORE_Array.h:246
tReal l2Distance2(const CORE_Array< Q, I1 > &X) const
compute the L2 distance squared
Definition: CORE_Array.h:640
void copy(const tIndex &n, const std::initializer_list< T > &vs)
initialize the array to the values of list
Definition: CORE_Array.h:335
tReal linfNorm(tIndex &i) const
compute the L infinity norm
Definition: CORE_Array.h:656
I & operator+=(const T &v)
add operator
Definition: CORE_Array.h:400
I & operator/=(const T &v)
divisor operator
Definition: CORE_Array.h:421
void axpy(const Q &alpha, const CORE_Array< Q, I1 > &X, const T &beta)
compute This=beta.This+ alpha .X
Definition: CORE_Array.h:573
void swap(Self &a)
swap the contents of the array
Definition: CORE_Array.h:545
virtual ~CORE_Array()
destroy an instance of class
Definition: CORE_Array.h:109
T & scalarProduct(const CORE_Array< Q, I1 > &X, T &s) const
return the scalar product
Definition: CORE_Array.h:617
void uniformRandomize()
randomize the field
Definition: CORE_Array.h:377
Self & operator=(const Self &cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:270
void copy(const tIndex &n, const std::array< Q, N > &vs)
initialize the array to the values of array of size N
Definition: CORE_Array.h:310
void copy(const tIndex &n, std::initializer_list< T > &&vs)
initialize the array to the values of list
Definition: CORE_Array.h:341
Self & operator=(std::initializer_list< T > values)
build an array by a copy of c
Definition: CORE_Array.h:227
tReal l2Distance(const CORE_Array< Q, I1 > &X) const
compute the L2 distance
Definition: CORE_Array.h:648
this class describes a general container of values of type T where implemented class is I
Definition: CORE_Collection.h:49
tIndex getSize() const
return the size of the container
Definition: CORE_Collection.h:111
this class describes a const iterator with a constant stride 2 templates parameter:
Definition: CORE_ConstantStrideIterator.h:12
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
this class describes a const iterator with a constant stride 2 templates parameter:
Definition: CORE_StrideIterator.h:12
Definition: functions.h:66
concept isOrderedType
Definition: functions.h:90
requires core_functions::isArithmeticType< T > const T & min(const T &a, const T &b)
min function
Definition: functions.h:415
requires core_functions::isArithmeticType< T > const T & max(const T &a, const T &b)
max function
Definition: functions.h:425
#define tIndex
Definition: types.h:157
#define tUChar
Definition: types.h:25
#define tMemSize
Definition: types.h:166
#define tBoolean
Definition: types.h:151
#define tReal
Definition: types.h:137