1 /* a generic and efficient heap */
3 /* Copyright (c) 2004, 2005, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
11 #include "heap_private.h"
16 /** @addtogroup XBT_heap
17 * \brief This section describes the API to generic heap with O(log(n)) access.
21 * @brief Creates a new heap.
22 * \param init_size initial size of the heap
23 * \param free_func function to call on each element when you want to free
24 * the whole heap (or NULL if nothing to do).
28 XBT_INLINE xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t const free_func)
30 xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
33 H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
39 * @brief Set the update callback function.
40 * @param H the heap we're working on
41 * \param update_callback function to call on each element to update its index when needed.
43 XBT_INLINE void xbt_heap_set_update_callback(xbt_heap_t H,
44 void (*update_callback) (void *, int))
46 H->update_callback = update_callback;
51 * @brief kilkil a heap and its content
52 * @param H poor victim
54 void xbt_heap_free(xbt_heap_t H)
58 for (i = 0; i < H->count; i++)
59 (*(H->free)) (H->items[i].content);
66 * @brief returns the number of elements in the heap
67 * @param H the heap we're working on
68 * @return the number of elements in the heap
70 XBT_INLINE int xbt_heap_size(xbt_heap_t H)
76 * @brief Add an element into the heap.
77 * \param H the heap we're working on
78 * \param content the object you want to add to the heap
79 * \param key the key associated to this object
81 * The element with the smallest key is automatically moved at the top of the heap.
83 void xbt_heap_push(xbt_heap_t H, void *content, double key)
85 int count = ++(H->count);
91 H->size = 2 * size + 1;
93 (void *) realloc(H->items, (H->size) * sizeof(struct xbt_heapItem));
96 item = &(H->items[count - 1]);
98 item->content = content;
99 xbt_heap_increaseKey(H, count - 1);
104 * @brief Extracts from the heap and returns the element with the smallest key.
105 * \param H the heap we're working on
106 * \return the element with the smallest key
108 * Extracts from the heap and returns the element with the smallest
109 * key. The element with the next smallest key is automatically moved
110 * at the top of the heap.
112 void *xbt_heap_pop(xbt_heap_t H)
121 H->items[0] = H->items[(H->count) - 1];
123 xbt_heap_maxHeapify(H);
124 if (H->count < H->size / 4 && H->size > 16) {
125 H->size = H->size / 2 + 1;
127 (void *) realloc(H->items, (H->size) * sizeof(struct xbt_heapItem));
130 if(H->update_callback) H->update_callback(max, -1);
135 * @brief Extracts from the heap and returns the element at position i.
136 * \param H the heap we're working on
137 * \param i element position
138 * \return the element at position i if ok, NULL otherwise
140 * Extracts from the heap and returns the element at position i. The head is automatically reorded.
142 void *xbt_heap_remove(xbt_heap_t H, int i)
144 if ((i < 0) || (i > H->count - 1))
146 /* put element i at head */
148 KEY(H, i) = MIN_KEY_VALUE;
149 xbt_heap_increaseKey(H, i);
152 return xbt_heap_pop(H);
156 * @brief returns the smallest key in the heap (heap unchanged)
157 * \param H the heap we're working on
159 * \return the smallest key in the heap without modifying the heap.
161 XBT_INLINE double xbt_heap_maxkey(xbt_heap_t H)
163 xbt_assert0(H->count != 0, "Empty heap");
168 * @brief returns the value associated to the smallest key in the heap (heap unchanged)
169 * \param H the heap we're working on
171 * \return the value associated to the smallest key in the heap
172 * without modifying the heap.
174 void *xbt_heap_maxcontent(xbt_heap_t H)
176 xbt_assert0(H->count != 0, "Empty heap");
177 return CONTENT(H, 0);
181 * \param H the heap we're working on
183 * Restores the heap property once an element has been deleted.
185 static void xbt_heap_maxHeapify(xbt_heap_t H)
192 int count = H->count;
193 if (l < count && KEY(H, l) < KEY(H, i))
195 if (r < count && KEY(H, r) < KEY(H, greatest))
198 struct xbt_heapItem tmp = H->items[i];
199 H->items[i] = H->items[greatest];
200 H->items[greatest] = tmp;
201 if(H->update_callback) H->update_callback(CONTENT(H, i), i);
204 if(H->update_callback) H->update_callback(CONTENT(H, i), i);
211 * \param H the heap we're working on
212 * \param i an item position in the heap
214 * Moves up an item at position i to its correct position. Works only
215 * when called from xbt_heap_push. Do not use otherwise.
217 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
219 while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
220 struct xbt_heapItem tmp = H->items[i];
221 H->items[i] = H->items[PARENT(i)];
222 H->items[PARENT(i)] = tmp;
223 if(H->update_callback) H->update_callback(CONTENT(H, i), i);
226 if(H->update_callback) H->update_callback(CONTENT(H, i), i);