Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Array.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/basic.h>
36 #include <ogdf/basic/comparer.h>
37 #include <ogdf/basic/exceptions.h>
38 #include <ogdf/basic/memory.h>
39 
40 #include <algorithm>
41 #include <cstdlib>
42 #include <initializer_list>
43 #include <new>
44 #include <ostream>
45 #include <random>
46 #include <type_traits>
47 
48 namespace ogdf {
49 
50 template<class E, bool isConst>
52 template<class E, class INDEX>
54 
55 template<class E>
56 using ArrayConstIterator = const E*;
57 template<class E>
58 using ArrayIterator = E*;
59 template<class E>
61 template<class E>
63 
65 
73 template<class E, bool isConst>
75  friend class ArrayReverseIteratorBase<E, !isConst>;
76 
79 
82 
83 public:
85  ArrayReverseIteratorBase(E* pX) : m_pX(pX) { }
86 
89  ArrayReverseIteratorBase(const E* pX) : m_pX(pX) { }
90 
93 
98 
103 
105  operator std::conditional<isConst, const E, E>*() const { return m_pX; }
106 
109  return m_pX == it.m_pX;
110  }
111 
114  return m_pX != it.m_pX;
115  }
116 
118  Elem& operator*() const { return *m_pX; }
119 
122  m_pX = it.m_pX;
123  return *this;
124  }
125 
128  m_pX--;
129  return *this;
130  }
131 
135  m_pX--;
136  return it;
137  }
138 
141  m_pX++;
142  return *this;
143  }
144 
148  m_pX++;
149  return it;
150  }
151 
154  m_pX -= rhs;
155  return *this;
156  }
157 
160  m_pX += rhs;
161  return *this;
162  }
163 
167  }
168 
173  return ArrayReverseIteratorBase<E, isConst>(rhs.m_pX - lhs);
174  }
175 
179  }
180 
182  template<bool isArgConst>
184  return rhs.m_pX - m_pX;
185  }
186 
188  bool operator<(ArrayReverseIteratorBase<E, isConst>& it) const { return m_pX > it.m_pX; }
189 
191  bool operator>(ArrayReverseIteratorBase<E, isConst>& it) const { return m_pX < it.m_pX; }
192 
194  bool operator<=(ArrayReverseIteratorBase<E, isConst>& it) const { return m_pX >= it.m_pX; }
195 
197  bool operator>=(ArrayReverseIteratorBase<E, isConst>& it) const { return m_pX <= it.m_pX; }
198 
200  Elem& operator[](std::size_t idx) { return m_pX[-idx]; }
201 
203  const Elem& operator[](std::size_t idx) const { return m_pX[-idx]; }
204 
206 };
207 
209 
218 template<class E, class INDEX = int>
219 class Array {
220 public:
223  static const int maxSizeInsertionSort = 40;
224 
226  using value_type = E;
228  using reference = E&;
230  using const_reference = const E&;
239 
241  Array() { construct(0, -1); }
242 
244  explicit Array(INDEX s) : Array(0, s - 1) { }
245 
247  Array(INDEX a, INDEX b) {
248  construct(a, b);
249  initialize();
250  }
251 
253  Array(INDEX a, INDEX b, const E& x) {
254  construct(a, b);
255  initialize(x);
256  }
257 
259 
262  Array(std::initializer_list<E> initList) {
263  construct(0, ((INDEX)initList.size()) - 1);
264  initialize(initList);
265  }
266 
268  Array(const Array<E, INDEX>& A) { copy(A); }
269 
271 
274  Array(Array<E, INDEX>&& A) noexcept
275  : m_vpStart(A.m_vpStart)
276  , m_pStart(A.m_pStart)
277  , m_pStop(A.m_pStop)
278  , m_low(A.m_low)
279  , m_high(A.m_high) {
280  A.construct(0, -1);
281  }
282 
284  Array(const ArrayBuffer<E, INDEX>& A);
285 
288 
293 
296  INDEX low() const { return m_low; }
297 
299  INDEX high() const { return m_high; }
300 
302  INDEX size() const { return m_high - m_low + 1; }
303 
305  bool empty() const { return size() == 0; }
306 
308  const_reference operator[](INDEX i) const {
309  OGDF_ASSERT(m_low <= i);
310  OGDF_ASSERT(i <= m_high);
311  return m_vpStart[i];
312  }
313 
316  OGDF_ASSERT(m_low <= i);
317  OGDF_ASSERT(i <= m_high);
318  return m_vpStart[i];
319  }
320 
322 
326 
329  iterator begin() { return m_pStart; }
330 
332  const_iterator begin() const { return m_pStart; }
333 
335  const_iterator cbegin() const { return m_pStart; }
336 
338  iterator end() { return m_pStop; }
339 
341  const_iterator end() const { return m_pStop; }
342 
344  const_iterator cend() const { return m_pStop; }
345 
347  reverse_iterator rbegin() { return m_pStop - 1; }
348 
350  const_reverse_iterator rbegin() const { return m_pStop - 1; }
351 
353  const_reverse_iterator crbegin() const { return m_pStop - 1; }
354 
356  reverse_iterator rend() { return m_pStart - 1; }
357 
359  const_reverse_iterator rend() const { return m_pStart - 1; }
360 
362  const_reverse_iterator crend() const { return m_pStart - 1; }
363 
365 
369 
372  void init() {
373  deconstruct();
374  construct(0, -1);
375  }
376 
378 
381  void init(INDEX s) { init(0, s - 1); }
382 
384 
387  void init(INDEX a, INDEX b) {
388  deconstruct();
389  construct(a, b);
390  initialize();
391  }
392 
394  void init(INDEX a, INDEX b, const E& x) {
395  deconstruct();
396  construct(a, b);
397  initialize(x);
398  }
399 
401  void fill(const E& x) {
402  E* pDest = m_pStop;
403  while (pDest > m_pStart) {
404  *--pDest = x;
405  }
406  }
407 
409  void fill(INDEX i, INDEX j, const E& x) {
410  OGDF_ASSERT(m_low <= i);
411  OGDF_ASSERT(i <= m_high);
412  OGDF_ASSERT(m_low <= j);
413  OGDF_ASSERT(j <= m_high);
414 
415  E *pI = m_vpStart + i, *pJ = m_vpStart + j + 1;
416  while (pJ > pI) {
417  *--pJ = x;
418  }
419  }
420 
422 
427  void grow(INDEX add, const E& x);
428 
430 
434  void grow(INDEX add);
435 
437 
442  void resize(INDEX newSize, const E& x) { grow(newSize - size(), x); }
443 
445 
449  void resize(INDEX newSize) { grow(newSize - size()); }
450 
453  deconstruct();
454  copy(A);
455  return *this;
456  }
457 
459 
463  deconstruct();
464 
465  m_vpStart = A.m_vpStart;
466  m_pStart = A.m_pStart;
467  m_pStop = A.m_pStop;
468  m_low = A.m_low;
469  m_high = A.m_high;
470 
471  A.construct(0, -1);
472  return *this;
473  }
474 
478 
480  bool operator==(const Array<E, INDEX>& L) const {
481  if (size() != L.size()) {
482  return false;
483  }
484 
485  auto thisIterator = begin();
486  auto thatIterator = L.begin();
487 
488  while (thisIterator != end() && thatIterator != L.end()) {
489  if (*thisIterator != *thatIterator) {
490  return false;
491  }
492  ++thisIterator;
493  ++thatIterator;
494  }
495 
496  OGDF_ASSERT(thisIterator == end() && thatIterator == L.end());
497  return true;
498  }
499 
501  bool operator!=(const Array<E, INDEX>& L) const { return !operator==(L); }
502 
504 
508 
511  void swap(INDEX i, INDEX j) {
512  OGDF_ASSERT(m_low <= i);
513  OGDF_ASSERT(i <= m_high);
514  OGDF_ASSERT(m_low <= j);
515  OGDF_ASSERT(j <= m_high);
516 
517  std::swap(m_vpStart[i], m_vpStart[j]);
518  }
519 
521  void permute(INDEX l, INDEX r) {
522  std::minstd_rand rng(randomSeed());
523  permute(l, r, rng);
524  }
525 
527  void permute() { permute(low(), high()); }
528 
535  template<class RNG>
536  void permute(INDEX l, INDEX r, RNG& rng);
537 
542  template<class RNG>
543  void permute(RNG& rng) {
544  if (!empty()) {
545  permute(low(), high(), rng);
546  }
547  }
548 
550 
554 
557 
561  inline int binarySearch(const E& e) const {
562  return binarySearch(low(), high(), e, StdComparer<E>());
563  }
564 
566 
570  inline int binarySearch(INDEX l, INDEX r, const E& e) const {
571  return binarySearch(l, r, e, StdComparer<E>());
572  }
573 
575 
579  template<class COMPARER>
580  inline int binarySearch(const E& e, const COMPARER& comp) const {
581  return binarySearch(low(), high(), e, comp);
582  }
583 
585 
589  template<class COMPARER>
590  INDEX binarySearch(INDEX l, INDEX r, const E& e, const COMPARER& comp) const {
591  if (r < l) {
592  return low() - 1;
593  }
594  while (r > l) {
595  INDEX m = (r + l) / 2;
596  if (comp.greater(e, m_vpStart[m])) {
597  l = m + 1;
598  } else {
599  r = m;
600  }
601  }
602  return comp.equal(e, m_vpStart[l]) ? l : low() - 1;
603  }
604 
606 
611  inline INDEX linearSearch(const E& e) const {
612  int i;
613  for (i = size(); i-- > 0;) {
614  if (e == m_pStart[i]) {
615  break;
616  }
617  }
618  return i + low();
619  }
620 
622 
627  template<class COMPARER>
628  INDEX linearSearch(const E& e, const COMPARER& comp) const {
629  int i;
630  for (i = size(); i-- > 0;) {
631  if (comp.equal(e, m_pStart[i])) {
632  break;
633  }
634  }
635  return i + low();
636  }
637 
639  inline void quicksort() { quicksort(StdComparer<E>()); }
640 
642  inline void quicksort(INDEX l, INDEX r) { quicksort(l, r, StdComparer<E>()); }
643 
645 
648  template<class COMPARER>
649  inline void quicksort(const COMPARER& comp) {
650  if (low() < high()) {
651  quicksortInt(m_pStart, m_pStop - 1, comp);
652  }
653  }
654 
656 
661  template<class COMPARER>
662  void quicksort(INDEX l, INDEX r, const COMPARER& comp) {
663  OGDF_ASSERT(low() <= l);
664  OGDF_ASSERT(l <= high());
665  OGDF_ASSERT(low() <= r);
666  OGDF_ASSERT(r <= high());
667  if (l < r) {
668  quicksortInt(m_vpStart + l, m_vpStart + r, comp);
669  }
670  }
671 
673 
685 
687 
697  void leftShift(ArrayBuffer<INDEX, INDEX>& ind, const E& val) {
698  leftShift(ind);
699  fill(high() - ind.size(), high(), val);
700  }
701 
703 
704  template<class F, class I>
705  friend class ArrayBuffer; // for efficient ArrayBuffer::compact-method
706 
707 private:
709  E* m_pStart;
710  E* m_pStop;
711  INDEX m_low;
712  INDEX m_high;
713 
715  void construct(INDEX a, INDEX b);
716 
718  void initialize();
719 
721  void initialize(const E& x);
722 
724  void initialize(std::initializer_list<E> initList);
725 
727  void deconstruct();
728 
730  void copy(const Array<E, INDEX>& A);
731 
733  void expandArray(INDEX add);
734 
736  template<typename EE = E, typename std::enable_if<OGDF_TRIVIALLY_COPYABLE<EE>::value, int>::type = 0>
737  void expandArrayHelper(INDEX sOld, INDEX sNew) {
738  // If the element type is trivially copiable, just use realloc.
739  E* p = static_cast<E*>(realloc(m_pStart, sNew * sizeof(E)));
740  if (p == nullptr) {
742  }
743  m_pStart = p;
744  }
745 
747  template<typename EE = E, typename std::enable_if<!OGDF_TRIVIALLY_COPYABLE<EE>::value, int>::type = 0>
748  void expandArrayHelper(INDEX sOld, INDEX sNew) {
749  // If the element type is not trivially copiable,
750  // allocate a new block, move the elements, and free the old block.
751  E* p = static_cast<E*>(malloc(sNew * sizeof(E)));
752  if (p == nullptr) {
754  }
755 
756  for (int i = 0; i < min(sOld, sNew); ++i) {
757  new (&p[i]) E(std::move(m_pStart[i]));
758  }
759 
760  deconstruct();
761  m_pStart = p;
762  }
763 
765  template<class COMPARER>
766  static void quicksortInt(E* pL, E* pR, const COMPARER& comp) {
767  size_t s = pR - pL;
768 
769  // use insertion sort for small instances
770  if (s < maxSizeInsertionSort) {
771  for (E* pI = pL + 1; pI <= pR; pI++) {
772  E v = *pI;
773  E* pJ = pI;
774  while (--pJ >= pL && comp.less(v, *pJ)) {
775  *(pJ + 1) = *pJ;
776  }
777  *(pJ + 1) = v;
778  }
779  return;
780  }
781 
782  E *pI = pL, *pJ = pR;
783  E x = *(pL + (s >> 1));
784 
785  do {
786  while (comp.less(*pI, x)) {
787  pI++;
788  }
789  while (comp.less(x, *pJ)) {
790  pJ--;
791  }
792  if (pI <= pJ) {
793  std::swap(*pI++, *pJ--);
794  }
795  } while (pI <= pJ);
796 
797  if (pL < pJ) {
798  quicksortInt(pL, pJ, comp);
799  }
800  if (pI < pR) {
801  quicksortInt(pI, pR, comp);
802  }
803  }
804 
806 };
807 
808 // enlarges storage for array and moves old entries
809 template<class E, class INDEX>
811  INDEX sOld = size(), sNew = sOld + add;
812 
813  // expand allocated memory block
814  if (m_pStart != nullptr) {
815  expandArrayHelper(sOld, sNew);
816  } else {
817  m_pStart = static_cast<E*>(malloc(sNew * sizeof(E)));
818  if (m_pStart == nullptr) {
820  }
821  }
822 
823  m_vpStart = m_pStart - m_low;
824  m_pStop = m_pStart + sNew;
825  m_high += add;
826 }
827 
828 // enlarges array by add elements and sets new elements to x
829 template<class E, class INDEX>
830 void Array<E, INDEX>::grow(INDEX add, const E& x) {
831  if (add == 0) {
832  return;
833  }
834 
835  INDEX sOld = size();
836  expandArray(add);
837 
838  // initialize new array entries
839  for (E* pDest = m_pStart + sOld; pDest < m_pStop; pDest++) {
840  new (pDest) E(x);
841  }
842 }
843 
844 // enlarges array by add elements (initialized with default constructor)
845 template<class E, class INDEX>
846 void Array<E, INDEX>::grow(INDEX add) {
847  if (add == 0) {
848  return;
849  }
850 
851  INDEX sOld = size();
852  expandArray(add);
853 
854  // initialize new array entries
855  for (E* pDest = m_pStart + sOld; pDest < m_pStop; pDest++) {
856  new (pDest) E();
857  }
858 }
859 
860 template<class E, class INDEX>
861 void Array<E, INDEX>::construct(INDEX a, INDEX b) {
862  m_low = a;
863  m_high = b;
864  INDEX s = b - a + 1;
865 
866  if (s < 1) {
867  m_pStart = m_vpStart = m_pStop = nullptr;
868 
869  } else {
870  m_pStart = static_cast<E*>(malloc(s * sizeof(E)));
871  if (m_pStart == nullptr) {
873  }
874 
875  m_vpStart = m_pStart - a;
876  m_pStop = m_pStart + s;
877  }
878 }
879 
880 template<class E, class INDEX>
882  E* pDest = m_pStart;
883  try {
884  for (; pDest < m_pStop; pDest++) {
885  new (pDest) E();
886  }
887  } catch (...) {
888  while (--pDest >= m_pStart) {
889  pDest->~E();
890  }
891  free(m_pStart);
892  throw;
893  }
894 }
895 
896 template<class E, class INDEX>
897 void Array<E, INDEX>::initialize(const E& x) {
898  E* pDest = m_pStart;
899  try {
900  for (; pDest < m_pStop; pDest++) {
901  new (pDest) E(x);
902  }
903  } catch (...) {
904  while (--pDest >= m_pStart) {
905  pDest->~E();
906  }
907  free(m_pStart);
908  throw;
909  }
910 }
911 
912 template<class E, class INDEX>
913 void Array<E, INDEX>::initialize(std::initializer_list<E> initList) {
914  E* pDest = m_pStart;
915  try {
916  for (const E& x : initList) {
917  new (pDest++) E(x);
918  }
919  } catch (...) {
920  while (--pDest >= m_pStart) {
921  pDest->~E();
922  }
923  free(m_pStart);
924  throw;
925  }
926 }
927 
928 template<class E, class INDEX>
930  if (!std::is_trivially_destructible<E>::value) {
931  for (E* pDest = m_pStart; pDest < m_pStop; pDest++) {
932  pDest->~E();
933  }
934  }
935  free(m_pStart);
936 }
937 
938 template<class E, class INDEX>
940  construct(array2.m_low, array2.m_high);
941 
942  if (m_pStart != nullptr) {
943  E* pSrc = array2.m_pStop;
944  E* pDest = m_pStop;
945  while (pDest > m_pStart)
946 #if 0
947  *--pDest = *--pSrc;
948 #endif
949  new (--pDest) E(*--pSrc);
950  }
951 }
952 
953 // permutes array a from a[l] to a[r] randomly
954 template<class E, class INDEX>
955 template<class RNG>
956 void Array<E, INDEX>::permute(INDEX l, INDEX r, RNG& rng) {
957  OGDF_ASSERT(low() <= l);
958  OGDF_ASSERT(l <= high());
959  OGDF_ASSERT(low() <= r);
960  OGDF_ASSERT(r <= high());
961 
962  std::uniform_int_distribution<int> dist(0, r - l);
963 
964  E *pI = m_vpStart + l, *pStart = m_vpStart + l, *pStop = m_vpStart + r;
965  while (pI <= pStop) {
966  std::swap(*pI++, *(pStart + dist(rng)));
967  }
968 }
969 
971 template<class E, class INDEX>
972 void print(std::ostream& os, const Array<E, INDEX>& a, char delim = ' ') {
973  for (int i = a.low(); i <= a.high(); i++) {
974  if (i > a.low()) {
975  os << delim;
976  }
977  os << a[i];
978  }
979 }
980 
982 template<class E, class INDEX>
983 std::ostream& operator<<(std::ostream& os, const ogdf::Array<E, INDEX>& a) {
984  print(os, a);
985  return os;
986 }
987 
988 }
989 
990 namespace ogdf {
991 
993 template<class E, class INDEX>
995  const INDEX nInd = ind.size();
996  if (nInd == 0) {
997  return;
998  }
999 
1000  OGDF_ASSERT(ind[0] >= low());
1001  OGDF_ASSERT(ind[0] <= high());
1002 
1003  INDEX j, current = ind[0];
1004  for (INDEX i = 0; i < nInd - 1; i++) {
1005  OGDF_ASSERT(ind[i + 1] >= low());
1006  OGDF_ASSERT(ind[i + 1] <= high());
1007 
1008  const INDEX last = ind[i + 1];
1009  for (j = ind[i] + 1; j < last; j++) {
1010  m_vpStart[current++] = m_vpStart[j];
1011  }
1012  }
1013 
1015  for (j = ind[nInd - 1] + 1; j < size(); j++) {
1016  m_vpStart[current++] = m_vpStart[j];
1017  }
1018 }
1019 
1020 template<class E, class INDEX>
1022  construct(0, -1);
1023  A.compactCopy(*this);
1024 }
1025 
1026 }
ogdf::Array::quicksort
void quicksort(INDEX l, INDEX r)
Sorts subarray with index set [l, ..., r] using Quicksort.
Definition: Array.h:642
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:53
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::ArrayReverseIteratorBase::operator=
ArrayReverseIteratorBase< E, isConst > & operator=(const ArrayReverseIteratorBase< E, isConst > &it)
Assignment operator.
Definition: Array.h:121
ogdf::Array::resize
void resize(INDEX newSize)
Resizes (enlarges or shrinks) the array to hold newSize elements.
Definition: Array.h:449
ogdf::Array::m_vpStart
E * m_vpStart
The virtual start of the array (address of A[0]).
Definition: Array.h:708
ogdf::Array::m_high
INDEX m_high
The highest index.
Definition: Array.h:712
ogdf::Array::Array
Array(INDEX a, INDEX b, const E &x)
Creates an array with index set [a..b] and initializes each element with x.
Definition: Array.h:253
exceptions.h
Definition of exception classes.
ogdf::ArrayReverseIteratorBase::operator>
bool operator>(ArrayReverseIteratorBase< E, isConst > &it) const
Greater-than operator.
Definition: Array.h:191
ogdf::InsufficientMemoryException
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:209
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::Array::operator!=
bool operator!=(const Array< E, INDEX > &L) const
Inequality operator.
Definition: Array.h:501
ogdf::ArrayReverseIteratorBase::ArrayReverseIteratorBase
ArrayReverseIteratorBase(const ArrayReverseIteratorBase< E, isConst > &it)
Copy constructor. clang10 does not see the above templated one match this case and requires it explic...
Definition: Array.h:101
ogdf::ArrayReverseIteratorBase::operator+=
ArrayReverseIteratorBase< E, isConst > & operator+=(const int &rhs)
Compound assignment operator (+).
Definition: Array.h:153
ogdf::ArrayReverseIteratorBase::operator!=
bool operator!=(const ArrayReverseIteratorBase< E, isConst > &it) const
Inequality operator.
Definition: Array.h:113
ogdf::StdComparer
Standard comparer (valid as a static comparer).
Definition: comparer.h:40
ogdf::Array::operator[]
reference operator[](INDEX i)
Returns a reference to the element at position i.
Definition: Array.h:315
ogdf::Array::Array
Array(INDEX s)
Creates an array with index set [0..s-1].
Definition: Array.h:244
ogdf::ArrayReverseIteratorBase::operator<
bool operator<(ArrayReverseIteratorBase< E, isConst > &it) const
Less-than operator.
Definition: Array.h:188
ogdf::Array::construct
void construct(INDEX a, INDEX b)
Allocates new array with index set [a, ..., b].
Definition: Array.h:861
ogdf::Array::initialize
void initialize()
Initializes elements with default constructor.
Definition: Array.h:881
ogdf::whaType::A
@ A
ogdf::ArrayIterator
E * ArrayIterator
Definition: Array.h:58
ogdf::Array::high
INDEX high() const
Returns the maximal array index.
Definition: Array.h:299
ogdf::Array::Array
Array(const Array< E, INDEX > &A)
Creates an array that is a copy of A.
Definition: Array.h:268
ogdf::ArrayReverseIteratorBase::ArrayReverseIteratorBase
ArrayReverseIteratorBase(E *pX)
Constructs an iterator that points to E* pX.
Definition: Array.h:85
ogdf::Array< PathType >::const_reference
const PathType & const_reference
Provides a reference to a const element stored in an array for reading and performing const operation...
Definition: Array.h:230
ogdf::Array::swap
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
Definition: Array.h:511
ogdf::Array::deconstruct
void deconstruct()
Deallocates array.
Definition: Array.h:929
ogdf::Array::Array
Array(Array< E, INDEX > &&A) noexcept
Creates an array containing the elements of A (move semantics).
Definition: Array.h:274
ogdf::ArrayReverseIteratorBase
Random-access reverse iterator based on a pointer to an array element.
Definition: Array.h:51
ogdf::Array::leftShift
void leftShift(ArrayBuffer< INDEX, INDEX > &ind)
Removes the components listed in ind by shifting the remaining components to the left.
Definition: Array.h:994
ogdf::Array::permute
void permute()
Randomly permutes the array.
Definition: Array.h:527
ogdf::ArrayReverseIteratorBase::operator[]
const Elem & operator[](std::size_t idx) const
Const member access operator.
Definition: Array.h:203
ogdf::Array::Array
Array()
Creates an array with empty index set.
Definition: Array.h:241
ogdf::Array< PathType >::iterator
ArrayIterator< PathType > iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition: Array.h:234
ogdf::ArrayReverseIteratorBase::operator-
ArrayReverseIteratorBase< E, isConst > operator-(const int &rhs)
Subtraction operator with int on the right-hand side.
Definition: Array.h:177
ogdf::Array::m_pStop
E * m_pStop
Successor of last element (address of A[m_high+1]).
Definition: Array.h:710
ogdf::Array::init
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:372
ogdf::Array::end
const_iterator end() const
Returns a const iterator to one past the last element.
Definition: Array.h:341
OGDF_NEW_DELETE
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition: memory.h:85
ogdf::ArrayReverseIteratorBase::operator--
ArrayReverseIteratorBase< E, isConst > & operator--()
Decrement operator (prefix).
Definition: Array.h:140
ogdf::Array::expandArray
void expandArray(INDEX add)
Used by grow() to enlarge the array.
Definition: Array.h:810
ogdf::Array::crend
const_reverse_iterator crend() const
Returns a const reverse iterator to one before the first element.
Definition: Array.h:362
ogdf::Array::operator=
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:452
ogdf::ArrayReverseIteratorBase::ArrayReverseIteratorBase
ArrayReverseIteratorBase(const E *pX)
Constructs an iterator that points to const E* pX.
Definition: Array.h:89
ogdf::Array::leftShift
void leftShift(ArrayBuffer< INDEX, INDEX > &ind, const E &val)
Removes the components listed in ind by shifting the remaining components to the left.
Definition: Array.h:697
ogdf::Array::permute
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
Definition: Array.h:521
ogdf::Array::init
void init(INDEX a, INDEX b, const E &x)
Reinitializes the array to an array with index set [a..b] and sets all entries to x.
Definition: Array.h:394
ogdf::Array::m_low
INDEX m_low
The lowest index.
Definition: Array.h:711
r
int r[]
Definition: hierarchical-ranking.cpp:13
ogdf::Array::quicksort
void quicksort(INDEX l, INDEX r, const COMPARER &comp)
Sorts the subarray with index set [l, ..., r] using Quicksort and a user-defined comparer comp.
Definition: Array.h:662
ogdf::ArrayReverseIteratorBase::operator--
ArrayReverseIteratorBase< E, isConst > operator--(int)
Decrement operator (postfix).
Definition: Array.h:146
ogdf::Array::quicksortInt
static void quicksortInt(E *pL, E *pR, const COMPARER &comp)
Internal Quicksort implementation with comparer template.
Definition: Array.h:766
ogdf::ArrayReverseIteratorBase::operator-
int operator-(ArrayReverseIteratorBase< E, isArgConst > &rhs)
Subtraction operator.
Definition: Array.h:183
ogdf::ArrayReverseIteratorBase::operator+
ArrayReverseIteratorBase< E, isConst > operator+(const int &rhs)
Addition operator with int on the right-hand side.
Definition: Array.h:165
ogdf::Array::init
void init(INDEX s)
Reinitializes the array to an array with index set [0..s-1].
Definition: Array.h:381
backward::Color::type
type
Definition: backward.hpp:1716
ogdf::ArrayReverseIteratorBase::operator++
ArrayReverseIteratorBase< E, isConst > operator++(int)
Increment operator (postfix).
Definition: Array.h:133
OGDF_THROW
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:74
ogdf::Array::binarySearch
int binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
Definition: Array.h:580
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:243
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:219
ogdf::Array::binarySearch
INDEX binarySearch(INDEX l, INDEX r, const E &e, const COMPARER &comp) const
Performs a binary search for element e within the array section [l, ..., r] with comparer comp.
Definition: Array.h:590
ogdf::ArrayReverseIteratorBase::operator++
ArrayReverseIteratorBase< E, isConst > & operator++()
Increment operator (prefix).
Definition: Array.h:127
ogdf::Array::expandArrayHelper
void expandArrayHelper(INDEX sOld, INDEX sNew)
Used by expandArray() to reallocate the array elements.
Definition: Array.h:737
ogdf::Array::rbegin
reverse_iterator rbegin()
Returns an reverse iterator to the last element.
Definition: Array.h:347
ogdf::Array::rbegin
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Definition: Array.h:350
ogdf::Array::operator[]
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:308
ogdf::ArrayReverseIteratorBase::operator+
friend ArrayReverseIteratorBase< E, isConst > operator+(const int &lhs, ArrayReverseIteratorBase< E, isConst > rhs)
Addition operator with int on the left-hand side. Returns the same result as addition with int on the...
Definition: Array.h:171
ogdf::ArrayBuffer::size
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:244
ogdf::Array::fill
void fill(const E &x)
Sets all elements to x.
Definition: Array.h:401
ogdf::Array< PathType >::reference
PathType & reference
Provides a reference to an element stored in an array.
Definition: Array.h:228
ogdf::Array::init
void init(INDEX a, INDEX b)
Reinitializes the array to an array with index set [a..b].
Definition: Array.h:387
ogdf::ArrayReverseIteratorBase::ArrayReverseIteratorBase
ArrayReverseIteratorBase(const ArrayReverseIteratorBase< E, isArgConst > &it)
Constructs an iterator that is a copy of it.
Definition: Array.h:96
ogdf::Array::begin
const_iterator begin() const
Returns a const iterator to the first element.
Definition: Array.h:332
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:983
ogdf::Array::begin
iterator begin()
Returns an iterator to the first element.
Definition: Array.h:329
ogdf::ArrayReverseIteratorBase::operator*
Elem & operator*() const
Returns the element this iterator points to.
Definition: Array.h:118
ogdf::Array::rend
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
Definition: Array.h:356
ogdf::Array::operator==
bool operator==(const Array< E, INDEX > &L) const
Equality operator.
Definition: Array.h:480
ogdf::ArrayReverseIteratorBase::operator<=
bool operator<=(ArrayReverseIteratorBase< E, isConst > &it) const
Less-than-or-equals operator.
Definition: Array.h:194
ogdf::Array::fill
void fill(INDEX i, INDEX j, const E &x)
Sets elements in the intervall [i..j] to x.
Definition: Array.h:409
ogdf::Array::end
iterator end()
Returns an iterator to one past the last element.
Definition: Array.h:338
ogdf::Array::quicksort
void quicksort(const COMPARER &comp)
Sorts array using Quicksort and a user-defined comparer comp.
Definition: Array.h:649
ogdf::Array::cend
const_iterator cend() const
Returns a const iterator to one past the last element.
Definition: Array.h:344
ogdf::ArrayReverseIteratorBase::operator[]
Elem & operator[](std::size_t idx)
Member access operator.
Definition: Array.h:200
ogdf::randomSeed
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
ogdf::ArrayReverseIteratorBase::ArrayReverseIteratorBase
ArrayReverseIteratorBase()
Constructs an invalid iterator.
Definition: Array.h:92
ogdf::Array::resize
void resize(INDEX newSize, const E &x)
Resizes (enlarges or shrinks) the array to hold newSize elements and sets new elements to x.
Definition: Array.h:442
ogdf::Array::operator=
Array< E, INDEX > & operator=(Array< E, INDEX > &&A)
Assignment operator (move semantics).
Definition: Array.h:462
ogdf::Array::m_pStart
E * m_pStart
The real start of the array (address of A[m_low]).
Definition: Array.h:709
ogdf::Array< PathType >::const_iterator
ArrayConstIterator< PathType > const_iterator
Provides a random-access iterator that can read a const element in an array.
Definition: Array.h:232
basic.h
Basic declarations, included by all source files.
ogdf::ArrayReverseIteratorBase::Elem
typename std::conditional< isConst, const E, E >::type Elem
The underlying element, depending on isConst.
Definition: Array.h:78
ogdf::Array::permute
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
Definition: Array.h:543
ogdf::Array::binarySearch
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition: Array.h:561
ogdf::Array::crbegin
const_reverse_iterator crbegin() const
Returns a const reverse iterator to the last element.
Definition: Array.h:353
ogdf::Array::binarySearch
int binarySearch(INDEX l, INDEX r, const E &e) const
Performs a binary search for element e within the array section [l, ..., r] .
Definition: Array.h:570
ogdf::print
void print(std::ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
Definition: Array.h:972
ogdf::ArrayReverseIteratorBase::operator==
bool operator==(const ArrayReverseIteratorBase< E, isConst > &it) const
Equality operator.
Definition: Array.h:108
ogdf::Array::low
INDEX low() const
Returns the minimal array index.
Definition: Array.h:296
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:639
ogdf::Array::size
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:302
ogdf::Array::grow
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:830
ogdf::Array::~Array
~Array()
Destruction.
Definition: Array.h:287
ogdf::ArrayReverseIteratorBase::m_pX
Elem * m_pX
The pointer to the array element.
Definition: Array.h:81
ogdf::Array::copy
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition: Array.h:939
ogdf::Array::maxSizeInsertionSort
static const int maxSizeInsertionSort
Threshold used by quicksort() such that insertion sort is called for instances smaller than maxSizeIn...
Definition: Array.h:223
ogdf::ArrayReverseIteratorBase::operator>=
bool operator>=(ArrayReverseIteratorBase< E, isConst > &it) const
Greater-than-or-equals operator.
Definition: Array.h:197
ogdf::ArrayReverseIteratorBase::operator-=
ArrayReverseIteratorBase< E, isConst > & operator-=(const int &rhs)
Compound assignment operator (-).
Definition: Array.h:159
ogdf::Array::linearSearch
INDEX linearSearch(const E &e, const COMPARER &comp) const
Performs a linear search for element e with comparer comp.
Definition: Array.h:628
comparer.h
Declarations for Comparer objects.
memory.h
Declaration of memory manager for allocating small pieces of memory.
ogdf::Array::linearSearch
INDEX linearSearch(const E &e) const
Performs a linear search for element e.
Definition: Array.h:611
ogdf::Array< PathType >::value_type
PathType value_type
Represents the data type stored in an array element.
Definition: Array.h:226
ogdf::Array::Array
Array(INDEX a, INDEX b)
Creates an array with index set [a..b].
Definition: Array.h:247
ogdf::Array::empty
bool empty() const
Returns true iff there are no elements in the array.
Definition: Array.h:305
ogdf::Array::rend
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
Definition: Array.h:359
ogdf::Array::Array
Array(std::initializer_list< E > initList)
Creates an array containing the elements in the initializer list initList.
Definition: Array.h:262
ogdf::Array::cbegin
const_iterator cbegin() const
Returns a const iterator to the first element.
Definition: Array.h:335
ogdf::ArrayConstIterator
const E * ArrayConstIterator
Definition: Array.h:56