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>
35 
36 #include <cstring>
37 
38 namespace ogdf {
39 
41 
55 template<class E, class INDEX = int>
56 class ArrayBuffer : private Array<E, INDEX> {
57  INDEX num;
58  bool growable;
59 
60 public:
61  using key_type = INDEX;
62  using value_type = E;
67 
69  ArrayBuffer() : Array<E, INDEX>(), num(0), growable(true) { }
70 
73  explicit ArrayBuffer(INDEX size, bool autogrow = true)
74  : Array<E, INDEX>(size), num(0), growable(autogrow) { }
75 
77  explicit ArrayBuffer(const Array<E, INDEX>& source, bool autogrow = true)
78  : Array<E, INDEX>(source), num(0), growable(autogrow) { }
79 
82  : Array<E, INDEX>(buffer), num(buffer.num), growable(buffer.growable) { }
83 
85 
89  : Array<E, INDEX>(std::move(buffer)), num(buffer.num), growable(buffer.growable) {
90  buffer.num = 0;
91  buffer.growable = true;
92  }
93 
95  void clear() { num = 0; }
96 
98 
111  const INDEX nInd = ind.size();
112  if (nInd == 0) {
113  return;
114  }
115 
116  OGDF_ASSERT(ind[0] >= 0);
117  OGDF_ASSERT(ind[0] < num);
118 
119  INDEX j, current = ind[0];
120  for (INDEX i = 0; i < nInd - 1; i++) {
121  OGDF_ASSERT(ind[i + 1] >= 0);
122  OGDF_ASSERT(ind[i + 1] < num);
123 
124  const INDEX last = ind[i + 1];
125  for (j = ind[i] + 1; j < last; j++) {
126  operator[](current++) = operator[](j);
127  }
128  }
129 
131  for (j = ind[nInd - 1] + 1; j < size(); j++) {
132  operator[](current++) = operator[](j);
133  }
134 
135  num -= nInd;
136  }
137 
139  bool isGrowable() const { return growable; }
140 
142  void setGrowable(bool _growable) { growable = _growable; }
143 
148 
152 
155 
158 
161 
164 
167 
170 
173 
175 
180 
183  const E& operator[](INDEX i) const {
184  OGDF_ASSERT(0 <= i);
185  OGDF_ASSERT(i < num);
186  return Array<E, INDEX>::operator[](i);
187  }
188 
190  E& operator[](INDEX i) {
191  OGDF_ASSERT(0 <= i);
192  OGDF_ASSERT(i < num);
193  return Array<E, INDEX>::operator[](i);
194  }
195 
197  const E& top() const {
198  OGDF_ASSERT(num > 0);
199  return Array<E, INDEX>::operator[](num - 1);
200  }
201 
203  E& top() {
204  OGDF_ASSERT(num > 0);
205  return Array<E, INDEX>::operator[](num - 1);
206  }
207 
209  void push(E e) {
210  if (num == Array<E, INDEX>::size()) {
212  Array<E, INDEX>::grow(max(num, static_cast<INDEX>(1))); // double the size
213  }
215  }
216 
218  void pop() {
219  OGDF_ASSERT(num > 0);
220  --num;
221  }
222 
224  E popRet() {
225  OGDF_ASSERT(num > 0);
227  }
228 
230  bool empty() const { return !num; }
231 
233  bool full() const { return !growable && num == Array<E, INDEX>::size(); }
234 
236  INDEX size() const { return num; }
237 
239  INDEX capacity() const { return Array<E, INDEX>::size(); }
240 
242 
247 
251 
254 
258  num = buffer.num;
259  growable = buffer.growable;
260  return *this;
261  }
262 
264 
269  num = buffer.num;
270  growable = buffer.growable;
271  buffer.num = 0;
272  buffer.growable = true;
273  return *this;
274  }
275 
277 
287  void compactCpycon(Array<E, INDEX>& A2) const {
288  OGDF_ASSERT(this != &A2);
289  if (num) {
290  INDEX tmp = Array<E, INDEX>::m_high; // thank god i'm a friend of Array
291  Array<E, INDEX>::m_high = num - 1; // fake smaller size
292  A2.copy(*this); // copy
294  } else {
295  A2.init(0);
296  }
297  }
298 
300 
310  template<typename EE = E, typename std::enable_if<!OGDF_TRIVIALLY_COPYABLE<EE>::value, int>::type = 0>
311  void compactCopy(Array<E, INDEX>& A2) const {
312  OGDF_ASSERT(this != &A2);
313  if (num) {
314  A2.init(num);
315  for (INDEX i = num; i-- > 0;) {
316  A2[i] = (*this)[i];
317  }
318  } else {
319  A2.init(0);
320  }
321  }
322 
324 
334  template<typename EE = E, typename std::enable_if<OGDF_TRIVIALLY_COPYABLE<EE>::value, int>::type = 0>
335  void compactCopy(Array<E, INDEX>& A2) const {
336  OGDF_ASSERT(this != &A2);
337  if (num) {
338  A2.init(num);
339  OGDF_ASSERT(sizeof(E) <= size_t(std::numeric_limits<INDEX>::max() / num));
340 
341  memcpy(A2.m_pStart, this->m_pStart,
342  sizeof(E) <= size_t(std::numeric_limits<INDEX>::max() / num)
343  ? sizeof(E) * num
344  : size_t(std::numeric_limits<INDEX>::max()));
345  } else {
346  A2.init(0);
347  }
348  }
349 
353 
355  bool operator==(const ArrayBuffer<E, INDEX>& L) const {
356  if (size() != L.size()) {
357  return false;
358  }
359 
360  auto thisIterator = begin();
361  auto thatIterator = L.begin();
362 
363  while (thisIterator != end() && thatIterator != L.end()) {
364  if (*thisIterator != *thatIterator) {
365  return false;
366  }
367  ++thisIterator;
368  ++thatIterator;
369  }
370 
371  OGDF_ASSERT(thisIterator == end() && thatIterator == L.end());
372  return true;
373  }
374 
376  bool operator!=(const ArrayBuffer<E, INDEX>& L) const { return !operator==(L); }
377 
379 
384 
387 
392  INDEX linearSearch(const E& x) const {
393  INDEX i;
394  for (i = num; i-- > 0;) {
395  if (x == Array<E, INDEX>::m_vpStart[i]) {
396  break;
397  }
398  }
399  return i;
400  }
401 
403 
408  template<class COMPARER>
409  INDEX linearSearch(const E& x, const COMPARER& comp) const {
410  INDEX i;
411  for (i = num; i-- > 0;) {
412  if (comp.equal(x, Array<E, INDEX>::m_vpStart[i])) {
413  break;
414  }
415  }
416  return i;
417  }
418 
420  inline void quicksort() {
421  if (num == 0) {
422  return;
423  }
425  }
426 
428 
431  template<class COMPARER>
432  inline void quicksort(const COMPARER& comp) {
433  if (num == 0) {
434  return;
435  }
436  Array<E, INDEX>::quicksort(0, num - 1, comp);
437  }
438 
440 
444  inline INDEX binarySearch(const E& e) const {
445  return Array<E, INDEX>::binarySearch(0, num - 1, e, StdComparer<E>());
446  }
447 
449 
453  template<class COMPARER>
454  inline INDEX binarySearch(const E& e, const COMPARER& comp) const {
455  return Array<E, INDEX>::binarySearch(0, num - 1, e, comp);
456  }
457 
459 
462 
463  using Array<E, INDEX>::swap;
464 
466  template<class RNG>
467  void permute(INDEX l, INDEX r, RNG& rng) {
468  Array<E, INDEX>::permute(l, r, rng);
469  }
470 
472  template<class RNG>
473  void permute(RNG& rng) {
474  permute(0, num - 1, rng);
475  }
476 
478  void permute(INDEX l, INDEX r) {
479  std::minstd_rand rng(randomSeed());
480  permute(l, r, rng);
481  }
482 
484  void permute() { permute(0, num - 1); }
485 
487 
489 
493  void setCapacity(INDEX newCapacity) { Array<E, INDEX>::resize(newCapacity); }
494 
496 };
497 
499 template<class E, class INDEX>
500 void print(std::ostream& os, const ArrayBuffer<E, INDEX>& a, char delim = ' ') {
501  for (int i = 0; i < a.size(); i++) {
502  if (i > 0) {
503  os << delim;
504  }
505  os << a[i];
506  }
507 }
508 
510 template<class E, class INDEX>
511 std::ostream& operator<<(std::ostream& os, const ogdf::ArrayBuffer<E, INDEX>& a) {
512  print(os, a);
513  return os;
514 }
515 
516 }
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:46
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::ArrayBuffer::top
E & top()
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:203
ogdf::ArrayBuffer::compactCopy
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:311
ogdf::ArrayBuffer::rbegin
reverse_iterator rbegin()
Returns a reverse iterator to the last element.
Definition: ArrayBuffer.h:163
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:239
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::ArrayBuffer::empty
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:230
ogdf::ArrayBuffer::isGrowable
bool isGrowable() const
Returns whether the buffer will automatically expand if the initial size is insufficient.
Definition: ArrayBuffer.h:139
ogdf::ArrayBuffer::top
const E & top() const
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:197
ogdf::StdComparer
Standard comparer (valid as a static comparer).
Definition: comparer.h:59
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer(ArrayBuffer< E, INDEX > &&buffer)
Creates an array buffer containing the elements of buffer (move semantics).
Definition: ArrayBuffer.h:88
ogdf::ArrayBuffer::init
void init(INDEX size)
Reinitializes the array, clearing it, and allocating memory for up to size elements.
Definition: ArrayBuffer.h:253
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:454
ogdf::ArrayBuffer::permute
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
Definition: ArrayBuffer.h:478
ogdf::ArrayBuffer::full
bool full() const
Returns true iff the buffer is non-growable and filled.
Definition: ArrayBuffer.h:233
ogdf::ArrayReverseIteratorBase
Random-access reverse iterator based on a pointer to an array element.
Definition: Array.h:49
ogdf::Array::permute
void permute()
Randomly permutes the array.
Definition: Array.h:522
ogdf::Array::iterator
ArrayIterator< E > iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition: Array.h:229
ogdf::ArrayBuffer::popRet
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:224
ogdf::Array::init
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:367
ogdf::ArrayBuffer::setCapacity
void setCapacity(INDEX newCapacity)
Changes the capacity of the buffer (independent whether the buffer is growable of not).
Definition: ArrayBuffer.h:493
OGDF_NEW_DELETE
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition: memory.h:84
ogdf::ArrayBuffer::operator=
ArrayBuffer< E, INDEX > & operator=(const ArrayBuffer< E, INDEX > &buffer)
Assignment operator.
Definition: ArrayBuffer.h:256
ogdf::ArrayBuffer::linearSearch
INDEX linearSearch(const E &x) const
Performs a linear search for element x.
Definition: ArrayBuffer.h:392
ogdf::ArrayBuffer::permute
void permute()
Randomly permutes the array.
Definition: ArrayBuffer.h:484
ogdf::Array::operator=
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:447
ogdf::ArrayBuffer< abacus::Constraint * >::const_reverse_iterator
typename Array< abacus::Constraint *, int >::const_reverse_iterator const_reverse_iterator
Definition: ArrayBuffer.h:65
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:110
ogdf::ArrayBuffer::num
INDEX num
The number of elements in the buffer.
Definition: ArrayBuffer.h:57
ogdf::ArrayBuffer::rbegin
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Definition: ArrayBuffer.h:166
r
int r[]
Definition: hierarchical-ranking.cpp:8
ogdf::ArrayBuffer::quicksort
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:420
ogdf::ArrayBuffer::init
void init()
Reinitializes the array, clearing it, and without initial memory allocation.
Definition: ArrayBuffer.h:250
ogdf::AlgorithmFailureCode::Array
@ Array
ogdf::ArrayBuffer::permute
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
Definition: ArrayBuffer.h:473
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:63
ogdf::ArrayBuffer::end
const_iterator end() const
Returns a const iterator to one past the last element.
Definition: ArrayBuffer.h:160
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:214
ogdf::Array::operator[]
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:303
ogdf::ArrayBuffer< abacus::Constraint * >::iterator
typename Array< abacus::Constraint *, int >::iterator iterator
Definition: ArrayBuffer.h:64
ogdf::ArrayBuffer::size
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:236
ogdf::ArrayBuffer::quicksort
void quicksort(const COMPARER &comp)
Sorts buffer using Quicksort and a user-defined comparer comp.
Definition: ArrayBuffer.h:432
ogdf::ArrayBuffer::push
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:209
ogdf::ArrayBuffer::rend
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
Definition: ArrayBuffer.h:172
ogdf::ArrayBuffer::end
iterator end()
Returns an iterator to one past the last element.
Definition: ArrayBuffer.h:157
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:978
ogdf::Array::begin
iterator begin()
Returns an iterator to the first element.
Definition: Array.h:324
ogdf::Array::rend
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
Definition: Array.h:351
ogdf::ArrayBuffer< abacus::Constraint * >::key_type
int key_type
Definition: ArrayBuffer.h:61
ogdf::ArrayBuffer::binarySearch
INDEX binarySearch(const E &e) const
Performs a binary search for element e.
Definition: ArrayBuffer.h:444
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:142
ogdf::ArrayBuffer::begin
const_iterator begin() const
Returns a const iterator to the first element.
Definition: ArrayBuffer.h:154
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:409
ogdf::ArrayBuffer::growable
bool growable
Definition: ArrayBuffer.h:58
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:467
ogdf::ArrayBuffer::operator[]
E & operator[](INDEX i)
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:190
ogdf::ArrayBuffer::begin
iterator begin()
Returns an iterator to the first element.
Definition: ArrayBuffer.h:151
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:437
ogdf::Array::m_pStart
E * m_pStart
The real start of the array (address of A[m_low]).
Definition: Array.h:704
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:77
ogdf::ArrayBuffer< abacus::Constraint * >::reverse_iterator
typename Array< abacus::Constraint *, int >::reverse_iterator reverse_iterator
Definition: ArrayBuffer.h:66
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:227
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:73
std
Definition: GML.h:110
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer()
Creates an empty array buffer, without initial memory allocation.
Definition: ArrayBuffer.h:69
ogdf::ArrayBuffer::pop
void pop()
Removes the newest element from the buffer.
Definition: ArrayBuffer.h:218
ogdf::ArrayBuffer::operator==
bool operator==(const ArrayBuffer< E, INDEX > &L) const
Equality operator.
Definition: ArrayBuffer.h:355
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:556
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:967
ogdf::ArrayBuffer::operator=
ArrayBuffer< E, INDEX > & operator=(ArrayBuffer< E, INDEX > &&buffer)
Assignment operator (move semantics).
Definition: ArrayBuffer.h:267
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:634
ogdf::Array::size
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:297
ogdf::ArrayBuffer::compactCpycon
void compactCpycon(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:287
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:825
ogdf::Array::copy
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition: Array.h:934
ogdf::ArrayBuffer::clear
void clear()
Clears the buffer.
Definition: ArrayBuffer.h:95
ogdf::ArrayBuffer::operator[]
const E & operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:183
ogdf::ArrayBuffer::operator!=
bool operator!=(const ArrayBuffer< E, INDEX > &L) const
Inequality operator.
Definition: ArrayBuffer.h:376
ogdf::ArrayBuffer::rend
reverse_iterator rend()
Returns a reverse iterator to one before the first element.
Definition: ArrayBuffer.h:169
ogdf::ArrayBuffer::ArrayBuffer
ArrayBuffer(const ArrayBuffer< E, INDEX > &buffer)
Creates an array buffer that is a copy of buffer.
Definition: ArrayBuffer.h:81