C++ mpi module for stochmagnet_main Package
CORE_StdPtrField.h
1 #ifndef CORE_StdPtrField_H
2 #define CORE_StdPtrField_H
3 
4 //inherited header
5 #include "CORE_Field.h"
6 
7 //corresponding array header
8 #include "CORE_StdPtrArray.h"
9 
16 template <typename T,typename K,K D>
17 class CORE_StdPtrField : public CORE_Field<T,K,D,CORE_StdPtrArray<T>,CORE_StdPtrField<T,K,D>> {
18 
19 private:
20 
21  //This class type
23 
24 
25 
26  // CONSTRUCTORS
27 public:
31  }
32 
33 
34 
35  // DESTRUCTORS
38  virtual ~CORE_StdPtrField() {
39  }
40 
41 
42 private:
43 
44 
45 
46 
47  //MEMORY
48  //=====
49 public:
57  virtual tMemSize getMemorySize() const override {
58  return sizeof(*this)+this->getContentsMemorySize();
59  }
60 
61  //allocation methods
62  //===================
63 
64 public:
68  static inline CORE_UniquePointer<Self> New() {
69  return CORE_UniquePointer<Self>(new Self(),CORE_Object::Delete());
70  }
71 
72 
73 
74 
75  //accessor operators
76  //====================
77 
78 
79  //accessor iterator
80  //=================
81 
82 
83  //accessor methods
84  //=================
85 
86 
87  //assignment operators
88  //====================
92  inline Self& operator=(const T& v) {
93  this->initialize(v);
94  return *this;
95  }
99  inline Self& operator=(std::initializer_list<T>&& values) {
100  this->copy(values);
101  return *this;
102  }
106  inline Self& operator=(const std::initializer_list<T>& values) {
107  this->copy(values);
108  return *this;
109  }
110 
116  template<size_t N,typename Q>
117  inline Self& operator=(const std::array<Q,N>& values) {
118  this->copy(values);
119  return *this;
120 
121  }
122 
127  template<typename Q>
128  inline Self& operator=(const std::valarray<Q>& values) {
129  this->copy(values);
130  return *this;
131  }
132 
137  template<typename Q>
138  inline Self& operator=(const std::vector<Q>& values) {
139  this->copy(values);
140  return *this;
141  }
142 
146  inline Self& operator=(const Self& values) {
147  this->copy(values);
148  return *this;
149  }
153  inline Self& operator=(Self&& values) {
154  this->copy(values);
155  return *this;
156  }
157 
164  template<typename Q,class S1,class I1>
165  inline Self& operator=(const CORE_Field<Q,K,D,S1,I1>& values) {
166  this->copy(values);
167  return *this;
168  }
175  template<typename Q,class S1,class I1>
177  copy(values);
178  return *this;
179  }
180 
181 
182 
183 
184  //initializers methods
185  //====================
186 
191  template<typename Q>
192  inline void initialize(const Q& v) {
193  this->getStorage().initialize(v);
194  }
199  template<typename Q>
200  inline void initialize(const std::array<Q,D>& a) {
201  //begin iterator on a
202  const Q* iA=a.data();
203  //end iterator on a
204  const Q *eA=iA+D;
205 
206  //begin iterator on values
207  T* iV=this->getStorage().getValues();
208 
209  //end iterator on values
210  const T* eV=iV+this->getSize();
211 
212  while (iV!=eV) {
213  iA=a.data();
214  while (iA!=eA) {
215  (*iV)=(*iA);
216  iV++;
217  iA++;
218  }
219 
220  }
221  }
229  template<typename Q>
230  inline tBoolean setSharedValues(const tIndex& capacity,const tIndex& size,Q* values) {
231  return this->getStorage().setSharedValues(capacity,size,values);
232  }
233 
242  template<typename Q>
243  inline tBoolean setSharedValues(const tIndex& capacity,Q* values) {
244  return setSharedValues(capacity,capacity,values);
245 
246  }
253  template<typename Q,class I1>
254  inline tBoolean setSharedValues(CORE_PtrArray<Q,I1>& array) {
255  return setSharedValues(array.mCapacity,array.getSize(),array.getValues());
256  }
257 
258 
259 
260  //compound assignement operators (+=,-=,*=,/=,^=...)
261  //===================================================
262 
263  //arithmetic type compound operators
264  //====================================
268  inline Self& operator+=(const T& v) {
269  this->getStorage()+=v;
270  return (*this);
271  }
275  inline Self& operator-=(const T& v) {
276  this->getStorage()-=v;
277  return (*this);
278 
279  }
280 
284  inline Self& operator*=(const T& v) {
285  this->getStorage()*=v;
286  return (*this);
287 
288  }
292  inline Self& operator/=(const T& v) {
293  this->getStorage()/=v;
294  return (*this);
295  }
296 
297  //integer type compond operators
298  //===============================
302  inline Self& operator%=(const T& v) requires functions_type::isIntegerType<T>{
303  this->getStorage()%=v;
304  return (*this);
305  }
306 
310  inline Self& operator&=(const T& v) requires functions_type::isIntegerType<T>{
311  this->getStorage()&=v;
312  return (*this);
313  }
314 
318  inline Self& operator|=(const T& v) requires functions_type::isIntegerType<T>{
319  this->getStorage()|=v;
320  return (*this);
321  }
322 
326  inline Self& operator^=(const T& v) requires functions_type::isIntegerType<T> {
327  this->getStorage()^=v;
328  return (*this);
329  }
333  inline Self& operator<<=(const T& v) requires functions_type::isIntegerType<T> {
334  this->getStorage()<<=v;
335  return (*this);
336  }
337 
341  inline Self& operator>>=(const T& v) requires functions_type::isIntegerType<T> {
342  this->getStorage()>>=v;
343  return (*this);
344  }
345 
346  //class type compound operatos
347  //=============================
348 
355  template<class Q,class S,class I>
356  inline Self& operator+=(const CORE_Field<Q,K,D,S,I>& X) {
357  this->getStorage()+=X.getStorage();
358  return (*this);
359  }
366  template<class Q,class S,class I>
368  this->getStorage()-=X.getStorage();
369  return (*this);
370  }
377  template<class Q,class S,class I>
379  //T=F(T,V)
380  this->getStorage()*=X.getStorage();
381  return (*this);
382  }
389  template<class Q,class S,class I>
391  this->getStorage()/=X.getStorage();
392  return (*this);
393  }
394 
395 
396  //transform methods
397  //=================
398 
399 
400 
406  template<typename LambdaFct>
407  static inline void ElementsTransform(LambdaFct&& F,
408  Self& X) {
409 
410  std::transform(X.sbegin(),X.send(),
411  X.sbegin(),F);
412 
413  }
414 
418  template<typename LambdaFct>
419  inline void elementsTransform(LambdaFct&& F) {
420  this->ElementsTransform(F,(*this));
421  }
422 
423 
424 
429  inline void normalize() {
430  Normalize(this->getSize(),this->getValues());
431  }
432 
439  inline static void Normalize(const tIndex& n,T* values) {
440 
441  //iterator on values
442  T* iV=values;
443 
444  //end iterator on values
445  T *iVe=values+n;
446 
447  //iterato on corrdinates of elements
448  T* iVd=null;
449 
450  //end iterator on values for a D length
451  T *iVde=null;
452 
453  T invNi;//inverse of the norm of the field Vi
454 
455  //error to detect null
456  T eps=std::numeric_limits<T>::epsilon();
457 
458  while (iV!=iVe) {
459 
460 
461 
462  //invNi=\sum_{d=0}^{D-1} V_{i,d}
463  invNi=0;
464  iVd=iV;//begin of loop of size D
465  iVde=iV+D;//end of loop on size D
466  while (iVd!=iVde) {
467  invNi+=(*iVd)*(*iVd);
468  iVd++;
469  }
470  if (invNi>eps) {
471  //not null value
472  invNi=1./sqrt(invNi);
473  }
474  //normalize vector i
475  while (iV!=iVde) {
476  (*iV)*=invNi;
477  iV++;
478  }
479  }//end loop on elements
480  }
481 
482 
483 
484  //Data consistency
485  //=================
486 
487 
488  //transform methods
489  //==================
490 
491 
492  //algebric methods
493  //================
494 
495 
496 
501  template<class I>
502  inline void mod2(CORE_Array<T,I>& X) const {
503  //number of elements = size() / D
504  tInteger n=this->getElementsNumber();
505 
506  //resize X to n
507  X.setSize(n);
508 
509  //start ietrator on values
510  const T* iV=this->getValues();
511 
512  //end iterator on values
513  const T* iVe=iV+n*D;
514 
515  //end iterator on values on dimension
516  const T* iVde=null;
517 
518  //start iterator on X
519  T* iX=&X[0];
520 
521  while (iV!=iVe) {
522 
523  //loop only on D elements
524  iVde=iV+D;
525  //Xi=0
526  (*iX)=0;
527  //Xi=\sum_d Vi^2
528  while (iV!=iVde) {
529  (*iX)+=(*iV)*(*iV);
530  iV++;
531  }
532  iX++;
533  }
534 
535  }
536 
545  template<class Q,class S1,class I1>
546  inline T& scalarProduct(const CORE_Field<Q,K,D,S1,I1>& X,T& s) const {
547  return this->getStorage().scalarProduct(X.getStorage(),s);
548  }
549 
559  template<class Q,class S1,class I1>
560  inline T& scalarProduct(const std::valarray<Q>& weights,const CORE_Field<Q,K,D,S1,I1>& X,T& s) const {
561  //number of elements
562  tIndex n=this->getElementsNumber();
563 
564 
565 
566 
567  if (weights.size()==0) {
569  }
570 
571  //increment of W
572  tBoolean incW=(weights.size()>1);
573  //values of W
574  const tReal *iWs=&weights[0];
575 
576  //values of this
577  const T* iYs=this->getValues();
578  const T* iYe=iYs+n*D;
579  const T* iYd=null;
580 
581  //values of X
582  const Q* iXs=X.getValues();
583  tBoolean incX=(X.getElementsNumber()>1);
584  const Q* iXd=null;
585 
586  //s=sum_i Xi Yi
587  s=0;
588  while (iYs!=iYe) {//loop on elements
589  iYd=iYs+D;
590  iXd=iXs;
591  while(iYs!=iYd) {//loop on D
592  s+=(*iWs)*(*iXd)*(*iYs);
593  iYs++;
594  iXd++;
595  }//end loop on D
596  iWs+=incW;
597  iXs+=incX*D;
598  }//end loop on element
599 
600  return s;
601  }
602 
603 
604 
605 };
606 
607 //defualt dimension
608 #ifndef DIMENSION_TYPE
609 #define DIMENSION_TYPE
610 typedef tUCInt tDimension;//d in [0,256[
611 #endif
612 
614 
615 
616 
617 //default field
618 #ifndef DEFAULT_FIELD
619 #define DEFAULT_FIELD
621 #endif
622 
623 #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
void setSize(const tIndex &n)
set the size of the container
Definition: CORE_Collection.h:104
this class describes an field. A field is composed by
Definition: CORE_Field.h:49
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_Field.h:102
void copy(const tIndex &n, const Q *vs)
initialize the field to the values of pointer of size n
Definition: CORE_Field.h:447
const T * getValues() const
get the values of the array for reading
Definition: CORE_Field.h:323
auto sbegin()
return begin stride iterator for writing
Definition: CORE_Field.h:276
auto send()
return end N-stride iterator for writing
Definition: CORE_Field.h:282
tIndex getElementsNumber() const
return the number values of the container
Definition: CORE_Field.h:135
tIndex getSize() const
return the number values of the container
Definition: CORE_Field.h:161
const CORE_StdPtrArray< T > & getStorage() const
get the storage
Definition: CORE_Field.h:336
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
this class describes an arithmetic array type implemented with as a pointer allocation object with im...
Definition: CORE_PtrArray.h:69
const T * getValues() const
get the values of the array for reading
Definition: CORE_PtrArray.h:357
tIndex getSize() const
return the size of the array for writing
Definition: CORE_PtrArray.h:155
this class describes a standart arithmetic array type implemented with a memory allocation with type ...
Definition: CORE_StdPtrArray.h:13
this class describes a standart arithmetic array type implemented with a CORE_StdPtrArray object
Definition: CORE_StdPtrField.h:17
static CORE_UniquePointer< Self > New()
return a new unique pointer of this
Definition: CORE_StdPtrField.h:68
void normalize()
normalize all the elements of the field return false if the method is not compatible with the floatin...
Definition: CORE_StdPtrField.h:429
Self & operator+=(const T &v)
add operator
Definition: CORE_StdPtrField.h:268
Self & operator=(const std::valarray< Q > &values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:128
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_StdPtrField.h:57
T & scalarProduct(const std::valarray< Q > &weights, const CORE_Field< Q, K, D, S1, I1 > &X, T &s) const
return the scalar product
Definition: CORE_StdPtrField.h:560
virtual ~CORE_StdPtrField()
destroy an array of T*
Definition: CORE_StdPtrField.h:38
Self & operator-=(const T &v)
sub operator
Definition: CORE_StdPtrField.h:275
CORE_StdPtrField()
build an array of T*
Definition: CORE_StdPtrField.h:30
T & scalarProduct(const CORE_Field< Q, K, D, S1, I1 > &X, T &s) const
return the weight scalar product
Definition: CORE_StdPtrField.h:546
void elementsTransform(LambdaFct &&F)
apply the transform element with the lambda function Xid = F(Xid)
Definition: CORE_StdPtrField.h:419
Self & operator*=(const T &v)
multiplicator operator
Definition: CORE_StdPtrField.h:284
Self & operator=(Self &&values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:153
static void ElementsTransform(LambdaFct &&F, Self &X)
apply the transform element with the lambda function Ri = F(Xi)
Definition: CORE_StdPtrField.h:407
Self & operator=(CORE_Field< Q, K, D, S1, I1 > &&values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:176
Self & operator=(const Self &values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:146
void initialize(const Q &v)
initailize all the values with v
Definition: CORE_StdPtrField.h:192
Self & operator/=(const T &v)
divisor operator
Definition: CORE_StdPtrField.h:292
tBoolean setSharedValues(const tIndex &capacity, const tIndex &size, Q *values)
set shared values with the capacity and size
Definition: CORE_StdPtrField.h:230
void initialize(const std::array< Q, D > &a)
copy at element element the array of size D
Definition: CORE_StdPtrField.h:200
Self & operator=(const std::array< Q, N > &values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:117
Self & operator/=(const CORE_Field< Q, K, D, S, I > &X)
array divisor operator
Definition: CORE_StdPtrField.h:390
tBoolean setSharedValues(const tIndex &capacity, Q *values)
set shared values with the capacity and size
Definition: CORE_StdPtrField.h:243
Self & operator*=(const CORE_Field< Q, K, D, S, I > &X)
array multiply operator
Definition: CORE_StdPtrField.h:378
Self & operator=(const T &v)
fill the values of the array with v
Definition: CORE_StdPtrField.h:92
Self & operator=(std::initializer_list< T > &&values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:99
Self & operator%=(const T &v) requires functions_type
modulo operator
Definition: CORE_StdPtrField.h:302
Self & operator=(const std::vector< Q > &values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:138
static void Normalize(const tIndex &n, T *values)
normalize all the elements of the field
Definition: CORE_StdPtrField.h:439
Self & operator=(const CORE_Field< Q, K, D, S1, I1 > &values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:165
Self & operator=(const std::initializer_list< T > &values)
build an array by a copy of c
Definition: CORE_StdPtrField.h:106
Self & operator-=(const CORE_Field< Q, K, D, S, I > &X)
array sub operator
Definition: CORE_StdPtrField.h:367
tBoolean setSharedValues(CORE_PtrArray< Q, I1 > &array)
share the values of array
Definition: CORE_StdPtrField.h:254
void mod2(CORE_Array< T, I > &X) const
return the norm2 array per each element
Definition: CORE_StdPtrField.h:502