Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
46namespace ogdf {
47
49
63template<class E, class INDEX = int>
64class ArrayBuffer : private Array<E, INDEX> {
65 INDEX num;
67
68public:
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
157
160
163
166
169
172
175
178
181
183
189
191 const E& operator[](INDEX i) const {
192 OGDF_ASSERT(0 <= i);
193 OGDF_ASSERT(i < num);
195 }
196
198 E& operator[](INDEX i) {
199 OGDF_ASSERT(0 <= i);
200 OGDF_ASSERT(i < num);
202 }
203
205 const E& top() const {
206 OGDF_ASSERT(num > 0);
208 }
209
211 E& top() {
212 OGDF_ASSERT(num > 0);
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
256
259
262
266 num = buffer.num;
267 growable = buffer.growable;
268 return *this;
269 }
270
272
276 Array<E, INDEX>::operator=(std::move(buffer));
277 num = buffer.num;
278 growable = buffer.growable;
279 buffer.num = 0;
280 buffer.growable = true;
281 return *this;
282 }
283
285
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
393
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 {
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) {
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
507template<class E, class INDEX>
508void 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
518template<class E, class INDEX>
519std::ostream& operator<<(std::ostream& os, const ogdf::ArrayBuffer<E, INDEX>& a) {
520 print(os, a);
521 return os;
522}
523
524}
Declaration and implementation of Array class and Array algorithms.
Basic declarations, included by all source files.
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition ArrayBuffer.h:64
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
iterator end()
Returns an iterator to one past the last element.
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
const E & top() const
Returns the newest element of the buffer.
void clear()
Clears the buffer.
E popRet()
Removes the newest element from the buffer and returns it.
void setGrowable(bool _growable)
Sets the flag whether the buffer will automatically expand if the initial size is insufficient.
reverse_iterator rend()
Returns a reverse iterator to one before the first element.
void quicksort(const COMPARER &comp)
Sorts buffer using Quicksort and a user-defined comparer comp.
void push(E e)
Puts a new element in the buffer.
INDEX binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
const E & operator[](INDEX i) const
Returns a reference to the element at position i.
const_iterator begin() const
Returns a const iterator to the first element.
void quicksort()
Sorts buffer using Quicksort.
void permute()
Randomly permutes the array.
INDEX linearSearch(const E &x) const
Performs a linear search for element x.
void setCapacity(INDEX newCapacity)
Changes the capacity of the buffer (independent whether the buffer is growable of not).
reverse_iterator rbegin()
Returns a reverse iterator to the last element.
bool full() const
Returns true iff the buffer is non-growable and filled.
void compactCpycon(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
bool isGrowable() const
Returns whether the buffer will automatically expand if the initial size is insufficient.
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
bool operator==(const ArrayBuffer< E, INDEX > &L) const
Equality operator.
void pop()
Removes the newest element from the buffer.
INDEX num
The number of elements in the buffer.
Definition ArrayBuffer.h:65
E & operator[](INDEX i)
Returns a reference to the element at position i.
INDEX linearSearch(const E &x, const COMPARER &comp) const
Performs a linear search for element x with comparer comp.
ArrayBuffer< E, INDEX > & operator=(ArrayBuffer< E, INDEX > &&buffer)
Assignment operator (move semantics).
void permute(INDEX l, INDEX r, RNG &rng)
Randomly permutes the subarray with index set [l..r] using random number generator rng.
void init()
Reinitializes the array, clearing it, and without initial memory allocation.
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
ArrayBuffer< E, INDEX > & operator=(const ArrayBuffer< E, INDEX > &buffer)
Assignment operator.
bool operator!=(const ArrayBuffer< E, INDEX > &L) const
Inequality operator.
INDEX binarySearch(const E &e) const
Performs a binary search for element e.
ArrayBuffer()
Creates an empty array buffer, without initial memory allocation.
Definition ArrayBuffer.h:77
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
E & top()
Returns the newest element of the buffer.
ArrayBuffer(ArrayBuffer< E, INDEX > &&buffer)
Creates an array buffer containing the elements of buffer (move semantics).
Definition ArrayBuffer.h:96
const_iterator end() const
Returns a const iterator to one past the last element.
ArrayBuffer(const ArrayBuffer< E, INDEX > &buffer)
Creates an array buffer that is a copy of buffer.
Definition ArrayBuffer.h:89
bool empty() const
Returns true if the buffer is empty, false otherwise.
typename Array< E, INDEX >::iterator iterator
Definition ArrayBuffer.h:72
typename Array< E, INDEX >::const_reverse_iterator const_reverse_iterator
Definition ArrayBuffer.h:73
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
iterator begin()
Returns an iterator to the first element.
INDEX size() const
Returns number of elements in the buffer.
typename Array< E, INDEX >::reverse_iterator reverse_iterator
Definition ArrayBuffer.h:74
INDEX capacity() const
Returns the current capacity of the datastructure. Note that this value is rather irrelevant if the a...
void leftShift(ArrayBuffer< INDEX, INDEX > &ind)
Removes the components listed in the buffer ind by shifting the remaining components to the left.
void init(INDEX size)
Reinitializes the array, clearing it, and allocating memory for up to size elements.
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
typename Array< E, INDEX >::const_iterator const_iterator
Definition ArrayBuffer.h:71
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:219
iterator begin()
Returns an iterator to the first element.
Definition Array.h:329
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition Array.h:308
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
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition Array.h:830
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
Definition Array.h:511
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
Definition Array.h:356
void init()
Reinitializes the array to an array with empty index set.
Definition Array.h:372
void quicksort()
Sorts array using Quicksort.
Definition Array.h:639
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition Array.h:561
ArrayConstIterator< E > const_iterator
Provides a random-access iterator that can read a const element in an array.
Definition Array.h:232
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition Array.h:452
void permute()
Randomly permutes the array.
Definition Array.h:527
INDEX size() const
Returns the size (number of elements) of the array.
Definition Array.h:302
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition Array.h:939
ArrayIterator< E > iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition Array.h:234
E * m_pStart
The real start of the array (address of A[m_low]).
Definition Array.h:709
Random-access reverse iterator based on a pointer to an array element.
Definition Array.h:74
Standard comparer (valid as a static comparer).
Definition comparer.h:63
Declarations for Comparer objects.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition memory.h:85
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
Declaration of memory manager for allocating small pieces of memory.
The namespace for all OGDF objects.
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition Array.h:983
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
Definition GML.h:111