4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <avahi-common/malloc.h>
33 AvahiPrioQueue* avahi_prio_queue_new(AvahiPQCompareFunc compare) {
37 if (!(q = avahi_new(AvahiPrioQueue, 1)))
38 return NULL; /* OOM */
40 q->root = q->last = NULL;
47 void avahi_prio_queue_free(AvahiPrioQueue *q) {
51 avahi_prio_queue_remove(q, q->last);
57 static AvahiPrioQueueNode* get_node_at_xy(AvahiPrioQueue *q, unsigned x, unsigned y) {
59 AvahiPrioQueueNode *n;
65 for (r = 0; r < y; r++) {
68 if ((x >> (y-r-1)) & 1)
80 static void exchange_nodes(AvahiPrioQueue *q, AvahiPrioQueueNode *a, AvahiPrioQueueNode *b) {
81 AvahiPrioQueueNode *l, *r, *p, *ap, *an, *bp, *bn;
89 t = a->x; a->x = b->x; b->x = t;
90 t = a->y; a->y = b->y; b->y = t;
93 /* B is parent of A */
98 if ((a->parent = p)) {
99 if (a->parent->left == b)
102 a->parent->right = a;
107 if ((b->left = a->left))
112 if ((a->right = b->right))
113 a->right->parent = a;
115 b->right->parent = b;
118 if ((b->right = a->right))
119 b->right->parent = b;
123 if ((a->left = b->left))
128 } else if (b->parent == a) {
129 /* A ist parent of B */
134 if ((b->parent = p)) {
135 if (b->parent->left == a)
138 b->parent->right = b;
143 if ((a->left = b->left))
148 if ((a->right = b->right))
149 a->right->parent = a;
151 b->right->parent = b;
153 if ((a->right = b->right))
154 a->right->parent = a;
158 if ((a->left = b->left))
164 AvahiPrioQueueNode *apl = NULL, *bpl = NULL;
175 if ((a->parent = bp)) {
183 if ((b->parent = ap)) {
195 if ((a->left = b->left))
201 if ((a->right = b->right))
202 a->right->parent = a;
205 b->right->parent = b;
209 ap = a->prev; an = a->next;
210 bp = b->prev; bn = b->next;
213 /* A is predecessor of B */
225 } else if (b->next == a) {
226 /* B is predecessor of A */
239 /* A is no neighbour of B */
259 /* Move a node to the correct position */
260 void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n) {
263 assert(n->queue == q);
265 /* Move up until the position is OK */
266 while (n->parent && q->compare(n->parent->data, n->data) > 0)
267 exchange_nodes(q, n, n->parent);
269 /* Move down until the position is OK */
271 AvahiPrioQueueNode *min;
273 if (!(min = n->left)) {
279 if (n->right && q->compare(n->right->data, min->data) < 0)
282 /* min now contains the smaller one of our two children */
284 if (q->compare(n->data, min->data) <= 0)
288 exchange_nodes(q, n, min);
292 AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, void* data) {
293 AvahiPrioQueueNode *n;
296 if (!(n = avahi_new(AvahiPrioQueueNode, 1)))
297 return NULL; /* OOM */
309 if (n->x >= ((unsigned) 1 << n->y)) {
318 n->parent = get_node_at_xy(q, n->x/2, n->y-1);
321 n->parent->right = n;
330 n->prev = n->parent = NULL;
333 n->next = n->left = n->right = NULL;
337 avahi_prio_queue_shuffle(q, n);
342 void avahi_prio_queue_remove(AvahiPrioQueue *q, AvahiPrioQueueNode *n) {
345 assert(q == n->queue);
348 AvahiPrioQueueNode *replacement = q->last;
349 exchange_nodes(q, replacement, n);
350 avahi_prio_queue_remove(q, n);
351 avahi_prio_queue_shuffle(q, replacement);
355 assert(n == q->last);
363 n->prev->next = NULL;
370 if (n->parent->left == n) {
371 assert(n->parent->right == NULL);
372 n->parent->left = NULL;
374 assert(n->parent->right == n);
375 assert(n->parent->left != NULL);
376 n->parent->right = NULL;
379 assert(q->root == n);
381 assert(q->n_nodes == 1);
387 assert(q->n_nodes > 0);