blob: d3f488cb05f29bc90e4e03810d9bf4b972d0834f [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{
René Scharfe35d803b2017-01-28 22:40:58 +010015 SWAP(queue->array[i], queue->array[j]);
Jeff King6d63baa2014-07-14 01:42:50 -040016}
17
Junio C Hamanoda24b102013-06-06 21:58:12 -070018void prio_queue_reverse(struct prio_queue *queue)
19{
20 int i, j;
21
22 if (queue->compare != NULL)
Johannes Schindelin033abf92018-05-02 11:38:39 +020023 BUG("prio_queue_reverse() on non-LIFO queue");
Jeff King1f9e18b2017-04-24 07:49:20 -040024 for (i = 0; i < (j = (queue->nr - 1) - i); i++)
Jeff King6d63baa2014-07-14 01:42:50 -040025 swap(queue, i, j);
Junio C Hamanoda24b102013-06-06 21:58:12 -070026}
27
Junio C Hamanob4b594a2013-06-06 19:13:50 -070028void clear_prio_queue(struct prio_queue *queue)
29{
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +000030 FREE_AND_NULL(queue->array);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070031 queue->nr = 0;
32 queue->alloc = 0;
Jeff Kinge8f91e32014-07-14 01:51:59 -040033 queue->insertion_ctr = 0;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070034}
35
36void prio_queue_put(struct prio_queue *queue, void *thing)
37{
Junio C Hamanob4b594a2013-06-06 19:13:50 -070038 int ix, parent;
39
40 /* Append at the end */
41 ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
Jeff Kinge8f91e32014-07-14 01:51:59 -040042 queue->array[queue->nr].ctr = queue->insertion_ctr++;
43 queue->array[queue->nr].data = thing;
44 queue->nr++;
Jeff King6d63baa2014-07-14 01:42:50 -040045 if (!queue->compare)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070046 return; /* LIFO */
47
48 /* Bubble up the new one */
49 for (ix = queue->nr - 1; ix; ix = parent) {
50 parent = (ix - 1) / 2;
Jeff King6d63baa2014-07-14 01:42:50 -040051 if (compare(queue, parent, ix) <= 0)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070052 break;
53
Jeff King6d63baa2014-07-14 01:42:50 -040054 swap(queue, parent, ix);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070055 }
56}
57
58void *prio_queue_get(struct prio_queue *queue)
59{
Jeff King6d63baa2014-07-14 01:42:50 -040060 void *result;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070061 int ix, child;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070062
63 if (!queue->nr)
64 return NULL;
Jeff King6d63baa2014-07-14 01:42:50 -040065 if (!queue->compare)
Jeff Kinge8f91e32014-07-14 01:51:59 -040066 return queue->array[--queue->nr].data; /* LIFO */
Junio C Hamanob4b594a2013-06-06 19:13:50 -070067
Jeff Kinge8f91e32014-07-14 01:51:59 -040068 result = queue->array[0].data;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070069 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 */
Jeff King6d63baa2014-07-14 01:42:50 -040077 if (child + 1 < queue->nr &&
78 compare(queue, child, child + 1) >= 0)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070079 child++; /* use right child */
80
Jeff King6d63baa2014-07-14 01:42:50 -040081 if (compare(queue, ix, child) <= 0)
Junio C Hamanob4b594a2013-06-06 19:13:50 -070082 break;
83
Jeff King6d63baa2014-07-14 01:42:50 -040084 swap(queue, child, ix);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070085 }
86 return result;
87}
Derrick Stoleeaca42402018-11-01 13:46:17 +000088
89void *prio_queue_peek(struct prio_queue *queue)
90{
91 if (!queue->nr)
92 return NULL;
93 if (!queue->compare)
94 return queue->array[queue->nr - 1].data;
95 return queue->array[0].data;
96}