]>
Commit | Line | Data |
---|---|---|
b4b594a3 | 1 | #include "cache.h" |
b4b594a3 JH |
2 | #include "prio-queue.h" |
3 | ||
6d63baa4 JK |
4 | static inline int compare(struct prio_queue *queue, int i, int j) |
5 | { | |
e8f91e3d | 6 | int cmp = queue->compare(queue->array[i].data, queue->array[j].data, |
6d63baa4 | 7 | queue->cb_data); |
e8f91e3d JK |
8 | if (!cmp) |
9 | cmp = queue->array[i].ctr - queue->array[j].ctr; | |
6d63baa4 JK |
10 | return cmp; |
11 | } | |
12 | ||
13 | static inline void swap(struct prio_queue *queue, int i, int j) | |
14 | { | |
35d803bc | 15 | SWAP(queue->array[i], queue->array[j]); |
6d63baa4 JK |
16 | } |
17 | ||
da24b104 JH |
18 | void prio_queue_reverse(struct prio_queue *queue) |
19 | { | |
20 | int i, j; | |
21 | ||
22 | if (queue->compare != NULL) | |
23 | die("BUG: prio_queue_reverse() on non-LIFO queue"); | |
1f9e18b7 | 24 | for (i = 0; i < (j = (queue->nr - 1) - i); i++) |
6d63baa4 | 25 | swap(queue, i, j); |
da24b104 JH |
26 | } |
27 | ||
b4b594a3 JH |
28 | void clear_prio_queue(struct prio_queue *queue) |
29 | { | |
88ce3ef6 | 30 | FREE_AND_NULL(queue->array); |
b4b594a3 JH |
31 | queue->nr = 0; |
32 | queue->alloc = 0; | |
e8f91e3d | 33 | queue->insertion_ctr = 0; |
b4b594a3 JH |
34 | } |
35 | ||
36 | void prio_queue_put(struct prio_queue *queue, void *thing) | |
37 | { | |
b4b594a3 JH |
38 | int ix, parent; |
39 | ||
40 | /* Append at the end */ | |
41 | ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc); | |
e8f91e3d JK |
42 | queue->array[queue->nr].ctr = queue->insertion_ctr++; |
43 | queue->array[queue->nr].data = thing; | |
44 | queue->nr++; | |
6d63baa4 | 45 | if (!queue->compare) |
b4b594a3 JH |
46 | return; /* LIFO */ |
47 | ||
48 | /* Bubble up the new one */ | |
49 | for (ix = queue->nr - 1; ix; ix = parent) { | |
50 | parent = (ix - 1) / 2; | |
6d63baa4 | 51 | if (compare(queue, parent, ix) <= 0) |
b4b594a3 JH |
52 | break; |
53 | ||
6d63baa4 | 54 | swap(queue, parent, ix); |
b4b594a3 JH |
55 | } |
56 | } | |
57 | ||
58 | void *prio_queue_get(struct prio_queue *queue) | |
59 | { | |
6d63baa4 | 60 | void *result; |
b4b594a3 | 61 | int ix, child; |
b4b594a3 JH |
62 | |
63 | if (!queue->nr) | |
64 | return NULL; | |
6d63baa4 | 65 | if (!queue->compare) |
e8f91e3d | 66 | return queue->array[--queue->nr].data; /* LIFO */ |
b4b594a3 | 67 | |
e8f91e3d | 68 | result = queue->array[0].data; |
b4b594a3 JH |
69 | if (!--queue->nr) |
70 | return result; | |
71 | ||
72 | queue->array[0] = queue->array[queue->nr]; | |
73 | ||
74 | /* Push down the one at the root */ | |
75 | for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) { | |
76 | child = ix * 2 + 1; /* left */ | |
6d63baa4 JK |
77 | if (child + 1 < queue->nr && |
78 | compare(queue, child, child + 1) >= 0) | |
b4b594a3 JH |
79 | child++; /* use right child */ |
80 | ||
6d63baa4 | 81 | if (compare(queue, ix, child) <= 0) |
b4b594a3 JH |
82 | break; |
83 | ||
6d63baa4 | 84 | swap(queue, child, ix); |
b4b594a3 JH |
85 | } |
86 | return result; | |
87 | } |