15 #include "../config/config.hpp"
42 BaseArray(
int asize,
int ainc,
int elmentsize);
48 void GrowSize(
int minsize,
int elementsize);
72 explicit inline Array(
int asize = 0,
int ainc = 0)
78 inline Array(T *_data,
int asize,
int ainc = 0)
85 inline operator T *() {
return (T *)
data; }
88 inline operator const T *()
const {
return (
const T *)
data; }
112 inline void SetSize(
int nsize);
115 inline void SetSize(
int nsize,
const T &initval);
132 inline int Append(
const T & el);
138 inline int Prepend(
const T &el);
142 inline const T &
Last()
const;
145 inline int Union(
const T & el);
148 inline int Find(
const T &el)
const;
175 void Print(std::ostream &out = std::cout,
int width = 4);
178 void Save(std::ostream &out);
211 inline void Assign(
const T *);
225 if ( LHS.
Size() != RHS.
Size() ) {
return false; }
226 for (
int i=0; i<LHS.
Size(); i++)
227 if ( LHS[i] != RHS[i] ) {
return false; }
234 return !( LHS == RHS );
254 Array2D(
int m,
int n) : array1d(m*n) { N = n; }
256 void SetSize(
int m,
int n) { array1d.SetSize(m*n); N = n; }
258 int NumRows()
const {
return array1d.Size()/N; }
261 inline const T &
operator()(
int i,
int j)
const;
270 const T *
GetRow(
int i)
const {
return (*
this)[i]; }
274 { copy.N = N; array1d.Copy(copy.array1d); }
281 { N = master.N; array1d.MakeRef(master.array1d);}
295 : array1d(n1*n2*n3) { N2 = n2; N3 = n3; }
298 { array1d.SetSize(n1*n2*n3); N2 = n2; N3 = n3; }
300 inline const T &
operator()(
int i,
int j,
int k)
const;
325 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
326 if (nsize > abs(allocsize))
328 GrowSize(nsize,
sizeof(T));
336 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
339 if (nsize > abs(allocsize))
341 GrowSize(nsize,
sizeof(T));
343 for (
int i = size; i < nsize; i++)
345 ((T*)data)[i] = initval;
354 MFEM_ASSERT( i>=0 && i<size,
355 "Access element " << i <<
" of array, size = " << size );
356 return ((T*)data)[i];
362 MFEM_ASSERT( i>=0 && i<size,
363 "Access element " << i <<
" of array, size = " << size );
364 return ((T*)data)[i];
371 ((T*)data)[size-1] = el;
380 SetSize(size + els.
Size());
381 for (
int i = 0; i < els.
Size(); i++)
383 ((T*)data)[old_size+i] = els[i];
393 for (
int i = size-1; i > 0; i--)
395 ((T*)data)[i] = ((T*)data)[i-1];
405 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
406 return ((T*)data)[size-1];
412 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
413 return ((T*)data)[size-1];
420 while ((i < size) && (((T*)data)[i] != el)) { i++; }
431 for (
int i = 0; i < size; i++)
432 if (((T*)data)[i] == el)
442 for (
int i = 0; i < size; i++)
443 if (((T*)data)[i] == el)
445 for (i++; i < size; i++)
447 ((T*)data)[i-1] = ((T*)data)[i];
459 delete [] (
char*)data;
462 size = allocsize = 0;
470 delete [] (
char*)data;
482 delete [] (
char*)data;
494 for (
int i = 0; i < sa_size; i++)
496 sa[i] = (*this)[offset+i];
503 for (
int i = 0; i < size; i++)
512 memcpy(data, p, Size()*
sizeof(T));
519 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
520 "Array2D: invalid access of element (" << i <<
',' << j
521 <<
") in array of size (" << array1d.Size()/N <<
',' << N
523 return array1d[i*N+j];
529 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
530 "Array2D: invalid access of element (" << i <<
',' << j
531 <<
") in array of size (" << array1d.Size()/N <<
',' << N
533 return array1d[i*N+j];
539 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
540 "Array2D: invalid access of row " << i <<
" in array with "
541 << array1d.Size()/N <<
" rows.");
542 return &array1d[i*N];
548 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
549 "Array2D: invalid access of row " << i <<
" in array with "
550 << array1d.Size()/N <<
" rows.");
551 return &array1d[i*N];
558 Swap(a.array1d, b.array1d);
566 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
568 "Array3D: invalid access of element ("
569 << i <<
',' << j <<
',' << k <<
") in array of size ("
570 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
571 return array1d[(i*N2+j)*N3+k];
577 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
579 "Array3D: invalid access of element ("
580 << i <<
',' << j <<
',' << k <<
") in array of size ("
581 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
582 return array1d[(i*N2+j)*N3+k];
int Size() const
Logical size of the array.
~BaseArray()
Free the allocated memory.
const T * operator()(int i) const
void * data
Pointer to data.
Array(T *_data, int asize, int ainc=0)
Base class for array container.
void Copy(Array ©) const
Create a copy of the current array.
T * GetData()
Returns the data.
void operator=(const T &a)
void operator=(const T &a)
void MakeRef(const Array2D &master)
Make this Array a reference to 'master'.
void DeleteFirst(const T &el)
Delete the first 'el' entry.
void SetSize(int n1, int n2, int n3)
void DeleteAll()
Delete whole array.
void GrowSize(int minsize, int elementsize)
void GetSubArray(int offset, int sa_size, Array< T > &sa)
int size
Size of the array.
void SetSize(int m, int n)
void Copy(Array2D ©) const
bool OwnsData() const
Return true if the data will be deleted by the array.
const T & operator()(int i, int j) const
const T * GetData() const
Returns the data.
int Append(const T &el)
Append element to array, resize if necessary.
T & operator[](int i)
Access element.
void LoseData()
NULL-ifies the data.
void MakeDataOwner()
Make the Array own the data.
void Reserve(int capacity)
Ensures that the allocated size is at least the given size.
bool operator!=(const Array< T > &LHS, const Array< T > &RHS)
void Assign(const T *)
Copy data from a pointer. Size() elements are copied.
void Sort()
Sorts the array. This requires operator< to be defined for T.
int allocsize
Size of the allocated memory.
void StealData(T **p)
Changes the ownership of the the data.
int Union(const T &el)
Append element when it is not yet in the array, return index.
void Print(std::ostream &out=std::cout, int width=4)
Prints array to stream with width elements per row.
void Swap(Array< T > &, Array< T > &)
bool operator==(const Array< T > &LHS, const Array< T > &RHS)
int IsSorted()
return true if the array is sorted.
int Find(const T &el) const
Return the first index where 'el' is found; return -1 if not found.
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
const T * operator[](int i) const
void PartialSum()
Partial Sum.
void Save(std::ostream &out)
Prints array to stream out.
void DeleteLast()
Delete the last entry.
T & Last()
Return the last element in the array.
void MakeRef(T *, int)
Make this Array a reference to a pointer.
const T & operator()(int i, int j, int k) const
Array3D(int n1, int n2, int n3)
Array(int asize=0, int ainc=0)
Creates array of asize elements.
const T * GetRow(int i) const
int Prepend(const T &el)
Prepend an element to the array, resize if necessary.