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);
192 template<
class Compare>
215 inline void Assign(
const T *);
233 if ( LHS.
Size() != RHS.
Size() ) {
return false; }
234 for (
int i=0; i<LHS.
Size(); i++)
235 if ( LHS[i] != RHS[i] ) {
return false; }
242 return !( LHS == RHS );
262 Array2D(
int m,
int n) : array1d(m*n) { N = n; }
264 void SetSize(
int m,
int n) { array1d.SetSize(m*n); N = n; }
266 int NumRows()
const {
return array1d.Size()/N; }
269 inline const T &
operator()(
int i,
int j)
const;
278 const T *
GetRow(
int i)
const {
return (*
this)[i]; }
282 { copy.N = N; array1d.Copy(copy.array1d); }
289 { N = master.N; array1d.MakeRef(master.array1d);}
303 : array1d(n1*n2*n3) { N2 = n2; N3 = n3; }
306 { array1d.SetSize(n1*n2*n3); N2 = n2; N3 = n3; }
308 inline const T &
operator()(
int i,
int j,
int k)
const;
333 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
334 if (nsize > abs(allocsize))
336 GrowSize(nsize,
sizeof(T));
344 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
347 if (nsize > abs(allocsize))
349 GrowSize(nsize,
sizeof(T));
351 for (
int i = size; i < nsize; i++)
353 ((T*)data)[i] = initval;
362 MFEM_ASSERT( i>=0 && i<size,
363 "Access element " << i <<
" of array, size = " << size );
364 return ((T*)data)[i];
370 MFEM_ASSERT( i>=0 && i<size,
371 "Access element " << i <<
" of array, size = " << size );
372 return ((T*)data)[i];
379 ((T*)data)[size-1] = el;
388 SetSize(size + els.
Size());
389 for (
int i = 0; i < els.
Size(); i++)
391 ((T*)data)[old_size+i] = els[i];
401 for (
int i = size-1; i > 0; i--)
403 ((T*)data)[i] = ((T*)data)[i-1];
413 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
414 return ((T*)data)[size-1];
420 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
421 return ((T*)data)[size-1];
428 while ((i < size) && (((T*)data)[i] != el)) { i++; }
439 for (
int i = 0; i < size; i++)
440 if (((T*)data)[i] == el)
450 for (
int i = 0; i < size; i++)
451 if (((T*)data)[i] == el)
453 for (i++; i < size; i++)
455 ((T*)data)[i-1] = ((T*)data)[i];
467 delete [] (
char*)data;
470 size = allocsize = 0;
478 delete [] (
char*)data;
490 delete [] (
char*)data;
502 for (
int i = 0; i < sa_size; i++)
504 sa[i] = (*this)[offset+i];
511 for (
int i = 0; i < size; i++)
520 memcpy(data, p, Size()*
sizeof(T));
527 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
528 "Array2D: invalid access of element (" << i <<
',' << j
529 <<
") in array of size (" << array1d.Size()/N <<
',' << N
531 return array1d[i*N+j];
537 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
538 "Array2D: invalid access of element (" << i <<
',' << j
539 <<
") in array of size (" << array1d.Size()/N <<
',' << N
541 return array1d[i*N+j];
547 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
548 "Array2D: invalid access of row " << i <<
" in array with "
549 << array1d.Size()/N <<
" rows.");
550 return &array1d[i*N];
556 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
557 "Array2D: invalid access of row " << i <<
" in array with "
558 << array1d.Size()/N <<
" rows.");
559 return &array1d[i*N];
566 Swap(a.array1d, b.array1d);
574 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
576 "Array3D: invalid access of element ("
577 << i <<
',' << j <<
',' << k <<
") in array of size ("
578 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
579 return array1d[(i*N2+j)*N3+k];
585 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
587 "Array3D: invalid access of element ("
588 << i <<
',' << j <<
',' << k <<
") in array of size ("
589 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
590 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 Sort(Compare cmp)
Sorts the array using the supplied comparison function object.
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 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.