Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 2 | #include "prio-queue.h" |
| 3 | |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 4 | static inline int compare(struct prio_queue *queue, int i, int j) |
| 5 | { |
Jeff King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 6 | int cmp = queue->compare(queue->array[i].data, queue->array[j].data, |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 7 | queue->cb_data); |
Jeff King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 8 | if (!cmp) |
| 9 | cmp = queue->array[i].ctr - queue->array[j].ctr; |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 10 | return cmp; |
| 11 | } |
| 12 | |
| 13 | static inline void swap(struct prio_queue *queue, int i, int j) |
| 14 | { |
Jeff King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 15 | struct prio_queue_entry tmp = queue->array[i]; |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 16 | queue->array[i] = queue->array[j]; |
| 17 | queue->array[j] = tmp; |
| 18 | } |
| 19 | |
Junio C Hamano | da24b10 | 2013-06-06 21:58:12 -0700 | [diff] [blame] | 20 | void 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 King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 26 | for (i = 0; i <= (j = (queue->nr - 1) - i); i++) |
| 27 | swap(queue, i, j); |
Junio C Hamano | da24b10 | 2013-06-06 21:58:12 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 30 | void 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 King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 36 | queue->insertion_ctr = 0; |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void prio_queue_put(struct prio_queue *queue, void *thing) |
| 40 | { |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 41 | int ix, parent; |
| 42 | |
| 43 | /* Append at the end */ |
| 44 | ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc); |
Jeff King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 45 | queue->array[queue->nr].ctr = queue->insertion_ctr++; |
| 46 | queue->array[queue->nr].data = thing; |
| 47 | queue->nr++; |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 48 | if (!queue->compare) |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 49 | return; /* LIFO */ |
| 50 | |
| 51 | /* Bubble up the new one */ |
| 52 | for (ix = queue->nr - 1; ix; ix = parent) { |
| 53 | parent = (ix - 1) / 2; |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 54 | if (compare(queue, parent, ix) <= 0) |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 55 | break; |
| 56 | |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 57 | swap(queue, parent, ix); |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | void *prio_queue_get(struct prio_queue *queue) |
| 62 | { |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 63 | void *result; |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 64 | int ix, child; |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 65 | |
| 66 | if (!queue->nr) |
| 67 | return NULL; |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 68 | if (!queue->compare) |
Jeff King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 69 | return queue->array[--queue->nr].data; /* LIFO */ |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 70 | |
Jeff King | e8f91e3 | 2014-07-14 01:51:59 -0400 | [diff] [blame] | 71 | result = queue->array[0].data; |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 72 | 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 King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 80 | if (child + 1 < queue->nr && |
| 81 | compare(queue, child, child + 1) >= 0) |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 82 | child++; /* use right child */ |
| 83 | |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 84 | if (compare(queue, ix, child) <= 0) |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 85 | break; |
| 86 | |
Jeff King | 6d63baa | 2014-07-14 01:42:50 -0400 | [diff] [blame] | 87 | swap(queue, child, ix); |
Junio C Hamano | b4b594a | 2013-06-06 19:13:50 -0700 | [diff] [blame] | 88 | } |
| 89 | return result; |
| 90 | } |