blob: e4365b00d6c3366e6753fc6da3fe1a165ab1222d [file] [log] [blame]
Junio C Hamanob4b594a2013-06-06 19:13:50 -07001#include "cache.h"
Junio C Hamanob4b594a2013-06-06 19:13:50 -07002#include "prio-queue.h"
3
Jeff King6d63baa2014-07-14 01:42:50 -04004static inline int compare(struct prio_queue *queue, int i, int j)
5{
Jeff Kinge8f91e32014-07-14 01:51:59 -04006 int cmp = queue->compare(queue->array[i].data, queue->array[j].data,
Jeff King6d63baa2014-07-14 01:42:50 -04007 queue->cb_data);
Jeff Kinge8f91e32014-07-14 01:51:59 -04008 if (!cmp)
9 cmp = queue->array[i].ctr - queue->array[j].ctr;
Jeff King6d63baa2014-07-14 01:42:50 -040010 return cmp;
11}
12
13static inline void swap(struct prio_queue *queue, int i, int j)
14{
Jeff Kinge8f91e32014-07-14 01:51:59 -040015 struct prio_queue_entry tmp = queue->array[i];
Jeff King6d63baa2014-07-14 01:42:50 -040016 queue->array[i] = queue->array[j];
17 queue->array[j] = tmp;
18}
19
Junio C Hamanoda24b102013-06-06 21:58:12 -070020void prio_queue_reverse(struct prio_queue *queue)
21{
22 int i, j;
23
24 if (queue->compare != NULL)
25 die("BUG: prio_queue_reverse() on non-LIFO queue");
Jeff King6d63baa2014-07-14 01:42:50 -040026 for (i = 0; i <= (j = (queue->nr - 1) - i); i++)
27 swap(queue, i, j);
Junio C Hamanoda24b102013-06-06 21:58:12 -070028}
29
Junio C Hamanob4b594a2013-06-06 19:13:50 -070030void clear_prio_queue(struct prio_queue *queue)
31{
32 free(queue->array);
33 queue->nr = 0;
34 queue->alloc = 0;
35 queue->array = NULL;
Jeff Kinge8f91e32014-07-14 01:51:59 -040036 queue->insertion_ctr = 0;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070037}
38
39void prio_queue_put(struct prio_queue *queue, void *thing)
40{
Junio C Hamanob4b594a2013-06-06 19:13:50 -070041 int ix, parent;
42
43 /* Append at the end */
44 ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
Jeff Kinge8f91e32014-07-14 01:51:59 -040045 queue->array[queue->nr].ctr = queue->insertion_ctr++;
46 queue->array[queue->nr].data = thing;
47 queue->nr++;
Jeff King6d63baa2014-07-14 01:42:50 -040048 if (!queue->compare)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070049 return; /* LIFO */
50
51 /* Bubble up the new one */
52 for (ix = queue->nr - 1; ix; ix = parent) {
53 parent = (ix - 1) / 2;
Jeff King6d63baa2014-07-14 01:42:50 -040054 if (compare(queue, parent, ix) <= 0)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070055 break;
56
Jeff King6d63baa2014-07-14 01:42:50 -040057 swap(queue, parent, ix);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070058 }
59}
60
61void *prio_queue_get(struct prio_queue *queue)
62{
Jeff King6d63baa2014-07-14 01:42:50 -040063 void *result;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070064 int ix, child;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070065
66 if (!queue->nr)
67 return NULL;
Jeff King6d63baa2014-07-14 01:42:50 -040068 if (!queue->compare)
Jeff Kinge8f91e32014-07-14 01:51:59 -040069 return queue->array[--queue->nr].data; /* LIFO */
Junio C Hamanob4b594a2013-06-06 19:13:50 -070070
Jeff Kinge8f91e32014-07-14 01:51:59 -040071 result = queue->array[0].data;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070072 if (!--queue->nr)
73 return result;
74
75 queue->array[0] = queue->array[queue->nr];
76
77 /* Push down the one at the root */
78 for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) {
79 child = ix * 2 + 1; /* left */
Jeff King6d63baa2014-07-14 01:42:50 -040080 if (child + 1 < queue->nr &&
81 compare(queue, child, child + 1) >= 0)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070082 child++; /* use right child */
83
Jeff King6d63baa2014-07-14 01:42:50 -040084 if (compare(queue, ix, child) <= 0)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070085 break;
86
Jeff King6d63baa2014-07-14 01:42:50 -040087 swap(queue, child, ix);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070088 }
89 return result;
90}