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_heap_private.h"
12 * @init_size: initial size of the heap
13 * @free_func: function to call on each element when you want to free the whole heap (or NULL if nothing to do).
17 xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t * const free_func)
19 xbt_heap_t H = calloc(1, sizeof(struct xbt_heap));
22 H->items = (xbt_heapItem_t) calloc(init_size, sizeof(struct xbt_heapItem));
31 * kilkil a heap and its content
33 void xbt_heap_free(xbt_heap_t H)
37 for(i=0;i < H->size;i++)
38 H->free(H->items[i].content);
46 * @H: the heap we're working on
47 * @content: the object you want to add to the heap
48 * @key: the key associated to this object
50 * Add an element int the heap. The element with the smallest key is
51 * automatically moved at the top of the heap.
53 void xbt_heap_push(xbt_heap_t H, void *content, xbt_heap_float_t key)
55 int count = ++(H->count);
59 H->size = 2 * size + 1;
61 (void *) realloc(H->items,
62 (H->size) * sizeof(struct xbt_heapItem));
64 item = &(H->items[count - 1]);
66 item->content = content;
67 xbt_heap_increaseKey(H, count - 1);
73 * @H: the heap we're working on
75 * Extracts from the heap and returns the element with the smallest
76 * key. The element with the next smallest key is automatically moved
77 * at the top of the heap.
79 void *xbt_heap_pop(xbt_heap_t H)
81 void *max = CONTENT(H, 0);
82 H->items[0] = H->items[(H->count) - 1];
84 xbt_heap_maxHeapify(H);
85 if (H->count < H->size / 4 && H->size > 16) {
86 H->size = H->size / 2 + 1;
88 (void *) realloc(H->items,
89 (H->size) * sizeof(struct xbt_heapItem));
96 * @H: the heap we're working on
98 * Returns the smallest key in the heap without modifying the heap.
100 xbt_heap_float_t xbt_heap_maxkey(xbt_heap_t H)
106 * xbt_heap_maxcontent:
107 * @H: the heap we're working on
109 * Returns the value associated to the smallest key in the heap
110 * without modifying the heap.
112 void *xbt_heap_maxcontent(xbt_heap_t H)
114 return CONTENT(H, 0);
118 * xbt_heap_maxcontent:
119 * @H: the heap we're working on
121 * Restores the heap property once an element has been deleted.
123 void xbt_heap_maxHeapify(xbt_heap_t H)
130 int count = H->count;
131 if (l < count && KEY(H, l) < KEY(H, i))
133 if (r < count && KEY(H, r) < KEY(H, greatest))
136 struct xbt_heapItem tmp = H->items[i];
137 H->items[i] = H->items[greatest];
138 H->items[greatest] = tmp;
145 * xbt_heap_maxcontent:
146 * @H: the heap we're working on
147 * @i: an item position in the heap
149 * Moves up an item at position i to its correct position. Works only
150 * when called from xbt_heap_push. Do not use otherwise.
152 void xbt_heap_increaseKey(xbt_heap_t H, int i)
154 while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
155 struct xbt_heapItem tmp = H->items[i];
156 H->items[i] = H->items[PARENT(i)];
157 H->items[PARENT(i)] = tmp;