Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No more xbt_maxmin_float_t not xbt_heap_float_t. I use double everywhere. In
[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/error.h"
12 #include "heap_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(heap, xbt, "Heap");
14
15 /**
16  * xbt_heap_new:
17  * @init_size: initial size of the heap
18  * @free_func: function to call on each element when you want to free
19  *             the whole heap (or NULL if nothing to do).
20  *
21  * Creates a new heap.
22  */
23 xbt_heap_t xbt_heap_new(int init_size, void_f_pvoid_t * const free_func)
24 {
25   xbt_heap_t H = xbt_new0(struct xbt_heap, 1);
26   H->size = init_size;
27   H->count = 0;
28   H->items = (xbt_heapItem_t) xbt_new0(struct xbt_heapItem, init_size);
29   H->free = free_func;
30   return H;
31 }
32
33 /**
34  * xbt_heap_free:
35  * @H: poor victim
36  *
37  * kilkil a heap and its content
38  */
39 void xbt_heap_free(xbt_heap_t H)
40 {
41   int i;
42   if (H->free)
43     for (i = 0; i < H->count; i++)
44       H->free(H->items[i].content);
45   xbt_free(H->items);
46   xbt_free(H);
47   return;
48 }
49
50 /**
51  * xbt_heap_size:
52  * @H: the heap we're working on
53  *
54  * returns the number of elements in the heap
55  */
56 int xbt_heap_size(xbt_heap_t H)
57 {
58   return (H->count);
59 }
60
61 /**
62  * xbt_heap_push:
63  * @H: the heap we're working on
64  * @content: the object you want to add to the heap
65  * @key: the key associated to this object
66  *
67  * Add an element int the heap. The element with the smallest key is
68  * 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  * xbt_heap_pop:
90  * @H: the heap we're working on
91  *
92  * Extracts from the heap and returns the element with the smallest
93  * key. The element with the next smallest key is automatically moved
94  * at the top of the heap.
95  */
96 void *xbt_heap_pop(xbt_heap_t H)
97 {
98   void *max;
99
100   if (H->count == 0)
101     return NULL;
102
103   max = CONTENT(H, 0);
104
105   H->items[0] = H->items[(H->count) - 1];
106   (H->count)--;
107   xbt_heap_maxHeapify(H);
108   if (H->count < H->size / 4 && H->size > 16) {
109     H->size = H->size / 2 + 1;
110     H->items =
111         (void *) realloc(H->items,
112                          (H->size) * sizeof(struct xbt_heapItem));
113   }
114   return max;
115 }
116
117 /**
118  * xbt_heap_maxkey:
119  * @H: the heap we're working on
120  *
121  * Returns the smallest key in the heap without modifying the heap.
122  */
123 double xbt_heap_maxkey(xbt_heap_t H)
124 {
125   if (H->count == 0)
126     abort();
127   return KEY(H, 0);
128 }
129
130 /**
131  * xbt_heap_maxcontent:
132  * @H: the heap we're working on
133  *
134  * Returns 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   if (H->count == 0)
140     abort();
141   return CONTENT(H, 0);
142 }
143
144 /**
145  * xbt_heap_maxcontent:
146  * @H: the heap we're working on
147  * 
148  * Restores the heap property once an element has been deleted.
149  */
150 static void xbt_heap_maxHeapify(xbt_heap_t H)
151 {
152   int i = 0;
153   while (1) {
154     int greatest = i;
155     int l = LEFT(i);
156     int r = RIGHT(i);
157     int count = H->count;
158     if (l < count && KEY(H, l) < KEY(H, i))
159       greatest = l;
160     if (r < count && KEY(H, r) < KEY(H, greatest))
161       greatest = r;
162     if (greatest != i) {
163       struct xbt_heapItem tmp = H->items[i];
164       H->items[i] = H->items[greatest];
165       H->items[greatest] = tmp;
166       i = greatest;
167     } else
168       return;
169   }
170 }
171
172 /**
173  * xbt_heap_maxcontent:
174  * @H: the heap we're working on
175  * @i: an item position in the heap
176  * 
177  * Moves up an item at position i to its correct position. Works only
178  * when called from xbt_heap_push. Do not use otherwise.
179  */
180 static void xbt_heap_increaseKey(xbt_heap_t H, int i)
181 {
182   while (i > 0 && KEY(H, PARENT(i)) > KEY(H, i)) {
183     struct xbt_heapItem tmp = H->items[i];
184     H->items[i] = H->items[PARENT(i)];
185     H->items[PARENT(i)] = tmp;
186     i = PARENT(i);
187   }
188   return;
189 }