1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
8 * The prioq object implements a priority queue. That is, it orders objects by
9 * their priority and allows O(1) access to the object with the highest
10 * priority. Insertion and removal are Θ(log n). Optionally, the caller can
11 * provide a pointer to an index which will be kept up-to-date by the prioq.
13 * The underlying algorithm used in this implementation is a Heap.
19 #include "alloc-util.h"
29 compare_func_t compare_func;
30 unsigned n_items, n_allocated;
32 struct prioq_item *items;
35 Prioq *prioq_new(compare_func_t compare_func) {
42 q->compare_func = compare_func;
46 Prioq* prioq_free(Prioq *q) {
54 int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) {
60 *q = prioq_new(compare_func);
67 static void swap(Prioq *q, unsigned j, unsigned k) {
72 assert(j < q->n_items);
73 assert(k < q->n_items);
75 assert(!q->items[j].idx || *(q->items[j].idx) == j);
76 assert(!q->items[k].idx || *(q->items[k].idx) == k);
78 saved_data = q->items[j].data;
79 saved_idx = q->items[j].idx;
80 q->items[j].data = q->items[k].data;
81 q->items[j].idx = q->items[k].idx;
82 q->items[k].data = saved_data;
83 q->items[k].idx = saved_idx;
92 static unsigned shuffle_up(Prioq *q, unsigned idx) {
100 if (q->compare_func(q->items[k].data, q->items[idx].data) <= 0)
110 static unsigned shuffle_down(Prioq *q, unsigned idx) {
116 k = (idx+1)*2; /* right child */
117 j = k-1; /* left child */
122 if (q->compare_func(q->items[j].data, q->items[idx].data) < 0)
124 /* So our left child is smaller than we are, let's
125 * remember this fact */
130 if (k < q->n_items &&
131 q->compare_func(q->items[k].data, q->items[s].data) < 0)
133 /* So our right child is smaller than we are, let's
134 * remember this fact */
137 /* s now points to the smallest of the three items */
140 /* No swap necessary, we're done */
150 int prioq_put(Prioq *q, void *data, unsigned *idx) {
151 struct prioq_item *i;
156 if (q->n_items >= q->n_allocated) {
158 struct prioq_item *j;
160 n = MAX((q->n_items+1) * 2, 16u);
161 j = reallocarray(q->items, n, sizeof(struct prioq_item));
182 static void remove_item(Prioq *q, struct prioq_item *i) {
183 struct prioq_item *l;
188 l = q->items + q->n_items - 1;
191 /* Last entry, let's just remove it */
196 /* Not last entry, let's replace the last entry with
197 * this one, and reshuffle */
207 k = shuffle_down(q, k);
212 _pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) {
213 struct prioq_item *i;
218 if (*idx == PRIOQ_IDX_NULL ||
228 for (i = q->items; i < q->items + q->n_items; i++)
235 int prioq_remove(Prioq *q, void *data, unsigned *idx) {
236 struct prioq_item *i;
241 i = find_item(q, data, idx);
249 int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
250 struct prioq_item *i;
255 i = find_item(q, data, idx);
260 k = shuffle_down(q, k);
265 void *prioq_peek(Prioq *q) {
273 return q->items[0].data;
276 void *prioq_pop(Prioq *q) {
285 data = q->items[0].data;
286 remove_item(q, q->items);
290 unsigned prioq_size(Prioq *q) {
298 bool prioq_isempty(Prioq *q) {
303 return q->n_items <= 0;