Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
int (*gt)(void *a, void *b);
};
int (*gt)(void *a, void *b);
};
+#ifdef DEBUG_HEAP
+void check_heap(const struct ptr_heap *heap);
+#else
+static inline
+void check_heap(const struct ptr_heap *heap)
+{
+}
+#endif
+
/**
* heap_maximum - return the largest element in the heap
* @heap: the heap to be operated on
/**
* heap_maximum - return the largest element in the heap
* @heap: the heap to be operated on
*/
static inline void *heap_maximum(const struct ptr_heap *heap)
{
*/
static inline void *heap_maximum(const struct ptr_heap *heap)
{
return heap->len ? heap->ptrs[0] : NULL;
}
return heap->len ? heap->ptrs[0] : NULL;
}
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifndef max_t
#define max_t(type, a, b) \
((type) (a) > (type) (b) ? (type) (a) : (type) (b))
#endif
#ifndef max_t
#define max_t(type, a, b) \
((type) (a) > (type) (b) ? (type) (a) : (type) (b))
#endif
+#ifdef DEBUG_HEAP
+void check_heap(const struct ptr_heap *heap)
+{
+ size_t i;
+
+ if (!heap->len)
+ return;
+
+ for (i = 1; i < heap->len; i++)
+ assert(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
+}
+#endif
+
static
size_t parent(size_t i)
{
static
size_t parent(size_t i)
{
}
static
size_t left(size_t i)
{
}
static
size_t left(size_t i)
{
}
static
size_t right(size_t i)
{
}
static
size_t right(size_t i)
{
size_t l, r, largest;
for (;;) {
size_t l, r, largest;
for (;;) {
l = left(i);
r = right(i);
l = left(i);
r = right(i);
- if (l <= heap->len && ptrs[l] > ptrs[i])
+ if (l < heap->len && heap->gt(ptrs[l], ptrs[i]))
largest = l;
else
largest = i;
largest = l;
else
largest = i;
- if (r <= heap->len && ptrs[r] > ptrs[largest])
+ if (r < heap->len && heap->gt(ptrs[r], ptrs[largest]))
- if (largest != i) {
- void *tmp;
-
- tmp = ptrs[i];
- ptrs[i] = ptrs[largest];
- ptrs[largest] = tmp;
- i = largest;
- continue;
- } else {
+ tmp = ptrs[i];
+ ptrs[i] = ptrs[largest];
+ ptrs[largest] = tmp;
+ i = largest;
}
void *heap_replace_max(struct ptr_heap *heap, void *p)
}
void *heap_replace_max(struct ptr_heap *heap, void *p)
if (!heap->len) {
(void) heap_set_len(heap, 1);
heap->ptrs[0] = p;
if (!heap->len) {
(void) heap_set_len(heap, 1);
heap->ptrs[0] = p;
if (ret)
return ret;
ptrs = heap->ptrs;
if (ret)
return ret;
ptrs = heap->ptrs;
- /* Add the element to the end */
- ptrs[heap->len - 1] = p;
- /* Bubble it up to the appropriate position. */
- for (;;) {
- if (pos > 0 && heap->gt(ptrs[pos], ptrs[parent(pos)])) {
- void *tmp;
-
- /* Need to exchange */
- tmp = ptrs[pos];
- ptrs[pos] = ptrs[parent(pos)];
- ptrs[parent(pos)] = tmp;
- pos = parent(pos);
- /*
- * No need to rebalance: if we are larger than
- * our parent, we are necessarily larger than
- * its other child.
- */
- } else {
- break;
- }
+ while (pos > 0 && heap->gt(p, ptrs[parent(pos)])) {
+ /* Move parent down until we find the right spot */
+ ptrs[pos] = ptrs[parent(pos)];
+ pos = parent(pos);
+ ptrs[pos] = p;
+ check_heap(heap);
}
/* Shrink, replace the current max by previous last entry and heapify */
heap_set_len(heap, heap->len - 1);
}
/* Shrink, replace the current max by previous last entry and heapify */
heap_set_len(heap, heap->len - 1);
- return heap_replace_max(heap, heap->ptrs[heap->len - 1]);
+ /* len changed. previous last entry is at heap->len */
+ return heap_replace_max(heap, heap->ptrs[heap->len]);
}
void *heap_cherrypick(struct ptr_heap *heap, void *p)
}
void *heap_cherrypick(struct ptr_heap *heap, void *p)
found:
if (heap->len == 1) {
(void) heap_set_len(heap, 0);
found:
if (heap->len == 1) {
(void) heap_set_len(heap, 0);
return heap->ptrs[0];
}
/* Replace p with previous last entry and heapify. */
heap_set_len(heap, heap->len - 1);
return heap->ptrs[0];
}
/* Replace p with previous last entry and heapify. */
heap_set_len(heap, heap->len - 1);
- heap->ptrs[pos] = heap->ptrs[heap->len - 1];
+ /* len changed. previous last entry is at heap->len */
+ heap->ptrs[pos] = heap->ptrs[heap->len];
heapify(heap, pos);
return p;
}
heapify(heap, pos);
return p;
}