Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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:
61  ArrayBuffer() : Array<E,INDEX>(), num(0), growable(true) {}
62 
64  explicit ArrayBuffer(INDEX size, bool autogrow = true) : Array<E,INDEX>(size), num(0), growable(autogrow) {}
65 
67  explicit ArrayBuffer(const Array<E,INDEX>& source, bool autogrow = true) : Array<E,INDEX>(source), num(0), growable(autogrow) {}
68 
70  ArrayBuffer(const ArrayBuffer<E,INDEX> &buffer) : Array<E,INDEX>(buffer), num(buffer.num), growable(buffer.growable) { }
71 
73 
76  ArrayBuffer(ArrayBuffer<E,INDEX> &&buffer) : Array<E,INDEX>(std::move(buffer)), num(buffer.num), growable(buffer.growable) {
77  buffer.num = 0;
78  buffer.growable = true;
79  }
80 
82  void init() { Array<E,INDEX>::init(); }
84  void init(INDEX size) { Array<E,INDEX>::init(size); }
85 
87  void clear() { num = 0; }
88 
90  const E &top() const { OGDF_ASSERT(num>0); return Array<E,INDEX>::operator[](num-1); }
92  E &top() { OGDF_ASSERT(num>0); return Array<E,INDEX>::operator[](num-1); }
93 
95  void push(E e) {
96  if (num == Array<E,INDEX>::size()) {
97  if (growable)
98  Array<E,INDEX>::grow(max(num,1)); // double the size
99  else
101  }
102  Array<E,INDEX>::operator[](num++) = e;
103  }
104 
106  void pop() { OGDF_ASSERT(num>0); --num; }
108  E popRet() { OGDF_ASSERT(num>0); return Array<E,INDEX>::operator[](--num); }
109 
111  bool empty() const { return !num; }
112 
114  bool full() const { return (!growable) && (num == Array<E,INDEX>::size()); }
115 
117  INDEX size() const { return num; }
118 
120  INDEX capacity() const { return Array<E,INDEX>::size(); }
121 
123  bool isGrowable() const { return growable; }
124 
126  void setGrowable(bool _growable) { growable = _growable; }
127 
129  E *begin() { return Array<E, INDEX>::begin(); }
130 
132  const E *begin() const { return Array<E, INDEX>::begin(); }
133 
135  E *end() { return Array<E, INDEX>::begin()+num; }
136 
138  const E *end() const { return Array<E, INDEX>::begin()+num; }
139 
141  E *rbegin() { return Array<E, INDEX>::begin()+(num-1); }
142 
144  const E *rbegin() const { return Array<E, INDEX>::begin()+(num-1); }
145 
147  E *rend() { return Array<E, INDEX>::rend(); }
148 
150  const E *rend() const { return Array<E, INDEX>::rend(); }
151 
153  const E &operator[](INDEX i) const {
154  OGDF_ASSERT(0 <= i);
155  OGDF_ASSERT(i < num);
156  return Array<E,INDEX>::operator[](i);
157  }
159  E &operator[](INDEX i) {
160  OGDF_ASSERT(0 <= i);
161  OGDF_ASSERT(i < num);
162  return Array<E,INDEX>::operator[](i);
163  }
164 
168  num = buffer.num;
169  growable = buffer.growable;
170  return *this;
171  }
172 
174 
179  num = buffer.num;
180  growable = buffer.growable;
181  buffer.num = 0;
182  buffer.growable = true;
183  return *this;
184  }
185 
186 
188 
199  void compactCopy(Array<E,INDEX>& A2) const {
200  OGDF_ASSERT(this != &A2);
201  if(num) {
202  A2.init(num);
203  for(INDEX i = num; i-- > 0;)
204  A2[i] = (*this)[i];
205  } else
206  A2.init(0);
207  }
208 
210 
221  void compactCpycon(Array<E,INDEX>& A2) const {
222  OGDF_ASSERT(this != &A2);
223  if(num) {
224  INDEX tmp = Array<E,INDEX>::m_high; // thank god i'm a friend of Array
225  Array<E,INDEX>::m_high = num-1; // fake smaller size
226  A2.copy(*this); // copy
228  } else
229  A2.init(0);
230  }
231 
233 
244  void compactMemcpy(Array<E,INDEX>& A2) const {
245  OGDF_ASSERT(this != &A2);
246  if(num) {
247  A2.init(num);
248  memcpy(A2.m_pStart,this->m_pStart,sizeof(E)*num);
249  } else
250  A2.init(0);
251  }
252 
254 
259  INDEX linearSearch (const E& x) const {
260  INDEX i;
261  for(i = num; i-- > 0;)
262  if(x == Array<E,INDEX>::m_vpStart[i]) break;
263  return i;
264  }
265 
267 
272  template<class COMPARER>
273  INDEX linearSearch (const E& x, const COMPARER &comp) const {
274  INDEX i;
275  for(i = num; i-- > 0;)
276  if(comp.equal(x, Array<E,INDEX>::m_vpStart[i])) break;
277  return i;
278  }
279 
281  inline void quicksort() {
282  if (num == 0) {
283  return;
284  }
286  }
287 
289 
292  template<class COMPARER>
293  inline void quicksort(const COMPARER &comp) {
294  if (num == 0) {
295  return;
296  }
297  Array<E,INDEX>::quicksort(0,num-1,comp);
298  }
299 
301 
305  inline INDEX binarySearch (const E& e) const {
306  return Array<E,INDEX>::binarySearch(0, num-1, e, StdComparer<E>());
307  }
308 
310 
314  template<class COMPARER>
315  inline INDEX binarySearch(const E& e, const COMPARER &comp) const {
316  return Array<E,INDEX>::binarySearch(0, num-1, e, comp);
317  }
318 
320  void permute() {
321  Array<E,INDEX>::permute(0, num-1);
322  }
323 
325 
338  const INDEX nInd = ind.size();
339  if (nInd == 0) return;
340 
341  OGDF_ASSERT(ind[0] >= 0);
342  OGDF_ASSERT(ind[0] < num);
343 
344  INDEX j, current = ind[0];
345  for (INDEX i = 0; i < nInd - 1; i++) {
346  OGDF_ASSERT(ind[i+1] >= 0);
347  OGDF_ASSERT(ind[i+1] < num);
348 
349  const INDEX last = ind[i+1];
350  for(j = ind[i]+1; j < last; j++)
351  operator[](current++) = operator[](j);
352  }
353 
355  for (j = ind[nInd - 1]+1; j < size(); j++)
356  operator[](current++) = operator[](j);
357 
358  num -= nInd;
359  }
360 
362 
366  void setCapacity(INDEX newCapacity) {
367  Array<E, INDEX>::resize(newCapacity);
368  }
369 
371 };
372 
373 
375 template<class E, class INDEX>
376 void print(ostream &os, const ArrayBuffer<E,INDEX> &a, char delim = ' ')
377 {
378  for (int i = 0; i < a.size(); i++) {
379  if (i > 0) os << delim;
380  os << a[i];
381  }
382 }
383 
384 
386 template<class E, class INDEX>
387 ostream &operator<<(ostream &os, const ogdf::ArrayBuffer<E,INDEX> &a)
388 {
389  print(os,a);
390  return os;
391 }
392 
393 } // end namespace ogdf
ArrayBuffer< E, INDEX > & operator=(ArrayBuffer< E, INDEX > &&buffer)
Assignment operator (move semantics).
Definition: ArrayBuffer.h:177
void init(INDEX size)
Reinitializes the array, clearing it, and allocating memory for up to size elements.
Definition: ArrayBuffer.h:84
Exception thrown when preconditions are violated.
Definition: exceptions.h:276
INDEX binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
Definition: ArrayBuffer.h:315
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:64
ArrayBuffer(const ArrayBuffer< E, INDEX > &buffer)
Creates an array buffer that is a copy of buffer.
Definition: ArrayBuffer.h:70
void clear()
Clears the buffer.
Definition: ArrayBuffer.h:87
INDEX linearSearch(const E &x) const
Performs a linear search for element x.
Definition: ArrayBuffer.h:259
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
void pop()
Removes the newest element from the buffer.
Definition: ArrayBuffer.h:106
void permute()
Randomly permutes the array.
Definition: Array.h:332
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
E & operator[](INDEX i)
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:159
INDEX capacity() const
Returns the current capacity of the datastructure. Note that this value is rather irrelevant if the a...
Definition: ArrayBuffer.h:120
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:95
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:44
E * rbegin()
Returns a pointer to the last element.
Definition: ArrayBuffer.h:141
void permute()
Randomly permutes the array.
Definition: ArrayBuffer.h:320
INDEX binarySearch(const E &e) const
Performs a binary search for element e.
Definition: ArrayBuffer.h:305
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:281
const E * end() const
Returns a pointer to one past the last element.
Definition: ArrayBuffer.h:138
Declaration and implementation of Array class and Array algorithms.
void compactCpycon(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:221
ArrayBuffer< E, INDEX > & operator=(const ArrayBuffer< E, INDEX > &buffer)
Assignment operator.
Definition: ArrayBuffer.h:166
Standard comparer (valid as a static comparer).
Definition: comparer.h:56
datastructure is already full
E * rend()
Returns a pointer to one before the first element.
Definition: ArrayBuffer.h:147
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
Definition: Array.h:717
void quicksort(const COMPARER &comp)
Sorts buffer using Quicksort and a user-defined comparer comp.
Definition: ArrayBuffer.h:293
void compactMemcpy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:244
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:111
bool full() const
Returns true iff the buffer is non-growable and filled.
Definition: ArrayBuffer.h:114
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:134
const E * rbegin() const
Returns a pointer to the last element.
Definition: ArrayBuffer.h:144
ArrayBuffer()
Creates an empty array buffer, without initial memory allocation.
Definition: ArrayBuffer.h:61
#define OGDF_THROW_PARAM(CLASS, PARAM)
Replacement for throw.
Definition: exceptions.h:51
E & top()
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:92
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:611
iterator rend()
Returns an iterator to one before the first element.
Definition: Array.h:189
#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:126
ArrayBuffer(ArrayBuffer< E, INDEX > &&buffer)
Creates an array buffer containing the elements of buffer (move semantics).
Definition: ArrayBuffer.h:76
const T & move(const T &v)
Definition: backward.hpp:243
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:117
int binarySearch(const E &e) const
Performs a binary search for element e.
Definition: Array.h:367
iterator begin()
Returns an iterator to the first element.
Definition: Array.h:162
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:108
const E * begin() const
Returns a pointer to the first element.
Definition: ArrayBuffer.h:132
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:274
void setCapacity(INDEX newCapacity)
Changes the capacity of the buffer (independent whether the buffer is growable of not)...
Definition: ArrayBuffer.h:366
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:434
INDEX linearSearch(const E &x, const COMPARER &comp) const
Performs a linear search for element x with comparer comp.
Definition: ArrayBuffer.h:273
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:67
const E * rend() const
Returns a pointer to one before the first element.
Definition: ArrayBuffer.h:150
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
Definition: ArrayBuffer.h:199
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:284
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:140
E * m_pStart
The real start of the array (address of A[m_low]).
Definition: Array.h:505
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:337
bool isGrowable() const
Returns whether the buffer will automatically expand if the initial size is insufficient.
Definition: ArrayBuffer.h:123
void init()
Reinitializes the array, clearing it, and without initial memory allocation.
Definition: ArrayBuffer.h:82
E * begin()
Returns a pointer to the first element.
Definition: ArrayBuffer.h:129
void print(ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
Definition: Array.h:753
const E & operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: ArrayBuffer.h:153
const E & top() const
Returns the newest element of the buffer.
Definition: ArrayBuffer.h:90
E * end()
Returns a pointer to one past the last element.
Definition: ArrayBuffer.h:135
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
INDEX num
The number of elements in the buffer.
Definition: ArrayBuffer.h:57