Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

ArrayBuffer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h>
35 #include <cstring>
36 
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 public:
60  using key_type = INDEX;
61  using value_type = E;
66 
68  ArrayBuffer() : Array<E,INDEX>(), num(0), growable(true) {}
69 
71  explicit ArrayBuffer(INDEX size, bool autogrow = true) : Array<E,INDEX>(size), num(0), growable(autogrow) {}
72 
74  explicit ArrayBuffer(const Array<E,INDEX>& source, bool autogrow = true) : Array<E,INDEX>(source), num(0), growable(autogrow) {}
75 
77  ArrayBuffer(const ArrayBuffer<E,INDEX> &buffer) : Array<E,INDEX>(buffer), num(buffer.num), growable(buffer.growable) { }
78 
80 
83  ArrayBuffer(ArrayBuffer<E,INDEX> &&buffer) : Array<E,INDEX>(std::move(buffer)), num(buffer.num), growable(buffer.growable) {
84  buffer.num = 0;
85  buffer.growable = true;
86  }
87 
89  void init() { Array<E,INDEX>::init(); }
91  void init(INDEX size) { Array<E,INDEX>::init(size); }
92 
94  void clear() { num = 0; }
95 
97  const E &top() const { OGDF_ASSERT(num>0); return Array<E,INDEX>::operator[](num-1); }
99  E &top() { OGDF_ASSERT(num>0); return Array<E,INDEX>::operator[](num-1); }
100 
102  void push(E e) {
103  if (num == Array<E,INDEX>::size()) {
104  OGDF_ASSERT(growable);
105  Array<E,INDEX>::grow(max(num,1)); // double the size
106  }
107  Array<E,INDEX>::operator[](num++) = e;
108  }
109 
111  void pop() { OGDF_ASSERT(num>0); --num; }
113  E popRet() { OGDF_ASSERT(num>0); return Array<E,INDEX>::operator[](--num); }
114 
116  bool empty() const { return !num; }
117 
119  bool full() const { return !growable && num == Array<E,INDEX>::size(); }
120 
122  INDEX size() const { return num; }
123 
125  INDEX capacity() const { return Array<E,INDEX>::size(); }
126 
128  bool isGrowable() const { return growable; }
129 
131  void setGrowable(bool _growable) { growable = _growable; }
132 
135 
138 
141 
144 
147 
150 
153 
156 
158  const E &operator[](INDEX i) const {
159  OGDF_ASSERT(0 <= i);
160  OGDF_ASSERT(i < num);
161  return Array<E,INDEX>::operator[](i);
162  }
164  E &operator[](INDEX i) {
165  OGDF_ASSERT(0 <= i);
166  OGDF_ASSERT(i < num);
167  return Array<E,INDEX>::operator[](i);
168  }
169 
173  num = buffer.num;
174  growable = buffer.growable;
175  return *this;
176  }
177 
179 
184  num = buffer.num;
185  growable = buffer.growable;
186  buffer.num = 0;
187  buffer.growable = true;
188  return *this;
189  }
190 
192  bool operator==(const ArrayBuffer<E, INDEX> &L) const {
193  if (size() != L.size()) {
194  return false;
195  }
196 
197  auto thisIterator = begin();
198  auto thatIterator = L.begin();
199 
200  while (thisIterator != end() && thatIterator != L.end()) {
201  if (*thisIterator != *thatIterator) {
202  return false;
203  }
204  ++thisIterator;
205  ++thatIterator;
206  }
207 
208  OGDF_ASSERT(thisIterator == end() && thatIterator == L.end());
209  return true;
210  }
211 
213  bool operator!=(const ArrayBuffer<E, INDEX> &L) const {
214  return !operator==(L);
215  }
216 
218 
219 
221 
232  void compactCopy(Array<E,INDEX>& A2) const {
233  OGDF_ASSERT(this != &A2);
234  if(num) {
235  A2.init(num);
236  for(INDEX i = num; i-- > 0;)
237  A2[i] = (*this)[i];
238  } else
239  A2.init(0);
240  }
241 
243 
254  void compactCpycon(Array<E,INDEX>& A2) const {
255  OGDF_ASSERT(this != &A2);
256  if(num) {
257  INDEX tmp = Array<E,INDEX>::m_high; // thank god i'm a friend of Array
258  Array<E,INDEX>::m_high = num-1; // fake smaller size
259  A2.copy(*this); // copy
261  } else
262  A2.init(0);
263  }
264 
266 
277  void compactMemcpy(Array<E,INDEX>& A2) const {
278  OGDF_ASSERT(this != &A2);
279  if(num) {
280  A2.init(num);
281  memcpy(A2.m_pStart,this->m_pStart,sizeof(E)*num);
282  } else
283  A2.init(0);
284  }
285 
287 
292  INDEX linearSearch (const E& x) const {
293  INDEX i;
294  for(i = num; i-- > 0;)
295  if(x == Array<E,INDEX>::m_vpStart[i]) break;
296  return i;
297  }
298 
300 
305  template<class COMPARER>
306  INDEX linearSearch (const E& x, const COMPARER &comp) const {
307  INDEX i;
308  for(i = num; i-- > 0;)
309  if(comp.equal(x, Array<E,INDEX>::m_vpStart[i])) break;
310  return i;
311  }
312 
314  inline void quicksort() {
315  if (num == 0) {
316  return;
317  }
319  }
320 
322 
325  template<class COMPARER>
326  inline void quicksort(const COMPARER &comp) {
327  if (num == 0) {
328  return;
329  }
330  Array<E,INDEX>::quicksort(0,num-1,comp);
331  }
332 
334 
338  inline INDEX binarySearch (const E& e) const {
339  return Array<E,INDEX>::binarySearch(0, num-1, e, StdComparer<E>());
340  }
341 
343 
347  template<class COMPARER>
348  inline INDEX binarySearch(const E& e, const COMPARER &comp) const {
349  return Array<E,INDEX>::binarySearch(0, num-1, e, comp);
350  }
351 
354 
355  using Array<E, INDEX>::swap;
356 
358  template<class RNG>
359  void permute(INDEX l, INDEX r, RNG &rng) {
360  Array<E,INDEX>::permute(l, r, rng);
361  }
362 
364  template<class RNG>
365  void permute(RNG &rng) {
366  permute(0, num - 1, rng);
367  }
368 
370  void permute(INDEX l, INDEX r) {
371  std::minstd_rand rng(randomSeed());
372  permute(l, r, rng);
373  }
374 
376  void permute() {
377  permute(0, num-1);
378  }
379 
381 
383 
396  const INDEX nInd = ind.size();
397  if (nInd == 0) return;
398 
399  OGDF_ASSERT(ind[0] >= 0);
400  OGDF_ASSERT(ind[0] < num);
401 
402  INDEX j, current = ind[0];
403  for (INDEX i = 0; i < nInd - 1; i++) {
404  OGDF_ASSERT(ind[i+1] >= 0);
405  OGDF_ASSERT(ind[i+1] < num);
406 
407  const INDEX last = ind[i+1];
408  for(j = ind[i]+1; j < last; j++)
409  operator[](current++) = operator[](j);
410  }
411 
413  for (j = ind[nInd - 1]+1; j < size(); j++)
414  operator[](current++) = operator[](j);
415 
416  num -= nInd;
417  }
418 
420 
424  void setCapacity(INDEX newCapacity) {
425  Array<E, INDEX>::resize(newCapacity);
426  }
427 
429 };
430 
431 
433 template<class E, class INDEX>
434 void print(std::ostream &os, const ArrayBuffer<E,INDEX> &a, char delim = ' ')
435 {
436  for (int i = 0; i < a.size(); i++) {
437  if (i > 0) os << delim;
438  os << a[i];
439  }
440 }
441 
442 
444 template<class E, class INDEX>
445 std::ostream &operator<<(std::ostream &os, const ogdf::ArrayBuffer<E,INDEX> &a)
446 {
447  print(os,a);
448  return os;
449 }
450 
451 }
bool operator!=(const ArrayBuffer< E, INDEX > &L) const
Inequality operator.
Definition: ArrayBuffer.h:213
typename Array< abacus::Sub *, int >::const_iterator const_iterator
Definition: ArrayBuffer.h:62
ArrayBuffer< E, INDEX > & operator=(ArrayBuffer< E, INDEX > &&buffer)
Assignment operator (move semantics).
Definition: ArrayBuffer.h:182
typename Array< abacus::Sub *, int >::const_reverse_iterator const_reverse_iterator
Definition: ArrayBuffer.h:64
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:359
void init(INDEX size)
Reinitializes the array, clearing it, and allocating memory for up to size elements.
Definition: ArrayBuffer.h:91
INDEX binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
Definition: ArrayBuffer.h:348
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:71
ArrayBuffer(const ArrayBuffer< E, INDEX > &buffer)
Creates an array buffer that is a copy of buffer.
Definition: ArrayBuffer.h:77
void clear()
Clears the buffer.
Definition: ArrayBuffer.h:94
INDEX linearSearch(const E &x) const
Performs a linear search for element x.
Definition: ArrayBuffer.h:292
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
void pop()
Removes the newest element from the buffer.
Definition: ArrayBuffer.h:111
void permute()
Randomly permutes the array.
Definition: Array.h:509
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:354
bool operator==(const ArrayBuffer< E, INDEX > &L) const
Equality operator.
Definition: ArrayBuffer.h:192
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
Definition: ArrayBuffer.h:155
E & operator[](INDEX i)
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:164
INDEX capacity() const
Returns the current capacity of the datastructure. Note that this value is rather irrelevant if the a...
Definition: ArrayBuffer.h:125
reverse_iterator rend()
Returns a reverse iterator to one before the first element.
Definition: ArrayBuffer.h:152
typename Array< abacus::Sub *, int >::reverse_iterator reverse_iterator
Definition: ArrayBuffer.h:65
The subproblem.
Definition: sub.h:68
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
Definition: GML.h:110
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
void permute()
Randomly permutes the array.
Definition: ArrayBuffer.h:376
iterator begin()
Returns an iterator to the first element.
Definition: ArrayBuffer.h:134
INDEX binarySearch(const E &e) const
Performs a binary search for element e.
Definition: ArrayBuffer.h:338
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:314
const_iterator begin() const
Returns a const iterator to the first element.
Definition: ArrayBuffer.h:137
Declaration and implementation of Array class and Array algorithms.
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Definition: ArrayBuffer.h:149
void compactCpycon(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:254
ArrayBuffer< E, INDEX > & operator=(const ArrayBuffer< E, INDEX > &buffer)
Assignment operator.
Definition: ArrayBuffer.h:171
Standard comparer (valid as a static comparer).
Definition: comparer.h:58
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition: Array.h:895
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
Definition: ArrayBuffer.h:370
void quicksort(const COMPARER &comp)
Sorts buffer using Quicksort and a user-defined comparer comp.
Definition: ArrayBuffer.h:326
void compactMemcpy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:277
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:116
bool full() const
Returns true iff the buffer is non-growable and filled.
Definition: ArrayBuffer.h:119
Random-access reverse iterator based on a pointer to an array element.
Definition: Array.h:47
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:282
ArrayBuffer()
Creates an empty array buffer, without initial memory allocation.
Definition: ArrayBuffer.h:68
E & top()
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:99
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:789
reverse_iterator rbegin()
Returns a reverse iterator to the last element.
Definition: ArrayBuffer.h:146
ArrayIterator< E > iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition: Array.h:215
typename Array< abacus::Sub *, int >::iterator iterator
Definition: ArrayBuffer.h:63
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
void setGrowable(bool _growable)
Sets the flag whether the buffer will automatically expand if the initial size is insufficient...
Definition: ArrayBuffer.h:131
ArrayBuffer(ArrayBuffer< E, INDEX > &&buffer)
Creates an array buffer containing the elements of buffer (move semantics).
Definition: ArrayBuffer.h:83
const T & move(const T &v)
Definition: backward.hpp:243
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:122
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition: Array.h:546
iterator begin()
Returns an iterator to the first element.
Definition: Array.h:310
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
Definition: Array.h:337
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
Definition: ArrayBuffer.h:365
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:113
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:422
void setCapacity(INDEX newCapacity)
Changes the capacity of the buffer (independent whether the buffer is growable of not)...
Definition: ArrayBuffer.h:424
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:613
INDEX linearSearch(const E &x, const COMPARER &comp) const
Performs a linear search for element x with comparer comp.
Definition: ArrayBuffer.h:306
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:74
ArrayConstIterator< E > const_iterator
Provides a random-access iterator that can read a const element in an array.
Definition: Array.h:213
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:232
iterator end()
Returns an iterator to one past the last element.
Definition: ArrayBuffer.h:140
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:432
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:288
E * m_pStart
The real start of the array (address of A[m_low]).
Definition: Array.h:684
The namespace for all OGDF objects.
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:395
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:931
bool isGrowable() const
Returns whether the buffer will automatically expand if the initial size is insufficient.
Definition: ArrayBuffer.h:128
void init()
Reinitializes the array, clearing it, and without initial memory allocation.
Definition: ArrayBuffer.h:89
const E & operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:158
const E & top() const
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:97
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
const_iterator end() const
Returns a const iterator to one past the last element.
Definition: ArrayBuffer.h:143
INDEX num
The number of elements in the buffer.
Definition: ArrayBuffer.h:57