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
24 AvahiPrioQueue* avahi_prio_queue_new(gint (*compare) (gconstpointer a, gconstpointer b)) {
28 q = g_new(AvahiPrioQueue, 1);
29 q->root = q->last = NULL;
35 void avahi_prio_queue_free(AvahiPrioQueue *q) {
39 avahi_prio_queue_remove(q, q->last);
41 g_assert(!q->n_nodes);
45 static AvahiPrioQueueNode* get_node_at_xy(AvahiPrioQueue *q, guint x, guint y) {
47 AvahiPrioQueueNode *n;
53 for (r = 0; r < y; r++) {
56 if ((x >> (y-r-1)) & 1)
68 static void exchange_nodes(AvahiPrioQueue *q, AvahiPrioQueueNode *a, AvahiPrioQueueNode *b) {
69 AvahiPrioQueueNode *l, *r, *p, *ap, *an, *bp, *bn;
77 t = a->x; a->x = b->x; b->x = t;
78 t = a->y; a->y = b->y; b->y = t;
81 /* B is parent of A */
86 if ((a->parent = p)) {
87 if (a->parent->left == b)
95 if ((b->left = a->left))
100 if ((a->right = b->right))
101 a->right->parent = a;
103 b->right->parent = b;
106 if ((b->right = a->right))
107 b->right->parent = b;
111 if ((a->left = b->left))
116 } else if (b->parent == a) {
117 /* A ist parent of B */
122 if ((b->parent = p)) {
123 if (b->parent->left == a)
126 b->parent->right = b;
131 if ((a->left = b->left))
136 if ((a->right = b->right))
137 a->right->parent = a;
139 b->right->parent = b;
141 if ((a->right = b->right))
142 a->right->parent = a;
146 if ((a->left = b->left))
152 AvahiPrioQueueNode *apl = NULL, *bpl = NULL;
163 if ((a->parent = bp)) {
171 if ((b->parent = ap)) {
183 if ((a->left = b->left))
189 if ((a->right = b->right))
190 a->right->parent = a;
193 b->right->parent = b;
197 ap = a->prev; an = a->next;
198 bp = b->prev; bn = b->next;
201 /* A is predecessor of B */
213 } else if (b->next == a) {
214 /* B is predecessor of A */
227 /* A is no neighbour of B */
247 /* Move a node to the correct position */
248 void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n) {
252 /* Move up until the position is OK */
253 while (n->parent && q->compare(n->parent->data, n->data) > 0)
254 exchange_nodes(q, n, n->parent);
256 /* Move down until the position is OK */
258 AvahiPrioQueueNode *min;
260 if (!(min = n->left)) {
266 if (n->right && q->compare(n->right->data, min->data) < 0)
269 /* min now contains the smaller one of our two children */
271 if (q->compare(n->data, min->data) <= 0)
275 exchange_nodes(q, n, min);
279 AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data) {
280 AvahiPrioQueueNode *n;
283 n = g_new(AvahiPrioQueueNode, 1);
289 g_assert(q->n_nodes);
294 if (n->x >= ((guint) 1 << n->y)) {
303 n->parent = get_node_at_xy(q, n->x/2, n->y-1);
306 n->parent->right = n;
311 g_assert(!q->n_nodes);
315 n->prev = n->parent = NULL;
318 n->next = n->left = n->right = NULL;
322 avahi_prio_queue_shuffle(q, n);
327 void avahi_prio_queue_remove(AvahiPrioQueue *q, AvahiPrioQueueNode *n) {
332 AvahiPrioQueueNode *replacement = q->last;
333 exchange_nodes(q, replacement, n);
334 avahi_prio_queue_remove(q, n);
335 avahi_prio_queue_shuffle(q, replacement);
339 g_assert(n == q->last);
347 n->prev->next = NULL;
350 g_assert(!n->parent);
354 if (n->parent->left == n) {
355 if (n->parent->right != NULL) {
361 g_assert(n->parent->right == NULL);
362 n->parent->left = NULL;
364 g_assert(n->parent->right == n);
365 g_assert(n->parent->left != NULL);
366 n->parent->right = NULL;
369 g_assert(q->root == n);
371 g_assert(q->n_nodes == 1);
377 g_assert(q->n_nodes > 0);