Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Array.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/comparer.h>
36 #include <ogdf/basic/memory.h>
37 #include <ogdf/basic/exceptions.h>
38 #include <random>
39 #include <type_traits>
40 
41 
42 namespace ogdf {
43 
44 template<class E, class INDEX> class ArrayBuffer;
45 
47 
56 template<class E, class INDEX = int> class Array {
57 public:
60  static const int maxSizeInsertionSort = 40;
61 
63  using value_type = E;
65  using reference = E&;
67  using const_reference = const E&;
69  using const_iterator = const E*;
71  using iterator = E*;
72 
74  Array() { construct(0,-1); }
75 
77  explicit Array(INDEX s) : Array(0, s - 1) { }
78 
80  Array(INDEX a, INDEX b) {
81  construct(a,b); initialize();
82  }
83 
85  Array(INDEX a, INDEX b, const E &x) {
86  construct(a,b); initialize(x);
87  }
88 
90 
93  Array(std::initializer_list<E> initList) {
94  construct(0, ((INDEX) initList.size()) - 1);
95  initialize(initList);
96  }
97 
100  copy(A);
101  }
102 
104 
109  {
110  A.construct(0,-1);
111  }
112 
114  Array(const ArrayBuffer<E,INDEX> &A);
115 
117  ~Array() {
118  deconstruct();
119  }
120 
126 
128  INDEX low() const { return m_low; }
129 
131  INDEX high() const { return m_high; }
132 
134  INDEX size() const { return m_high - m_low + 1; }
135 
137  bool empty() const { return size() == 0; }
138 
140  const_reference operator[](INDEX i) const {
141  OGDF_ASSERT(m_low <= i);
142  OGDF_ASSERT(i <= m_high);
143  return m_vpStart[i];
144  }
145 
148  OGDF_ASSERT(m_low <= i);
149  OGDF_ASSERT(i <= m_high);
150  return m_vpStart[i];
151  }
152 
153 
155 
160 
162  iterator begin() { return m_pStart; }
163 
165  const_iterator begin() const { return m_pStart; }
166 
168  const_iterator cbegin() const { return m_pStart; }
169 
171  iterator end() { return m_pStop; }
172 
174  const_iterator end() const { return m_pStop; }
175 
177  const_iterator cend() const { return m_pStop; }
178 
180  iterator rbegin() { return m_pStop-1; }
181 
183  const_iterator rbegin() const { return m_pStop-1; }
184 
186  const_iterator crbegin() const { return m_pStop-1; }
187 
189  iterator rend() { return m_pStart-1; }
190 
192  const_iterator rend() const { return m_pStart-1; }
193 
195  const_iterator crend() const { return m_pStart-1; }
196 
197 
199 
204 
206  void init() {
207  deconstruct();
208  construct(0,-1);
209  }
210 
212 
215  void init(INDEX s) { init(0,s-1); }
216 
218 
221  void init(INDEX a, INDEX b) {
222  deconstruct();
223  construct(a,b);
224  initialize();
225  }
226 
228  void init(INDEX a, INDEX b, const E &x) {
229  deconstruct();
230  construct(a,b);
231  initialize(x);
232  }
233 
235  void fill(const E &x) {
236  E *pDest = m_pStop;
237  while(pDest > m_pStart)
238  *--pDest = x;
239  }
240 
242  void fill(INDEX i, INDEX j, const E &x) {
243  OGDF_ASSERT(m_low <= i);
244  OGDF_ASSERT(i <= m_high);
245  OGDF_ASSERT(m_low <= j);
246  OGDF_ASSERT(j <= m_high);
247 
248  E *pI = m_vpStart + i, *pJ = m_vpStart + j+1;
249  while(pJ > pI)
250  *--pJ = x;
251  }
252 
254 
259  void grow(INDEX add, const E &x);
260 
262 
266  void grow(INDEX add);
267 
269 
274  void resize(INDEX newSize, const E &x) { grow(newSize - size(), x); }
275 
277 
281  void resize(INDEX newSize) { grow(newSize - size()); }
282 
285  deconstruct();
286  copy(A);
287  return *this;
288  }
289 
291 
295  deconstruct();
296 
297  m_vpStart = A.m_vpStart;
298  m_pStart = A.m_pStart;
299  m_pStop = A.m_pStop;
300  m_low = A.m_low;
301  m_high = A.m_high;
302 
303  A.construct(0,-1);
304  return *this;
305  }
306 
307 
309 
314 
316  void swap(INDEX i, INDEX j) {
317  OGDF_ASSERT(m_low <= i);
318  OGDF_ASSERT(i <= m_high);
319  OGDF_ASSERT(m_low <= j);
320  OGDF_ASSERT(j <= m_high);
321 
322  std::swap(m_vpStart[i], m_vpStart[j]);
323  }
324 
326  void permute(INDEX l, INDEX r) {
327  std::minstd_rand rng(randomSeed());
328  permute(l, r, rng);
329  }
330 
332  void permute() {
333  permute(low(), high());
334  }
335 
342  template<class RNG>
343  void permute(INDEX l, INDEX r, RNG &rng);
344 
349  template<class RNG>
350  void permute(RNG &rng) {
351  permute(low(), high(), rng);
352  }
353 
354 
356 
361 
363 
367  inline int binarySearch (const E& e) const {
368  return binarySearch(low(), high(), e, StdComparer<E>());
369  }
370 
372 
376  inline int binarySearch (INDEX l, INDEX r, const E& e) const {
377  return binarySearch(l, r, e, StdComparer<E>());
378  }
379 
381 
385  template<class COMPARER>
386  inline int binarySearch(const E& e, const COMPARER &comp) const {
387  return binarySearch(low(), high(), e, comp);
388  }
389 
391 
395  template<class COMPARER>
396  INDEX binarySearch(INDEX l, INDEX r, const E& e, const COMPARER &comp) const {
397  if(r<l) return low()-1;
398  while(r>l) {
399  INDEX m = (r + l)/2;
400  if(comp.greater(e, m_vpStart[m]))
401  l = m+1;
402  else
403  r = m;
404  }
405  return comp.equal(e, m_vpStart[l]) ? l : low()-1;
406  }
408 
413  inline INDEX linearSearch (const E& e) const {
414  int i;
415  for(i = size(); i-- > 0; )
416  if(e == m_pStart[i]) break;
417  return i+low(); }
418 
420 
425  template<class COMPARER>
426  INDEX linearSearch(const E& e, const COMPARER &comp) const {
427  int i;
428  for(i = size(); i-- > 0; )
429  if(comp.equal(e, m_pStart[i])) break;
430  return i+low();
431  }
432 
434  inline void quicksort() {
436  }
437 
439  inline void quicksort(INDEX l, INDEX r) {
440  quicksort(l, r, StdComparer<E>());
441  }
442 
444 
447  template<class COMPARER>
448  inline void quicksort(const COMPARER &comp) {
449  if(low() < high())
450  quicksortInt(m_pStart,m_pStop-1,comp);
451  }
452 
454 
459  template<class COMPARER>
460  void quicksort(INDEX l, INDEX r, const COMPARER &comp) {
461  OGDF_ASSERT(low() <= l);
462  OGDF_ASSERT(l <= high());
463  OGDF_ASSERT(low() <= r);
464  OGDF_ASSERT(r <= high());
465  if(l < r)
466  quicksortInt(m_vpStart+l,m_vpStart+r,comp);
467  }
468 
470 
482 
484 
494  void leftShift(ArrayBuffer<INDEX, INDEX> &ind, const E& val) {
495  leftShift(ind);
496  fill(high()-ind.size(),high(),val);
497  }
498 
500 
501  template<class F, class I> friend class ArrayBuffer; // for efficient ArrayBuffer::compact-method
502 
503 private:
505  E *m_pStart;
506  E *m_pStop;
507  INDEX m_low;
508  INDEX m_high;
509 
511  void construct(INDEX a, INDEX b);
512 
514  void initialize();
515 
517  void initialize(const E &x);
518 
520  void initialize(std::initializer_list<E> initList);
521 
523  void deconstruct();
524 
526  void copy(const Array<E,INDEX> &A);
527 
529  void expandArray(INDEX add);
530 
532  template<class COMPARER>
533  static void quicksortInt(E *pL, E *pR, const COMPARER &comp) {
534  size_t s = pR-pL;
535 
536  // use insertion sort for small instances
537  if (s < maxSizeInsertionSort) {
538  for (E *pI = pL+1; pI <= pR; pI++) {
539  E v = *pI;
540  E *pJ = pI;
541  while (--pJ >= pL && comp.less(v,*pJ)) {
542  *(pJ+1) = *pJ;
543  }
544  *(pJ+1) = v;
545  }
546  return;
547  }
548 
549  E *pI = pL, *pJ = pR;
550  E x = *(pL+(s>>1));
551 
552  do {
553  while (comp.less(*pI,x)) pI++;
554  while (comp.less(x,*pJ)) pJ--;
555  if (pI <= pJ) std::swap(*pI++,*pJ--);
556  } while (pI <= pJ);
557 
558  if (pL < pJ) quicksortInt(pL,pJ,comp);
559  if (pI < pR) quicksortInt(pI,pR,comp);
560  }
561 
563 }; // class Array
564 
565 
566 // enlarges storage for array and moves old entries
567 template<class E, class INDEX>
569 {
570  INDEX sOld = size(), sNew = sOld + add;
571 
572  // expand allocated memory block
573  if (m_pStart != nullptr) {
574  // if the element type is trivially copiable, just use realloc
575 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
576  // g++ 4.8/4.9 does not have is_trivially_copyable, but
577  // clang 3.5 (which is also __GNUC__ < 5) has it
578  if (std::has_trivial_copy_assign<E>::value) {
579 #else
580  if (std::is_trivially_copyable<E>::value) {
581 #endif
582  E *p = static_cast<E *>( realloc(m_pStart, sNew*sizeof(E)) );
583  if (p == nullptr) OGDF_THROW(InsufficientMemoryException);
584  m_pStart = p;
585 
586  // otherwise allocate new block, move elements, and free old block
587  } else {
588  E *p = static_cast<E *>( malloc(sNew*sizeof(E)) );
589  if (p == nullptr) OGDF_THROW(InsufficientMemoryException);
590 
591  for (int i = 0; i < min(sOld,sNew); ++i) {
592  new (&p[i]) E(std::move(m_pStart[i]));
593  }
594 
595  deconstruct();
596  m_pStart = p;
597  }
598 
599  } else {
600  m_pStart = static_cast<E *>( malloc(sNew*sizeof(E)) );
602  }
603 
605  m_pStop = m_pStart + sNew;
606  m_high += add;
607 }
608 
609 // enlarges array by add elements and sets new elements to x
610 template<class E, class INDEX>
611 void Array<E,INDEX>::grow(INDEX add, const E &x)
612 {
613  if(add == 0) return;
614 
615  INDEX sOld = size();
616  expandArray(add);
617 
618  // initialize new array entries
619  for (E *pDest = m_pStart+sOld; pDest < m_pStop; pDest++)
620  new (pDest) E(x);
621 }
622 
623 // enlarges array by add elements (initialized with default constructor)
624 template<class E, class INDEX>
625 void Array<E,INDEX>::grow(INDEX add)
626 {
627  if(add == 0) return;
628 
629  INDEX sOld = size();
630  expandArray(add);
631 
632  // initialize new array entries
633  for (E *pDest = m_pStart+sOld; pDest < m_pStop; pDest++)
634  new (pDest) E;
635 }
636 
637 template<class E, class INDEX>
638 void Array<E,INDEX>::construct(INDEX a, INDEX b)
639 {
640  m_low = a; m_high = b;
641  INDEX s = b-a+1;
642 
643  if (s < 1) {
644  m_pStart = m_vpStart = m_pStop = nullptr;
645 
646  } else {
647  m_pStart = static_cast<E *>( malloc(s*sizeof(E)) );
649 
650  m_vpStart = m_pStart - a;
651  m_pStop = m_pStart + s;
652  }
653 }
654 
655 
656 template<class E, class INDEX>
658 {
659  E *pDest = m_pStart;
660  try {
661  for (; pDest < m_pStop; pDest++)
662  new(pDest) E;
663  } catch (...) {
664  while(--pDest >= m_pStart)
665  pDest->~E();
666  free(m_pStart);
667  throw;
668  }
669 }
670 
671 
672 template<class E, class INDEX>
674 {
675  E *pDest = m_pStart;
676  try {
677  for (; pDest < m_pStop; pDest++)
678  new(pDest) E(x);
679  } catch (...) {
680  while(--pDest >= m_pStart)
681  pDest->~E();
682  free(m_pStart);
683  throw;
684  }
685 }
686 
687 
688 template<class E, class INDEX>
689 void Array<E, INDEX>::initialize(std::initializer_list<E> initList)
690 {
691  E *pDest = m_pStart;
692  try {
693  for (const E &x : initList)
694  new(pDest++) E(x);
695  }
696  catch (...) {
697  while (--pDest >= m_pStart)
698  pDest->~E();
699  free(m_pStart);
700  throw;
701  }
702 }
703 
704 
705 template<class E, class INDEX>
707 {
708  if (!std::is_trivially_destructible<E>::value) {
709  for (E *pDest = m_pStart; pDest < m_pStop; pDest++)
710  pDest->~E();
711  }
712  free(m_pStart);
713 }
714 
715 
716 template<class E, class INDEX>
718 {
719  construct(array2.m_low, array2.m_high);
720 
721  if (m_pStart != nullptr) {
722  E *pSrc = array2.m_pStop;
723  E *pDest = m_pStop;
724  while(pDest > m_pStart)
725 #if 0
726  *--pDest = *--pSrc;
727 #endif
728  new (--pDest) E(*--pSrc);
729  }
730 }
731 
732 
733 // permutes array a from a[l] to a[r] randomly
734 template<class E, class INDEX>
735 template<class RNG>
736 void Array<E,INDEX>::permute (INDEX l, INDEX r, RNG &rng)
737 {
738  OGDF_ASSERT(low() <= l);
739  OGDF_ASSERT(l <= high());
740  OGDF_ASSERT(low() <= r);
741  OGDF_ASSERT(r <= high());
742 
743  std::uniform_int_distribution<int> dist(0,r-l);
744 
745  E *pI = m_vpStart+l, *pStart = m_vpStart+l, *pStop = m_vpStart+r;
746  while(pI <= pStop)
747  std::swap( *pI++, *(pStart + dist(rng)) );
748 }
749 
750 
752 template<class E, class INDEX>
753 void print(ostream &os, const Array<E,INDEX> &a, char delim = ' ')
754 {
755  for (int i = a.low(); i <= a.high(); i++) {
756  if (i > a.low()) os << delim;
757  os << a[i];
758  }
759 }
760 
761 
763 template<class E, class INDEX>
764 ostream &operator<<(ostream &os, const ogdf::Array<E,INDEX> &a)
765 {
766  print(os,a);
767  return os;
768 }
769 
770 }
771 
772 #include <ogdf/basic/ArrayBuffer.h>
773 
774 namespace ogdf {
775 
777 template<class E, class INDEX>
779  const INDEX nInd = ind.size();
780  if (nInd == 0) return;
781 
782  OGDF_ASSERT(ind[0] >= low());
783  OGDF_ASSERT(ind[0] <= high());
784 
785  INDEX j, current = ind[0];
786  for (INDEX i = 0; i < nInd - 1; i++) {
787  OGDF_ASSERT(ind[i+1] >= low());
788  OGDF_ASSERT(ind[i+1] <= high());
789 
790  const INDEX last = ind[i+1];
791  for(j = ind[i]+1; j < last; j++)
792  m_vpStart[current++] = m_vpStart[j];
793  }
794 
796  for (j = ind[nInd - 1]+1; j < size(); j++)
797  m_vpStart[current++] = m_vpStart[j];
798 }
799 
800 template<class E, class INDEX>
802  construct(0,-1);
803  A.compactCopy(*this);
804 }
805 
806 } // end namespace ogdf
Array(Array< E, INDEX > &&A)
Creates an array containing the elements of A (move semantics).
Definition: Array.h:107
Declarations for Comparer objects.
Array()
Creates an array with empty index set.
Definition: Array.h:74
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:85
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
Declaration of memory manager for allocating small pieces of memory.
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:396
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:376
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
Definition: Array.h:316
static const int maxSizeInsertionSort
Threshold used by quicksort() such that insertion sort is called for instances smaller than maxSizeIn...
Definition: Array.h:60
void deconstruct()
Deallocates array.
Definition: Array.h:706
void leftShift(ArrayBuffer< INDEX, INDEX > &ind)
Removes the components listed in ind by shifting the remaining components to the left.
Definition: Array.h:778
Declaration and implementation of ArrayBuffer class.
void permute()
Randomly permutes the array.
Definition: Array.h:332
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:60
INDEX high() const
Returns the maximal array index.
Definition: Array.h:131
void fill(INDEX i, INDEX j, const E &x)
Sets elements in the intervall [i..j] to x.
Definition: Array.h:242
INDEX m_high
The highest index.
Definition: Array.h:508
const_iterator crend() const
Returns a const iterator to one before the first element.
Definition: Array.h:195
Further information about the cages of vertices in UML diagrams.
Definition: OrthoRep.h:279
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:460
Array(INDEX a, INDEX b)
Creates an array with index set [a..b].
Definition: Array.h:80
void init(INDEX s)
Reinitializes the array to an array with index set [0..s-1].
Definition: Array.h:215
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:44
const_iterator crbegin() const
Returns a const iterator to the last element.
Definition: Array.h:186
INDEX linearSearch(const E &e) const
Performs a linear search for element e.
Definition: Array.h:413
const_iterator cend() const
Returns a const iterator to one past the last element.
Definition: Array.h:177
int binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
Definition: Array.h:386
Array< E, INDEX > & operator=(Array< E, INDEX > &&A)
Assignment operator (move semantics).
Definition: Array.h:294
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
Definition: Array.h:326
void init(INDEX a, INDEX b)
Reinitializes the array to an array with index set [a..b].
Definition: Array.h:221
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:228
Standard comparer (valid as a static comparer).
Definition: comparer.h:56
const_iterator rbegin() const
Returns a const iterator to the last element.
Definition: Array.h:183
iterator rbegin()
Returns an iterator to the last element.
Definition: Array.h:180
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
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:494
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition: Array.h:717
INDEX m_low
The lowest index.
Definition: Array.h:507
bool empty() const
Returns true iff there are no elements in the array.
Definition: Array.h:137
E * m_vpStart
The virtual start of the array (address of A[0]).
Definition: Array.h:504
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:134
Definition of exception classes.
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:611
iterator rend()
Returns an iterator to one before the first element.
Definition: Array.h:189
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:235
const_iterator end() const
Returns a const iterator to one past the last element.
Definition: Array.h:174
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
const T & move(const T &v)
Definition: backward.hpp:243
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:117
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition: Array.h:367
iterator begin()
Returns an iterator to the first element.
Definition: Array.h:162
reference operator[](INDEX i)
Returns a reference to the element at position i.
Definition: Array.h:147
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:274
iterator end()
Returns an iterator to one past the last element.
Definition: Array.h:171
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:434
void construct(INDEX a, INDEX b)
Allocates new array with index set [a, ..., b].
Definition: Array.h:638
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:199
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:284
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:140
E * m_pStart
The real start of the array (address of A[m_low]).
Definition: Array.h:505
void fill(const E &x)
Sets all elements to x.
Definition: Array.h:235
The namespace for all OGDF objects.
E * m_pStop
Successor of last element (address of A[m_high+1]).
Definition: Array.h:506
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
Definition: Array.h:350
const_iterator begin() const
Returns a const iterator to the first element.
Definition: Array.h:165
void quicksort(INDEX l, INDEX r)
Sorts subarray with index set [l, ..., r] using Quicksort.
Definition: Array.h:439
static void quicksortInt(E *pL, E *pR, const COMPARER &comp)
Internal Quicksort implementation with comparer template.
Definition: Array.h:533
void resize(INDEX newSize)
Resizes (enlarges or shrinks) the array to hold newSize elements.
Definition: Array.h:281
Array(std::initializer_list< E > initList)
Creates an array containing the elements in the initializer list initList.
Definition: Array.h:93
INDEX linearSearch(const E &e, const COMPARER &comp) const
Performs a linear search for element e with comparer comp.
Definition: Array.h:426
void print(ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
Definition: Array.h:753
INDEX low() const
Returns the minimal array index.
Definition: Array.h:128
void quicksort(const COMPARER &comp)
Sorts array using Quicksort and a user-defined comparer comp.
Definition: Array.h:448
Array(INDEX s)
Creates an array with index set [0..s-1].
Definition: Array.h:77
void initialize()
Initializes elements with default constructor.
Definition: Array.h:657
Array(const Array< E, INDEX > &A)
Creates an array that is a copy of A.
Definition: Array.h:99
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
~Array()
Destruction.
Definition: Array.h:117
void expandArray(INDEX add)
Used by grow() to enlarge the array.
Definition: Array.h:568
const_iterator cbegin() const
Returns a const iterator to the first element.
Definition: Array.h:168
const_iterator rend() const
Returns a const iterator to one before the first element.
Definition: Array.h:192