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);
183 void Save(std::ostream &out,
int fmt = 0)
const;
191 void Load(std::istream &in,
int fmt = 0);
195 void Load(
int new_size, std::istream &in)
210 template<
class Compare>
233 inline void Assign(
const T *);
251 if ( LHS.
Size() != RHS.
Size() ) {
return false; }
252 for (
int i=0; i<LHS.
Size(); i++)
253 if ( LHS[i] != RHS[i] ) {
return false; }
260 return !( LHS == RHS );
280 Array2D(
int m,
int n) : array1d(m*n) { N = n; }
282 void SetSize(
int m,
int n) { array1d.SetSize(m*n); N = n; }
284 int NumRows()
const {
return array1d.Size()/N; }
287 inline const T &
operator()(
int i,
int j)
const;
296 const T *
GetRow(
int i)
const {
return (*
this)[i]; }
300 { copy.N = N; array1d.Copy(copy.array1d); }
307 { N = master.N; array1d.MakeRef(master.array1d);}
321 : array1d(n1*n2*n3) { N2 = n2; N3 = n3; }
324 { array1d.SetSize(n1*n2*n3); N2 = n2; N3 = n3; }
326 inline const T &
operator()(
int i,
int j,
int k)
const;
351 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
352 if (nsize > abs(allocsize))
354 GrowSize(nsize,
sizeof(T));
362 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
365 if (nsize > abs(allocsize))
367 GrowSize(nsize,
sizeof(T));
369 for (
int i = size; i < nsize; i++)
371 ((T*)data)[i] = initval;
380 MFEM_ASSERT( i>=0 && i<size,
381 "Access element " << i <<
" of array, size = " << size );
382 return ((T*)data)[i];
388 MFEM_ASSERT( i>=0 && i<size,
389 "Access element " << i <<
" of array, size = " << size );
390 return ((T*)data)[i];
397 ((T*)data)[size-1] = el;
406 SetSize(size + els.
Size());
407 for (
int i = 0; i < els.
Size(); i++)
409 ((T*)data)[old_size+i] = els[i];
419 for (
int i = size-1; i > 0; i--)
421 ((T*)data)[i] = ((T*)data)[i-1];
431 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
432 return ((T*)data)[size-1];
438 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
439 return ((T*)data)[size-1];
446 while ((i < size) && (((T*)data)[i] != el)) { i++; }
457 for (
int i = 0; i < size; i++)
458 if (((T*)data)[i] == el)
468 for (
int i = 0; i < size; i++)
469 if (((T*)data)[i] == el)
471 for (i++; i < size; i++)
473 ((T*)data)[i-1] = ((T*)data)[i];
485 delete [] (
char*)data;
488 size = allocsize = 0;
496 delete [] (
char*)data;
508 delete [] (
char*)data;
520 for (
int i = 0; i < sa_size; i++)
522 sa[i] = (*this)[offset+i];
529 for (
int i = 0; i < size; i++)
538 memcpy(data, p, Size()*
sizeof(T));
545 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
546 "Array2D: invalid access of element (" << i <<
',' << j
547 <<
") in array of size (" << array1d.Size()/N <<
',' << N
549 return array1d[i*N+j];
555 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
556 "Array2D: invalid access of element (" << i <<
',' << j
557 <<
") in array of size (" << array1d.Size()/N <<
',' << N
559 return array1d[i*N+j];
565 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
566 "Array2D: invalid access of row " << i <<
" in array with "
567 << array1d.Size()/N <<
" rows.");
568 return &array1d[i*N];
574 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
575 "Array2D: invalid access of row " << i <<
" in array with "
576 << array1d.Size()/N <<
" rows.");
577 return &array1d[i*N];
584 Swap(a.array1d, b.array1d);
592 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
594 "Array3D: invalid access of element ("
595 << i <<
',' << j <<
',' << k <<
") in array of size ("
596 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
597 return array1d[(i*N2+j)*N3+k];
603 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
605 "Array3D: invalid access of element ("
606 << i <<
',' << j <<
',' << k <<
") in array of size ("
607 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
608 return array1d[(i*N2+j)*N3+k];
int Size() const
Logical size of the array.
void Load(std::istream &in, int fmt=0)
Read an Array from the stream in using format fmt. The format fmt can be:
~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 Save(std::ostream &out, int fmt=0) const
Save the Array to the stream out using the format fmt. The format fmt can be:
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 Load(int new_size, std::istream &in)
Set the Array size to new_size and read that many entries from the stream in.
void DeleteFirst(const T &el)
Delete the first 'el' entry.
void SetSize(int n1, int n2, int n3)
T Min() const
Find the minimal element in the array, using the comparison operator < for class T.
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.
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
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 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.