MFEM  v3.3.2
Finite element discretization library
array.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_ARRAY
13 #define MFEM_ARRAY
14 
15 #include "../config/config.hpp"
16 #include "error.hpp"
17 #include "globals.hpp"
18 
19 #include <iostream>
20 #include <cstdlib>
21 #include <cstring>
22 #include <algorithm>
23 
24 namespace mfem
25 {
26 
27 /// Base class for array container.
28 class BaseArray
29 {
30 protected:
31  /// Pointer to data
32  void *data;
33  /// Size of the array
34  int size;
35  /// Size of the allocated memory
36  int allocsize;
37  /** Increment of allocated memory on overflow,
38  inc = 0 doubles the array */
39  int inc;
40 
41  BaseArray() { }
42  /// Creates array of asize elements of size elementsize
43  BaseArray(int asize, int ainc, int elmentsize);
44  /// Free the allocated memory
45  ~BaseArray();
46  /** Increases the allocsize of the array to be at least minsize.
47  The current content of the array is copied to the newly allocated
48  space. minsize must be > abs(allocsize). */
49  void GrowSize(int minsize, int elementsize);
50 };
51 
52 template <class T>
53 class Array;
54 
55 template <class T>
56 void Swap(Array<T> &, Array<T> &);
57 
58 /**
59  Abstract data type Array.
60 
61  Array<T> is an automatically increasing array containing elements of the
62  generic type T. The allocated size may be larger then the logical size
63  of the array.
64  The elements can be accessed by the [] operator, the range is 0 to size-1.
65 */
66 template <class T>
67 class Array : public BaseArray
68 {
69 public:
70  friend void Swap<T>(Array<T> &, Array<T> &);
71 
72  /// Creates array of asize elements
73  explicit inline Array(int asize = 0, int ainc = 0)
74  : BaseArray(asize, ainc, sizeof (T)) { }
75 
76  /** Creates array using an existing c-array of asize elements;
77  allocsize is set to -asize to indicate that the data will not
78  be deleted. */
79  inline Array(T *_data, int asize, int ainc = 0)
80  { data = _data; size = asize; allocsize = -asize; inc = ainc; }
81 
82  /// Destructor
83  inline ~Array() { }
84 
85  /// Return the data as 'T *'
86  inline operator T *() { return (T *)data; }
87 
88  /// Return the data as 'const T *'
89  inline operator const T *() const { return (const T *)data; }
90 
91  /// Returns the data
92  inline T *GetData() { return (T *)data; }
93  /// Returns the data
94  inline const T *GetData() const { return (T *)data; }
95 
96  /// Return true if the data will be deleted by the array
97  inline bool OwnsData() const { return (allocsize > 0); }
98 
99  /// Changes the ownership of the data
100  inline void StealData(T **p)
101  { *p = (T*)data; data = 0; size = allocsize = 0; }
102 
103  /// NULL-ifies the data
104  inline void LoseData() { data = 0; size = allocsize = 0; }
105 
106  /// Make the Array own the data
107  void MakeDataOwner() { allocsize = abs(allocsize); }
108 
109  /// Logical size of the array
110  inline int Size() const { return size; }
111 
112  /// Change logical size of the array, keep existing entries
113  inline void SetSize(int nsize);
114 
115  /// Same as SetSize(int) plus initialize new entries with 'initval'
116  inline void SetSize(int nsize, const T &initval);
117 
118  /** Maximum number of entries the array can store without allocating more
119  memory. */
120  inline int Capacity() const { return abs(allocsize); }
121 
122  /// Ensures that the allocated size is at least the given size.
123  inline void Reserve(int capacity)
124  { if (capacity > abs(allocsize)) { GrowSize(capacity, sizeof(T)); } }
125 
126  /// Access element
127  inline T & operator[](int i);
128 
129  /// Access const element
130  inline const T &operator[](int i) const;
131 
132  /// Append element to array, resize if necessary
133  inline int Append(const T & el);
134 
135  /// Append another array to this array, resize if necessary
136  inline int Append(const Array<T> &els);
137 
138  /// Prepend an element to the array, resize if necessary
139  inline int Prepend(const T &el);
140 
141  /// Return the last element in the array
142  inline T &Last();
143  inline const T &Last() const;
144 
145  /// Append element when it is not yet in the array, return index
146  inline int Union(const T & el);
147 
148  /// Return the first index where 'el' is found; return -1 if not found
149  inline int Find(const T &el) const;
150 
151  /// Delete the last entry
152  inline void DeleteLast() { if (size > 0) { size--; } }
153 
154  /// Delete the first 'el' entry
155  inline void DeleteFirst(const T &el);
156 
157  /// Delete whole array
158  inline void DeleteAll();
159 
160  /// Create a copy of the current array
161  inline void Copy(Array &copy) const
162  {
163  copy.SetSize(Size());
164  memcpy(copy.GetData(), data, Size()*sizeof(T));
165  }
166 
167  /// Make this Array a reference to a pointer
168  inline void MakeRef(T *, int);
169 
170  /// Make this Array a reference to 'master'
171  inline void MakeRef(const Array &master);
172 
173  inline void GetSubArray(int offset, int sa_size, Array<T> &sa);
174 
175  /// Prints array to stream with width elements per row
176  void Print(std::ostream &out = mfem::out, int width = 4) const;
177 
178  /** @brief Save the Array to the stream @a out using the format @a fmt.
179  The format @a fmt can be:
180 
181  0 - write the size followed by all entries
182  1 - write only the entries
183  */
184  void Save(std::ostream &out, int fmt = 0) const;
185 
186  /** @brief Read an Array from the stream @a in using format @a fmt.
187  The format @a fmt can be:
188 
189  0 - read the size then the entries
190  1 - read Size() entries
191  */
192  void Load(std::istream &in, int fmt = 0);
193 
194  /** @brief Set the Array size to @a new_size and read that many entries from
195  the stream @a in. */
196  void Load(int new_size, std::istream &in)
197  { SetSize(new_size); Load(in, 1); }
198 
199  /** @brief Find the maximal element in the array, using the comparison
200  operator `<` for class T. */
201  T Max() const;
202 
203  /** @brief Find the minimal element in the array, using the comparison
204  operator `<` for class T. */
205  T Min() const;
206 
207  /// Sorts the array. This requires operator< to be defined for T.
208  void Sort() { std::sort((T*) data, (T*) data + size); }
209 
210  /// Sorts the array using the supplied comparison function object.
211  template<class Compare>
212  void Sort(Compare cmp) { std::sort((T*) data, (T*) data + size, cmp); }
213 
214  /** Removes duplicities from a sorted array. This requires operator== to be
215  defined for T. */
216  void Unique()
217  {
218  T* end = std::unique((T*) data, (T*) data + size);
219  SetSize(end - (T*) data);
220  }
221 
222  /// return true if the array is sorted.
223  int IsSorted();
224 
225  /// Partial Sum
226  void PartialSum();
227 
228  /// Sum all entries
229  T Sum();
230 
231  inline void operator=(const T &a);
232 
233  /// Copy data from a pointer. Size() elements are copied.
234  inline void Assign(const T *);
235 
236  // STL-like begin/end
237  inline T* begin() const { return (T*) data; }
238  inline T* end() const { return (T*) data + size; }
239 
240  long MemoryUsage() const { return Capacity() * sizeof(T); }
241 
242 private:
243  /// Array copy is not supported
244  Array<T> &operator=(Array<T> &);
245  /// Array copy is not supported
246  Array(const Array<T> &);
247 };
248 
249 template <class T>
250 inline bool operator==(const Array<T> &LHS, const Array<T> &RHS)
251 {
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; }
255  return true;
256 }
257 
258 template <class T>
259 inline bool operator!=(const Array<T> &LHS, const Array<T> &RHS)
260 {
261  return !( LHS == RHS );
262 }
263 
264 
265 template <class T>
266 class Array2D;
267 
268 template <class T>
269 void Swap(Array2D<T> &, Array2D<T> &);
270 
271 template <class T>
272 class Array2D
273 {
274 private:
275  friend void Swap<T>(Array2D<T> &, Array2D<T> &);
276 
277  Array<T> array1d;
278  int N;
279 
280 public:
281  Array2D() { N = 0; }
282  Array2D(int m, int n) : array1d(m*n) { N = n; }
283 
284  void SetSize(int m, int n) { array1d.SetSize(m*n); N = n; }
285 
286  int NumRows() const { return array1d.Size()/N; }
287  int NumCols() const { return N; }
288 
289  inline const T &operator()(int i, int j) const;
290  inline T &operator()(int i, int j);
291 
292  inline const T *operator[](int i) const;
293  inline T *operator[](int i);
294 
295  const T *operator()(int i) const { return (*this)[i]; }
296  T *operator()(int i) { return (*this)[i]; }
297 
298  const T *GetRow(int i) const { return (*this)[i]; }
299  T *GetRow(int i) { return (*this)[i]; }
300 
301  void Copy(Array2D &copy) const
302  { copy.N = N; array1d.Copy(copy.array1d); }
303 
304  inline void operator=(const T &a)
305  { array1d = a; }
306 
307  /// Make this Array a reference to 'master'
308  inline void MakeRef(const Array2D &master)
309  { N = master.N; array1d.MakeRef(master.array1d);}
310 
311  /// Prints array to stream with width elements per row
312  void Print(std::ostream &out = mfem::out, int width = 4);
313 };
314 
315 
316 template <class T>
317 class Array3D
318 {
319 private:
320  Array<T> array1d;
321  int N2, N3;
322 
323 public:
324  Array3D() { N2 = N3 = 0; }
325  Array3D(int n1, int n2, int n3)
326  : array1d(n1*n2*n3) { N2 = n2; N3 = n3; }
327 
328  void SetSize(int n1, int n2, int n3)
329  { array1d.SetSize(n1*n2*n3); N2 = n2; N3 = n3; }
330 
331  inline const T &operator()(int i, int j, int k) const;
332  inline T &operator()(int i, int j, int k);
333 };
334 
335 
336 /** A container for items of type T. Dynamically grows as items are added.
337  * Each item is accessible by its index. Items are allocated in larger chunks
338  * (blocks), so the 'Append' method is very fast on average.
339  */
340 template<typename T>
342 {
343 public:
344  BlockArray(int block_size = 16*1024);
345  BlockArray(const BlockArray<T> &other); // deep copy
346  ~BlockArray();
347 
348  /// Allocate and construct a new item in the array, return its index.
349  int Append();
350 
351  /// Allocate and copy-construct a new item in the array, return its index.
352  int Append(const T &item);
353 
354  /// Access item of the array.
355  inline T& At(int index)
356  {
357  CheckIndex(index);
358  return blocks[index >> shift][index & mask];
359  }
360  inline const T& At(int index) const
361  {
362  CheckIndex(index);
363  return blocks[index >> shift][index & mask];
364  }
365 
366  /// Access item of the array.
367  inline T& operator[](int index) { return At(index); }
368  inline const T& operator[](int index) const { return At(index); }
369 
370  /// Return the number of items actually stored.
371  int Size() const { return size; }
372 
373  /// Return the current capacity of the BlockArray.
374  int Capacity() const { return blocks.Size()*(mask+1); }
375 
376  void Swap(BlockArray<T> &other);
377 
378  long MemoryUsage() const;
379 
380 protected:
381  template <typename cA, typename cT>
383  {
384  public:
385  cT& operator*() const { return *ptr; }
386  cT* operator->() const { return ptr; }
387 
388  bool good() const { return !stop; }
389  int index() const { return (ptr - ref); }
390 
391  protected:
392  cA *array;
393  cT *ptr, *b_end, *ref;
395  bool stop;
396 
398  iterator_base(bool stop) : stop(stop) { }
400  : array(a), ptr(a->blocks[0]), ref(ptr), stop(false)
401  {
402  b_end_idx = std::min(a->size, a->mask+1);
403  b_end = ptr + b_end_idx;
404  }
405 
406  void next()
407  {
408  MFEM_ASSERT(!stop, "invalid use");
409  if (++ptr == b_end)
410  {
411  if (b_end_idx < array->size)
412  {
413  ptr = &array->At(b_end_idx);
414  ref = ptr - b_end_idx;
415  b_end_idx = std::min(array->size, (b_end_idx|array->mask) + 1);
416  b_end = &array->At(b_end_idx-1) + 1;
417  }
418  else
419  {
420  MFEM_ASSERT(b_end_idx == array->size, "invalid use");
421  stop = true;
422  }
423  }
424  }
425  };
426 
427 public:
428  class iterator : public iterator_base<BlockArray, T>
429  {
430  protected:
431  friend class BlockArray;
433 
434  iterator() { }
435  iterator(bool stop) : base(stop) { }
436  iterator(BlockArray *a) : base(a) { }
437 
438  public:
439  iterator &operator++() { base::next(); return *this; }
440 
441  bool operator==(const iterator &other) const { return base::stop; }
442  bool operator!=(const iterator &other) const { return !base::stop; }
443  };
444 
445  class const_iterator : public iterator_base<const BlockArray, const T>
446  {
447  protected:
448  friend class BlockArray;
450 
452  const_iterator(bool stop) : base(stop) { }
453  const_iterator(const BlockArray *a) : base(a) { }
454 
455  public:
456  const_iterator &operator++() { base::next(); return *this; }
457 
458  bool operator==(const const_iterator &other) const { return base::stop; }
459  bool operator!=(const const_iterator &other) const { return !base::stop; }
460  };
461 
462  iterator begin() { return size ? iterator(this) : iterator(true); }
463  iterator end() { return iterator(); }
464 
465  const_iterator cbegin() const
466  { return size ? const_iterator(this) : const_iterator(true); }
467  const_iterator cend() const { return const_iterator(); }
468 
469 protected:
471  int size, shift, mask;
472 
473  int Alloc();
474 
475  inline void CheckIndex(int index) const
476  {
477  MFEM_ASSERT(index >= 0 && index < size,
478  "Out of bounds access: " << index << ", size = " << size);
479  }
480 };
481 
482 
483 /// inlines ///
484 
485 template <class T>
486 inline void Swap(T &a, T &b)
487 {
488  T c = a;
489  a = b;
490  b = c;
491 }
492 
493 template <class T>
494 inline void Swap(Array<T> &a, Array<T> &b)
495 {
496  Swap(a.data, b.data);
497  Swap(a.size, b.size);
498  Swap(a.allocsize, b.allocsize);
499  Swap(a.inc, b.inc);
500 }
501 
502 template <class T>
503 inline void Array<T>::SetSize(int nsize)
504 {
505  MFEM_ASSERT( nsize>=0, "Size must be non-negative. It is " << nsize );
506  if (nsize > abs(allocsize))
507  {
508  GrowSize(nsize, sizeof(T));
509  }
510  size = nsize;
511 }
512 
513 template <class T>
514 inline void Array<T>::SetSize(int nsize, const T &initval)
515 {
516  MFEM_ASSERT( nsize>=0, "Size must be non-negative. It is " << nsize );
517  if (nsize > size)
518  {
519  if (nsize > abs(allocsize))
520  {
521  GrowSize(nsize, sizeof(T));
522  }
523  for (int i = size; i < nsize; i++)
524  {
525  ((T*)data)[i] = initval;
526  }
527  }
528  size = nsize;
529 }
530 
531 template <class T>
532 inline T &Array<T>::operator[](int i)
533 {
534  MFEM_ASSERT( i>=0 && i<size,
535  "Access element " << i << " of array, size = " << size );
536  return ((T*)data)[i];
537 }
538 
539 template <class T>
540 inline const T &Array<T>::operator[](int i) const
541 {
542  MFEM_ASSERT( i>=0 && i<size,
543  "Access element " << i << " of array, size = " << size );
544  return ((T*)data)[i];
545 }
546 
547 template <class T>
548 inline int Array<T>::Append(const T &el)
549 {
550  SetSize(size+1);
551  ((T*)data)[size-1] = el;
552  return size;
553 }
554 
555 template <class T>
556 inline int Array<T>::Append(const Array<T> & els)
557 {
558  int old_size = size;
559 
560  SetSize(size + els.Size());
561  for (int i = 0; i < els.Size(); i++)
562  {
563  ((T*)data)[old_size+i] = els[i];
564  }
565  return size;
566 }
567 
568 template <class T>
569 inline int Array<T>::Prepend(const T &el)
570 {
571  SetSize(size+1);
572  for (int i = size-1; i > 0; i--)
573  {
574  ((T*)data)[i] = ((T*)data)[i-1];
575  }
576  ((T*)data)[0] = el;
577  return size;
578 }
579 
580 template <class T>
581 inline T &Array<T>::Last()
582 {
583  MFEM_ASSERT(size > 0, "Array size is zero: " << size);
584  return ((T*)data)[size-1];
585 }
586 
587 template <class T>
588 inline const T &Array<T>::Last() const
589 {
590  MFEM_ASSERT(size > 0, "Array size is zero: " << size);
591  return ((T*)data)[size-1];
592 }
593 
594 template <class T>
595 inline int Array<T>::Union(const T &el)
596 {
597  int i = 0;
598  while ((i < size) && (((T*)data)[i] != el)) { i++; }
599  if (i == size)
600  {
601  Append(el);
602  }
603  return i;
604 }
605 
606 template <class T>
607 inline int Array<T>::Find(const T &el) const
608 {
609  for (int i = 0; i < size; i++)
610  if (((T*)data)[i] == el)
611  {
612  return i;
613  }
614  return -1;
615 }
616 
617 template <class T>
618 inline void Array<T>::DeleteFirst(const T &el)
619 {
620  for (int i = 0; i < size; i++)
621  if (((T*)data)[i] == el)
622  {
623  for (i++; i < size; i++)
624  {
625  ((T*)data)[i-1] = ((T*)data)[i];
626  }
627  size--;
628  return;
629  }
630 }
631 
632 template <class T>
633 inline void Array<T>::DeleteAll()
634 {
635  if (allocsize > 0)
636  {
637  delete [] (char*)data;
638  }
639  data = NULL;
640  size = allocsize = 0;
641 }
642 
643 template <class T>
644 inline void Array<T>::MakeRef(T *p, int s)
645 {
646  if (allocsize > 0)
647  {
648  delete [] (char*)data;
649  }
650  data = p;
651  size = s;
652  allocsize = -s;
653 }
654 
655 template <class T>
656 inline void Array<T>::MakeRef(const Array &master)
657 {
658  if (allocsize > 0)
659  {
660  delete [] (char*)data;
661  }
662  data = master.data;
663  size = master.size;
664  allocsize = -abs(master.allocsize);
665  inc = master.inc;
666 }
667 
668 template <class T>
669 inline void Array<T>::GetSubArray(int offset, int sa_size, Array<T> &sa)
670 {
671  sa.SetSize(sa_size);
672  for (int i = 0; i < sa_size; i++)
673  {
674  sa[i] = (*this)[offset+i];
675  }
676 }
677 
678 template <class T>
679 inline void Array<T>::operator=(const T &a)
680 {
681  for (int i = 0; i < size; i++)
682  {
683  ((T*)data)[i] = a;
684  }
685 }
686 
687 template <class T>
688 inline void Array<T>::Assign(const T *p)
689 {
690  memcpy(data, p, Size()*sizeof(T));
691 }
692 
693 
694 template <class T>
695 inline const T &Array2D<T>::operator()(int i, int j) const
696 {
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
700  << ")." );
701  return array1d[i*N+j];
702 }
703 
704 template <class T>
705 inline T &Array2D<T>::operator()(int i, int j)
706 {
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
710  << ")." );
711  return array1d[i*N+j];
712 }
713 
714 template <class T>
715 inline const T *Array2D<T>::operator[](int i) const
716 {
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];
721 }
722 
723 template <class T>
724 inline T *Array2D<T>::operator[](int i)
725 {
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];
730 }
731 
732 
733 template <class T>
734 inline void Swap(Array2D<T> &a, Array2D<T> &b)
735 {
736  Swap(a.array1d, b.array1d);
737  Swap(a.N, b.N);
738 }
739 
740 
741 template <class T>
742 inline const T &Array3D<T>::operator()(int i, int j, int k) const
743 {
744  MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
745  && k >= 0 && k < N3,
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];
750 }
751 
752 template <class T>
753 inline T &Array3D<T>::operator()(int i, int j, int k)
754 {
755  MFEM_ASSERT(i >= 0 && i < array1d.Size() / N2 / N3 && j >= 0 && j < N2
756  && k >= 0 && k < N3,
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];
761 }
762 
763 
764 template<typename T>
766 {
767  mask = block_size-1;
768  MFEM_VERIFY(!(block_size & mask), "block_size must be a power of two.");
769 
770  size = shift = 0;
771  while ((1 << shift) < block_size) { shift++; }
772 }
773 
774 template<typename T>
776 {
777  blocks.SetSize(other.blocks.Size());
778 
779  size = other.size;
780  shift = other.shift;
781  mask = other.mask;
782 
783  int bsize = mask+1;
784  for (int i = 0; i < blocks.Size(); i++)
785  {
786  blocks[i] = (T*) new char[bsize * sizeof(T)];
787  }
788 
789  // copy all items
790  for (int i = 0; i < size; i++)
791  {
792  new (&At(i)) T(other[i]);
793  }
794 }
795 
796 template<typename T>
798 {
799  int bsize = mask+1;
800  if (size >= blocks.Size() * bsize)
801  {
802  T* new_block = (T*) new char[bsize * sizeof(T)];
803  blocks.Append(new_block);
804  }
805  return size++;
806 }
807 
808 template<typename T>
810 {
811  int index = Alloc();
812  new (&At(index)) T();
813  return index;
814 }
815 
816 template<typename T>
817 int BlockArray<T>::Append(const T &item)
818 {
819  int index = Alloc();
820  new (&At(index)) T(item);
821  return index;
822 }
823 
824 template<typename T>
826 {
827  mfem::Swap(blocks, other.blocks);
828  std::swap(size, other.size);
829  std::swap(shift, other.shift);
830  std::swap(mask, other.mask);
831 }
832 
833 template<typename T>
835 {
836  return blocks.Size()*(mask+1)*sizeof(T) +
837  blocks.MemoryUsage();
838 }
839 
840 template<typename T>
842 {
843  int bsize = size & mask;
844  for (int i = blocks.Size(); i != 0; )
845  {
846  T *block = blocks[--i];
847  for (int j = bsize; j != 0; )
848  {
849  block[--j].~T();
850  }
851  delete [] (char*) block;
852  bsize = mask+1;
853  }
854 }
855 
856 } // namespace mfem
857 
858 #endif
int Size() const
Logical size of the array.
Definition: array.hpp:110
const_iterator(const BlockArray *a)
Definition: array.hpp:453
void Load(std::istream &in, int fmt=0)
Read an Array from the stream in using format fmt. The format fmt can be:
Definition: array.cpp:93
void Unique()
Definition: array.hpp:216
int NumCols() const
Definition: array.hpp:287
const_iterator cbegin() const
Definition: array.hpp:465
~BaseArray()
Free the allocated memory.
Definition: array.cpp:35
iterator_base< const BlockArray, const T > base
Definition: array.hpp:449
const T * operator()(int i) const
Definition: array.hpp:295
void * data
Pointer to data.
Definition: array.hpp:32
Array(T *_data, int asize, int ainc=0)
Definition: array.hpp:79
T * GetRow(int i)
Definition: array.hpp:299
int Capacity() const
Return the current capacity of the BlockArray.
Definition: array.hpp:374
int Size() const
Return the number of items actually stored.
Definition: array.hpp:371
Base class for array container.
Definition: array.hpp:28
void Copy(Array &copy) const
Create a copy of the current array.
Definition: array.hpp:161
T * GetData()
Returns the data.
Definition: array.hpp:92
T * operator()(int i)
Definition: array.hpp:296
void operator=(const T &a)
Definition: array.hpp:304
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:
Definition: array.cpp:80
void Sort(Compare cmp)
Sorts the array using the supplied comparison function object.
Definition: array.hpp:212
void operator=(const T &a)
Definition: array.hpp:679
T Sum()
Sum all entries.
Definition: array.cpp:151
bool operator==(const iterator &other) const
Definition: array.hpp:441
void MakeRef(const Array2D &master)
Make this Array a reference to 'master'.
Definition: array.hpp:308
iterator & operator++()
Definition: array.hpp:439
T * end() const
Definition: array.hpp:238
void Load(int new_size, std::istream &in)
Set the Array size to new_size and read that many entries from the stream in.
Definition: array.hpp:196
const T & At(int index) const
Definition: array.hpp:360
T * begin() const
Definition: array.hpp:237
bool operator==(const const_iterator &other) const
Definition: array.hpp:458
void DeleteFirst(const T &el)
Delete the first 'el' entry.
Definition: array.hpp:618
T & operator[](int index)
Access item of the array.
Definition: array.hpp:367
void SetSize(int n1, int n2, int n3)
Definition: array.hpp:328
T Min() const
Find the minimal element in the array, using the comparison operator < for class T.
Definition: array.cpp:123
iterator(BlockArray *a)
Definition: array.hpp:436
void DeleteAll()
Delete whole array.
Definition: array.hpp:633
void GrowSize(int minsize, int elementsize)
Definition: array.cpp:43
iterator begin()
Definition: array.hpp:462
~Array()
Destructor.
Definition: array.hpp:83
void GetSubArray(int offset, int sa_size, Array< T > &sa)
Definition: array.hpp:669
int size
Size of the array.
Definition: array.hpp:34
void SetSize(int m, int n)
Definition: array.hpp:284
void Copy(Array2D &copy) const
Definition: array.hpp:301
bool OwnsData() const
Return true if the data will be deleted by the array.
Definition: array.hpp:97
const T & operator()(int i, int j) const
Definition: array.hpp:695
const T * GetData() const
Returns the data.
Definition: array.hpp:94
int Append(const T &el)
Append element to array, resize if necessary.
Definition: array.hpp:548
T & operator[](int i)
Access element.
Definition: array.hpp:532
void LoseData()
NULL-ifies the data.
Definition: array.hpp:104
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Definition: array.cpp:108
long MemoryUsage() const
Definition: array.hpp:834
void MakeDataOwner()
Make the Array own the data.
Definition: array.hpp:107
void Reserve(int capacity)
Ensures that the allocated size is at least the given size.
Definition: array.hpp:123
bool operator!=(const Array< T > &LHS, const Array< T > &RHS)
Definition: array.hpp:259
void Assign(const T *)
Copy data from a pointer. Size() elements are copied.
Definition: array.hpp:688
long MemoryUsage() const
Definition: array.hpp:240
void Sort()
Sorts the array. This requires operator< to be defined for T.
Definition: array.hpp:208
int allocsize
Size of the allocated memory.
Definition: array.hpp:36
void StealData(T **p)
Changes the ownership of the data.
Definition: array.hpp:100
int Union(const T &el)
Append element when it is not yet in the array, return index.
Definition: array.hpp:595
void Print(std::ostream &out=mfem::out, int width=4)
Prints array to stream with width elements per row.
Definition: array.cpp:180
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:494
bool operator==(const Array< T > &LHS, const Array< T > &RHS)
Definition: array.hpp:250
int IsSorted()
return true if the array is sorted.
Definition: array.cpp:163
BlockArray(int block_size=16 *1024)
Definition: array.hpp:765
int Find(const T &el) const
Return the first index where 'el' is found; return -1 if not found.
Definition: array.hpp:607
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
Definition: array.hpp:503
const T * operator[](int i) const
Definition: array.hpp:715
void PartialSum()
Partial Sum.
Definition: array.cpp:139
void DeleteLast()
Delete the last entry.
Definition: array.hpp:152
const T & operator[](int index) const
Definition: array.hpp:368
Array2D(int m, int n)
Definition: array.hpp:282
bool operator!=(const iterator &other) const
Definition: array.hpp:442
void CheckIndex(int index) const
Definition: array.hpp:475
Array< T * > blocks
Definition: array.hpp:470
int Capacity() const
Definition: array.hpp:120
bool operator!=(const const_iterator &other) const
Definition: array.hpp:459
const_iterator & operator++()
Definition: array.hpp:456
T & Last()
Return the last element in the array.
Definition: array.hpp:581
void Print(std::ostream &out=mfem::out, int width=4) const
Prints array to stream with width elements per row.
Definition: array.cpp:63
iterator_base< BlockArray, T > base
Definition: array.hpp:432
void MakeRef(T *, int)
Make this Array a reference to a pointer.
Definition: array.hpp:644
iterator end()
Definition: array.hpp:463
const_iterator cend() const
Definition: array.hpp:467
const T & operator()(int i, int j, int k) const
Definition: array.hpp:742
Array3D(int n1, int n2, int n3)
Definition: array.hpp:325
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64
Array(int asize=0, int ainc=0)
Creates array of asize elements.
Definition: array.hpp:73
T & At(int index)
Access item of the array.
Definition: array.hpp:355
const T * GetRow(int i) const
Definition: array.hpp:298
int NumRows() const
Definition: array.hpp:286
int Prepend(const T &el)
Prepend an element to the array, resize if necessary.
Definition: array.hpp:569
int Append()
Allocate and construct a new item in the array, return its index.
Definition: array.hpp:809
void Swap(BlockArray< T > &other)
Definition: array.hpp:825