2 * Copyright 1997-2003 John-Mark Gurney.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $Id: fib.c,v 1.2 2007-07-04 22:44:39 martin-s Exp $
36 #define swap(type, a, b) \
44 #define INT_BITS (sizeof(int) * 8)
46 ceillog2(unsigned int a)
71 * Private Heap Functions
74 fh_deleteel(struct fibheap *h, struct fibheap_el *x)
83 fh_replacedata(h, x, h->fh_neginf);
85 fh_replacekey(h, x, INT_MIN);
86 if (fh_extractminel(h) != x) {
88 * XXX - This should never happen as fh_replace should set it
99 fh_initheap(struct fibheap *new)
101 new->fh_cmp_fnct = NULL;
102 new->fh_neginf = NULL;
111 new->fh_ninserts = 0;
112 new->fh_nextracts = 0;
117 fh_destroyheap(struct fibheap *h)
119 h->fh_cmp_fnct = NULL;
121 if (h->fh_cons != NULL)
128 * Public Heap Functions
135 if ((n = malloc(sizeof *n)) == NULL)
149 if ((n = malloc(sizeof *n)) == NULL)
158 fh_setcmp(struct fibheap *h, voidcmp fnct)
162 oldfnct = h->fh_cmp_fnct;
163 h->fh_cmp_fnct = fnct;
169 fh_setneginf(struct fibheap *h, void *data)
180 fh_union(struct fibheap *ha, struct fibheap *hb)
182 struct fibheap_el *x;
184 if (ha->fh_root == NULL || hb->fh_root == NULL) {
185 /* either one or both are empty */
186 if (ha->fh_root == NULL) {
194 ha->fh_root->fhe_left->fhe_right = hb->fh_root;
195 hb->fh_root->fhe_left->fhe_right = ha->fh_root;
196 x = ha->fh_root->fhe_left;
197 ha->fh_root->fhe_left = hb->fh_root->fhe_left;
198 hb->fh_root->fhe_left = x;
199 ha->fh_n += hb->fh_n;
201 * we probably should also keep stats on number of unions
204 /* set fh_min if necessary */
205 if (fh_compare(ha, hb->fh_min, ha->fh_min) < 0)
206 ha->fh_min = hb->fh_min;
213 fh_deleteheap(struct fibheap *h)
216 * We could do this even faster by walking each binomial tree, but
217 * this is simpler to code.
219 while (h->fh_min != NULL)
220 fhe_destroy(fh_extractminel(h));
226 * Public Key Heap Functions
229 fh_insertkey(struct fibheap *h, int key, void *data)
231 struct fibheap_el *x;
233 if ((x = fhe_newelem()) == NULL)
236 /* just insert on root list, and make sure it's not the new min */
246 fh_minkey(struct fibheap *h)
248 if (h->fh_min == NULL)
250 return h->fh_min->fhe_key;
254 fh_replacekey(struct fibheap *h, struct fibheap_el *x, int key)
259 (void)fh_replacekeydata(h, x, key, x->fhe_data);
267 fh_replacekeydata(struct fibheap *h, struct fibheap_el *x, int key, void *data)
271 struct fibheap_el *y;
278 * we can increase a key by deleting and reinserting, that
279 * requires O(lgn) time.
281 if ((r = fh_comparedata(h, key, data, x)) > 0) {
282 printf("fh_comparedata r=%d key=%d data=%p\n", r, key, data);
283 /* XXX - bad code! */
298 /* because they are equal, we don't have to do anything */
304 if (h->fh_keys && okey == key)
307 if (y != NULL && fh_compare(h, x, y) <= 0) {
309 fh_cascading_cut(h, y);
313 * the = is so that the call from fh_delete will delete the proper
316 if (fh_compare(h, x, h->fh_min) <= 0)
323 * Public void * Heap Functions
326 * this will return these values:
327 * NULL failed for some reason
328 * ptr token to use for manipulation of data
331 fh_insert(struct fibheap *h, void *data)
333 struct fibheap_el *x;
335 if ((x = fhe_newelem()) == NULL)
338 /* just insert on root list, and make sure it's not the new min */
347 fh_min(struct fibheap *h)
349 if (h->fh_min == NULL)
351 return h->fh_min->fhe_data;
355 fh_extractmin(struct fibheap *h)
357 struct fibheap_el *z;
362 if (h->fh_min != NULL) {
363 z = fh_extractminel(h);
375 fh_replacedata(struct fibheap *h, struct fibheap_el *x, void *data)
377 return fh_replacekeydata(h, x, x->fhe_key, data);
381 fh_delete(struct fibheap *h, struct fibheap_el *x)
387 fh_replacedata(h, x, h->fh_neginf);
389 fh_replacekey(h, x, INT_MIN);
396 * Statistics Functions
400 fh_maxn(struct fibheap *h)
406 fh_ninserts(struct fibheap *h)
408 return h->fh_ninserts;
412 fh_nextracts(struct fibheap *h)
414 return h->fh_nextracts;
419 * begin of private element fuctions
421 static struct fibheap_el *
422 fh_extractminel(struct fibheap *h)
424 struct fibheap_el *ret;
425 struct fibheap_el *x, *y, *orig;
430 /* put all the children on the root list */
431 /* for true consistancy, we should use fhe_remove */
432 for(x = ret->fhe_child; x != orig && x != NULL;) {
437 fh_insertrootlist(h, x);
440 /* remove minimum from root list */
441 fh_removerootlist(h, ret);
444 /* if we aren't empty, consolidate the heap */
448 h->fh_min = ret->fhe_right;
460 fh_insertrootlist(struct fibheap *h, struct fibheap_el *x)
462 if (h->fh_root == NULL) {
469 fhe_insertafter(h->fh_root, x);
473 fh_removerootlist(struct fibheap *h, struct fibheap_el *x)
475 if (x->fhe_left == x)
478 h->fh_root = fhe_remove(x);
482 fh_consolidate(struct fibheap *h)
484 struct fibheap_el **a;
485 struct fibheap_el *w;
486 struct fibheap_el *y;
487 struct fibheap_el *x;
494 /* assign a the value of h->fh_cons so I don't have to rewrite code */
498 for (i = 0; i < D; i++)
501 while ((w = h->fh_root) != NULL) {
503 fh_removerootlist(h, w);
505 /* XXX - assert that d < D */
506 while(a[d] != NULL) {
508 if (fh_compare(h, x, y) > 0)
509 swap(struct fibheap_el *, x, y);
510 fh_heaplink(h, y, x);
517 for (i = 0; i < D; i++)
519 fh_insertrootlist(h, a[i]);
520 if (h->fh_min == NULL || fh_compare(h, a[i],
527 fh_heaplink(struct fibheap *h, struct fibheap_el *y, struct fibheap_el *x)
529 /* make y a child of x */
530 if (x->fhe_child == NULL)
533 fhe_insertbefore(x->fhe_child, y);
540 fh_cut(struct fibheap *h, struct fibheap_el *x, struct fibheap_el *y)
544 fh_insertrootlist(h, x);
550 fh_cascading_cut(struct fibheap *h, struct fibheap_el *y)
552 struct fibheap_el *z;
554 while ((z = y->fhe_p) != NULL) {
555 if (y->fhe_mark == 0) {
566 * begining of handling elements of fibheap
568 static struct fibheap_el *
571 struct fibheap_el *e;
573 if ((e = malloc(sizeof *e)) == NULL)
582 fhe_initelem(struct fibheap_el *e)
594 fhe_insertafter(struct fibheap_el *a, struct fibheap_el *b)
596 if (a == a->fhe_right) {
602 b->fhe_right = a->fhe_right;
603 a->fhe_right->fhe_left = b;
610 fhe_insertbefore(struct fibheap_el *a, struct fibheap_el *b)
612 fhe_insertafter(a->fhe_left, b);
615 static struct fibheap_el *
616 fhe_remove(struct fibheap_el *x)
618 struct fibheap_el *ret;
620 if (x == x->fhe_left)
625 /* fix the parent pointer */
626 if (x->fhe_p != NULL && x->fhe_p->fhe_child == x)
627 x->fhe_p->fhe_child = ret;
629 x->fhe_right->fhe_left = x->fhe_left;
630 x->fhe_left->fhe_right = x->fhe_right;
632 /* clear out hanging pointers */
641 fh_checkcons(struct fibheap *h)
645 /* make sure we have enough memory allocated to "reorganize" */
646 if (h->fh_Dl == -1 || h->fh_n > (1 << h->fh_Dl)) {
648 if ((h->fh_Dl = ceillog2(h->fh_n) + 1) < 8)
651 h->fh_cons = (struct fibheap_el **)realloc(h->fh_cons,
652 sizeof *h->fh_cons * (h->fh_Dl + 1));
653 if (h->fh_cons == NULL)
659 fh_compare(struct fibheap *h, struct fibheap_el *a, struct fibheap_el *b)
662 if (a->fhe_key < b->fhe_key)
664 if (a->fhe_key == b->fhe_key)
668 return h->fh_cmp_fnct(a->fhe_data, b->fhe_data);
672 fh_comparedata(struct fibheap *h, int key, void *data, struct fibheap_el *b)
679 return fh_compare(h, &a, b);
683 fh_insertel(struct fibheap *h, struct fibheap_el *x)
685 fh_insertrootlist(h, x);
687 if (h->fh_min == NULL || (h->fh_keys ? x->fhe_key < h->fh_min->fhe_key
688 : h->fh_cmp_fnct(x->fhe_data, h->fh_min->fhe_data) < 0))
694 if (h->fh_n > h->fh_maxn)
695 h->fh_maxn = h->fh_n;