49template<
typename T,
typename C = std::less<T>>
60 explicit BinaryHeap(
const C& comp = C(),
int initialSize = 128);
64 for (
int i = 1; i <=
m_size; i++) {
77 const T&
top()
const override;
106 const T&
value(
int* handle)
const override;
173 void init(
int initialSize);
193template<
typename T,
typename C>
196 return value(&firstIndex);
199template<
typename T,
typename C>
205 if (m_size == m_arraySize) {
209 for (
int i = 1; i <= m_arraySize; i++) {
210 tempHeap[i] = m_heapArray[i];
213 delete[] m_heapArray;
214 m_heapArray = tempHeap;
215 m_arraySize = higherArraySize(m_arraySize);
224 int* result = entry.
handle;
226 OGDF_ASSERT(*result == *(m_heapArray[*result].handle));
230 OGDF_ASSERT(*result == *(m_heapArray[*result].handle));
235template<
typename T,
typename C>
238 delete m_heapArray[1].handle;
243 m_heapArray[1] = m_heapArray[m_size + 1];
247 if (m_size < m_arraySize / 3 && m_arraySize > 2 * m_initialSize - 1) {
250 for (
int i = 1; i <= m_size; i++) {
251 tempHeap[i] = m_heapArray[i];
254 delete[] m_heapArray;
255 m_heapArray = tempHeap;
256 m_arraySize = lowerArraySize(m_arraySize);
263template<
typename T,
typename C>
272template<
typename T,
typename C>
278 return m_heapArray[*handle].value;
281template<
typename T,
typename C>
286template<
typename T,
typename C>
288 m_arraySize = initialSize;
289 m_initialSize = initialSize;
294 m_heapArray =
new HeapEntry[arrayBound(m_arraySize)];
297template<
typename T,
typename C>
299 for (
int i = 1; i <= m_size; i++) {
300 delete m_heapArray[i].
handle;
303 delete[] m_heapArray;
307template<
typename T,
typename C>
313 *(m_heapArray[1].handle) = 1;
318 while ((parentIndex(run) >= 1)
319 && this->comparator()(tempEntry.
value, m_heapArray[parentIndex(run)].value)) {
320 m_heapArray[run] = m_heapArray[parentIndex(run)];
321 *(m_heapArray[run].handle) = run;
323 run = parentIndex(run);
326 m_heapArray[run] = tempEntry;
327 *(m_heapArray[run].handle) = run;
331template<
typename T,
typename C>
337 if (pos >= m_size / 2 + 1) {
338 *(m_heapArray[pos].handle) = pos;
341 T value = m_heapArray[pos].
value;
343 const C& compare = this->comparator();
346 if (hasLeft(pos) && compare(m_heapArray[leftChildIndex(pos)].value, value)
347 && compare(m_heapArray[leftChildIndex(pos)].value,
348 m_heapArray[rightChildIndex(pos)].value)) {
349 sIndex = leftChildIndex(pos);
351 }
else if (hasRight(pos) && compare(m_heapArray[rightChildIndex(pos)].value, value)) {
352 sIndex = rightChildIndex(pos);
358 m_heapArray[pos] = m_heapArray[sIndex];
359 m_heapArray[sIndex] = tempEntry;
362 *(m_heapArray[pos].handle) = pos;
363 *(m_heapArray[sIndex].handle) = sIndex;
367 *(m_heapArray[pos].handle) = pos;
Interface for heap implementations.
Basic declarations, included by all source files.
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.
The namespace for all OGDF objects.