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");
20 * \param init_size initial size of the heap
21 * \param free_func function to call on each element when you want to free
22 * the whole heap (or NULL if nothing to do).
26 xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t * const free_func)
28 xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
31 H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
37 * \param H poor victim
39 * kilkil a heap and its content
41 void xbt_heap_free(xbt_heap_t H)
45 for (i = 0; i < H->count; i++)
46 H->free(H->items[i].content);
53 * \param H the heap we're working on
54 * \return the number of elements in the heap
56 int xbt_heap_size(xbt_heap_t H)
62 * \param H the heap we're working on
63 * \param content the object you want to add to the heap
64 * \param key the key associated to this object
66 * Add an element int the heap. The element with the smallest key is
67 * automatically moved at the top of the heap.
69 void xbt_heap_push(xbt_heap_t H, void *content, double key)
71 int count = ++(H->count);
75 H->size = 2 * size + 1;
77 (void *) realloc(H->items,
78 (H->size) * sizeof(struct xbt_heapItem));
80 item = &(H->items[count - 1]);
82 item->content = content;
83 xbt_heap_increaseKey(H, count - 1);
88 * \param H the heap we're working on
89 * \return the element with the smallest key
91 * Extracts from the heap and returns the element with the smallest
92 * key. The element with the next smallest key is automatically moved
93 * at the top of the heap.
95 void *xbt_heap_pop(xbt_heap_t H)
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));
117 * \param H the heap we're working on
119 * \return the smallest key in the heap without modifying the heap.
121 double xbt_heap_maxkey(xbt_heap_t H)
123 xbt_assert0(H->count != 0,"Empty heap");
128 * \param H the heap we're working on
130 * \return the value associated to the smallest key in the heap
131 * without modifying the heap.
133 void *xbt_heap_maxcontent(xbt_heap_t H)
135 xbt_assert0(H->count != 0,"Empty heap");
136 return CONTENT(H, 0);
140 * \param H the heap we're working on
142 * Restores the heap property once an element has been deleted.
144 static void xbt_heap_maxHeapify(xbt_heap_t H)
151 int count = H->count;
152 if (l < count && KEY(H, l) < KEY(H, i))
154 if (r < count && KEY(H, r) < KEY(H, greatest))
157 struct xbt_heapItem tmp = H->items[i];
158 H->items[i] = H->items[greatest];
159 H->items[greatest] = tmp;
167 * \param H the heap we're working on
168 * \param i an item position in the heap
170 * Moves up an item at position i to its correct position. Works only
171 * when called from xbt_heap_push. Do not use otherwise.
173 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
175 while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
176 struct xbt_heapItem tmp = H->items[i];
177 H->items[i] = H->items[PARENT(i)];
178 H->items[PARENT(i)] = tmp;