15 #include "../config/config.hpp"
43 BaseArray(
int asize,
int ainc,
int elmentsize);
49 void GrowSize(
int minsize,
int elementsize);
73 explicit inline Array(
int asize = 0,
int ainc = 0)
79 inline Array(T *_data,
int asize,
int ainc = 0)
86 inline operator T *() {
return (T *)
data; }
89 inline operator const T *()
const {
return (
const T *)
data; }
113 inline void SetSize(
int nsize);
116 inline void SetSize(
int nsize,
const T &initval);
133 inline int Append(
const T & el);
139 inline int Prepend(
const T &el);
143 inline const T &
Last()
const;
146 inline int Union(
const T & el);
149 inline int Find(
const T &el)
const;
184 void Save(std::ostream &
out,
int fmt = 0)
const;
192 void Load(std::istream &in,
int fmt = 0);
196 void Load(
int new_size, std::istream &in)
211 template<
class Compare>
234 inline void Assign(
const T *);
252 if ( LHS.
Size() != RHS.
Size() ) {
return false; }
253 for (
int i=0; i<LHS.
Size(); i++)
254 if ( LHS[i] != RHS[i] ) {
return false; }
261 return !( LHS == RHS );
282 Array2D(
int m,
int n) : array1d(m*n) { N = n; }
284 void SetSize(
int m,
int n) { array1d.SetSize(m*n); N = n; }
286 int NumRows()
const {
return array1d.Size()/N; }
289 inline const T &
operator()(
int i,
int j)
const;
298 const T *
GetRow(
int i)
const {
return (*
this)[i]; }
302 { copy.N = N; array1d.Copy(copy.array1d); }
309 { N = master.N; array1d.MakeRef(master.array1d);}
326 : array1d(n1*n2*n3) { N2 = n2; N3 = n3; }
329 { array1d.SetSize(n1*n2*n3); N2 = n2; N3 = n3; }
331 inline const T &
operator()(
int i,
int j,
int k)
const;
352 int Append(
const T &item);
355 inline T&
At(
int index)
360 inline const T&
At(
int index)
const
381 template <
typename cA,
typename cT>
402 b_end_idx = std::min(a->size, a->mask+1);
408 MFEM_ASSERT(!
stop,
"invalid use");
411 if (b_end_idx < array->
size)
415 b_end_idx = std::min(
array->size, (b_end_idx|
array->mask) + 1);
462 iterator
begin() {
return size ? iterator(
this) : iterator(
true); }
463 iterator
end() {
return iterator(); }
466 {
return size ? const_iterator(
this) : const_iterator(
true); }
467 const_iterator
cend()
const {
return const_iterator(); }
477 MFEM_ASSERT(index >= 0 && index <
size,
478 "Out of bounds access: " << index <<
", size = " <<
size);
505 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
506 if (nsize > abs(allocsize))
508 GrowSize(nsize,
sizeof(T));
516 MFEM_ASSERT( nsize>=0,
"Size must be non-negative. It is " << nsize );
519 if (nsize > abs(allocsize))
521 GrowSize(nsize,
sizeof(T));
523 for (
int i = size; i < nsize; i++)
525 ((T*)data)[i] = initval;
534 MFEM_ASSERT( i>=0 && i<size,
535 "Access element " << i <<
" of array, size = " << size );
536 return ((T*)data)[i];
542 MFEM_ASSERT( i>=0 && i<size,
543 "Access element " << i <<
" of array, size = " << size );
544 return ((T*)data)[i];
551 ((T*)data)[size-1] = el;
560 SetSize(size + els.
Size());
561 for (
int i = 0; i < els.
Size(); i++)
563 ((T*)data)[old_size+i] = els[i];
572 for (
int i = size-1; i > 0; i--)
574 ((T*)data)[i] = ((T*)data)[i-1];
583 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
584 return ((T*)data)[size-1];
590 MFEM_ASSERT(size > 0,
"Array size is zero: " << size);
591 return ((T*)data)[size-1];
598 while ((i < size) && (((T*)data)[i] != el)) { i++; }
609 for (
int i = 0; i < size; i++)
610 if (((T*)data)[i] == el)
620 for (
int i = 0; i < size; i++)
621 if (((T*)data)[i] == el)
623 for (i++; i < size; i++)
625 ((T*)data)[i-1] = ((T*)data)[i];
637 delete [] (
char*)data;
640 size = allocsize = 0;
648 delete [] (
char*)data;
660 delete [] (
char*)data;
672 for (
int i = 0; i < sa_size; i++)
674 sa[i] = (*this)[offset+i];
681 for (
int i = 0; i < size; i++)
690 memcpy(data, p, Size()*
sizeof(T));
697 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
698 "Array2D: invalid access of element (" << i <<
',' << j
699 <<
") in array of size (" << array1d.Size()/N <<
',' << N
701 return array1d[i*N+j];
707 MFEM_ASSERT( i>=0 && i< array1d.Size()/N && j>=0 && j<N,
708 "Array2D: invalid access of element (" << i <<
',' << j
709 <<
") in array of size (" << array1d.Size()/N <<
',' << N
711 return array1d[i*N+j];
717 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
718 "Array2D: invalid access of row " << i <<
" in array with "
719 << array1d.Size()/N <<
" rows.");
720 return &array1d[i*N];
726 MFEM_ASSERT( i>=0 && i< array1d.Size()/N,
727 "Array2D: invalid access of row " << i <<
" in array with "
728 << array1d.Size()/N <<
" rows.");
729 return &array1d[i*N];
736 Swap(a.array1d, b.array1d);
744 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
746 "Array3D: invalid access of element ("
747 << i <<
',' << j <<
',' << k <<
") in array of size ("
748 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
749 return array1d[(i*N2+j)*N3+k];
755 MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
757 "Array3D: invalid access of element ("
758 << i <<
',' << j <<
',' << k <<
") in array of size ("
759 << array1d.Size() / N2 / N3 <<
',' << N2 <<
',' << N3 <<
").");
760 return array1d[(i*N2+j)*N3+k];
768 MFEM_VERIFY(!(block_size & mask),
"block_size must be a power of two.");
771 while ((1 << shift) < block_size) { shift++; }
784 for (
int i = 0; i < blocks.Size(); i++)
786 blocks[i] = (T*)
new char[bsize *
sizeof(T)];
790 for (
int i = 0; i < size; i++)
792 new (&At(i)) T(other[i]);
800 if (size >= blocks.Size() * bsize)
802 T* new_block = (T*)
new char[bsize *
sizeof(T)];
803 blocks.Append(new_block);
812 new (&At(index)) T();
820 new (&At(index)) T(item);
828 std::swap(size, other.
size);
829 std::swap(shift, other.
shift);
830 std::swap(mask, other.
mask);
836 return blocks.Size()*(mask+1)*
sizeof(T) +
837 blocks.MemoryUsage();
843 int bsize = size & mask;
844 for (
int i = blocks.Size(); i != 0; )
846 T *block = blocks[--i];
847 for (
int j = bsize; j != 0; )
851 delete [] (
char*) block;
int Size() const
Logical size of the array.
const_iterator(const BlockArray *a)
void Load(std::istream &in, int fmt=0)
Read an Array from the stream in using format fmt. The format fmt can be:
const_iterator cbegin() const
~BaseArray()
Free the allocated memory.
iterator_base< const BlockArray, const T > base
const T * operator()(int i) const
void * data
Pointer to data.
Array(T *_data, int asize, int ainc=0)
int Capacity() const
Return the current capacity of the BlockArray.
int Size() const
Return the number of items actually stored.
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)
bool operator==(const iterator &other) const
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.
const T & At(int index) const
bool operator==(const const_iterator &other) const
void DeleteFirst(const T &el)
Delete the first 'el' entry.
T & operator[](int index)
Access item of the array.
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=mfem::out, 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.
BlockArray(int block_size=16 *1024)
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.
const T & operator[](int index) const
bool operator!=(const iterator &other) const
const_iterator(bool stop)
void CheckIndex(int index) const
bool operator!=(const const_iterator &other) const
const_iterator & operator++()
T & Last()
Return the last element in the array.
void Print(std::ostream &out=mfem::out, int width=4) const
Prints array to stream with width elements per row.
iterator_base< BlockArray, T > base
void MakeRef(T *, int)
Make this Array a reference to a pointer.
const_iterator cend() const
const T & operator()(int i, int j, int k) const
Array3D(int n1, int n2, int n3)
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Array(int asize=0, int ainc=0)
Creates array of asize elements.
T & At(int index)
Access item of the array.
const T * GetRow(int i) const
int Prepend(const T &el)
Prepend an element to the array, resize if necessary.
int Append()
Allocate and construct a new item in the array, return its index.
void Swap(BlockArray< T > &other)