1 /* a generic and efficient heap */
3 /* Copyright (c) 2004-2005, 2007-2014. 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"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_heap, xbt, "Heap");
16 static void xbt_heap_max_heapify(xbt_heap_t H, int i);
17 static void xbt_heap_increase_key(xbt_heap_t H, int i);
19 /** @addtogroup XBT_heap
20 * \brief This section describes the API to generic heap with O(log(n)) access.
24 * @brief Creates a new heap.
25 * \param init_size initial size of the heap
26 * \param free_func function to call on each element when you want to free
27 * the whole heap (or NULL if nothing to do).
31 XBT_INLINE xbt_heap_t xbt_heap_new(int init_size,
32 void_f_pvoid_t const free_func)
34 xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
37 H->items = (xbt_heap_item_t) xbt_new0(struct xbt_heap_item, init_size);
43 * @brief Set the update callback function.
44 * @param H the heap we're working on
45 * \param update_callback function to call on each element to update its index when needed.
47 XBT_INLINE void xbt_heap_set_update_callback(xbt_heap_t H,
48 void (*update_callback) (void
52 H->update_callback = update_callback;
57 * @brief kilkil a heap and its content
58 * @param H poor victim
60 void xbt_heap_free(xbt_heap_t H)
64 for (i = 0; i < H->count; i++)
65 H->free(H->items[i].content);
72 * @brief returns the number of elements in the heap
73 * @param H the heap we're working on
74 * @return the number of elements in the heap
76 XBT_INLINE int xbt_heap_size(xbt_heap_t H)
82 * @brief Add an element into the heap.
83 * \param H the heap we're working on
84 * \param content the object you want to add to the heap
85 * \param key the key associated to this object
87 * The element with the smallest key is automatically moved at the top of the heap.
89 void xbt_heap_push(xbt_heap_t H, void *content, double key)
91 int count = ++(H->count);
97 H->size = (size << 1) + 1;
99 (void *) xbt_realloc(H->items,
100 (H->size) * sizeof(struct xbt_heap_item));
103 item = &(H->items[count - 1]);
105 item->content = content;
106 xbt_heap_increase_key(H, count - 1);
107 XBT_DEBUG("Heap has now %d elements and max elem is %g",xbt_heap_size(H),xbt_heap_maxkey(H));
113 * @brief Extracts from the heap and returns the element with the smallest key.
114 * \param H the heap we're working on
115 * \return the element with the smallest key
117 * Extracts from the heap and returns the element with the smallest
118 * key. The element with the next smallest key is automatically moved
119 * at the top of the heap.
121 void *xbt_heap_pop(xbt_heap_t H)
123 xbt_heap_item_t items = H->items;
130 XBT_DEBUG("Heap has %d elements before extraction and max elem was %g",xbt_heap_size(H),xbt_heap_maxkey(H));
134 items[0] = items[(H->count) - 1];
136 xbt_heap_max_heapify(H,0);
137 if (H->count < size >> 2 && size > 16) {
138 size = (size >> 1) + 1;
140 (void *) xbt_realloc(items,
141 size * sizeof(struct xbt_heap_item));
145 if (H->update_callback)
146 H->update_callback(max, -1);
151 * @brief Extracts from the heap and returns the element at position i.
152 * \param H the heap we're working on
153 * \param i element position
154 * \return the element at position i if ok, NULL otherwise
156 * Extracts from the heap and returns the element at position i. The heap is automatically reorded.
158 void *xbt_heap_remove(xbt_heap_t H, int i)
160 XBT_DEBUG("Heap has %d elements: extracting element %d",xbt_heap_size(H),i);
162 if ((i < 0) || (i > H->count - 1))
164 /* put element i at head */
166 KEY(H, i) = MIN_KEY_VALUE;
167 xbt_heap_increase_key(H, i);
170 return xbt_heap_pop(H);
174 * @brief Updates an element of the heap with a new value.
175 * \param H the heap we're working on
176 * \param i element position
177 * \param key new value for the element
179 * Updates an element of the heap with a new value.
181 void xbt_heap_update(xbt_heap_t H, int i, double key)
183 XBT_DEBUG("Heap has %d elements: updating element %d : was %1.12f to %1.12f ",xbt_heap_size(H),i,KEY(H, i), key);
185 if ((i < 0) || (i > H->count - 1) || key == KEY(H, i))
190 xbt_heap_increase_key(H, i);
193 xbt_heap_max_heapify(H,i);
198 * @brief returns the smallest key in the heap (heap unchanged)
199 * \param H the heap we're working on
201 * \return the smallest key in the heap without modifying the heap.
203 XBT_INLINE double xbt_heap_maxkey(xbt_heap_t H)
205 xbt_assert(H->count != 0, "Empty heap");
210 * @brief returns the value associated to the smallest key in the heap (heap unchanged)
211 * \param H the heap we're working on
213 * \return the value associated to the smallest key in the heap
214 * without modifying the heap.
216 void *xbt_heap_maxcontent(xbt_heap_t H)
218 xbt_assert(H->count != 0, "Empty heap");
219 return CONTENT(H, 0);
223 * \param H the heap we're working on
225 * Restores the heap property once an element has been deleted.
227 static void xbt_heap_max_heapify(xbt_heap_t H, int index)
230 int count = H->count;
231 xbt_heap_item_t items = H->items;
237 if (l < count && items[l].key < items[i].key)
239 if (r < count && items[r].key < items[greatest].key)
242 struct xbt_heap_item tmp = items[i];
243 items[i] = items[greatest];
244 items[greatest] = tmp;
245 if (H->update_callback)
246 H->update_callback(items[i].content, i);
249 if (H->update_callback)
250 H->update_callback(items[i].content, i);
257 * \param H the heap we're working on
258 * \param i an item position in the heap
260 * Moves up an item at position i to its correct position. Works only
261 * when called from xbt_heap_push. Do not use otherwise.
263 static void xbt_heap_increase_key(xbt_heap_t H, int i)
265 xbt_heap_item_t items = H->items;
267 while (i > 0 && items[p].key > items[i].key) {
268 struct xbt_heap_item tmp = items[i];
271 if (H->update_callback)
272 H->update_callback(items[i].content, i);
276 if (H->update_callback)
277 H->update_callback(items[i].content, i);