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