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
28 AvahiPrioQueue* avahi_prio_queue_new(gint (*compare) (gconstpointer a, gconstpointer b)) {
32 q = g_new(AvahiPrioQueue, 1);
33 q->root = q->last = NULL;
39 void avahi_prio_queue_free(AvahiPrioQueue *q) {
43 avahi_prio_queue_remove(q, q->last);
45 g_assert(!q->n_nodes);
49 static AvahiPrioQueueNode* get_node_at_xy(AvahiPrioQueue *q, guint x, guint y) {
51 AvahiPrioQueueNode *n;
57 for (r = 0; r < y; r++) {
60 if ((x >> (y-r-1)) & 1)
72 static void exchange_nodes(AvahiPrioQueue *q, AvahiPrioQueueNode *a, AvahiPrioQueueNode *b) {
73 AvahiPrioQueueNode *l, *r, *p, *ap, *an, *bp, *bn;
81 t = a->x; a->x = b->x; b->x = t;
82 t = a->y; a->y = b->y; b->y = t;
85 /* B is parent of A */
90 if ((a->parent = p)) {
91 if (a->parent->left == b)
99 if ((b->left = a->left))
104 if ((a->right = b->right))
105 a->right->parent = a;
107 b->right->parent = b;
110 if ((b->right = a->right))
111 b->right->parent = b;
115 if ((a->left = b->left))
120 } else if (b->parent == a) {
121 /* A ist parent of B */
126 if ((b->parent = p)) {
127 if (b->parent->left == a)
130 b->parent->right = b;
135 if ((a->left = b->left))
140 if ((a->right = b->right))
141 a->right->parent = a;
143 b->right->parent = b;
145 if ((a->right = b->right))
146 a->right->parent = a;
150 if ((a->left = b->left))
156 AvahiPrioQueueNode *apl = NULL, *bpl = NULL;
167 if ((a->parent = bp)) {
175 if ((b->parent = ap)) {
187 if ((a->left = b->left))
193 if ((a->right = b->right))
194 a->right->parent = a;
197 b->right->parent = b;
201 ap = a->prev; an = a->next;
202 bp = b->prev; bn = b->next;
205 /* A is predecessor of B */
217 } else if (b->next == a) {
218 /* B is predecessor of A */
231 /* A is no neighbour of B */
251 /* Move a node to the correct position */
252 void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n) {
256 /* Move up until the position is OK */
257 while (n->parent && q->compare(n->parent->data, n->data) > 0)
258 exchange_nodes(q, n, n->parent);
260 /* Move down until the position is OK */
262 AvahiPrioQueueNode *min;
264 if (!(min = n->left)) {
270 if (n->right && q->compare(n->right->data, min->data) < 0)
273 /* min now contains the smaller one of our two children */
275 if (q->compare(n->data, min->data) <= 0)
279 exchange_nodes(q, n, min);
283 AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data) {
284 AvahiPrioQueueNode *n;
287 n = g_new(AvahiPrioQueueNode, 1);
293 g_assert(q->n_nodes);
298 if (n->x >= ((guint) 1 << n->y)) {
307 n->parent = get_node_at_xy(q, n->x/2, n->y-1);
310 n->parent->right = n;
315 g_assert(!q->n_nodes);
319 n->prev = n->parent = NULL;
322 n->next = n->left = n->right = NULL;
326 avahi_prio_queue_shuffle(q, n);
331 void avahi_prio_queue_remove(AvahiPrioQueue *q, AvahiPrioQueueNode *n) {
336 AvahiPrioQueueNode *replacement = q->last;
337 exchange_nodes(q, replacement, n);
338 avahi_prio_queue_remove(q, n);
339 avahi_prio_queue_shuffle(q, replacement);
343 g_assert(n == q->last);
351 n->prev->next = NULL;
354 g_assert(!n->parent);
358 if (n->parent->left == n) {
359 g_assert(n->parent->right == NULL);
360 n->parent->left = NULL;
362 g_assert(n->parent->right == n);
363 g_assert(n->parent->left != NULL);
364 n->parent->right = NULL;
367 g_assert(q->root == n);
369 g_assert(q->n_nodes == 1);
375 g_assert(q->n_nodes > 0);