3 /* a generic and efficient heap */
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
11 #include "xbt/error.h"
12 #include "heap_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(heap,xbt,"Heap");
17 * @init_size: initial size of the heap
18 * @free_func: function to call on each element when you want to free
19 * the whole heap (or NULL if nothing to do).
23 xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t * const free_func)
25 xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
28 H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
37 * kilkil a heap and its content
39 void xbt_heap_free(xbt_heap_t H)
43 for (i = 0; i < H->count; i++)
44 H->free(H->items[i].content);
52 * @H: the heap we're working on
54 * returns the number of elements in the heap
56 int xbt_heap_size(xbt_heap_t H)
63 * @H: the heap we're working on
64 * @content: the object you want to add to the heap
65 * @key: the key associated to this object
67 * Add an element int the heap. The element with the smallest key is
68 * automatically moved at the top of the heap.
70 void xbt_heap_push(xbt_heap_t H, void *content, xbt_heap_float_t key)
72 int count = ++(H->count);
76 H->size = 2 * size + 1;
78 (void *) realloc(H->items,
79 (H->size) * sizeof(struct xbt_heapItem));
81 item = &(H->items[count - 1]);
83 item->content = content;
84 xbt_heap_increaseKey(H, count - 1);
90 * @H: the heap we're working on
92 * Extracts from the heap and returns the element with the smallest
93 * key. The element with the next smallest key is automatically moved
94 * at the top of the heap.
96 void *xbt_heap_pop(xbt_heap_t H)
100 if(H->count==0) return NULL;
104 H->items[0] = H->items[(H->count) - 1];
106 xbt_heap_maxHeapify(H);
107 if (H->count < H->size / 4 && H->size > 16) {
108 H->size = H->size / 2 + 1;
110 (void *) realloc(H->items,
111 (H->size) * sizeof(struct xbt_heapItem));
118 * @H: the heap we're working on
120 * Returns the smallest key in the heap without modifying the heap.
122 xbt_heap_float_t xbt_heap_maxkey(xbt_heap_t H)
124 if(H->count==0) abort();
129 * xbt_heap_maxcontent:
130 * @H: the heap we're working on
132 * Returns the value associated to the smallest key in the heap
133 * without modifying the heap.
135 void *xbt_heap_maxcontent(xbt_heap_t H)
137 if(H->count==0) abort();
138 return CONTENT(H, 0);
142 * xbt_heap_maxcontent:
143 * @H: the heap we're working on
145 * Restores the heap property once an element has been deleted.
147 static void xbt_heap_maxHeapify(xbt_heap_t H)
154 int count = H->count;
155 if (l < count && KEY(H, l) < KEY(H, i))
157 if (r < count && KEY(H, r) < KEY(H, greatest))
160 struct xbt_heapItem tmp = H->items[i];
161 H->items[i] = H->items[greatest];
162 H->items[greatest] = tmp;
170 * xbt_heap_maxcontent:
171 * @H: the heap we're working on
172 * @i: an item position in the heap
174 * Moves up an item at position i to its correct position. Works only
175 * when called from xbt_heap_push. Do not use otherwise.
177 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
179 while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
180 struct xbt_heapItem tmp = H->items[i];
181 H->items[i] = H->items[PARENT(i)];
182 H->items[PARENT(i)] = tmp;