49 template<
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);
193 template<
typename T,
typename C>
196 return value(&firstIndex);
199 template<
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));
235 template<
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);
263 template<
typename T,
typename C>
272 template<
typename T,
typename C>
278 return m_heapArray[*handle].value;
281 template<
typename T,
typename C>
286 template<
typename T,
typename C>
288 m_arraySize = initialSize;
289 m_initialSize = initialSize;
294 m_heapArray =
new HeapEntry[arrayBound(m_arraySize)];
297 template<
typename T,
typename C>
299 for (
int i = 1; i <= m_size; i++) {
300 delete m_heapArray[i].handle;
303 delete[] m_heapArray;
307 template<
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;
331 template<
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;