46template<
typename T,
typename C = std::less<T>>
61 for (
int i = 1; i <=
m_size; i++) {
74 const T&
top()
const override;
103 const T&
value(
int* handle)
const override;
190template<
typename T,
typename C>
196template<
typename T,
typename C>
202 if (m_size == m_arraySize) {
206 for (
int i = 1; i <= m_arraySize; i++) {
210 delete[] m_heapArray;
212 m_arraySize = higherArraySize(m_arraySize);
221 int* result = entry.
handle;
223 OGDF_ASSERT(*result == *(m_heapArray[*result].handle));
227 OGDF_ASSERT(*result == *(m_heapArray[*result].handle));
232template<
typename T,
typename C>
235 delete m_heapArray[1].handle;
240 m_heapArray[1] = m_heapArray[m_size + 1];
247 for (
int i = 1; i <= m_size; i++) {
251 delete[] m_heapArray;
253 m_arraySize = lowerArraySize(m_arraySize);
260template<
typename T,
typename C>
269template<
typename T,
typename C>
275 return m_heapArray[*handle].value;
278template<
typename T,
typename C>
283template<
typename T,
typename C>
291 m_heapArray =
new HeapEntry[arrayBound(m_arraySize)];
294template<
typename T,
typename C>
296 for (
int i = 1; i <= m_size; i++) {
297 delete m_heapArray[i].
handle;
300 delete[] m_heapArray;
304template<
typename T,
typename C>
310 *(m_heapArray[1].handle) = 1;
315 while ((parentIndex(run) >= 1)
316 && this->comparator()(
tempEntry.value, m_heapArray[parentIndex(run)].value)) {
317 m_heapArray[run] = m_heapArray[parentIndex(run)];
318 *(m_heapArray[run].handle) = run;
320 run = parentIndex(run);
324 *(m_heapArray[run].handle) = run;
328template<
typename T,
typename C>
334 if (pos >= m_size / 2 + 1) {
335 *(m_heapArray[pos].handle) = pos;
338 T value = m_heapArray[pos].
value;
340 const C& compare = this->comparator();
343 if (hasLeft(pos) && compare(m_heapArray[leftChildIndex(pos)].value, value)
344 && compare(m_heapArray[leftChildIndex(pos)].value,
345 m_heapArray[rightChildIndex(pos)].value)) {
346 sIndex = leftChildIndex(pos);
348 }
else if (hasRight(pos) && compare(m_heapArray[rightChildIndex(pos)].value, value)) {
349 sIndex = rightChildIndex(pos);
355 m_heapArray[pos] = m_heapArray[
sIndex];
359 *(m_heapArray[pos].handle) = pos;
364 *(m_heapArray[pos].handle) = pos;
Interface for heap implementations.
Heap realized by a data array.
bool hasRight(int num)
Returns true if right child exists.
int size() const
Returns the number of stored elements.
bool empty() const
Returns true iff the heap is empty.
void clear()
Reinitializes the data structure.
void siftUp(int pos)
Establishes heap property by moving element up in heap if necessary.
const T & top() const override
Returns the topmost value in the heap.
int * push(const T &value) override
Inserts a value into the heap.
int lowerArraySize(int arraySize)
void decrease(int *handle, const T &value) override
Decreases a single value.
int leftChildIndex(int num)
Array index of left child.
void siftDown(int pos)
Establishes heap property by moving element down in heap if necessary.
int capacity() const
Returns the current size.
int higherArraySize(int arraySize)
int rightChildIndex(int num)
Array index of right child.
void pop() override
Removes the topmost value from the heap.
int higherArrayBound(int arraySize)
int parentIndex(int num)
Array index of parent node.
int arrayBound(int arraySize)
int lowerArrayBound(int arraySize)
bool hasLeft(int num)
Returns true if left child exists.
BinaryHeap(const C &comp=C(), int initialSize=128)
Initializes an empty binary heap.
const T & value(int *handle) const override
Returns the value of that handle.
void init(int initialSize)
Common interface for all heap classes.
virtual const T & value(const Handle handle) const =0
Returns the value of that handle.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.