C++ mpi module for stochmagnet_main Package
CORE_VectorList.h
1 #ifndef CORE_VectorList_H
2 #define CORE_VectorList_H
3 
4 //inherited class header
5 #include "CORE_List.h"
6 
7 
11 template <typename T,class I>
12 class CORE_VectorList : public CORE_List<T,tIndex,I> {
13 
14 
15 private:
16 
17  typedef CORE_VectorList<T,I> Self;
18 
19  std::vector<T> mValues;
20 
21 
22  // CONSTRUCTORS
23 protected:
27 
28  }
31  CORE_VectorList(const tIndex& n) : mValues(n){
32 
33  }
34 
35  // DESTRUCTORS
38  virtual ~CORE_VectorList() {
39  }
40 
41 
42 public:
43 
44  //memory method
45  //=============
46 
54  virtual tMemSize getMemorySize() const override {
55  return sizeof(*this)+this->getContentsMemorySize();
56  }
65  virtual tMemSize getContentsMemorySize() const override {
67  mem+=mValues.capacity()*sizeof(T);
68  return mem;
69  }
70 
71  //allocation methods
72  //===================
73 
79  inline void setSize(const tIndex& n) {
80  mValues.resize(n);
81  };
82 
86  inline tIndex getSize() const {
87  return mValues.size();
88  }
89 
90 
91 
92  //accessor operators
93  //====================
94 public:
98  inline const T& operator[](const tIndex& i) const {
99 #ifdef DEBUG
100  if (i>=mValues.size()) {
101  throw CORE_Exception("core",
102  "CORE_VectorList",
103  "CORE_VectorList["+std::to_string(i)+"] is out of bounds [0,"+std::to_string(mValues.size())+"[ for reading");
104  }
105 #endif
106  return mValues[i];
107  };
111  inline T& operator[](const tIndex& i) {
112 #ifdef DEBUG
113  if (i>=mValues.size()) {
114  throw CORE_Exception("core",
115  "CORE_VectorList",
116  "CORE_VectorList["+std::to_string(i)+"] is out of bounds [0,"+std::to_string(mValues.size())+"[ for writing");
117  }
118 #endif
119  return mValues[i];
120  };
121 
122 
123 
124 
125  //acessor iterators
126  //==================
130  inline constexpr auto cbegin() const {
131  return mValues.cbegin();
132  }
136  inline constexpr auto cend() const {
137  return mValues.cend();
138  }
142  inline auto begin() {
143  return mValues.begin();
144  }
148  inline auto end() {
149  return mValues.end();
150  }
151 
155  inline auto rbegin() {
156  return mValues.rbegin();
157  }
161  inline auto rend() {
162  return mValues.rend();
163  }
164 
168  inline constexpr auto crbegin() const {
169  return mValues.crbegin();
170  }
174  inline constexpr auto crend() const {
175  return mValues.cend();
176  }
177 
178 
179 
180 
181 public:
182 
183  //NEW Instance
184  //============
185 
186 
187 
188 
189  //allocation methods
190  //===================
191 
192 
193 
194  //assignment operators
195  //====================
196 
197 
198 
199  //copy methods
200  //============
201 
205  inline void copy(const Self& cpy) {
206  const Self* selfCpy=dynamic_cast<const Self*>(&cpy);
207  if (selfCpy!=null) {
208  copy(*selfCpy);
209  } else {
210  copy(cpy.getSize(),cpy.getValues());
211  }
212  }
213 
217  template<typename Q,class I1>
218  inline void copy(Self&& cpy) {
219  const Self *selfCpy=dynamic_cast<const Self*>(&cpy);
220  if (selfCpy!=null) {
221  copy(*selfCpy);
222  } else {
223  copy(cpy.size(),cpy.getValues());
224  }
225  }
226 
227 
231  inline void copy(const std::initializer_list<T>& vs) {
232  Copy(vs,mValues);
233  }
234 
238  inline void copy(std::initializer_list<T>&& vs) {
239  Copy(vs,mValues);
240  }
241 
242 
243 
244 
251  inline static void Copy(const std::valarray<T>& X,std::vector<T>& R) {
252  Copy(X.size(),*X[0],R);
253  }
254 
255 
261  inline static void Copy(const std::initializer_list<T>& X,std::vector<T>& R) {
262  R=X;
263  }
269  inline static void Copy(std::initializer_list<T>&& X,std::vector<T>& R) {
270  R=X;
271  }
272 
273 
280  template<class Q>
281  inline static void Copy(const std::vector<Q>& X,std::vector<T>& R) {
282  if (R.size()!=X.size()) R.resize(X.size());
283  //copy the values
284  auto iX=X.cbegin() ;//begin iterator of This Values
285  for(auto &Ri : R) {
286  Ri=(*iX);
287  iX++;
288  }
289  }
290 
297  inline static void Copy(const std::vector<T>& X,std::vector<T>& R) {
298  R=X;
299  }
300 
308  template<typename Q>
309  inline static void Copy(const tIndex& n,const Q* X,std::vector<T>& R) {
310  if (R.size()!=n) R.resize(n);
311  //copy the values
312  const Q *iX=X;//begin iterator of This Values
313  for(auto &Ri : R) {
314  Ri=(*iX);
315  iX++;
316  }
317  }
318 
319 
320 
321  //initializers methods
322  //====================
323 
324 
325 
326  //accessor methods
327  //===================
328 
332  inline std::vector<T>& getValues() {
333  return mValues;
334  }
335 
339  inline const std::vector<T>& getValues() const {
340  return mValues;
341  }
342 
343 
344 
345 public:
346 
347 
348  //Data consistency
349  //=================
350 
354  inline tString getDataTypeName() const {
355  return functions_type::getTypeName<T>();
356  }
357 
358  //transformation methods
359  //=======================
360 
361 
365  template<typename LambdaFct>
366  inline void transform(LambdaFct&& F) {
367  std::transform(this->cbegin(),this->cend(),this->begin(),F);
368  }
369 
374  template<typename LambdaFct>
375  inline void transform(LambdaFct&& F,
376  const Self& X) {
377  this->setSize(X.getSize());
378  std::transform(X.cbegin(),X.cend(),
379  this->begin(),F);
380  }
381 
387  template<typename LambdaFct>
388  inline void transform(LambdaFct&& F,
389  const Self& X,
390  const Self& Y) {
391  if (X.getSize()!=Y.getSize())
392  throw CORE_Exception("core",
393  "CORE_VectorList::Transform(F,X,Y,R)",
394  "X ("+std::to_string(X.getSize())+") & Y ("+std::to_string(Y.getSize())+") have not same size");
395  this->setSize(X.getSize());
396 
397  std::transform(X.cbegin(),X.cend(),
398  Y.cbegin(),
399  this->begin(),F);
400  }
401 
402 
403 
407  inline void directionalSort(const tUChar& order) {
408  switch(order) {
409  case 'i':
410  std::sort(this->begin(),this->end(),std::less<tString>());
411  break;
412  case 'd':
413  std::sort(this->begin(),this->end(),std::greater<tString>());
414  break;
415  }
416 
417  }
418 
419 
420 
421 
422  //string representation
423  //=====================
424 
425 
426 };
427 
428 
429 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
this class describes a list K -> T whre I is the implemented class
Definition: CORE_List.h:12
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
this class describes a list K -> T whre I is the implemented class
Definition: CORE_VectorList.h:12
tString getDataTypeName() const
get the type of data
Definition: CORE_VectorList.h:354
static void Copy(const std::vector< T > &X, std::vector< T > &R)
copy the array
Definition: CORE_VectorList.h:297
tIndex getSize() const
return the size of the container
Definition: CORE_VectorList.h:86
constexpr auto crend() const
return reverse end iterator for reading
Definition: CORE_VectorList.h:174
static void Copy(const tIndex &n, const Q *X, std::vector< T > &R)
copy the array
Definition: CORE_VectorList.h:309
static void Copy(const std::vector< Q > &X, std::vector< T > &R)
copy the array R=X
Definition: CORE_VectorList.h:281
static void Copy(std::initializer_list< T > &&X, std::vector< T > &R)
copy the array R=X
Definition: CORE_VectorList.h:269
void copy(std::initializer_list< T > &&vs)
initialize the array to the values of list
Definition: CORE_VectorList.h:238
const std::vector< T > & getValues() const
return the values of the list
Definition: CORE_VectorList.h:339
const T & operator[](const tIndex &i) const
get the i-th element for reading. Do not verify the bounds
Definition: CORE_VectorList.h:98
virtual tMemSize getMemorySize() const override
return the memory size of the class and the memory size of all its attributes/associations
Definition: CORE_VectorList.h:54
void directionalSort(const tUChar &order)
sort the element
Definition: CORE_VectorList.h:407
void setSize(const tIndex &n)
set the size of the container
Definition: CORE_VectorList.h:79
void copy(const Self &cpy)
copy the container
Definition: CORE_VectorList.h:205
void copy(const std::initializer_list< T > &vs)
initialize the array to the values of list
Definition: CORE_VectorList.h:231
auto rbegin()
return reverse begin iterator for writing
Definition: CORE_VectorList.h:155
auto begin()
return begin iterator for writing
Definition: CORE_VectorList.h:142
std::vector< T > & getValues()
return the values of the list
Definition: CORE_VectorList.h:332
constexpr auto crbegin() const
return reverse begin iterator for reading
Definition: CORE_VectorList.h:168
virtual ~CORE_VectorList()
destroy the instance of class
Definition: CORE_VectorList.h:38
T & operator[](const tIndex &i)
get the i-th element for writting. Do not verify the bounds
Definition: CORE_VectorList.h:111
CORE_VectorList()
build a list
Definition: CORE_VectorList.h:26
auto end()
return end iterator for writing
Definition: CORE_VectorList.h:148
constexpr auto cend() const
return end iterator for reading
Definition: CORE_VectorList.h:136
static void Copy(const std::initializer_list< T > &X, std::vector< T > &R)
copy the array R=X
Definition: CORE_VectorList.h:261
void transform(LambdaFct &&F)
transform the transform element with the lambda function Ti = F(Ti)
Definition: CORE_VectorList.h:366
static void Copy(const std::valarray< T > &X, std::vector< T > &R)
copy the array
Definition: CORE_VectorList.h:251
constexpr auto cbegin() const
return begin iterator for reading
Definition: CORE_VectorList.h:130
void copy(Self &&cpy)
copy the conatiner : mv is destroyed after this
Definition: CORE_VectorList.h:218
void transform(LambdaFct &&F, const Self &X)
transform the transform element with the lambda function Ti = F(Xi)
Definition: CORE_VectorList.h:375
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_VectorList.h:65
CORE_VectorList(const tIndex &n)
build a list of size n
Definition: CORE_VectorList.h:31
auto rend()
return reverse end iterator for writing
Definition: CORE_VectorList.h:161
void transform(LambdaFct &&F, const Self &X, const Self &Y)
transform the transform element with the lambda function Ti = F(Xi,Yi)
Definition: CORE_VectorList.h:388