2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 * The prioq object implements a priority queue. That is, it orders objects by
23 * their priority and allows O(1) access to the object with the highest
24 * priority. Insertion and removal are Θ(log n). Optionally, the caller can
25 * provide a pointer to an index which will be kept up-to-date by the prioq.
27 * The underlying algorithm used in this implementation is a Heap.
33 #include "alloc-util.h"
43 compare_func_t compare_func;
44 unsigned n_items, n_allocated;
46 struct prioq_item *items;
49 Prioq *prioq_new(compare_func_t compare_func) {
56 q->compare_func = compare_func;
60 Prioq* prioq_free(Prioq *q) {
70 int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) {
76 *q = prioq_new(compare_func);
83 static void swap(Prioq *q, unsigned j, unsigned k) {
88 assert(j < q->n_items);
89 assert(k < q->n_items);
91 assert(!q->items[j].idx || *(q->items[j].idx) == j);
92 assert(!q->items[k].idx || *(q->items[k].idx) == k);
94 saved_data = q->items[j].data;
95 saved_idx = q->items[j].idx;
96 q->items[j].data = q->items[k].data;
97 q->items[j].idx = q->items[k].idx;
98 q->items[k].data = saved_data;
99 q->items[k].idx = saved_idx;
102 *q->items[j].idx = j;
105 *q->items[k].idx = k;
108 static unsigned shuffle_up(Prioq *q, unsigned idx) {
116 if (q->compare_func(q->items[k].data, q->items[idx].data) <= 0)
126 static unsigned shuffle_down(Prioq *q, unsigned idx) {
132 k = (idx+1)*2; /* right child */
133 j = k-1; /* left child */
138 if (q->compare_func(q->items[j].data, q->items[idx].data) < 0)
140 /* So our left child is smaller than we are, let's
141 * remember this fact */
146 if (k < q->n_items &&
147 q->compare_func(q->items[k].data, q->items[s].data) < 0)
149 /* So our right child is smaller than we are, let's
150 * remember this fact */
153 /* s now points to the smallest of the three items */
156 /* No swap necessary, we're done */
166 int prioq_put(Prioq *q, void *data, unsigned *idx) {
167 struct prioq_item *i;
172 if (q->n_items >= q->n_allocated) {
174 struct prioq_item *j;
176 n = MAX((q->n_items+1) * 2, 16u);
177 j = realloc(q->items, sizeof(struct prioq_item) * n);
198 static void remove_item(Prioq *q, struct prioq_item *i) {
199 struct prioq_item *l;
204 l = q->items + q->n_items - 1;
207 /* Last entry, let's just remove it */
212 /* Not last entry, let's replace the last entry with
213 * this one, and reshuffle */
223 k = shuffle_down(q, k);
228 _pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) {
229 struct prioq_item *i;
234 if (*idx == PRIOQ_IDX_NULL ||
244 for (i = q->items; i < q->items + q->n_items; i++)
251 int prioq_remove(Prioq *q, void *data, unsigned *idx) {
252 struct prioq_item *i;
257 i = find_item(q, data, idx);
265 int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
266 struct prioq_item *i;
271 i = find_item(q, data, idx);
276 k = shuffle_down(q, k);
281 void *prioq_peek(Prioq *q) {
289 return q->items[0].data;
292 void *prioq_pop(Prioq *q) {
301 data = q->items[0].data;
302 remove_item(q, q->items);
306 unsigned prioq_size(Prioq *q) {
314 bool prioq_isempty(Prioq *q) {
319 return q->n_items <= 0;