1 /* A Fibonacci heap datatype.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin (dan@cgsoftware.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 #include "libiberty.h"
38 #define FIBHEAPKEY_MIN LONG_MIN
40 static void fibheap_ins_root PARAMS ((fibheap_t, fibnode_t));
41 static void fibheap_rem_root PARAMS ((fibheap_t, fibnode_t));
42 static void fibheap_consolidate PARAMS ((fibheap_t));
43 static void fibheap_link PARAMS ((fibheap_t, fibnode_t, fibnode_t));
44 static void fibheap_cut PARAMS ((fibheap_t, fibnode_t, fibnode_t));
45 static void fibheap_cascading_cut PARAMS ((fibheap_t, fibnode_t));
46 static fibnode_t fibheap_extr_min_node PARAMS ((fibheap_t));
47 static int fibheap_compare PARAMS ((fibheap_t, fibnode_t, fibnode_t));
48 static int fibheap_comp_data PARAMS ((fibheap_t, fibheapkey_t, void *,
50 static fibnode_t fibnode_new PARAMS ((void));
51 static void fibnode_insert_after PARAMS ((fibnode_t, fibnode_t));
52 #define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b)
53 static fibnode_t fibnode_remove PARAMS ((fibnode_t));
56 /* Create a new fibonacci heap. */
60 return (fibheap_t) xcalloc (1, sizeof (struct fibheap));
63 /* Create a new fibonacci heap node. */
69 node = (fibnode_t) xcalloc (1, sizeof *node);
77 fibheap_compare (heap, a, b)
78 fibheap_t heap ATTRIBUTE_UNUSED;
90 fibheap_comp_data (heap, key, data, b)
101 return fibheap_compare (heap, &a, b);
104 /* Insert DATA, with priority KEY, into HEAP. */
106 fibheap_insert (heap, key, data)
113 /* Create the new node. */
114 node = fibnode_new ();
116 /* Set the node's data. */
120 /* Insert it into the root list. */
121 fibheap_ins_root (heap, node);
123 /* If their was no minimum, or this key is less than the min,
125 if (heap->min == NULL || node->key < heap->min->key)
133 /* Return the data of the minimum node (if we know it). */
138 /* If there is no min, we can't easily return it. */
139 if (heap->min == NULL)
141 return heap->min->data;
144 /* Return the key of the minimum node (if we know it). */
146 fibheap_min_key (heap)
149 /* If there is no min, we can't easily return it. */
150 if (heap->min == NULL)
152 return heap->min->key;
155 /* Union HEAPA and HEAPB into a new heap. */
157 fibheap_union (heapa, heapb)
161 fibnode_t a_root, b_root, temp;
163 /* If one of the heaps is empty, the union is just the other heap. */
164 if ((a_root = heapa->root) == NULL)
169 if ((b_root = heapb->root) == NULL)
175 /* Merge them to the next nodes on the opposite chain. */
176 a_root->left->right = b_root;
177 b_root->left->right = a_root;
179 a_root->left = b_root->left;
181 heapa->nodes += heapb->nodes;
183 /* And set the new minimum, if it's changed. */
184 if (fibheap_compare (heapa, heapb->min, heapa->min) < 0)
185 heapa->min = heapb->min;
191 /* Extract the data of the minimum node from HEAP. */
193 fibheap_extract_min (heap)
199 /* If we don't have a min set, it means we have no nodes. */
200 if (heap->min != NULL)
202 /* Otherwise, extract the min node, free the node, and return the
204 z = fibheap_extr_min_node (heap);
212 /* Replace both the KEY and the DATA associated with NODE. */
214 fibheap_replace_key_data (heap, node, key, data)
224 /* If we wanted to, we could actually do a real increase by redeleting and
225 inserting. However, this would require O (log n) time. So just bail out
227 if (fibheap_comp_data (heap, key, data, node) > 0)
239 /* These two compares are specifically <= 0 to make sure that in the case
240 of equality, a node we replaced the data on, becomes the new min. This
241 is needed so that delete's call to extractmin gets the right node. */
242 if (y != NULL && fibheap_compare (heap, node, y) <= 0)
244 fibheap_cut (heap, node, y);
245 fibheap_cascading_cut (heap, y);
248 if (fibheap_compare (heap, node, heap->min) <= 0)
254 /* Replace the DATA associated with NODE. */
256 fibheap_replace_data (heap, node, data)
261 return fibheap_replace_key_data (heap, node, node->key, data);
264 /* Replace the KEY associated with NODE. */
266 fibheap_replace_key (heap, node, key)
271 int okey = node->key;
272 fibheap_replace_key_data (heap, node, key, node->data);
276 /* Delete NODE from HEAP. */
278 fibheap_delete_node (heap, node)
282 void *ret = node->data;
284 /* To perform delete, we just make it the min key, and extract. */
285 fibheap_replace_key (heap, node, FIBHEAPKEY_MIN);
286 fibheap_extract_min (heap);
293 fibheap_delete (heap)
296 while (heap->min != NULL)
297 free (fibheap_extr_min_node (heap));
302 /* Determine if HEAP is empty. */
307 return heap->nodes == 0;
310 /* Extract the minimum node of the heap. */
312 fibheap_extr_min_node (heap)
315 fibnode_t ret = heap->min;
316 fibnode_t x, y, orig;
318 /* Attach the child list of the minimum node to the root list of the heap.
319 If there is no child list, we don't do squat. */
320 for (x = ret->child, orig = NULL; x != orig && x != NULL; x = y)
326 fibheap_ins_root (heap, x);
329 /* Remove the old root. */
330 fibheap_rem_root (heap, ret);
333 /* If we are left with no nodes, then the min is NULL. */
334 if (heap->nodes == 0)
338 /* Otherwise, consolidate to find new minimum, as well as do the reorg
339 work that needs to be done. */
340 heap->min = ret->right;
341 fibheap_consolidate (heap);
347 /* Insert NODE into the root list of HEAP. */
349 fibheap_ins_root (heap, node)
353 /* If the heap is currently empty, the new node becomes the singleton
354 circular root list. */
355 if (heap->root == NULL)
363 /* Otherwise, insert it in the circular root list between the root
364 and it's right node. */
365 fibnode_insert_after (heap->root, node);
368 /* Remove NODE from the rootlist of HEAP. */
370 fibheap_rem_root (heap, node)
374 if (node->left == node)
377 heap->root = fibnode_remove (node);
380 /* Consolidate the heap. */
382 fibheap_consolidate (heap)
385 fibnode_t a[1 + 8 * sizeof (long)];
393 D = 1 + 8 * sizeof (long);
395 memset (a, 0, sizeof (fibnode_t) * D);
397 while ((w = heap->root) != NULL)
400 fibheap_rem_root (heap, w);
405 if (fibheap_compare (heap, x, y) > 0)
412 fibheap_link (heap, y, x);
419 for (i = 0; i < D; i++)
422 fibheap_ins_root (heap, a[i]);
423 if (heap->min == NULL || fibheap_compare (heap, a[i], heap->min) < 0)
428 /* Make NODE a child of PARENT. */
430 fibheap_link (heap, node, parent)
431 fibheap_t heap ATTRIBUTE_UNUSED;
435 if (parent->child == NULL)
436 parent->child = node;
438 fibnode_insert_before (parent->child, node);
439 node->parent = parent;
444 /* Remove NODE from PARENT's child list. */
446 fibheap_cut (heap, node, parent)
451 fibnode_remove (node);
453 fibheap_ins_root (heap, node);
459 fibheap_cascading_cut (heap, y)
465 while ((z = y->parent) != NULL)
474 fibheap_cut (heap, y, z);
481 fibnode_insert_after (a, b)
502 fibnode_remove (node)
507 if (node == node->left)
512 if (node->parent != NULL && node->parent->child == node)
513 node->parent->child = ret;
515 node->right->left = node->left;
516 node->left->right = node->right;