Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Incorrect use of conditionnal expressions
[simgrid.git] / src / xbt / heap.c
1 /* a generic and efficient heap                                             */
2
3 /* Copyright (c) 2004, 2005, 2007, 2008, 2009, 2010. 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
15
16 /** @addtogroup XBT_heap
17  *  \brief This section describes the API to generic heap with O(log(n)) access.
18  */
19
20 /**
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).
25  *
26  * Creates a new heap.
27  */
28 XBT_INLINE xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t const free_func)
29 {
30   xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
31   H->size = init_size;
32   H->count = 0;
33   H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
34   H->free = free_func;
35   return H;
36 }
37
38 /**
39  * @brief Set the update callback function.
40  * @param H the heap we're working on
41  * \param update_callback function to call on each element to update its index when needed.
42  */
43 XBT_INLINE void xbt_heap_set_update_callback(xbt_heap_t H,
44                                   void (*update_callback) (void *, int))
45 {
46   H->update_callback = update_callback;
47 }
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   int i;
57   if (H->free)
58     for (i = 0; i < H->count; i++)
59       (*(H->free)) (H->items[i].content);
60   free(H->items);
61   free(H);
62   return;
63 }
64
65 /**
66  * @brief returns the number of elements in the heap
67  * @param H the heap we're working on
68  * @return the number of elements in the heap
69  */
70 XBT_INLINE int xbt_heap_size(xbt_heap_t H)
71 {
72   return (H->count);
73 }
74
75 /**
76  * @brief Add an element into the heap.
77  * \param H the heap we're working on
78  * \param content the object you want to add to the heap
79  * \param key the key associated to this object
80  *
81  * The element with the smallest key is automatically moved at the top of the heap.
82  */
83 void xbt_heap_push(xbt_heap_t H, void *content, double key)
84 {
85   int count = ++(H->count);
86
87   int size = H->size;
88   xbt_heapItem_t item;
89
90   if (count > size) {
91     H->size = 2 * size + 1;
92     H->items =
93       (void *) realloc(H->items, (H->size) * sizeof(struct xbt_heapItem));
94   }
95
96   item = &(H->items[count - 1]);
97   item->key = key;
98   item->content = content;
99   xbt_heap_increaseKey(H, count - 1);
100   return;
101 }
102
103 /**
104  * @brief Extracts from the heap and returns the element with the smallest key.
105  * \param H the heap we're working on
106  * \return the element with the smallest key
107  *
108  * Extracts from the heap and returns the element with the smallest
109  * key. The element with the next smallest key is automatically moved
110  * at the top of the heap.
111  */
112 void *xbt_heap_pop(xbt_heap_t H)
113 {
114   void *max;
115
116   if (H->count == 0)
117     return NULL;
118
119   max = CONTENT(H, 0);
120
121   H->items[0] = H->items[(H->count) - 1];
122   (H->count)--;
123   xbt_heap_maxHeapify(H);
124   if (H->count < H->size / 4 && H->size > 16) {
125     H->size = H->size / 2 + 1;
126     H->items =
127       (void *) realloc(H->items, (H->size) * sizeof(struct xbt_heapItem));
128   }
129
130   if(H->update_callback) H->update_callback(max, -1);
131   return max;
132 }
133
134 /**
135  * @brief Extracts from the heap and returns the element at position i.
136  * \param H the heap we're working on
137  * \param i     element position
138  * \return the element at position i if ok, NULL otherwise
139  *
140  * Extracts from the heap and returns the element at position i. The head is automatically reorded.
141  */
142 void *xbt_heap_remove(xbt_heap_t H, int i)
143 {
144   if ((i < 0) || (i > H->count - 1))
145     return NULL;
146   /* put element i at head */
147   if (i > 0) {
148     KEY(H, i) = MIN_KEY_VALUE;
149     xbt_heap_increaseKey(H, i);
150   }
151
152   return xbt_heap_pop(H);
153 }
154
155 /**
156  * @brief returns the smallest key in the heap (heap unchanged)
157  * \param H the heap we're working on
158  *
159  * \return the smallest key in the heap without modifying the heap.
160  */
161 XBT_INLINE double xbt_heap_maxkey(xbt_heap_t H)
162 {
163   xbt_assert0(H->count != 0, "Empty heap");
164   return KEY(H, 0);
165 }
166
167 /**
168  * @brief returns the value associated to the smallest key in the heap (heap unchanged)
169  * \param H the heap we're working on
170  *
171  * \return the value associated to the smallest key in the heap
172  * without modifying the heap.
173  */
174 void *xbt_heap_maxcontent(xbt_heap_t H)
175 {
176   xbt_assert0(H->count != 0, "Empty heap");
177   return CONTENT(H, 0);
178 }
179
180 /* <<<< private >>>>
181  * \param H the heap we're working on
182  *
183  * Restores the heap property once an element has been deleted.
184  */
185 static void xbt_heap_maxHeapify(xbt_heap_t H)
186 {
187   int i = 0;
188   while (1) {
189     int greatest = i;
190     int l = LEFT(i);
191     int r = RIGHT(i);
192     int count = H->count;
193     if (l < count && KEY(H, l) < KEY(H, i))
194       greatest = l;
195     if (r < count && KEY(H, r) < KEY(H, greatest))
196       greatest = r;
197     if (greatest != i) {
198       struct xbt_heapItem tmp = H->items[i];
199       H->items[i] = H->items[greatest];
200       H->items[greatest] = tmp;
201       if(H->update_callback) H->update_callback(CONTENT(H, i), i);
202       i = greatest;
203     } else {
204       if(H->update_callback) H->update_callback(CONTENT(H, i), i);
205       return;
206     }
207   }
208 }
209
210 /* <<<< private >>>>
211  * \param H the heap we're working on
212  * \param i an item position in the heap
213  *
214  * Moves up an item at position i to its correct position. Works only
215  * when called from xbt_heap_push. Do not use otherwise.
216  */
217 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
218 {
219   while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
220     struct xbt_heapItem tmp = H->items[i];
221     H->items[i] = H->items[PARENT(i)];
222     H->items[PARENT(i)] = tmp;
223     if(H->update_callback) H->update_callback(CONTENT(H, i), i);
224     i = PARENT(i);
225   }
226   if(H->update_callback) H->update_callback(CONTENT(H, i), i);
227   return;
228 }
229