1 #ifndef CORE_PtrArray_H
2 #define CORE_PtrArray_H
4 #include "CORE_Array.h"
7 #include "functions_numeric.h"
68 template <
typename T,
class I>
81 tBoolean mIsSharedValues;
91 mIsSharedValues=
false;
134 mem+=mCapacity*
sizeof(T);
147 if (n>=mCapacity) allocate(n,
false);
163 if (mSize!=n) allocate(n,
true);
172 if (size!=mCapacity) {
182 inline void allocate(
const tIndex& n,
const tBoolean& areOldValuesCopied) {
184 if (n==mCapacity)
return;
191 if (areOldValuesCopied) {
193 tIndex oldSize=mSize;
194 const T* oldValues=mValues;
196 tIndex m=functions_numeric::min(oldSize,n);
198 if (m>0) memcpy(newValues,oldValues,m*
sizeof(T));
200 if (n>m) memset(&newValues[m],0,(n-m)*
sizeof(T));
208 mIsSharedValues=
false;
216 if ( !mIsSharedValues && (mValues!=
null)) {
220 "CORE_PtrArray::desallocate()",
221 "impossible to desallocate the memory");
223 mIsSharedValues=
false;
238 T* newValues=
new T[n];
239 if (newValues==
null) {
241 "CORE_PtrArray::allocate("+std::to_string(n)+
")",
242 "not enought memory for allocation");
245 }
catch(std::exception& e) {
247 "CORE_PtrArray::allocate("+std::to_string(n)+
")",
248 "not enought memory for allocation");
258 if (mem!=
null)
delete[] mem;
274 "CORE_PtrArray["+std::to_string(i)+
"] is out of bounds [0,"+std::to_string(mSize)+
"[ for reading");
287 "CORE_PtrArray["+std::to_string(i)+
"] is out of bounds [0,"+std::to_string(mSize)+
"[ for writing ");
306 inline constexpr
auto cend()
const {
332 return CORE_StringIterator<T*,-1>(mValues-1);
344 inline constexpr
auto crend()
const {
390 inline tBoolean
setSharedValues(
const tIndex& capacity,
const tIndex& size,Q* values) {
391 if (
sizeof(T)!=
sizeof(Q))
return false;
397 mIsSharedValues=
true;
419 template<
typename Q,
class I1>
441 std::swap(mValues,a.mValues);
447 inline void swap(
const tIndex& i,
const tIndex& j) {
448 std::swap(mValues[i],mValues[j]);
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