X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=libiberty%2Ffibheap.c;h=a37ee4ef270a55d9df7d47c5722272a9b5f78cab;hb=c59e4eb006120b8dbd6b2d8b38f61601d1699f64;hp=3c8b34722f604266f8268da7ea51d091e2e10f1e;hpb=522a00cddd24663f132a5ef8807da1f9b729f374;p=pf3gnuchains%2Fgcc-fork.git diff --git a/libiberty/fibheap.c b/libiberty/fibheap.c index 3c8b34722f6..a37ee4ef270 100644 --- a/libiberty/fibheap.c +++ b/libiberty/fibheap.c @@ -1,4 +1,3 @@ - /* A Fibonacci heap datatype. Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc. Contributed by Daniel Berlin (dan@cgsoftware.com). @@ -17,67 +16,92 @@ General Public License for more details. You should have received a copy of the GNU General Public License along with GNU CC; see the file COPYING. If not, write to -the Free Software Foundation, 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ +the Free Software Foundation, 51 Franklin Street - Fifth Floor, +Boston, MA 02110-1301, USA. */ -/* Fibonacci heaps */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#ifdef HAVE_LIMITS_H #include +#endif +#ifdef HAVE_STDLIB_H #include +#endif +#ifdef HAVE_STRING_H +#include +#endif #include "libiberty.h" #include "fibheap.h" -static void fibheap_init PARAMS ((fibheap_t)); -static void fibheap_ins_root PARAMS ((fibheap_t, fibnode_t)); -static void fibheap_rem_root PARAMS ((fibheap_t, fibnode_t)); -static void fibheap_consolidate PARAMS ((fibheap_t)); -static void fibheap_link PARAMS ((fibheap_t, fibnode_t, fibnode_t)); -static void fibheap_cut PARAMS ((fibheap_t, fibnode_t, fibnode_t)); -static void fibheap_cascading_cut PARAMS ((fibheap_t, fibnode_t)); -static fibnode_t fibheap_extr_min_node PARAMS ((fibheap_t)); -static int fibheap_compare PARAMS ((fibheap_t, fibnode_t, fibnode_t)); -static int fibheap_comp_data PARAMS ((fibheap_t, fibheapkey_t, void *, fibnode_t)); -static fibnode_t fibnode_new PARAMS ((void)); -static void fibnode_init PARAMS ((fibnode_t)); -static void fibnode_insert_after PARAMS ((fibnode_t, fibnode_t)); +#define FIBHEAPKEY_MIN LONG_MIN + +static void fibheap_ins_root (fibheap_t, fibnode_t); +static void fibheap_rem_root (fibheap_t, fibnode_t); +static void fibheap_consolidate (fibheap_t); +static void fibheap_link (fibheap_t, fibnode_t, fibnode_t); +static void fibheap_cut (fibheap_t, fibnode_t, fibnode_t); +static void fibheap_cascading_cut (fibheap_t, fibnode_t); +static fibnode_t fibheap_extr_min_node (fibheap_t); +static int fibheap_compare (fibheap_t, fibnode_t, fibnode_t); +static int fibheap_comp_data (fibheap_t, fibheapkey_t, void *, fibnode_t); +static fibnode_t fibnode_new (void); +static void fibnode_insert_after (fibnode_t, fibnode_t); #define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b) -static fibnode_t fibnode_remove PARAMS ((fibnode_t)); +static fibnode_t fibnode_remove (fibnode_t); + /* Create a new fibonacci heap. */ fibheap_t -fibheap_new () +fibheap_new (void) { - fibheap_t result; + return (fibheap_t) xcalloc (1, sizeof (struct fibheap)); +} - if ((result = xmalloc (sizeof (*result))) == NULL) - return NULL; +/* Create a new fibonacci heap node. */ +static fibnode_t +fibnode_new (void) +{ + fibnode_t node; - fibheap_init (result); + node = (fibnode_t) xcalloc (1, sizeof *node); + node->left = node; + node->right = node; - return result; + return node; } -/* Initialize the passed in fibonacci heap. */ -static void -fibheap_init (heap) - fibheap_t heap; +static inline int +fibheap_compare (fibheap_t heap ATTRIBUTE_UNUSED, fibnode_t a, fibnode_t b) { - heap->nodes = 0; - heap->min = NULL; - heap->root = NULL; + if (a->key < b->key) + return -1; + if (a->key > b->key) + return 1; + return 0; +} + +static inline int +fibheap_comp_data (fibheap_t heap, fibheapkey_t key, void *data, fibnode_t b) +{ + struct fibnode a; + + a.key = key; + a.data = data; + + return fibheap_compare (heap, &a, b); } /* Insert DATA, with priority KEY, into HEAP. */ fibnode_t -fibheap_insert (heap, key, data) - fibheap_t heap; - fibheapkey_t key; - void *data; +fibheap_insert (fibheap_t heap, fibheapkey_t key, void *data) { fibnode_t node; - /* Create the new node, if we fail, return NULL. */ - if ((node = fibnode_new ()) == NULL) - return NULL; + + /* Create the new node. */ + node = fibnode_new (); + /* Set the node's data. */ node->data = data; node->key = key; @@ -85,8 +109,8 @@ fibheap_insert (heap, key, data) /* Insert it into the root list. */ fibheap_ins_root (heap, node); - /* If their was no minimum, or this key is less than the min, it's the new - min. */ + /* If their was no minimum, or this key is less than the min, + it's the new min. */ if (heap->min == NULL || node->key < heap->min->key) heap->min = node; @@ -97,8 +121,7 @@ fibheap_insert (heap, key, data) /* Return the data of the minimum node (if we know it). */ void * -fibheap_min (heap) - fibheap_t heap; +fibheap_min (fibheap_t heap) { /* If there is no min, we can't easily return it. */ if (heap->min == NULL) @@ -108,8 +131,7 @@ fibheap_min (heap) /* Return the key of the minimum node (if we know it). */ fibheapkey_t -fibheap_min_key (heap) - fibheap_t heap; +fibheap_min_key (fibheap_t heap) { /* If there is no min, we can't easily return it. */ if (heap->min == NULL) @@ -119,32 +141,28 @@ fibheap_min_key (heap) /* Union HEAPA and HEAPB into a new heap. */ fibheap_t -fibheap_union (heapa, heapb) - fibheap_t heapa; - fibheap_t heapb; +fibheap_union (fibheap_t heapa, fibheap_t heapb) { - fibnode_t temp; + fibnode_t a_root, b_root, temp; /* If one of the heaps is empty, the union is just the other heap. */ - if (heapa->root == NULL || heapb->root == NULL) + if ((a_root = heapa->root) == NULL) { - if (heapa->root == NULL) - { - free (heapa); - return heapb; - } - else - { - free (heapb); - return heapa; - } + free (heapa); + return heapb; } + if ((b_root = heapb->root) == NULL) + { + free (heapb); + return heapa; + } + /* Merge them to the next nodes on the opposite chain. */ - heapa->root->left->right = heapb->root; - heapb->root->left->right = heapa->root; - temp = heapa->root->left; - heapa->root->left = heapb->root->left; - heapb->root->left = temp; + a_root->left->right = b_root; + b_root->left->right = a_root; + temp = a_root->left; + a_root->left = b_root->left; + b_root->left = temp; heapa->nodes += heapb->nodes; /* And set the new minimum, if it's changed. */ @@ -157,13 +175,11 @@ fibheap_union (heapa, heapb) /* Extract the data of the minimum node from HEAP. */ void * -fibheap_extract_min (heap) - fibheap_t heap; +fibheap_extract_min (fibheap_t heap) { fibnode_t z; - void *ret; + void *ret = NULL; - ret = NULL; /* If we don't have a min set, it means we have no nodes. */ if (heap->min != NULL) { @@ -177,41 +193,13 @@ fibheap_extract_min (heap) return ret; } -/* Replace the DATA associated with NODE. */ -void * -fibheap_replace_data (heap, node, data) - fibheap_t heap; - fibnode_t node; - void *data; -{ - return fibheap_replace_key_data (heap, node, node->key, data); -} - -/* Replace the KEY associated with NODE. */ -fibheapkey_t -fibheap_replace_key (heap, node, key) - fibheap_t heap; - fibnode_t node; - fibheapkey_t key; -{ - int ret; - - ret = node->key; - (void) fibheap_replace_key_data (heap, node, key, node->data); - - return ret; -} - /* Replace both the KEY and the DATA associated with NODE. */ void * -fibheap_replace_key_data (heap, node, key, data) - fibheap_t heap; - fibnode_t node; - fibheapkey_t key; - void *data; +fibheap_replace_key_data (fibheap_t heap, fibnode_t node, + fibheapkey_t key, void *data) { void *odata; - int okey; + fibheapkey_t okey; fibnode_t y; /* If we wanted to, we could actually do a real increase by redeleting and @@ -226,7 +214,10 @@ fibheap_replace_key_data (heap, node, key, data) node->key = key; y = node->parent; - if (okey == key) + /* Short-circuit if the key is the same, as we then don't have to + do anything. Except if we're trying to force the new node to + be the new minimum for delete. */ + if (okey == key && okey != FIBHEAPKEY_MIN) return odata; /* These two compares are specifically <= 0 to make sure that in the case @@ -244,25 +235,43 @@ fibheap_replace_key_data (heap, node, key, data) return odata; } +/* Replace the DATA associated with NODE. */ +void * +fibheap_replace_data (fibheap_t heap, fibnode_t node, void *data) +{ + return fibheap_replace_key_data (heap, node, node->key, data); +} + +/* Replace the KEY associated with NODE. */ +fibheapkey_t +fibheap_replace_key (fibheap_t heap, fibnode_t node, fibheapkey_t key) +{ + int okey = node->key; + fibheap_replace_key_data (heap, node, key, node->data); + return okey; +} + /* Delete NODE from HEAP. */ void * -fibheap_delete_node (heap, node) - fibheap_t heap; - fibnode_t node; +fibheap_delete_node (fibheap_t heap, fibnode_t node) { - void *k; + void *ret = node->data; + /* To perform delete, we just make it the min key, and extract. */ - k = node->data; - fibheap_replace_key (heap, node, LONG_MIN); + fibheap_replace_key (heap, node, FIBHEAPKEY_MIN); + if (node != heap->min) + { + fprintf (stderr, "Can't force minimum on fibheap.\n"); + abort (); + } fibheap_extract_min (heap); - return k; + return ret; } /* Delete HEAP. */ void -fibheap_delete (heap) - fibheap_t heap; +fibheap_delete (fibheap_t heap) { while (heap->min != NULL) free (fibheap_extr_min_node (heap)); @@ -272,38 +281,33 @@ fibheap_delete (heap) /* Determine if HEAP is empty. */ int -fibheap_empty (heap) - fibheap_t heap; +fibheap_empty (fibheap_t heap) { return heap->nodes == 0; } - /* Extract the minimum node of the heap. */ static fibnode_t -fibheap_extr_min_node (heap) - fibheap_t heap; +fibheap_extr_min_node (fibheap_t heap) { - fibnode_t ret; + fibnode_t ret = heap->min; fibnode_t x, y, orig; - ret = heap->min; - - orig = NULL; /* Attach the child list of the minimum node to the root list of the heap. If there is no child list, we don't do squat. */ - for (x = ret->child; x != orig && x != NULL;) + for (x = ret->child, orig = NULL; x != orig && x != NULL; x = y) { if (orig == NULL) orig = x; y = x->right; x->parent = NULL; fibheap_ins_root (heap, x); - x = y; } + /* Remove the old root. */ fibheap_rem_root (heap, ret); heap->nodes--; + /* If we are left with no nodes, then the min is NULL. */ if (heap->nodes == 0) heap->min = NULL; @@ -320,9 +324,7 @@ fibheap_extr_min_node (heap) /* Insert NODE into the root list of HEAP. */ static void -fibheap_ins_root (heap, node) - fibheap_t heap; - fibnode_t node; +fibheap_ins_root (fibheap_t heap, fibnode_t node) { /* If the heap is currently empty, the new node becomes the singleton circular root list. */ @@ -333,16 +335,15 @@ fibheap_ins_root (heap, node) node->right = node; return; } - /* Otherwise, insert it in the circular root list between the root and it's - right node. */ + + /* Otherwise, insert it in the circular root list between the root + and it's right node. */ fibnode_insert_after (heap->root, node); } /* Remove NODE from the rootlist of HEAP. */ static void -fibheap_rem_root (heap, node) - fibheap_t heap; - fibnode_t node; +fibheap_rem_root (fibheap_t heap, fibnode_t node) { if (node->left == node) heap->root = NULL; @@ -352,8 +353,7 @@ fibheap_rem_root (heap, node) /* Consolidate the heap. */ static void -fibheap_consolidate (heap) - fibheap_t heap; +fibheap_consolidate (fibheap_t heap) { fibnode_t a[1 + 8 * sizeof (long)]; fibnode_t w; @@ -400,10 +400,8 @@ fibheap_consolidate (heap) /* Make NODE a child of PARENT. */ static void -fibheap_link (heap, node, parent) - fibheap_t heap ATTRIBUTE_UNUSED; - fibnode_t node; - fibnode_t parent; +fibheap_link (fibheap_t heap ATTRIBUTE_UNUSED, + fibnode_t node, fibnode_t parent) { if (parent->child == NULL) parent->child = node; @@ -416,10 +414,7 @@ fibheap_link (heap, node, parent) /* Remove NODE from PARENT's child list. */ static void -fibheap_cut (heap, node, parent) - fibheap_t heap; - fibnode_t node; - fibnode_t parent; +fibheap_cut (fibheap_t heap, fibnode_t node, fibnode_t parent) { fibnode_remove (node); parent->degree--; @@ -429,9 +424,7 @@ fibheap_cut (heap, node, parent) } static void -fibheap_cascading_cut (heap, y) - fibheap_t heap; - fibnode_t y; +fibheap_cascading_cut (fibheap_t heap, fibnode_t y) { fibnode_t z; @@ -450,37 +443,8 @@ fibheap_cascading_cut (heap, y) } } - -static fibnode_t -fibnode_new () -{ - fibnode_t e; - - if ((e = xmalloc (sizeof *e)) == NULL) - return NULL; - - fibnode_init (e); - - return e; -} - static void -fibnode_init (node) - fibnode_t node; -{ - node->degree = 0; - node->mark = 0; - node->parent = NULL; - node->child = NULL; - node->left = node; - node->right = node; - node->data = NULL; -} - -static void -fibnode_insert_after (a, b) - fibnode_t a; - fibnode_t b; +fibnode_insert_after (fibnode_t a, fibnode_t b) { if (a == a->right) { @@ -498,10 +462,8 @@ fibnode_insert_after (a, b) } } - static fibnode_t -fibnode_remove (node) - fibnode_t node; +fibnode_remove (fibnode_t node) { fibnode_t ret; @@ -522,31 +484,3 @@ fibnode_remove (node) return ret; } - -static int -fibheap_compare (heap, a, b) - fibheap_t heap ATTRIBUTE_UNUSED; - fibnode_t a; - fibnode_t b; -{ - if (a->key < b->key) - return -1; - if (a->key > b->key) - return 1; - return 0; -} - -static int -fibheap_comp_data (heap, key, data, b) - fibheap_t heap; - fibheapkey_t key; - void *data; - fibnode_t b; -{ - struct fibnode a; - - a.key = key; - a.data = data; - - return fibheap_compare (heap, &a, b); -}