]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/prioq.c
prioq: add one more assertion
[thirdparty/systemd.git] / src / basic / prioq.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
30bdd695 2
f36f8f78
DH
3/*
4 * Priority Queue
5 * The prioq object implements a priority queue. That is, it orders objects by
6 * their priority and allows O(1) access to the object with the highest
7 * priority. Insertion and removal are Θ(log n). Optionally, the caller can
8 * provide a pointer to an index which will be kept up-to-date by the prioq.
9 *
10 * The underlying algorithm used in this implementation is a Heap.
11 */
12
11c3a366
TA
13#include <errno.h>
14#include <stdlib.h>
15
b5efdb8a 16#include "alloc-util.h"
11c3a366 17#include "hashmap.h"
93cc7779 18#include "prioq.h"
30bdd695
LP
19
20struct prioq_item {
21 void *data;
22 unsigned *idx;
23};
24
25struct Prioq {
26 compare_func_t compare_func;
27 unsigned n_items, n_allocated;
28
29 struct prioq_item *items;
30};
31
32Prioq *prioq_new(compare_func_t compare_func) {
33 Prioq *q;
34
35 q = new0(Prioq, 1);
36 if (!q)
37 return q;
38
39 q->compare_func = compare_func;
40 return q;
41}
42
cab5b059 43Prioq* prioq_free(Prioq *q) {
30bdd695 44 if (!q)
cab5b059 45 return NULL;
30bdd695
LP
46
47 free(q->items);
6b430fdb 48 return mfree(q);
30bdd695
LP
49}
50
e3017af9
LP
51int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) {
52 assert(q);
53
54 if (*q)
55 return 0;
56
57 *q = prioq_new(compare_func);
58 if (!*q)
59 return -ENOMEM;
60
61 return 0;
62}
63
30bdd695
LP
64static void swap(Prioq *q, unsigned j, unsigned k) {
65 void *saved_data;
66 unsigned *saved_idx;
67
68 assert(q);
69 assert(j < q->n_items);
70 assert(k < q->n_items);
71
72 assert(!q->items[j].idx || *(q->items[j].idx) == j);
73 assert(!q->items[k].idx || *(q->items[k].idx) == k);
74
75 saved_data = q->items[j].data;
76 saved_idx = q->items[j].idx;
77 q->items[j].data = q->items[k].data;
78 q->items[j].idx = q->items[k].idx;
79 q->items[k].data = saved_data;
80 q->items[k].idx = saved_idx;
81
82 if (q->items[j].idx)
83 *q->items[j].idx = j;
84
85 if (q->items[k].idx)
86 *q->items[k].idx = k;
87}
88
89static unsigned shuffle_up(Prioq *q, unsigned idx) {
90 assert(q);
9f934223 91 assert(idx < q->n_items);
30bdd695
LP
92
93 while (idx > 0) {
94 unsigned k;
95
96 k = (idx-1)/2;
97
8a9d23e4 98 if (q->compare_func(q->items[k].data, q->items[idx].data) <= 0)
30bdd695
LP
99 break;
100
101 swap(q, idx, k);
102 idx = k;
103 }
104
105 return idx;
106}
107
108static unsigned shuffle_down(Prioq *q, unsigned idx) {
109 assert(q);
110
111 for (;;) {
112 unsigned j, k, s;
113
114 k = (idx+1)*2; /* right child */
115 j = k-1; /* left child */
116
117 if (j >= q->n_items)
118 break;
119
120 if (q->compare_func(q->items[j].data, q->items[idx].data) < 0)
121
122 /* So our left child is smaller than we are, let's
123 * remember this fact */
124 s = j;
125 else
126 s = idx;
127
128 if (k < q->n_items &&
129 q->compare_func(q->items[k].data, q->items[s].data) < 0)
130
131 /* So our right child is smaller than we are, let's
132 * remember this fact */
133 s = k;
134
135 /* s now points to the smallest of the three items */
136
137 if (s == idx)
138 /* No swap necessary, we're done */
139 break;
140
141 swap(q, idx, s);
142 idx = s;
143 }
144
145 return idx;
146}
147
148int prioq_put(Prioq *q, void *data, unsigned *idx) {
149 struct prioq_item *i;
150 unsigned k;
151
152 assert(q);
153
154 if (q->n_items >= q->n_allocated) {
155 unsigned n;
156 struct prioq_item *j;
157
9607d947 158 n = MAX((q->n_items+1) * 2, 16u);
62d74c78 159 j = reallocarray(q->items, n, sizeof(struct prioq_item));
30bdd695
LP
160 if (!j)
161 return -ENOMEM;
162
163 q->items = j;
164 q->n_allocated = n;
165 }
166
167 k = q->n_items++;
168 i = q->items + k;
169 i->data = data;
170 i->idx = idx;
171
172 if (idx)
173 *idx = k;
174
175 shuffle_up(q, k);
176
177 return 0;
178}
179
180static void remove_item(Prioq *q, struct prioq_item *i) {
181 struct prioq_item *l;
182
183 assert(q);
184 assert(i);
185
186 l = q->items + q->n_items - 1;
187
188 if (i == l)
189 /* Last entry, let's just remove it */
190 q->n_items--;
191 else {
192 unsigned k;
193
194 /* Not last entry, let's replace the last entry with
195 * this one, and reshuffle */
196
197 k = i - q->items;
198
199 i->data = l->data;
200 i->idx = l->idx;
201 if (i->idx)
202 *i->idx = k;
203 q->n_items--;
204
205 k = shuffle_down(q, k);
206 shuffle_up(q, k);
207 }
208}
209
44a6b1b6 210_pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) {
30bdd695
LP
211 struct prioq_item *i;
212
213 assert(q);
214
cd86deef
YW
215 if (q->n_items <= 0)
216 return NULL;
217
30bdd695 218 if (idx) {
c2ba3ad6
LP
219 if (*idx == PRIOQ_IDX_NULL ||
220 *idx > q->n_items)
e3017af9
LP
221 return NULL;
222
30bdd695 223 i = q->items + *idx;
e3017af9
LP
224 if (i->data != data)
225 return NULL;
30bdd695
LP
226
227 return i;
228 } else {
229 for (i = q->items; i < q->items + q->n_items; i++)
230 if (i->data == data)
231 return i;
232 return NULL;
233 }
234}
235
236int prioq_remove(Prioq *q, void *data, unsigned *idx) {
237 struct prioq_item *i;
238
a3de5ae1
LP
239 if (!q)
240 return 0;
30bdd695
LP
241
242 i = find_item(q, data, idx);
243 if (!i)
244 return 0;
245
246 remove_item(q, i);
247 return 1;
248}
249
250int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
251 struct prioq_item *i;
252 unsigned k;
253
254 assert(q);
255
256 i = find_item(q, data, idx);
257 if (!i)
258 return 0;
259
260 k = i - q->items;
261 k = shuffle_down(q, k);
262 shuffle_up(q, k);
263 return 1;
264}
265
266void *prioq_peek(Prioq *q) {
a3de5ae1
LP
267
268 if (!q)
269 return NULL;
30bdd695
LP
270
271 if (q->n_items <= 0)
272 return NULL;
273
274 return q->items[0].data;
275}
276
277void *prioq_pop(Prioq *q) {
278 void *data;
279
a3de5ae1
LP
280 if (!q)
281 return NULL;
30bdd695
LP
282
283 if (q->n_items <= 0)
284 return NULL;
285
286 data = q->items[0].data;
287 remove_item(q, q->items);
288 return data;
289}
290
291unsigned prioq_size(Prioq *q) {
a3de5ae1
LP
292
293 if (!q)
294 return 0;
30bdd695
LP
295
296 return q->n_items;
30bdd695 297}
718db961 298
30bdd695 299bool prioq_isempty(Prioq *q) {
a3de5ae1
LP
300
301 if (!q)
302 return true;
30bdd695
LP
303
304 return q->n_items <= 0;
305}