]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/prioq.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / basic / prioq.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
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
13 #include <errno.h>
14 #include <stdlib.h>
15
16 #include "alloc-util.h"
17 #include "hashmap.h"
18 #include "prioq.h"
19
20 struct prioq_item {
21 void *data;
22 unsigned *idx;
23 };
24
25 struct Prioq {
26 compare_func_t compare_func;
27 unsigned n_items, n_allocated;
28
29 struct prioq_item *items;
30 };
31
32 Prioq *prioq_new(compare_func_t compare_func) {
33 Prioq *q;
34
35 q = new(Prioq, 1);
36 if (!q)
37 return q;
38
39 *q = (Prioq) {
40 .compare_func = compare_func,
41 };
42
43 return q;
44 }
45
46 Prioq* prioq_free(Prioq *q) {
47 if (!q)
48 return NULL;
49
50 free(q->items);
51 return mfree(q);
52 }
53
54 int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) {
55 assert(q);
56
57 if (*q)
58 return 0;
59
60 *q = prioq_new(compare_func);
61 if (!*q)
62 return -ENOMEM;
63
64 return 0;
65 }
66
67 static void swap(Prioq *q, unsigned j, unsigned k) {
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 SWAP_TWO(q->items[j].data, q->items[k].data);
76 SWAP_TWO(q->items[j].idx, q->items[k].idx);
77
78 if (q->items[j].idx)
79 *q->items[j].idx = j;
80
81 if (q->items[k].idx)
82 *q->items[k].idx = k;
83 }
84
85 static unsigned shuffle_up(Prioq *q, unsigned idx) {
86 assert(q);
87 assert(idx < q->n_items);
88
89 while (idx > 0) {
90 unsigned k;
91
92 k = (idx-1)/2;
93
94 if (q->compare_func(q->items[k].data, q->items[idx].data) <= 0)
95 break;
96
97 swap(q, idx, k);
98 idx = k;
99 }
100
101 return idx;
102 }
103
104 static unsigned shuffle_down(Prioq *q, unsigned idx) {
105 assert(q);
106
107 for (;;) {
108 unsigned j, k, s;
109
110 k = (idx+1)*2; /* right child */
111 j = k-1; /* left child */
112
113 if (j >= q->n_items)
114 break;
115
116 if (q->compare_func(q->items[j].data, q->items[idx].data) < 0)
117
118 /* So our left child is smaller than we are, let's
119 * remember this fact */
120 s = j;
121 else
122 s = idx;
123
124 if (k < q->n_items &&
125 q->compare_func(q->items[k].data, q->items[s].data) < 0)
126
127 /* So our right child is smaller than we are, let's
128 * remember this fact */
129 s = k;
130
131 /* s now points to the smallest of the three items */
132
133 if (s == idx)
134 /* No swap necessary, we're done */
135 break;
136
137 swap(q, idx, s);
138 idx = s;
139 }
140
141 return idx;
142 }
143
144 int prioq_put(Prioq *q, void *data, unsigned *idx) {
145 struct prioq_item *i;
146 unsigned k;
147
148 assert(q);
149
150 if (q->n_items >= q->n_allocated) {
151 unsigned n;
152 struct prioq_item *j;
153
154 n = MAX((q->n_items+1) * 2, 16u);
155 j = reallocarray(q->items, n, sizeof(struct prioq_item));
156 if (!j)
157 return -ENOMEM;
158
159 q->items = j;
160 q->n_allocated = n;
161 }
162
163 k = q->n_items++;
164 i = q->items + k;
165 i->data = data;
166 i->idx = idx;
167
168 if (idx)
169 *idx = k;
170
171 shuffle_up(q, k);
172
173 return 0;
174 }
175
176 static void remove_item(Prioq *q, struct prioq_item *i) {
177 struct prioq_item *l;
178
179 assert(q);
180 assert(i);
181
182 l = q->items + q->n_items - 1;
183
184 if (i == l)
185 /* Last entry, let's just remove it */
186 q->n_items--;
187 else {
188 unsigned k;
189
190 /* Not last entry, let's replace the last entry with
191 * this one, and reshuffle */
192
193 k = i - q->items;
194
195 i->data = l->data;
196 i->idx = l->idx;
197 if (i->idx)
198 *i->idx = k;
199 q->n_items--;
200
201 k = shuffle_down(q, k);
202 shuffle_up(q, k);
203 }
204 }
205
206 _pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) {
207 struct prioq_item *i;
208
209 assert(q);
210
211 if (q->n_items <= 0)
212 return NULL;
213
214 if (idx) {
215 if (*idx == PRIOQ_IDX_NULL ||
216 *idx >= q->n_items)
217 return NULL;
218
219 i = q->items + *idx;
220 if (i->data != data)
221 return NULL;
222
223 return i;
224 } else {
225 for (i = q->items; i < q->items + q->n_items; i++)
226 if (i->data == data)
227 return i;
228 return NULL;
229 }
230 }
231
232 int prioq_remove(Prioq *q, void *data, unsigned *idx) {
233 struct prioq_item *i;
234
235 if (!q)
236 return 0;
237
238 i = find_item(q, data, idx);
239 if (!i)
240 return 0;
241
242 remove_item(q, i);
243 return 1;
244 }
245
246 int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
247 struct prioq_item *i;
248 unsigned k;
249
250 assert(q);
251
252 i = find_item(q, data, idx);
253 if (!i)
254 return 0;
255
256 k = i - q->items;
257 k = shuffle_down(q, k);
258 shuffle_up(q, k);
259 return 1;
260 }
261
262 void *prioq_peek_by_index(Prioq *q, unsigned idx) {
263 if (!q)
264 return NULL;
265
266 if (idx >= q->n_items)
267 return NULL;
268
269 return q->items[idx].data;
270 }
271
272 void *prioq_pop(Prioq *q) {
273 void *data;
274
275 if (!q)
276 return NULL;
277
278 if (q->n_items <= 0)
279 return NULL;
280
281 data = q->items[0].data;
282 remove_item(q, q->items);
283 return data;
284 }
285
286 unsigned prioq_size(Prioq *q) {
287
288 if (!q)
289 return 0;
290
291 return q->n_items;
292 }
293
294 bool prioq_isempty(Prioq *q) {
295
296 if (!q)
297 return true;
298
299 return q->n_items <= 0;
300 }