1 /* a generic and efficient heap */
3 /* Authors: Arnaud Legrand */
5 /* This program is free software; you can redistribute it and/or modify it
6 under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/sysdep.h"
9 #include "heap_private.h"
13 * @init_size: initial size of the heap
14 * @free_func: function to call on each element when you want to free the whole heap (or NULL if nothing to do).
18 xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t * const free_func)
20 xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
24 (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
33 * kilkil a heap and its content
35 void xbt_heap_free(xbt_heap_t H)
39 for (i = 0; i < H->size; i++)
40 H->free(H->items[i].content);
48 * @H: the heap we're working on
49 * @content: the object you want to add to the heap
50 * @key: the key associated to this object
52 * Add an element int the heap. The element with the smallest key is
53 * automatically moved at the top of the heap.
55 void xbt_heap_push(xbt_heap_t H, void *content, xbt_heap_float_t key)
57 int count = ++(H->count);
61 H->size = 2 * size + 1;
63 (void *) realloc(H->items,
64 (H->size) * sizeof(struct xbt_heapItem));
66 item = &(H->items[count - 1]);
68 item->content = content;
69 xbt_heap_increaseKey(H, count - 1);
75 * @H: the heap we're working on
77 * Extracts from the heap and returns the element with the smallest
78 * key. The element with the next smallest key is automatically moved
79 * at the top of the heap.
81 void *xbt_heap_pop(xbt_heap_t H)
83 void *max = CONTENT(H, 0);
84 H->items[0] = H->items[(H->count) - 1];
86 xbt_heap_maxHeapify(H);
87 if (H->count < H->size / 4 && H->size > 16) {
88 H->size = H->size / 2 + 1;
90 (void *) realloc(H->items,
91 (H->size) * sizeof(struct xbt_heapItem));
98 * @H: the heap we're working on
100 * Returns the smallest key in the heap without modifying the heap.
102 xbt_heap_float_t xbt_heap_maxkey(xbt_heap_t H)
108 * xbt_heap_maxcontent:
109 * @H: the heap we're working on
111 * Returns the value associated to the smallest key in the heap
112 * without modifying the heap.
114 void *xbt_heap_maxcontent(xbt_heap_t H)
116 return CONTENT(H, 0);
120 * xbt_heap_maxcontent:
121 * @H: the heap we're working on
123 * Restores the heap property once an element has been deleted.
125 static void xbt_heap_maxHeapify(xbt_heap_t H)
132 int count = H->count;
133 if (l < count && KEY(H, l) < KEY(H, i))
135 if (r < count && KEY(H, r) < KEY(H, greatest))
138 struct xbt_heapItem tmp = H->items[i];
139 H->items[i] = H->items[greatest];
140 H->items[greatest] = tmp;
148 * xbt_heap_maxcontent:
149 * @H: the heap we're working on
150 * @i: an item position in the heap
152 * Moves up an item at position i to its correct position. Works only
153 * when called from xbt_heap_push. Do not use otherwise.
155 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
157 while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
158 struct xbt_heapItem tmp = H->items[i];
159 H->items[i] = H->items[PARENT(i)];
160 H->items[PARENT(i)] = tmp;