15 #include "../config/config.hpp"
41 BaseArray(
int asize,
int ainc,
int elmentsize);
47 void GrowSize(
int minsize,
int elementsize);
71 explicit inline Array(
int asize = 0,
int ainc = 0)
77 inline Array(T *_data,
int asize,
int ainc = 0)
84 inline operator T *() {
return (T *)
data; }
87 inline operator const T *()
const {
return (
const T *)
data; }
115 inline void SetSize(
int nsize);
118 inline void SetSize(
int nsize,
const T &initval);
127 inline int Append(
const T & el);
133 inline int Prepend(
const T &el);
137 inline const T &
Last()
const;
140 inline int Union(
const T & el);
143 inline int Find(
const T &el)
const;
170 void Print(std::ostream &out,
int width);
173 void Save(std::ostream &out);
198 inline void Assign(
const T *);
224 Array2D(
int m,
int n) : array1d(m*n) { N = n; }
226 void SetSize(
int m,
int n) { array1d.SetSize(m*n); N = n; }
228 int NumRows()
const {
return array1d.Size()/N; }
231 inline const T &
operator()(
int i,
int j)
const;
240 const T *
GetRow(
int i)
const {
return (*
this)[i]; }
244 { copy.N = N; array1d.Copy(copy.array1d); }
251 { N = master.N; array1d.MakeRef(master.array1d);}
265 : array1d(n1*n2*n3) { N2 = n2; N3 = n3; }
268 { array1d.SetSize(n1*n2*n3); N2 = n2; N3 = n3; }
270 inline const T &
operator()(
int i,
int j,
int k)
const;
295 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
296 if (nsize > abs(allocsize))
297 GrowSize(nsize,
sizeof(T));
304 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
307 if (nsize > abs(allocsize))
308 GrowSize(nsize,
sizeof(T));
309 for (
int i = size; i < nsize; i++)
310 ((T*)data)[i] = initval;
318 MFEM_ASSERT( i>=0 && i<size,
"Access element " << i <<
" of array, size = " << size );
319 return ((T*)data)[i];
325 MFEM_ASSERT( i>=0 && i<size,
"Access element " << i <<
" of array, size = " << size );
326 return ((T*)data)[i];
333 ((T*)data)[size-1] = el;
342 SetSize(size + els.
Size());
343 for (
int i = 0; i < els.
Size(); i++)
344 ((T*)data)[old_size+i] = els[i];
353 for (
int i = size-1; i > 0; i--)
354 ((T*)data)[i] = ((T*)data)[i-1];
363 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
364 return ((T*)data)[size-1];
370 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
371 return ((T*)data)[size-1];
378 while ((i < size) && (((T*)data)[i] != el)) i++;
387 for (
int i = 0; i < size; i++)
388 if (((T*)data)[i] == el)
396 for (
int i = 0; i < size; i++)
397 if (((T*)data)[i] == el)
399 for (i++; i < size; i++)
400 ((T*)data)[i-1] = ((T*)data)[i];
410 delete [] (
char*)data;
412 size = allocsize = 0;
419 delete [] (
char*)data;
429 delete [] (
char*)data;
440 for (
int i = 0; i < sa_size; i++)
441 sa[i] = (*
this)[offset+i];
447 for (
int i = 0; i < size; i++)
454 memcpy(data, p, Size()*
sizeof(T));
461 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
462 "Array2D: invalid access of element (" << i <<
',' << j
463 <<
") in array of size (" << array1d.Size()/N <<
',' << N
465 return array1d[i*N+j];
471 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
472 "Array2D: invalid access of element (" << i <<
',' << j
473 <<
") in array of size (" << array1d.Size()/N <<
',' << N
475 return array1d[i*N+j];
481 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
482 "Array2D: invalid access of row " << i <<
" in array with "
483 << array1d.Size()/N <<
" rows.");
484 return &array1d[i*N];
490 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
491 "Array2D: invalid access of row " << i <<
" in array with "
492 << array1d.Size()/N <<
" rows.");
493 return &array1d[i*N];
500 Swap(a.array1d, b.array1d);
508 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
510 "Array3D: invalid access of element ("
511 << i <<
',' << j <<
',' << k <<
") in array of size ("
512 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
513 return array1d[(i*N2+j)*N3+k];
519 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
521 "Array3D: invalid access of element ("
522 << i <<
',' << j <<
',' << k <<
") in array of size ("
523 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
524 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 Print(std::ostream &out, int width)
Prints array to stream with width elements per row.
void MakeDataOwner()
Make the Array own the data.
void Assign(const T *)
Copy data from a pointer. Size() elements are copied.
void Sort()
Sorts the array.
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 Swap(Array< T > &, Array< T > &)
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 poiter.
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.