Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removes the gras_config.h inclusion, adds the portable.h inclusion for win32 portability.
[simgrid.git] / src / xbt / heap.c
1 /*      $Id$     */
2
3 /* a generic and efficient heap                                             */
4
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "heap_private.h"
13
14
15 /** @addtogroup XBT_heap 
16  *  \brief This section describes the API to generic heap with O(log(n)) access.
17  */
18
19 /**
20  * @brief Creates a new heap.
21  * \param init_size initial size of the heap
22  * \param free_func function to call on each element when you want to free
23  *             the whole heap (or NULL if nothing to do).
24  *
25  * Creates a new heap.
26  */
27 xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t * const free_func)
28 {
29   xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
30   H->size = init_size;
31   H->count = 0;
32   H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
33   H->free = free_func;
34   return H;
35 }
36
37 /**
38  * @brief kilkil a heap and its content
39  * @param H poor victim
40  */
41 void xbt_heap_free(xbt_heap_t H)
42 {
43   int i;
44   if (H->free)
45     for (i = 0; i < H->count; i++)
46       H->free(H->items[i].content);
47   free(H->items);
48   free(H);
49   return;
50 }
51
52 /**
53  * @brief returns the number of elements in the heap
54  * @param H the heap we're working on
55  * @return the number of elements in the heap
56  */
57 int xbt_heap_size(xbt_heap_t H)
58 {
59   return (H->count);
60 }
61
62 /**
63  * @brief Add an element into the heap.
64  * \param H the heap we're working on
65  * \param content the object you want to add to the heap
66  * \param key the key associated to this object
67  *
68  * The element with the smallest key is automatically moved at the top of the heap.
69  */
70 void xbt_heap_push(xbt_heap_t H, void *content, double key)
71 {
72   int count = ++(H->count);
73   int size = H->size;
74   xbt_heapItem_t item;
75   if (count > size) {
76     H->size = 2 * size + 1;
77     H->items =
78         (void *) realloc(H->items,
79                          (H->size) * sizeof(struct xbt_heapItem));
80   }
81   item = &(H->items[count - 1]);
82   item->key = key;
83   item->content = content;
84   xbt_heap_increaseKey(H, count - 1);
85   return;
86 }
87
88 /**
89  * @brief Extracts from the heap and returns the element with the smallest key.
90  * \param H the heap we're working on
91  * \return the element with the smallest key
92  *
93  * Extracts from the heap and returns the element with the smallest
94  * key. The element with the next smallest key is automatically moved
95  * at the top of the heap.
96  */
97 void *xbt_heap_pop(xbt_heap_t H)
98 {
99   void *max;
100
101   if (H->count == 0)
102     return NULL;
103
104   max = CONTENT(H, 0);
105
106   H->items[0] = H->items[(H->count) - 1];
107   (H->count)--;
108   xbt_heap_maxHeapify(H);
109   if (H->count < H->size / 4 && H->size > 16) {
110     H->size = H->size / 2 + 1;
111     H->items =
112         (void *) realloc(H->items,
113                          (H->size) * sizeof(struct xbt_heapItem));
114   }
115   return max;
116 }
117
118 /**
119  * @brief returns the smallest key in the heap (heap unchanged)
120  * \param H the heap we're working on
121  *
122  * \return the smallest key in the heap without modifying the heap.
123  */
124 double xbt_heap_maxkey(xbt_heap_t H)
125 {
126   xbt_assert0(H->count != 0,"Empty heap");
127   return KEY(H, 0);
128 }
129
130 /**
131  * @brief returns the value associated to the smallest key in the heap (heap unchanged)
132  * \param H the heap we're working on
133  *
134  * \return the value associated to the smallest key in the heap
135  * without modifying the heap.
136  */
137 void *xbt_heap_maxcontent(xbt_heap_t H)
138 {
139   xbt_assert0(H->count != 0,"Empty heap");
140   return CONTENT(H, 0);
141 }
142
143 /* <<<< private >>>>
144  * \param H the heap we're working on
145  * 
146  * Restores the heap property once an element has been deleted.
147  */
148 static void xbt_heap_maxHeapify(xbt_heap_t H)
149 {
150   int i = 0;
151   while (1) {
152     int greatest = i;
153     int l = LEFT(i);
154     int r = RIGHT(i);
155     int count = H->count;
156     if (l < count && KEY(H, l) < KEY(H, i))
157       greatest = l;
158     if (r < count && KEY(H, r) < KEY(H, greatest))
159       greatest = r;
160     if (greatest != i) {
161       struct xbt_heapItem tmp = H->items[i];
162       H->items[i] = H->items[greatest];
163       H->items[greatest] = tmp;
164       i = greatest;
165     } else
166       return;
167   }
168 }
169
170 /* <<<< private >>>>
171  * \param H the heap we're working on
172  * \param i an item position in the heap
173  * 
174  * Moves up an item at position i to its correct position. Works only
175  * when called from xbt_heap_push. Do not use otherwise.
176  */
177 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
178 {
179   while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
180     struct xbt_heapItem tmp = H->items[i];
181     H->items[i] = H->items[PARENT(i)];
182     H->items[PARENT(i)] = tmp;
183     i = PARENT(i);
184   }
185   return;
186 }