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,
29 void_f_pvoid_t const free_func)
31 xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
34 H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
40 * @brief Set the update callback function.
41 * @param H the heap we're working on
42 * \param update_callback function to call on each element to update its index when needed.
44 XBT_INLINE void xbt_heap_set_update_callback(xbt_heap_t H,
45 void (*update_callback) (void
49 H->update_callback = update_callback;
54 * @brief kilkil a heap and its content
55 * @param H poor victim
57 void xbt_heap_free(xbt_heap_t H)
61 for (i = 0; i < H->count; i++)
62 (*(H->free)) (H->items[i].content);
69 * @brief returns the number of elements in the heap
70 * @param H the heap we're working on
71 * @return the number of elements in the heap
73 XBT_INLINE int xbt_heap_size(xbt_heap_t H)
79 * @brief Add an element into the heap.
80 * \param H the heap we're working on
81 * \param content the object you want to add to the heap
82 * \param key the key associated to this object
84 * The element with the smallest key is automatically moved at the top of the heap.
86 void xbt_heap_push(xbt_heap_t H, void *content, double key)
88 int count = ++(H->count);
94 H->size = 2 * size + 1;
96 (void *) realloc(H->items,
97 (H->size) * sizeof(struct xbt_heapItem));
100 item = &(H->items[count - 1]);
102 item->content = content;
103 xbt_heap_increaseKey(H, count - 1);
108 * @brief Extracts from the heap and returns the element with the smallest key.
109 * \param H the heap we're working on
110 * \return the element with the smallest key
112 * Extracts from the heap and returns the element with the smallest
113 * key. The element with the next smallest key is automatically moved
114 * at the top of the heap.
116 void *xbt_heap_pop(xbt_heap_t H)
125 H->items[0] = H->items[(H->count) - 1];
127 xbt_heap_maxHeapify(H);
128 if (H->count < H->size / 4 && H->size > 16) {
129 H->size = H->size / 2 + 1;
131 (void *) realloc(H->items,
132 (H->size) * sizeof(struct xbt_heapItem));
135 if (H->update_callback)
136 H->update_callback(max, -1);
141 * @brief Extracts from the heap and returns the element at position i.
142 * \param H the heap we're working on
143 * \param i element position
144 * \return the element at position i if ok, NULL otherwise
146 * Extracts from the heap and returns the element at position i. The head is automatically reorded.
148 void *xbt_heap_remove(xbt_heap_t H, int i)
150 if ((i < 0) || (i > H->count - 1))
152 /* put element i at head */
154 KEY(H, i) = MIN_KEY_VALUE;
155 xbt_heap_increaseKey(H, i);
158 return xbt_heap_pop(H);
162 * @brief returns the smallest key in the heap (heap unchanged)
163 * \param H the heap we're working on
165 * \return the smallest key in the heap without modifying the heap.
167 XBT_INLINE double xbt_heap_maxkey(xbt_heap_t H)
169 xbt_assert0(H->count != 0, "Empty heap");
174 * @brief returns the value associated to the smallest key in the heap (heap unchanged)
175 * \param H the heap we're working on
177 * \return the value associated to the smallest key in the heap
178 * without modifying the heap.
180 void *xbt_heap_maxcontent(xbt_heap_t H)
182 xbt_assert0(H->count != 0, "Empty heap");
183 return CONTENT(H, 0);
187 * \param H the heap we're working on
189 * Restores the heap property once an element has been deleted.
191 static void xbt_heap_maxHeapify(xbt_heap_t H)
198 int count = H->count;
199 if (l < count && KEY(H, l) < KEY(H, i))
201 if (r < count && KEY(H, r) < KEY(H, greatest))
204 struct xbt_heapItem tmp = H->items[i];
205 H->items[i] = H->items[greatest];
206 H->items[greatest] = tmp;
207 if (H->update_callback)
208 H->update_callback(CONTENT(H, i), i);
211 if (H->update_callback)
212 H->update_callback(CONTENT(H, i), i);
219 * \param H the heap we're working on
220 * \param i an item position in the heap
222 * Moves up an item at position i to its correct position. Works only
223 * when called from xbt_heap_push. Do not use otherwise.
225 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
227 while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
228 struct xbt_heapItem tmp = H->items[i];
229 H->items[i] = H->items[PARENT(i)];
230 H->items[PARENT(i)] = tmp;
231 if (H->update_callback)
232 H->update_callback(CONTENT(H, i), i);
235 if (H->update_callback)
236 H->update_callback(CONTENT(H, i), i);