Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
db2431e707ec2427b839425fc1b9d18ca0a4b926
[simgrid.git] / src / xbt / heap.c
1 /* a generic and efficient heap                                             */
2
3 /* Copyright (c) 2004-2005, 2007-2017. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "heap_private.h"
12
13 #include <stdio.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_heap, xbt, "Heap");
15
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);
18
19 /** @addtogroup XBT_heap
20  *  \brief This section describes the API to generic heap with O(log(n)) access.
21  */
22
23 /**
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 the whole heap (or NULL if nothing to do).
27  *
28  * Creates a new heap.
29  */
30 inline xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t const free_func)
31 {
32   xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
33   H->size = init_size;
34   H->count = 0;
35   H->items = (xbt_heap_item_t) xbt_new0(struct xbt_heap_item, init_size);
36   H->free = free_func;
37   return H;
38 }
39
40 /**
41  * @brief Set the update callback function.
42  * @param H the heap we're working on
43  * \param update_callback function to call on each element to update its index when needed.
44  */
45 inline void xbt_heap_set_update_callback(xbt_heap_t H, void (*update_callback) (void*, int))
46 {
47   H->update_callback = update_callback;
48 }
49
50 /**
51  * @brief kilkil a heap and its content
52  * @param H poor victim
53  */
54 void xbt_heap_free(xbt_heap_t H)
55 {
56   if (!H)
57     return;
58
59   if (H->free)
60     for (int i = 0; i < H->count; i++)
61       H->free(H->items[i].content);
62   free(H->items);
63   free(H);
64 }
65
66 /**
67  * @brief returns the number of elements in the heap
68  * @param H the heap we're working on
69  * @return the number of elements in the heap
70  */
71 inline int xbt_heap_size(xbt_heap_t H)
72 {
73   return (H->count);
74 }
75
76 /**
77  * @brief Add an element into the heap.
78  * \param H the heap we're working on
79  * \param content the object you want to add to the heap
80  * \param key the key associated to this object
81  *
82  * The element with the smallest key is automatically moved at the top of the heap.
83  */
84 void xbt_heap_push(xbt_heap_t H, void *content, double key)
85 {
86   H->count += 1;
87   int count = H->count;
88
89   int size = H->size;
90   xbt_heap_item_t item;
91
92   if (count > size) {
93     H->size = (size << 1) + 1;
94     H->items = (void *) xbt_realloc(H->items, (H->size) * sizeof(struct xbt_heap_item));
95     xbt_assert(H->items != NULL);
96   }
97
98   item = &(H->items[count - 1]);
99   item->key = key;
100   item->content = content;
101   xbt_heap_increase_key(H, count - 1);
102   XBT_DEBUG("Heap has now %d elements and max elem is %g",xbt_heap_size(H),xbt_heap_maxkey(H));
103 }
104
105 /**
106  * @brief Extracts from the heap and returns the element with the smallest key.
107  * \param H the heap we're working on
108  * \return the element with the smallest key
109  *
110  * Extracts from the heap and returns the element with the smallest key. The element with the next smallest key is
111  * automatically moved at the top of the heap.
112  */
113 void *xbt_heap_pop(xbt_heap_t H)
114 {
115   xbt_heap_item_t items = H->items;
116   int size = H->size;
117   void *max;
118
119   if (H->count == 0)
120     return NULL;
121
122   XBT_DEBUG("Heap has %d elements before extraction and max elem was %g",xbt_heap_size(H),xbt_heap_maxkey(H));
123
124   max = CONTENT(H, 0);
125
126   items[0] = items[(H->count) - 1];
127   (H->count)--;
128   xbt_heap_max_heapify(H,0);
129   if (H->count < size >> 2 && size > 16) {
130     size = (size >> 1) + 1;
131     H->items = (void *) xbt_realloc(items, size * sizeof(struct xbt_heap_item));
132     H->size = size;
133   }
134
135   if (H->update_callback)
136     H->update_callback(max, -1);
137   return max;
138 }
139
140 /**
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
145  *
146  * Extracts from the heap and returns the element at position i. The heap is automatically reordered.
147  */
148 void *xbt_heap_remove(xbt_heap_t H, int i)
149 {
150   XBT_DEBUG("Heap has %d elements: extracting element %d",xbt_heap_size(H),i);
151
152   if ((i < 0) || (i > H->count - 1))
153     return NULL;
154   /* put element i at head */
155   if (i > 0) {
156     KEY(H, i) = MIN_KEY_VALUE;
157     xbt_heap_increase_key(H, i);
158   }
159
160   return xbt_heap_pop(H);
161 }
162
163 /** @brief Remove an arbitrary element from the heap
164  *  \param H the heap we're working on
165  *  \param content the object you want to remove from the heap
166  *  \param key the key associated to this object
167  *  \return the removed element if found, NULL otherwise
168  */
169 void *xbt_heap_rm_elm(xbt_heap_t H, void *content, double key)
170 {
171   int i=0;
172   while (i < H->count && (KEY(H, i) != key || CONTENT(H, i) != content))
173     i++;
174   return xbt_heap_remove(H, i);
175 }
176
177 /**
178  * @brief Updates an element of the heap with a new value.
179  * \param H the heap we're working on
180  * \param i  element position
181  * \param key new value for the element
182  *
183  * Updates an element of the heap with a new value.
184  */
185 void xbt_heap_update(xbt_heap_t H, int i, double key)
186 {
187   XBT_DEBUG("Heap has %d elements: updating element %d : was %1.12f to %1.12f ",xbt_heap_size(H),i,KEY(H, i), key);
188
189   if ((i < 0) || (i > H->count - 1) || key == KEY(H, i))
190     return ;
191
192   if(key< KEY(H, i)){
193     KEY(H, i)=key;
194     xbt_heap_increase_key(H, i);
195   }else{
196     KEY(H, i)=key;
197     xbt_heap_max_heapify(H,i);
198   }
199 }
200
201 /**
202  * @brief returns the smallest key in the heap (heap unchanged)
203  * \param H the heap we're working on
204  *
205  * \return the smallest key in the heap without modifying the heap.
206  */
207 inline double xbt_heap_maxkey(xbt_heap_t H)
208 {
209   xbt_assert(H->count != 0, "Empty heap");
210   return KEY(H, 0);
211 }
212
213 /**
214  * @brief returns the value associated to the smallest key in the heap (heap unchanged)
215  * \param H the heap we're working on
216  *
217  * \return the value associated to the smallest key in the heap
218  * without modifying the heap.
219  */
220 void *xbt_heap_maxcontent(xbt_heap_t H)
221 {
222   xbt_assert(H->count != 0, "Empty heap");
223   return CONTENT(H, 0);
224 }
225
226 /* <<<< private >>>>
227  * \param H the heap we're working on
228  *
229  * Restores the heap property once an element has been deleted.
230  */
231 static void xbt_heap_max_heapify(xbt_heap_t H, int index)
232 {
233   int i = index;
234   int count = H->count;
235   xbt_heap_item_t items = H->items;
236
237   while (1) {
238     int greatest = i;
239     int l = LEFT(i);
240     int r = l + 1;
241     if (l < count && items[l].key < items[i].key)
242       greatest = l;
243     if (r < count && items[r].key < items[greatest].key)
244       greatest = r;
245     if (greatest != i) {
246       struct xbt_heap_item tmp = items[i];
247       items[i] = items[greatest];
248       items[greatest] = tmp;
249       if (H->update_callback)
250         H->update_callback(items[i].content, i);
251       i = greatest;
252     } else {
253       if (H->update_callback)
254         H->update_callback(items[i].content, i);
255       return;
256     }
257   }
258 }
259
260 /* <<<< private >>>>
261  * \param H the heap we're working on
262  * \param i an item position in the heap
263  *
264  * Moves up an item at position i to its correct position. Works only when called from xbt_heap_push.
265  * Do not use otherwise.
266  */
267 static void xbt_heap_increase_key(xbt_heap_t H, int i)
268 {
269   xbt_heap_item_t items = H->items;
270   int p = PARENT(i);
271   while (i > 0 && items[p].key > items[i].key) {
272     struct xbt_heap_item tmp = items[i];
273     items[i] = items[p];
274     items[p] = tmp;
275     if (H->update_callback)
276       H->update_callback(items[i].content, i);
277     i = p;
278     p = PARENT(i);
279   }
280   if (H->update_callback)
281     H->update_callback(items[i].content, i);
282 }