C++ mpi module for stochmagnet_main Package
CORE_PtrArray.h
1 #ifndef CORE_PtrArray_H
2 #define CORE_PtrArray_H
3 
4 #include "CORE_Array.h"
5 
6 //numerics function header
7 #include "functions_numeric.h"
8 
9 
68 template <typename T,class I>
69 class CORE_PtrArray : public CORE_Array<T,I> {
70 
71 
72 private:
73 
74  //values of the array
75  T *mValues;
76  //capacity of the array
77  tIndex mCapacity;
78  //usefull size of the array
79  tIndex mSize;
80  //indicates if values is a shared pointer : if true the values is only deleted in the class where the mIsSharedValeus is false
81  tBoolean mIsSharedValues;
82 
83  // CONSTRUCTORS
84 public:
88  mValues=null;
89  mCapacity=0;
90  mSize=0;
91  mIsSharedValues=false;
92  }
93 
94 
95 
96 
97  // DESTRUCTORS
100  virtual ~CORE_PtrArray() {
101  desallocate();
102  }
103 
104 
105 private:
106 
107 
108 
109 
110  //MEMORY
111  //=====
112 public:
120  virtual tMemSize getMemorySize() const override {
121  return sizeof(*this)+this->getContentsMemorySize();
122  }
123 
132  virtual tMemSize getContentsMemorySize() const override {
134  mem+=mCapacity*sizeof(T);
135  return mem;
136  }
137 
138  //allocation methods
139  //===================
140 public:
146  inline void setSize(const tIndex& n) {
147  if (n>=mCapacity) allocate(n,false);
148  mSize=n;
149  };
155  inline tIndex getSize() const {
156  return mSize;
157  };
158 
159 
162  inline void resize(const tIndex& n) {
163  if (mSize!=n) allocate(n,true);
164  mSize=n;
165  }
166 
167 
171  inline void fitToSize(tInteger size) {
172  if (size!=mCapacity) {
173  allocate(size,true);
174  }
175  mSize=size;
176  }
177 
178 private:
179 
182  inline void allocate(const tIndex& n,const tBoolean& areOldValuesCopied) {
183  //nothink to do
184  if (n==mCapacity) return;
185 
186 
187  //allocate the new values
188  T* newValues=newAllocation(n);
189 
190 
191  if (areOldValuesCopied) {
192  //copy the old values in indices [0,oldSize[ and set to 0 in [oldSize,n[
193  tIndex oldSize=mSize;
194  const T* oldValues=mValues;
195  //number of old values to copy
196  tIndex m=functions_numeric::min(oldSize,n);
197  //copy a block of memory of size m
198  if (m>0) memcpy(newValues,oldValues,m*sizeof(T));
199  //init to 0 al values in [m,n]
200  if (n>m) memset(&newValues[m],0,(n-m)*sizeof(T));
201  }
202 
203  //delete the old values
204  desallocate();
205 
206  //update the class attributes
207  mValues=newValues;
208  mIsSharedValues=false;
209  mCapacity=n;
210 
211  }
212 protected:
215  inline void desallocate() {
216  if ( !mIsSharedValues && (mValues!=null)) {
217  deleteAllocation(mValues);
218  if (mValues!=null) {
219  throw CORE_Exception("core",
220  "CORE_PtrArray::desallocate()",
221  "impossible to desallocate the memory");
222  }
223  mIsSharedValues=false;
224  mCapacity=0;
225  mSize=0;
226  }
227  }
228 
229 protected:
230 
235  virtual T* newAllocation(const tIndex &n) const {
236  try {
237  //allocate with an element more for iteration with pointer
238  T* newValues=new T[n];
239  if (newValues==null) {
240  throw CORE_Exception("core",
241  "CORE_PtrArray::allocate("+std::to_string(n)+")",
242  "not enought memory for allocation");
243  }
244  return newValues;
245  } catch(std::exception& e) {
246  throw CORE_Exception("core",
247  "CORE_PtrArray::allocate("+std::to_string(n)+")",
248  "not enought memory for allocation");
249  }
250 
251  return null;
252  }
253 
257  virtual void deleteAllocation(T*& mem) const {
258  if (mem!=null) delete[] mem;
259  mem=null;
260  }
261 
262 
263  //accessor operators
264  //====================
265 public:
269  inline const T& operator[](const tIndex& i) const {
270 #ifdef DEBUG
271  if (i>=mSize) {
272  throw CORE_Exception("core",
273  "CORE_PtrArray[]",
274  "CORE_PtrArray["+std::to_string(i)+"] is out of bounds [0,"+std::to_string(mSize)+"[ for reading");
275  }
276 #endif
277  return mValues[i];
278  };
282  inline T& operator[](const tIndex& i) {
283 #ifdef DEBUG
284  if (i>=mSize) {
285  throw CORE_Exception("core",
286  "CORE_PtrArray[]",
287  "CORE_PtrArray["+std::to_string(i)+"] is out of bounds [0,"+std::to_string(mSize)+"[ for writing ");
288  }
289 #endif
290  return mValues[i];
291  };
292 
293 
294  //iterator accessor
295  //=================
296 public:
300  inline constexpr auto cbegin() const {
301  return CORE_ConstantStrideIterator<T*,1>(mValues);
302  }
306  inline constexpr auto cend() const {
307  return CORE_ConstantStrideIterator<T*,1>(mValues+mSize);
308  }
312  inline auto begin() {
313  return CORE_StrideIterator<T*,1>(mValues);
314  }
318  inline auto end() {
319  return CORE_StrideIterator<T*,1>(mValues+mSize);
320  }
321 
325  inline auto rbegin() {
326  return CORE_StrideIterator<T*,-1>(mValues+mSize-1);
327  }
331  inline auto rend() {
332  return CORE_StringIterator<T*,-1>(mValues-1);
333  }
334 
338  inline constexpr auto crbegin() const {
339  return CORE_ConstantStrideIterator<T*,-1>(mValues+mSize-1);
340  }
344  inline constexpr auto crend() const {
345  return CORE_ConstantStrideIterator<T*,-1>(mValues-1);
346 
347  }
348 
349  //accessor methods
350  //================
351 public:
352 
353 
357  inline const T* getValues() const {
358  return mValues;
359  }
363  inline T* getValues() {
364  return mValues;
365  }
366 
367 
368 
369 
370 
371  //assignment operators
372  //====================
373 
374 
375  //copy methods
376  //============
377 
378 
379  //Initializer methods
380  //==================
381 public:
389  template<typename Q>
390  inline tBoolean setSharedValues(const tIndex& capacity,const tIndex& size,Q* values) {
391  if (sizeof(T)!=sizeof(Q)) return false;
392 
393  desallocate();
394  mValues=(T*) values;
395  mSize=size;
396  mCapacity=capacity;
397  mIsSharedValues=true;
398  return true;
399  }
400 
401 
410  template<typename Q>
411  inline tBoolean setSharedValues(const tIndex& capacity,Q* values) {
412  return setSharedValues(capacity,capacity,values);
413 
414  }
419  template<typename Q,class I1>
420  inline tBoolean setSharedValues(CORE_PtrArray<Q,I1>& array) {
421  return setSharedValues(array.mCapacity,array.getSize(),array.getValues());
422  }
423 
424 
425  //compound assignement operator
426  //=============================
427 
428 
429  //transform methods
430  //==================
431 
432 
433 
439  template<class I1>
440  inline void swap(CORE_PtrArray<T,I1>& a) {
441  std::swap(mValues,a.mValues);
442  }
447  inline void swap(const tIndex& i,const tIndex& j) {
448  std::swap(mValues[i],mValues[j]);
449  }
450 
451 
452  //array algebric operators
453  //==========================
454 
455 
456 
457  //Data consistency
458  //=================
459 
460 
461 
462 
463 
464  //algebric methods
465  //================
466 
467 
468 
469 
470 };
471 
472 
473 #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 a const iterator with a constant stride
Definition: CORE_ConstantStrideIterator.h:12
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 as a pointer allocation object with im...
Definition: CORE_PtrArray.h:69
tBoolean setSharedValues(const tIndex &capacity, Q *values)
set shared values with the capacity and size
Definition: CORE_PtrArray.h:411
const T * getValues() const
get the values of the array for reading
Definition: CORE_PtrArray.h:357
T * getValues()
get the values of the array
Definition: CORE_PtrArray.h:363
const T & operator[](const tIndex &i) const
get the i-th element for reading. Do not verify the bounds
Definition: CORE_PtrArray.h:269
void swap(CORE_PtrArray< T, I1 > &a)
swap the contents of the array
Definition: CORE_PtrArray.h:440
virtual void deleteAllocation(T *&mem) const
delete a memory allocation
Definition: CORE_PtrArray.h:257
constexpr auto cend() const
return end iterator for reading
Definition: CORE_PtrArray.h:306
constexpr auto crbegin() const
return reverse begin iterator for reading
Definition: CORE_PtrArray.h:338
T & operator[](const tIndex &i)
get the i-th element for writting. Do not verify the bounds
Definition: CORE_PtrArray.h:282
void desallocate()
allocate the array
Definition: CORE_PtrArray.h:215
auto begin()
return begin iterator for writing
Definition: CORE_PtrArray.h:312
auto end()
return end iterator for writing
Definition: CORE_PtrArray.h:318
tBoolean setSharedValues(CORE_PtrArray< Q, I1 > &array)
set shared values with the capacity and size
Definition: CORE_PtrArray.h:420
void setSize(const tIndex &n)
set the number of values
Definition: CORE_PtrArray.h:146
tBoolean setSharedValues(const tIndex &capacity, const tIndex &size, Q *values)
set shared values with the capacity and size
Definition: CORE_PtrArray.h:390
virtual ~CORE_PtrArray()
destroy an instance of this
Definition: CORE_PtrArray.h:100
auto rbegin()
return reverse begin iterator for writing
Definition: CORE_PtrArray.h:325
auto rend()
return reverse end iterator for writing
Definition: CORE_PtrArray.h:331
void swap(const tIndex &i, const tIndex &j)
swap the 2 elements of the array
Definition: CORE_PtrArray.h:447
void fitToSize(tInteger size)
modify the size of the array
Definition: CORE_PtrArray.h:171
virtual T * newAllocation(const tIndex &n) const
create a memory allocation
Definition: CORE_PtrArray.h:235
CORE_PtrArray()
build an array of T*
Definition: CORE_PtrArray.h:87
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_PtrArray.h:120
constexpr auto cbegin() const
return begin iterator for reading
Definition: CORE_PtrArray.h:300
tIndex getSize() const
return the size of the array for writing
Definition: CORE_PtrArray.h:155
constexpr auto crend() const
return reverse end iterator for reading
Definition: CORE_PtrArray.h:344
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_PtrArray.h:132
void resize(const tIndex &n)
modify the size of the array and keep its old values
Definition: CORE_PtrArray.h:162
this class describes a const iterator with a constant stride 2 templates parameter:
Definition: CORE_StrideIterator.h:12