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>
35
36#include <cstring>
37
38namespace ogdf {
39
41
55template<class E, class INDEX = int>
56class ArrayBuffer : private Array<E, INDEX> {
59
60public:
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
83
85
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++) {
127 }
128 }
129
131 for (j = ind[nInd - 1] + 1; j < size(); j++) {
133 }
134
135 num -= nInd;
136 }
137
139 bool isGrowable() const { return growable; }
140
143
149
152
155
158
161
164
167
170
173
175
181
183 const E& operator[](INDEX i) const {
184 OGDF_ASSERT(0 <= i);
185 OGDF_ASSERT(i < num);
187 }
188
191 OGDF_ASSERT(0 <= i);
192 OGDF_ASSERT(i < num);
194 }
195
197 const E& top() const {
198 OGDF_ASSERT(num > 0);
200 }
201
203 E& top() {
204 OGDF_ASSERT(num > 0);
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
240
242
248
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
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
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
336 OGDF_ASSERT(this != &A2);
337 if (num) {
338 A2.init(num);
339 memcpy(A2.m_pStart, this->m_pStart, sizeof(E) * num);
340 } else {
341 A2.init(0);
342 }
343 }
344
348
350 bool operator==(const ArrayBuffer<E, INDEX>& L) const {
351 if (size() != L.size()) {
352 return false;
353 }
354
355 auto thisIterator = begin();
356 auto thatIterator = L.begin();
357
358 while (thisIterator != end() && thatIterator != L.end()) {
359 if (*thisIterator != *thatIterator) {
360 return false;
361 }
362 ++thisIterator;
363 ++thatIterator;
364 }
365
366 OGDF_ASSERT(thisIterator == end() && thatIterator == L.end());
367 return true;
368 }
369
371 bool operator!=(const ArrayBuffer<E, INDEX>& L) const { return !operator==(L); }
372
374
380
382
387 INDEX linearSearch(const E& x) const {
388 INDEX i;
389 for (i = num; i-- > 0;) {
390 if (x == Array<E, INDEX>::m_vpStart[i]) {
391 break;
392 }
393 }
394 return i;
395 }
396
398
403 template<class COMPARER>
404 INDEX linearSearch(const E& x, const COMPARER& comp) const {
405 INDEX i;
406 for (i = num; i-- > 0;) {
407 if (comp.equal(x, Array<E, INDEX>::m_vpStart[i])) {
408 break;
409 }
410 }
411 return i;
412 }
413
415 inline void quicksort() {
416 if (num == 0) {
417 return;
418 }
420 }
421
423
426 template<class COMPARER>
427 inline void quicksort(const COMPARER& comp) {
428 if (num == 0) {
429 return;
430 }
432 }
433
435
439 inline INDEX binarySearch(const E& e) const {
441 }
442
444
448 template<class COMPARER>
449 inline INDEX binarySearch(const E& e, const COMPARER& comp) const {
450 return Array<E, INDEX>::binarySearch(0, num - 1, e, comp);
451 }
452
454
457
458 using Array<E, INDEX>::swap;
459
461 template<class RNG>
465
467 template<class RNG>
468 void permute(RNG& rng) {
469 permute(0, num - 1, rng);
470 }
471
474 std::minstd_rand rng(randomSeed());
475 permute(l, r, rng);
476 }
477
479 void permute() { permute(0, num - 1); }
480
482
484
489
491};
492
494template<class E, class INDEX>
495void print(std::ostream& os, const ArrayBuffer<E, INDEX>& a, char delim = ' ') {
496 for (int i = 0; i < a.size(); i++) {
497 if (i > 0) {
498 os << delim;
499 }
500 os << a[i];
501 }
502}
503
505template<class E, class INDEX>
506std::ostream& operator<<(std::ostream& os, const ogdf::ArrayBuffer<E, INDEX>& a) {
507 print(os, a);
508 return os;
509}
510
511}
Declaration and implementation of Array class and Array algorithms.
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition ArrayBuffer.h:56
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
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.
Definition ArrayBuffer.h:95
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:57
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:69
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
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:88
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:81
bool empty() const
Returns true if the buffer is empty, false otherwise.
typename Array< E, INDEX >::iterator iterator
Definition ArrayBuffer.h:64
typename Array< E, INDEX >::const_reverse_iterator const_reverse_iterator
Definition ArrayBuffer.h:65
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:66
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:63
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:214
iterator begin()
Returns an iterator to the first element.
Definition Array.h:324
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition Array.h:303
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
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition Array.h:825
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
Definition Array.h:506
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
Definition Array.h:351
void init()
Reinitializes the array to an array with empty index set.
Definition Array.h:367
void quicksort()
Sorts array using Quicksort.
Definition Array.h:634
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition Array.h:556
ArrayConstIterator< E > const_iterator
Provides a random-access iterator that can read a const element in an array.
Definition Array.h:227
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition Array.h:447
void permute()
Randomly permutes the array.
Definition Array.h:522
INDEX size() const
Returns the size (number of elements) of the array.
Definition Array.h:297
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition Array.h:934
ArrayIterator< E > iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition Array.h:229
Random-access reverse iterator based on a pointer to an array element.
Definition Array.h:69
Standard comparer (valid as a static comparer).
Definition comparer.h:59
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition memory.h:84
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
int r[]
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
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:978
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
Definition GML.h:110