Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

ArrayBuffer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h> // IWYU pragma: keep
35 #include <ogdf/basic/basic.h>
36 #include <ogdf/basic/comparer.h>
37 #include <ogdf/basic/memory.h>
38 
39 #include <algorithm>
40 #include <cstring>
41 #include <limits>
42 #include <ostream>
43 #include <random>
44 #include <type_traits>
45 
46 namespace ogdf {
47 
49 
63 template<class E, class INDEX = int>
64 class ArrayBuffer : private Array<E, INDEX> {
65  INDEX num;
66  bool growable;
67 
68 public:
69  using key_type = INDEX;
70  using value_type = E;
75 
77  ArrayBuffer() : Array<E, INDEX>(), num(0), growable(true) { }
78 
81  explicit ArrayBuffer(INDEX size, bool autogrow = true)
82  : Array<E, INDEX>(size), num(0), growable(autogrow) { }
83 
85  explicit ArrayBuffer(const Array<E, INDEX>& source, bool autogrow = true)
86  : Array<E, INDEX>(source), num(0), growable(autogrow) { }
87 
90  : Array<E, INDEX>(buffer), num(buffer.num), growable(buffer.growable) { }
91 
93 
97  : Array<E, INDEX>(std::move(buffer)), num(buffer.num), growable(buffer.growable) {
98  buffer.num = 0;
99  buffer.growable = true;
100  }
101 
103  void clear() { num = 0; }
104 
106 
119  const INDEX nInd = ind.size();
120  if (nInd == 0) {
121  return;
122  }
123 
124  OGDF_ASSERT(ind[0] >= 0);
125  OGDF_ASSERT(ind[0] < num);
126 
127  INDEX j, current = ind[0];
128  for (INDEX i = 0; i < nInd - 1; i++) {
129  OGDF_ASSERT(ind[i + 1] >= 0);
130  OGDF_ASSERT(ind[i + 1] < num);
131 
132  const INDEX last = ind[i + 1];
133  for (j = ind[i] + 1; j < last; j++) {
134  operator[](current++) = operator[](j);
135  }
136  }
137 
139  for (j = ind[nInd - 1] + 1; j < size(); j++) {
140  operator[](current++) = operator[](j);
141  }
142 
143  num -= nInd;
144  }
145 
147  bool isGrowable() const { return growable; }
148 
150  void setGrowable(bool _growable) { growable = _growable; }
151 
156 
160 
163 
166 
169 
172 
175 
178 
181 
183 
188 
191  const E& operator[](INDEX i) const {
192  OGDF_ASSERT(0 <= i);
193  OGDF_ASSERT(i < num);
194  return Array<E, INDEX>::operator[](i);
195  }
196 
198  E& operator[](INDEX i) {
199  OGDF_ASSERT(0 <= i);
200  OGDF_ASSERT(i < num);
201  return Array<E, INDEX>::operator[](i);
202  }
203 
205  const E& top() const {
206  OGDF_ASSERT(num > 0);
207  return Array<E, INDEX>::operator[](num - 1);
208  }
209 
211  E& top() {
212  OGDF_ASSERT(num > 0);
213  return Array<E, INDEX>::operator[](num - 1);
214  }
215 
217  void push(E e) {
218  if (num == Array<E, INDEX>::size()) {
220  Array<E, INDEX>::grow(max(num, static_cast<INDEX>(1))); // double the size
221  }
223  }
224 
226  void pop() {
227  OGDF_ASSERT(num > 0);
228  --num;
229  }
230 
232  E popRet() {
233  OGDF_ASSERT(num > 0);
235  }
236 
238  bool empty() const { return !num; }
239 
241  bool full() const { return !growable && num == Array<E, INDEX>::size(); }
242 
244  INDEX size() const { return num; }
245 
247  INDEX capacity() const { return Array<E, INDEX>::size(); }
248 
250 
255 
259 
262 
266  num = buffer.num;
267  growable = buffer.growable;
268  return *this;
269  }
270 
272 
277  num = buffer.num;
278  growable = buffer.growable;
279  buffer.num = 0;
280  buffer.growable = true;
281  return *this;
282  }
283 
285 
295  void compactCpycon(Array<E, INDEX>& A2) const {
296  OGDF_ASSERT(this != &A2);
297  if (num) {
298  INDEX tmp = Array<E, INDEX>::m_high; // thank god i'm a friend of Array
299  Array<E, INDEX>::m_high = num - 1; // fake smaller size
300  A2.copy(*this); // copy
302  } else {
303  A2.init(0);
304  }
305  }
306 
308 
318  template<typename EE = E, typename std::enable_if<!OGDF_TRIVIALLY_COPYABLE<EE>::value, int>::type = 0>
319  void compactCopy(Array<E, INDEX>& A2) const {
320  OGDF_ASSERT(this != &A2);
321  if (num) {
322  A2.init(num);
323  for (INDEX i = num; i-- > 0;) {
324  A2[i] = (*this)[i];
325  }
326  } else {
327  A2.init(0);
328  }
329  }
330 
332 
342  template<typename EE = E, typename std::enable_if<OGDF_TRIVIALLY_COPYABLE<EE>::value, int>::type = 0>
343  void compactCopy(Array<E, INDEX>& A2) const {
344  OGDF_ASSERT(this != &A2);
345  if (num) {
346  A2.init(num);
347  OGDF_ASSERT(sizeof(E) <= size_t(std::numeric_limits<INDEX>::max() / num));
348 
349  memcpy(A2.m_pStart, this->m_pStart,
350  sizeof(E) <= size_t(std::numeric_limits<INDEX>::max() / num)
351  ? sizeof(E) * num
352  : size_t(std::numeric_limits<INDEX>::max()));
353  } else {
354  A2.init(0);
355  }
356  }
357 
361 
363  bool operator==(const ArrayBuffer<E, INDEX>& L) const {
364  if (size() != L.size()) {
365  return false;
366  }
367 
368  auto thisIterator = begin();
369  auto thatIterator = L.begin();
370 
371  while (thisIterator != end() && thatIterator != L.end()) {
372  if (*thisIterator != *thatIterator) {
373  return false;
374  }
375  ++thisIterator;
376  ++thatIterator;
377  }
378 
379  OGDF_ASSERT(thisIterator == end() && thatIterator == L.end());
380  return true;
381  }
382 
384  bool operator!=(const ArrayBuffer<E, INDEX>& L) const { return !operator==(L); }
385 
387 
392 
395 
400  INDEX linearSearch(const E& x) const {
401  INDEX i;
402  for (i = num; i-- > 0;) {
403  if (x == Array<E, INDEX>::m_vpStart[i]) {
404  break;
405  }
406  }
407  return i;
408  }
409 
411 
416  template<class COMPARER>
417  INDEX linearSearch(const E& x, const COMPARER& comp) const {
418  INDEX i;
419  for (i = num; i-- > 0;) {
420  if (comp.equal(x, Array<E, INDEX>::m_vpStart[i])) {
421  break;
422  }
423  }
424  return i;
425  }
426 
428  inline void quicksort() {
429  if (num == 0) {
430  return;
431  }
433  }
434 
436 
439  template<class COMPARER>
440  inline void quicksort(const COMPARER& comp) {
441  if (num == 0) {
442  return;
443  }
444  Array<E, INDEX>::quicksort(0, num - 1, comp);
445  }
446 
448 
452  inline INDEX binarySearch(const E& e) const {
453  return Array<E, INDEX>::binarySearch(0, num - 1, e, StdComparer<E>());
454  }
455 
457 
461  template<class COMPARER>
462  inline INDEX binarySearch(const E& e, const COMPARER& comp) const {
463  return Array<E, INDEX>::binarySearch(0, num - 1, e, comp);
464  }
465 
467 
470 
471  using Array<E, INDEX>::swap;
472 
474  template<class RNG>
475  void permute(INDEX l, INDEX r, RNG& rng) {
476  Array<E, INDEX>::permute(l, r, rng);
477  }
478 
480  template<class RNG>
481  void permute(RNG& rng) {
482  permute(0, num - 1, rng);
483  }
484 
486  void permute(INDEX l, INDEX r) {
487  std::minstd_rand rng(randomSeed());
488  permute(l, r, rng);
489  }
490 
492  void permute() { permute(0, num - 1); }
493 
495 
497 
501  void setCapacity(INDEX newCapacity) { Array<E, INDEX>::resize(newCapacity); }
502 
504 };
505 
507 template<class E, class INDEX>
508 void print(std::ostream& os, const ArrayBuffer<E, INDEX>& a, char delim = ' ') {
509  for (int i = 0; i < a.size(); i++) {
510  if (i > 0) {
511  os << delim;
512  }
513  os << a[i];
514  }
515 }
516 
518 template<class E, class INDEX>
519 std::ostream& operator<<(std::ostream& os, const ogdf::ArrayBuffer<E, INDEX>& a) {
520  print(os, a);
521  return os;
522 }
523 
524 }
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::ArrayBuffer::top
E & top()
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:211
ogdf::ArrayBuffer::compactCopy
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:319
ogdf::ArrayBuffer::rbegin
reverse_iterator rbegin()
Returns a reverse iterator to the last element.
Definition: ArrayBuffer.h:171
ogdf::ArrayBuffer::capacity
INDEX capacity() const
Returns the current capacity of the datastructure. Note that this value is rather irrelevant if the a...
Definition: ArrayBuffer.h:247
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::ArrayBuffer::empty
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:238
ogdf::ArrayBuffer::isGrowable
bool isGrowable() const
Returns whether the buffer will automatically expand if the initial size is insufficient.
Definition: ArrayBuffer.h:147
ogdf::ArrayBuffer::top
const E & top() const
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:205
ogdf::StdComparer
Standard comparer (valid as a static comparer).
Definition: comparer.h:40
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer(ArrayBuffer< E, INDEX > &&buffer)
Creates an array buffer containing the elements of buffer (move semantics).
Definition: ArrayBuffer.h:96
ogdf::ArrayBuffer::init
void init(INDEX size)
Reinitializes the array, clearing it, and allocating memory for up to size elements.
Definition: ArrayBuffer.h:261
ogdf::ArrayBuffer::binarySearch
INDEX binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
Definition: ArrayBuffer.h:462
ogdf::ArrayBuffer::permute
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
Definition: ArrayBuffer.h:486
ogdf::ArrayBuffer::full
bool full() const
Returns true iff the buffer is non-growable and filled.
Definition: ArrayBuffer.h:241
ogdf::ArrayReverseIteratorBase
Random-access reverse iterator based on a pointer to an array element.
Definition: Array.h:51
ogdf::Array::permute
void permute()
Randomly permutes the array.
Definition: Array.h:527
ogdf::Array::iterator
ArrayIterator< E > iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition: Array.h:234
ogdf::ArrayBuffer::popRet
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:232
ogdf::Array::init
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:372
ogdf::ArrayBuffer::setCapacity
void setCapacity(INDEX newCapacity)
Changes the capacity of the buffer (independent whether the buffer is growable of not).
Definition: ArrayBuffer.h:501
OGDF_NEW_DELETE
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition: memory.h:85
ogdf::ArrayBuffer::operator=
ArrayBuffer< E, INDEX > & operator=(const ArrayBuffer< E, INDEX > &buffer)
Assignment operator.
Definition: ArrayBuffer.h:264
ogdf::ArrayBuffer::linearSearch
INDEX linearSearch(const E &x) const
Performs a linear search for element x.
Definition: ArrayBuffer.h:400
ogdf::ArrayBuffer::permute
void permute()
Randomly permutes the array.
Definition: ArrayBuffer.h:492
ogdf::Array::operator=
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:452
ogdf::ArrayBuffer< abacus::Constraint * >::const_reverse_iterator
typename Array< abacus::Constraint *, int >::const_reverse_iterator const_reverse_iterator
Definition: ArrayBuffer.h:73
ogdf::ArrayBuffer::leftShift
void leftShift(ArrayBuffer< INDEX, INDEX > &ind)
Removes the components listed in the buffer ind by shifting the remaining components to the left.
Definition: ArrayBuffer.h:118
ogdf::ArrayBuffer::num
INDEX num
The number of elements in the buffer.
Definition: ArrayBuffer.h:65
ogdf::ArrayBuffer::rbegin
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Definition: ArrayBuffer.h:174
r
int r[]
Definition: hierarchical-ranking.cpp:13
ogdf::ArrayBuffer::quicksort
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:428
ogdf::ArrayBuffer::init
void init()
Reinitializes the array, clearing it, and without initial memory allocation.
Definition: ArrayBuffer.h:258
ogdf::AlgorithmFailureCode::Array
@ Array
ogdf::ArrayBuffer::permute
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
Definition: ArrayBuffer.h:481
backward::Color::type
type
Definition: backward.hpp:1716
ogdf::ArrayBuffer< abacus::Constraint * >::const_iterator
typename Array< abacus::Constraint *, int >::const_iterator const_iterator
Definition: ArrayBuffer.h:71
ogdf::ArrayBuffer::end
const_iterator end() const
Returns a const iterator to one past the last element.
Definition: ArrayBuffer.h:168
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::operator[]
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:308
ogdf::ArrayBuffer< abacus::Constraint * >::iterator
typename Array< abacus::Constraint *, int >::iterator iterator
Definition: ArrayBuffer.h:72
ogdf::ArrayBuffer::size
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:244
ogdf::ArrayBuffer::quicksort
void quicksort(const COMPARER &comp)
Sorts buffer using Quicksort and a user-defined comparer comp.
Definition: ArrayBuffer.h:440
ogdf::ArrayBuffer::push
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:217
ogdf::ArrayBuffer::rend
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
Definition: ArrayBuffer.h:180
ogdf::ArrayBuffer::end
iterator end()
Returns an iterator to one past the last element.
Definition: ArrayBuffer.h:165
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::Array::rend
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
Definition: Array.h:356
ogdf::ArrayBuffer< abacus::Constraint * >::key_type
int key_type
Definition: ArrayBuffer.h:69
ogdf::ArrayBuffer::binarySearch
INDEX binarySearch(const E &e) const
Performs a binary search for element e.
Definition: ArrayBuffer.h:452
ogdf::ArrayBuffer::setGrowable
void setGrowable(bool _growable)
Sets the flag whether the buffer will automatically expand if the initial size is insufficient.
Definition: ArrayBuffer.h:150
ogdf::ArrayBuffer::begin
const_iterator begin() const
Returns a const iterator to the first element.
Definition: ArrayBuffer.h:162
ogdf::ArrayBuffer::linearSearch
INDEX linearSearch(const E &x, const COMPARER &comp) const
Performs a linear search for element x with comparer comp.
Definition: ArrayBuffer.h:417
ogdf::ArrayBuffer::growable
bool growable
Definition: ArrayBuffer.h:66
ogdf::randomSeed
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
ogdf::ArrayBuffer::permute
void permute(INDEX l, INDEX r, RNG &rng)
Randomly permutes the subarray with index set [l..r] using random number generator rng.
Definition: ArrayBuffer.h:475
ogdf::ArrayBuffer::operator[]
E & operator[](INDEX i)
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:198
ogdf::ArrayBuffer::begin
iterator begin()
Returns an iterator to the first element.
Definition: ArrayBuffer.h:159
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::m_pStart
E * m_pStart
The real start of the array (address of A[m_low]).
Definition: Array.h:709
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer(const Array< E, INDEX > &source, bool autogrow=true)
Creates an array buffer, initialized by the given array; you may specify that the array should not gr...
Definition: ArrayBuffer.h:85
ogdf::ArrayBuffer< abacus::Constraint * >::reverse_iterator
typename Array< abacus::Constraint *, int >::reverse_iterator reverse_iterator
Definition: ArrayBuffer.h:74
ogdf::Array::const_iterator
ArrayConstIterator< E > const_iterator
Provides a random-access iterator that can read a const element in an array.
Definition: Array.h:232
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer(INDEX size, bool autogrow=true)
Creates an empty array buffer, allocating memory for up to size elements; you may specify that the ar...
Definition: ArrayBuffer.h:81
basic.h
Basic declarations, included by all source files.
std
Definition: GML.h:111
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer()
Creates an empty array buffer, without initial memory allocation.
Definition: ArrayBuffer.h:77
ogdf::ArrayBuffer::pop
void pop()
Removes the newest element from the buffer.
Definition: ArrayBuffer.h:226
ogdf::ArrayBuffer::operator==
bool operator==(const ArrayBuffer< E, INDEX > &L) const
Equality operator.
Definition: ArrayBuffer.h:363
abacus::Constraint
Forms the virtual base class for all possible constraints given in pool format.
Definition: constraint.h:56
ogdf::Array::binarySearch
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition: Array.h:561
Array.h
Declaration and implementation of Array class and Array algorithms.
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::ArrayBuffer::operator=
ArrayBuffer< E, INDEX > & operator=(ArrayBuffer< E, INDEX > &&buffer)
Assignment operator (move semantics).
Definition: ArrayBuffer.h:275
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::ArrayBuffer::compactCpycon
void compactCpycon(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:295
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::copy
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition: Array.h:939
ogdf::ArrayBuffer::clear
void clear()
Clears the buffer.
Definition: ArrayBuffer.h:103
ogdf::ArrayBuffer::operator[]
const E & operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:191
ogdf::ArrayBuffer::operator!=
bool operator!=(const ArrayBuffer< E, INDEX > &L) const
Inequality operator.
Definition: ArrayBuffer.h:384
ogdf::ArrayBuffer::rend
reverse_iterator rend()
Returns a reverse iterator to one before the first element.
Definition: ArrayBuffer.h:177
comparer.h
Declarations for Comparer objects.
memory.h
Declaration of memory manager for allocating small pieces of memory.
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer(const ArrayBuffer< E, INDEX > &buffer)
Creates an array buffer that is a copy of buffer.
Definition: ArrayBuffer.h:89