Logo AND Algorithmique Numérique Distribuée

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