1
#include "cache.h"
2
-#include "commit.h"
2
#include "prio-queue.h"
3
4
+static inline int compare(struct prio_queue *queue, int i, int j)
5
+{
6
+ int cmp = queue->compare(queue->array[i], queue->array[j],
7
+ queue->cb_data);
8
+ return cmp;
9
+}
10
+
11
+static inline void swap(struct prio_queue *queue, int i, int j)
12
+{
13
+ void *tmp = queue->array[i];
14
+ queue->array[i] = queue->array[j];
15
+ queue->array[j] = tmp;
16
+}
17
+
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");
11
- for (i = 0; i <= (j = (queue->nr - 1) - i); i++) {
12
- struct commit *swap = queue->array[i];
13
- queue->array[i] = queue->array[j];
14
- queue->array[j] = swap;
15
- }
24
+ for (i = 0; i <= (j = (queue->nr - 1) - i); i++)
25
+ swap(queue, i, j);
26
}
27
28
void clear_prio_queue(struct prio_queue *queue)
35
36
void prio_queue_put(struct prio_queue *queue, void *thing)
37
{
28
- prio_queue_compare_fn compare = queue->compare;
38
int ix, parent;
39
40
/* Append at the end */
41
ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
42
queue->array[queue->nr++] = thing;
34
- if (!compare)
43
+ if (!queue->compare)
44
return; /* LIFO */
45
46
/* Bubble up the new one */
47
for (ix = queue->nr - 1; ix; ix = parent) {
48
parent = (ix - 1) / 2;
40
- if (compare(queue->array[parent], queue->array[ix],
41
- queue->cb_data) <= 0)
49
+ if (compare(queue, parent, ix) <= 0)
50
break;
51
44
- thing = queue->array[parent];
45
- queue->array[parent] = queue->array[ix];
46
- queue->array[ix] = thing;
52
+ swap(queue, parent, ix);
53
}
54
}
55
56
void *prio_queue_get(struct prio_queue *queue)
57
{
52
- void *result, *swap;
58
+ void *result;
59
int ix, child;
54
- prio_queue_compare_fn compare = queue->compare;
60
61
if (!queue->nr)
62
return NULL;
58
- if (!compare)
63
+ if (!queue->compare)
64
return queue->array[--queue->nr]; /* LIFO */
65
66
result = queue->array[0];
72
/* Push down the one at the root */
73
for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) {
74
child = ix * 2 + 1; /* left */
70
- if ((child + 1 < queue->nr) &&
71
- (compare(queue->array[child], queue->array[child + 1],
72
- queue->cb_data) >= 0))
75
+ if (child + 1 < queue->nr &&
76
+ compare(queue, child, child + 1) >= 0)
77
child++; /* use right child */
78
75
- if (compare(queue->array[ix], queue->array[child],
76
- queue->cb_data) <= 0)
79
+ if (compare(queue, ix, child) <= 0)
80
break;
81
79
- swap = queue->array[child];
80
- queue->array[child] = queue->array[ix];
81
- queue->array[ix] = swap;
82
+ swap(queue, child, ix);
83
}
84
return result;
85
}