C++ mpi module for stochmagnet_main Package
CORE_Array.h
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
8 #include "CORE_ConstantStrideIterator.h"
9 
10 //stride iterator
11 #include "CORE_StrideIterator.h"
12 
13 //core
14 #include "functions_numeric.h"
15 
90 template <typename T,class I>
91 class CORE_Array : public CORE_Collection<T,I> {
92 
93 
94 private:
95  //shorter class name
96  typedef CORE_Array<T,I> Self;
97 
98 
99  // CONSTRUCTORS
100 protected:
104 
105  }
106 
107 
108  // DESTRUCTORS
111  virtual ~CORE_Array() {
112  }
113 
114 
115 public:
116 
117  //memory method
118  //=============
119 
127  virtual tMemSize getMemorySize() const override {
128  return sizeof(*this)+this->getContentsMemorySize();
129  }
130 
131 
132 
133 
134 
135 public:
136 
137 
138 
139  //allocation methods
140  //===================
141 
142 
143 
144  //accessors operator
145  //==================
146 public:
147 
148 
149 
150  //accessors iterator
151  //===================
152 public:
153 
157  template<int N>
158  inline auto sbegin() {
159  return CORE_StrideIterator<T*,N>(&(*this)[0]);
160  }
164  template<int N>
165  inline auto send() {
166  return CORE_StrideIterator<T*,N>(&(*this)[this->getSize()]);
167  }
168 
172  template<int N>
173  inline constexpr auto csbegin() const {
174  return CORE_ConstantStrideIterator<const T*,N>(&(*this)[0]);
175  }
179  template<int N>
180  inline constexpr auto csend() const {
181  return CORE_ConstantStrideIterator<const T*,N>(&(*this)[this->getSize()]);
182  }
183 
184 
185 
186  //accessor methods
187  //================
188 public:
192  inline const T& get(const tIndex& i) const {
193  return (*this)[i];
194  };
198  inline T& get(const tIndex& i) {
199  return (*this)[i];
200  }
201 
202 
206  inline const T* getValues() const {
207  return static_cast<const I*>(this)->getValues();
208  }
212  inline T* getValues() {
213  return static_cast<I*>(this)->getValues();
214  }
215 
216 
217  //assignment operators
218  //====================
219 public:
223  inline Self& operator=(const T& v) {
224  initialize(v);
225  return *this;
226  }
230  inline Self& operator=(std::initializer_list<T> values) {
231  copy(values.size(),values);
232  return *this;
233  }
234 
238  template<size_t N,typename Q>
239  inline Self& operator=(const std::array<Q,N>& values) {
240  copy(N,values);
241  return *this;
242 
243  }
244 
248  template<typename Q>
249  inline Self& operator=(const std::valarray<Q>& values) {
250  copy(values.size(),values);
251  return *this;
252  }
253 
257  template<typename Q>
258  inline Self& operator=(const std::vector<Q>& values) {
259  copy(values.size(),values);
260  return *this;
261  }
265  template<typename Q,class I1>
266  inline Self& operator=(const CORE_Array<Q,I1>& cpy) {
267  copy(cpy);
268  return *this;
269  }
273  inline Self& operator=(const Self& cpy) {
274  copy(cpy);
275  return *this;
276  }
280  template<typename Q,class I1>
281  inline Self& operator=(CORE_Array<Q,I1>&& cpy) {
282  copy(cpy);
283  return *this;
284  }
288  inline Self& operator=(Self&& cpy) {
289  copy(cpy);
290  return *this;
291  }
292 
293  //copy methods
294  //============
295 public:
296 
297 
298 
303  template<typename Q>
304  inline void copy(const tIndex& n , const Q* vs) {
305  static_cast<I*>(this)->copy(n,vs);
306  }
307 
308 
313  template<typename Q,size_t N>
314  inline void copy(const tIndex& n,const std::array<Q,N>& vs) {
315  static_cast<I*>(this)->copy(n,vs);
316  }
317 
322  template<typename Q>
323  inline void copy(const tIndex& n, const std::valarray<Q>& vs) {
324  static_cast<I*>(this)->copy(n,vs);
325  }
326 
327 
332  template<typename Q>
333  inline void copy(const tIndex& n,const std::vector<Q>& vs) {
334  static_cast<I*>(this)->copy(n,vs);
335  }
336 
337 
342  inline void copy(const tIndex& n,const std::initializer_list<T>& vs) {
343  static_cast<I*>(this)->copy(n,vs);
344  }
349  inline void copy(const tIndex& n,std::initializer_list<T>&& vs) {
350  static_cast<I*>(this)->copy(n,vs);
351  }
352 
353 
357  template<typename Q,class I1>
358  inline void copy(const CORE_Array<Q,I1>& cpy) {
359  static_cast<I*>(this)->copy(cpy);
360  }
361 
365  template<typename Q,class I1>
366  inline void copy(CORE_Array<Q,I1>&& cpy) {
367  static_cast<I>(*this)->copy(cpy);
368  }
369 
370 
371 
372 
373  //initializers methods
374  //====================
375 public:
378  inline void initialize(const T& v) {
379  static_cast<I*>(this)->initialize(v);
380  }
381 
382 
386  virtual void setUniformRandomizeSeed(const tULLInt& seed) {
387  std::srand(seed);
388  }
391  inline void setUniformRandomizeSeed() {
392  setUniformRandomizeSeed(std::time(nullptr));
393  }
394 
397  inline void uniformRandomize() {
398  if (std::is_floating_point_v<T>)
399  this->uniformRandomize(0,1);
400  else {
401  this->uniformRandomize(0,RAND_MAX);
402  }
403  }
404 
407  inline void uniformRandomize(const T& min,const T& max) {
408  static_cast<I*>(this)->uniformRandomize(min,max);
409  }
410 
411  //compound assignement operators (+=,-=,*=,/=,^=...)
412  //=================================================
413 public:
414  //T type compound assigement operators
415  //-------------------------------------
416 
420  inline I& operator+=(const T& v) {
421  return (*static_cast<I*>(this))+=v;
422  }
423 
427  inline I& operator-=(const T& v) {
428  return (*static_cast<I*>(this))-=v;
429  }
430 
434  inline I& operator*=(const T& v) {
435  return (*static_cast<I*>(this))*=v;
436 
437  }
441  inline I& operator/=(const T& v) {
442  return (*static_cast<I*>(this))/=v;
443  }
444 
445 
446  //integer type compound assignement operators
447  //---------------------------------------------
451  inline I& operator%=(const T& v) requires functions_type::isIntegerType<T> {
452  return (*static_cast<I>(this))%=v;
453  }
454 
458  inline I& operator&=(const T& v) requires functions_type::isIntegerType<T> {
459  return (*static_cast<I>(this))&=v;
460  }
461 
465  inline I& operator|=(const T& v)requires functions_type::isIntegerType<T> {
466  return (*static_cast<I>(this))|=v;
467  }
468 
472  inline I& operator^=(const T& v) requires functions_type::isIntegerType<T> {
473  return (*static_cast<I>(this))^=v;
474  }
478  inline I& operator<<=(const T& v) requires functions_type::isIntegerType<T> {
479  return (*static_cast<I>(this))<<=v;
480  }
481 
485  inline I& operator>>=(const T& v) requires functions_type::isIntegerType<T> {
486  return (*static_cast<I>(this))>>=v;
487  }
488 
489 
490  //class type compound operators
491  //-----------------------------
492 
496  template<typename Q,class I1>
497  inline I& operator+=(const CORE_Array<Q,I1>& v) {
498  return (*static_cast<I*>(this))+=v;
499  }
503  template<typename Q,class I1>
504  inline I& operator-=(const CORE_Array<Q,I1>& v) {
505  return (*static_cast<I*>(this))-=v;
506  }
510  template<typename Q,class I1>
511  inline I& operator*=(const CORE_Array<Q,I1>& v) {
512  return (*static_cast<I*>(this))*=v;
513  }
517  template<typename Q,class I1>
518  inline I& operator/=(const CORE_Array<Q,I1>& v) {
519  return (*static_cast<I*>(this))/=v;
520  }
521 
522 
523 
524 
525  //transformation method
526  //=====================
527 
531  template<typename LambdaFct>
532  inline void transform(LambdaFct&& F) {
533  static_cast<I*>(this)->transform(F);
534  }
535 
540  template<typename LambdaFct>
541  inline void transform(LambdaFct&& F,
542  const Self& X) {
543  static_cast<I*>(this)->transform(F,X);
544  }
545 
551  template<typename LambdaFct>
552  inline void transform(LambdaFct&& F,
553  const Self& X,
554  const Self& Y) {
555  static_cast<I*>(this)->transform(F,X,Y);
556  }
557 
558 
559 
560 
561 
565  inline void swap(Self& a) {
566  static_cast<I*>(this)->swap(a);
567  }
572  inline void swap(const tIndex& i,const tIndex& j) {
573  static_cast<I*>(this)->swap(i,j);
574  }
575 
576 
579  inline void normalize() {
580  static_cast<I*>(this)->normalize();
581  }
582 
583 
584 
585 
586 
592  template<typename Q,class I1>
593  inline void axpy(const Q& alpha, const CORE_Array<Q,I1>& X,const T& beta) {
594  static_cast<I*>(this)->axpy(alpha,X,beta);
595  }
596 
597 
598 
599  //Data consistency
600  //=================
601 
602 public:
607  inline tBoolean isNANContained() const {
608  return static_cast<const I*>(this)->isNANContained();
609  }
610 
611 
612 
613  //algebric methods
614  //================
615 public:
616 
617 
618 
622  inline void sum(T& s) const {
623  return static_cast<const I*>(this)->sum(s);
624  }
628  inline void prod(T& p) const {
629  static_cast<const I*>(this)->prod(p);
630  }
631 
636  template<typename Q,class I1>
637  inline T& scalarProduct(const CORE_Array<Q,I1>& X,T& s) const {
638  return static_cast<const I*>(this)->scalarProduct(X,s);
639  }
640 
641 
645  inline tReal l2Norm2() const {
646  return static_cast<const I*>(this)->l2Norm2();
647  }
652  inline tReal l2Norm() const {
653  return sqrt(l2Norm2());
654  }
659  template<typename Q,class I1>
660  inline tReal l2Distance2(const CORE_Array<Q,I1>& X) const {
661  return static_cast<const I*>(this)->l2Distance2(X);
662  }
667  template<typename Q,class I1>
668  inline tReal l2Distance(const CORE_Array<Q,I1>& X) const {
669  return sqrt(l2Distance2(X));
670  }
671 
676  inline tReal linfNorm(tIndex& i) const {
677  return static_cast<const I*>(this)->linfNorm(i);
678  }
683  template<typename Q,class I1>
684  inline tReal linfDistance(const CORE_Array<Q,I1>& X,tIndex& i) const {
685  return static_cast<const I*>(this)->linfDistance(X,i);
686  }
687 
688  //ordored type methods
689  //====================
690 
691 public:
695  inline void min(T& m) const requires functions_type::isOrderedType<T>{
696  static_cast<const I*>(this)->min(m);
697 
698  }
702  inline void max(T& m) const requires functions_type::isOrderedType<T> {
703  return static_cast<const I*>(this)->min(m);
704  }
705 
706 
709  inline void sort() requires functions_type::isOrderedType<T> {
710  directionalSort('i');
711  }
712 
716  inline void directionalSort(const tUChar& order) requires functions_type::isOrderedType<T> {
717  static_cast<I*>(this)->directionalSort(order);
718 
719  }
720 
721  //IO methods
722  //=========
726  virtual tString toString() const override {
727  return this->toString(this->getSize());
728  }
729 
732  inline tString toString(const tIndex& nPrintedValues) const {
733  const T* values=this->getValues();
734  tIndex n=this->getSize();
735  std::stringstream ss;
736  ss<<"{";
737  if ((nPrintedValues==0) || (nPrintedValues>=n)) {
738  //end iterator
739  const T* eValues=values;
740  eValues+=n;
741  while (values!=eValues) {
742  ss<<std::setprecision(12)<<(*values)<<",";
743  values++;
744  }
745  }else {
746  tIndex index;
747  for (tInteger k=0;k<=nPrintedValues;k++) {
748  index=((n-1)*k)/nPrintedValues;
749  ss<<index<<":"<<std::setprecision(12)<<values[index]<<",";
750  }
751  }
752  ss.seekp(-1, std::ios_base::end);
753  ss<<"}";
754  return ss.str();
755  }
756 
757 
758 };
759 
760 
761 #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
I & operator-=(const T &v)
sub operator
Definition: CORE_Array.h:427
auto sbegin()
return begin stride iterator for writing
Definition: CORE_Array.h:158
I & operator*=(const T &v)
multiplicator operator
Definition: CORE_Array.h:434
Self & operator=(const CORE_Array< Q, I1 > &cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:266
void swap(const tIndex &i, const tIndex &j)
swap the two elements of the array
Definition: CORE_Array.h:572
Self & operator=(const T &v)
fill the values of the array with v
Definition: CORE_Array.h:223
virtual void setUniformRandomizeSeed(const tULLInt &seed)
set the uniform randomize seed
Definition: CORE_Array.h:386
Self & operator=(const std::vector< Q > &values)
build an array by a copy of c
Definition: CORE_Array.h:258
Self & operator=(const std::array< Q, N > &values)
build an array by a copy of c
Definition: CORE_Array.h:239
Self & operator=(CORE_Array< Q, I1 > &&cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:281
I & operator*=(const CORE_Array< Q, I1 > &v)
array multiply operator
Definition: CORE_Array.h:511
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:127
CORE_Array()
instanciation method of a class
Definition: CORE_Array.h:103
void sum(T &s) const
Computes the sum of all the elements.
Definition: CORE_Array.h:622
const T & get(const tIndex &i) const
get the i-th element for reading
Definition: CORE_Array.h:192
I & operator/=(const CORE_Array< Q, I1 > &v)
array divisor operator
Definition: CORE_Array.h:518
void transform(LambdaFct &&F)
transform the transform element with the lambda function Ti = F(Ti)
Definition: CORE_Array.h:532
void initialize(const T &v)
randomize the field
Definition: CORE_Array.h:378
tReal linfDistance(const CORE_Array< Q, I1 > &X, tIndex &i) const
compute the Linfinitty norm
Definition: CORE_Array.h:684
auto send()
return end N-stride iterator for writing
Definition: CORE_Array.h:165
T * getValues()
get the values of the array
Definition: CORE_Array.h:212
T & get(const tIndex &i)
get the i-th element for writting
Definition: CORE_Array.h:198
void copy(const CORE_Array< Q, I1 > &cpy)
copy the container
Definition: CORE_Array.h:358
void copy(CORE_Array< Q, I1 > &&cpy)
copy the conatiner : mv is destroyed after this
Definition: CORE_Array.h:366
tReal l2Norm() const
compute the L2 norm
Definition: CORE_Array.h:652
tReal l2Norm2() const
compute the L2 norm squared
Definition: CORE_Array.h:645
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:552
void copy(const tIndex &n, const std::valarray< Q > &vs)
initialize the array to the values of val array
Definition: CORE_Array.h:323
void uniformRandomize(const T &min, const T &max)
randomize the field
Definition: CORE_Array.h:407
void prod(T &p) const
return the produc of all the elements
Definition: CORE_Array.h:628
void copy(const tIndex &n, const std::vector< Q > &vs)
initialize the array to the values of vector
Definition: CORE_Array.h:333
tBoolean isNANContained() const
return true if one value is Not A Number
Definition: CORE_Array.h:607
I & operator%=(const T &v) requires functions_type
modulo operator
Definition: CORE_Array.h:451
void copy(const tIndex &n, const Q *vs)
initialize the array to the values of pointer of size n
Definition: CORE_Array.h:304
void normalize()
normalize the array
Definition: CORE_Array.h:579
void setUniformRandomizeSeed()
set the uniform randomize seed
Definition: CORE_Array.h:391
Self & operator=(Self &&cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:288
const T * getValues() const
get the values of the array for reading
Definition: CORE_Array.h:206
constexpr auto csbegin() const
return begin N-stride const iterator for writing
Definition: CORE_Array.h:173
void transform(LambdaFct &&F, const Self &X)
transform the transform element with the lambda function Ti = F(Xi)
Definition: CORE_Array.h:541
I & operator-=(const CORE_Array< Q, I1 > &v)
array sub operator
Definition: CORE_Array.h:504
constexpr auto csend() const
return end N-stride const iterator for writing
Definition: CORE_Array.h:180
Self & operator=(const std::valarray< Q > &values)
build an array by a copy of c
Definition: CORE_Array.h:249
tReal l2Distance2(const CORE_Array< Q, I1 > &X) const
compute the L2 distance squared
Definition: CORE_Array.h:660
void copy(const tIndex &n, const std::initializer_list< T > &vs)
initialize the array to the values of list
Definition: CORE_Array.h:342
tReal linfNorm(tIndex &i) const
compute the L infinity norm
Definition: CORE_Array.h:676
I & operator+=(const T &v)
add operator
Definition: CORE_Array.h:420
I & operator/=(const T &v)
divisor operator
Definition: CORE_Array.h:441
void axpy(const Q &alpha, const CORE_Array< Q, I1 > &X, const T &beta)
compute This=beta.This+ alpha .X
Definition: CORE_Array.h:593
void swap(Self &a)
swap the contents of the array
Definition: CORE_Array.h:565
virtual ~CORE_Array()
destroy an instance of class
Definition: CORE_Array.h:111
T & scalarProduct(const CORE_Array< Q, I1 > &X, T &s) const
return the scalar product
Definition: CORE_Array.h:637
void uniformRandomize()
randomize the field
Definition: CORE_Array.h:397
Self & operator=(const Self &cpy)
build an array by a copy of cpy
Definition: CORE_Array.h:273
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:314
void copy(const tIndex &n, std::initializer_list< T > &&vs)
initialize the array to the values of list
Definition: CORE_Array.h:349
Self & operator=(std::initializer_list< T > values)
build an array by a copy of c
Definition: CORE_Array.h:230
tReal l2Distance(const CORE_Array< Q, I1 > &X) const
compute the L2 distance
Definition: CORE_Array.h:668
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
virtual tString toString() const override
return the string representation of the object node
Definition: CORE_Collection.h:257
this class describes a const iterator with a constant stride
Definition: CORE_ConstantStrideIterator.h:12
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
this class describes a const iterator with a constant stride 2 templates parameter:
Definition: CORE_StrideIterator.h:12