1 /* A Fibonacci heap datatype.
2 Copyright (C) 1998-2022 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, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
34 #include "libiberty.h"
38 #define FIBHEAPKEY_MIN LONG_MIN
40 static void fibheap_ins_root (fibheap_t
, fibnode_t
);
41 static void fibheap_rem_root (fibheap_t
, fibnode_t
);
42 static void fibheap_consolidate (fibheap_t
);
43 static void fibheap_link (fibheap_t
, fibnode_t
, fibnode_t
);
44 static void fibheap_cut (fibheap_t
, fibnode_t
, fibnode_t
);
45 static void fibheap_cascading_cut (fibheap_t
, fibnode_t
);
46 static fibnode_t
fibheap_extr_min_node (fibheap_t
);
47 static int fibheap_compare (fibheap_t
, fibnode_t
, fibnode_t
);
48 static int fibheap_comp_data (fibheap_t
, fibheapkey_t
, void *, fibnode_t
);
49 static fibnode_t
fibnode_new (void);
50 static void fibnode_insert_after (fibnode_t
, fibnode_t
);
51 #define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b)
52 static fibnode_t
fibnode_remove (fibnode_t
);
55 /* Create a new fibonacci heap. */
59 return (fibheap_t
) xcalloc (1, sizeof (struct fibheap
));
62 /* Create a new fibonacci heap node. */
68 node
= (fibnode_t
) xcalloc (1, sizeof *node
);
76 fibheap_compare (fibheap_t heap ATTRIBUTE_UNUSED
, fibnode_t a
, fibnode_t b
)
86 fibheap_comp_data (fibheap_t heap
, fibheapkey_t key
, void *data
, fibnode_t b
)
93 return fibheap_compare (heap
, &a
, b
);
96 /* Insert DATA, with priority KEY, into HEAP. */
98 fibheap_insert (fibheap_t heap
, fibheapkey_t key
, void *data
)
102 /* Create the new node. */
103 node
= fibnode_new ();
105 /* Set the node's data. */
109 /* Insert it into the root list. */
110 fibheap_ins_root (heap
, node
);
112 /* If their was no minimum, or this key is less than the min,
114 if (heap
->min
== NULL
|| node
->key
< heap
->min
->key
)
122 /* Return the data of the minimum node (if we know it). */
124 fibheap_min (fibheap_t heap
)
126 /* If there is no min, we can't easily return it. */
127 if (heap
->min
== NULL
)
129 return heap
->min
->data
;
132 /* Return the key of the minimum node (if we know it). */
134 fibheap_min_key (fibheap_t heap
)
136 /* If there is no min, we can't easily return it. */
137 if (heap
->min
== NULL
)
139 return heap
->min
->key
;
142 /* Union HEAPA and HEAPB into a new heap. */
144 fibheap_union (fibheap_t heapa
, fibheap_t heapb
)
146 fibnode_t a_root
, b_root
, temp
;
148 /* If one of the heaps is empty, the union is just the other heap. */
149 if ((a_root
= heapa
->root
) == NULL
)
154 if ((b_root
= heapb
->root
) == NULL
)
160 /* Merge them to the next nodes on the opposite chain. */
161 a_root
->left
->right
= b_root
;
162 b_root
->left
->right
= a_root
;
164 a_root
->left
= b_root
->left
;
166 heapa
->nodes
+= heapb
->nodes
;
168 /* And set the new minimum, if it's changed. */
169 if (fibheap_compare (heapa
, heapb
->min
, heapa
->min
) < 0)
170 heapa
->min
= heapb
->min
;
176 /* Extract the data of the minimum node from HEAP. */
178 fibheap_extract_min (fibheap_t heap
)
183 /* If we don't have a min set, it means we have no nodes. */
184 if (heap
->min
!= NULL
)
186 /* Otherwise, extract the min node, free the node, and return the
188 z
= fibheap_extr_min_node (heap
);
196 /* Replace both the KEY and the DATA associated with NODE. */
198 fibheap_replace_key_data (fibheap_t heap
, fibnode_t node
,
199 fibheapkey_t key
, void *data
)
205 /* If we wanted to, we could actually do a real increase by redeleting and
206 inserting. However, this would require O (log n) time. So just bail out
208 if (fibheap_comp_data (heap
, key
, data
, node
) > 0)
217 /* Short-circuit if the key is the same, as we then don't have to
218 do anything. Except if we're trying to force the new node to
219 be the new minimum for delete. */
220 if (okey
== key
&& okey
!= FIBHEAPKEY_MIN
)
223 /* These two compares are specifically <= 0 to make sure that in the case
224 of equality, a node we replaced the data on, becomes the new min. This
225 is needed so that delete's call to extractmin gets the right node. */
226 if (y
!= NULL
&& fibheap_compare (heap
, node
, y
) <= 0)
228 fibheap_cut (heap
, node
, y
);
229 fibheap_cascading_cut (heap
, y
);
232 if (fibheap_compare (heap
, node
, heap
->min
) <= 0)
238 /* Replace the DATA associated with NODE. */
240 fibheap_replace_data (fibheap_t heap
, fibnode_t node
, void *data
)
242 return fibheap_replace_key_data (heap
, node
, node
->key
, data
);
245 /* Replace the KEY associated with NODE. */
247 fibheap_replace_key (fibheap_t heap
, fibnode_t node
, fibheapkey_t key
)
249 int okey
= node
->key
;
250 fibheap_replace_key_data (heap
, node
, key
, node
->data
);
254 /* Delete NODE from HEAP. */
256 fibheap_delete_node (fibheap_t heap
, fibnode_t node
)
258 void *ret
= node
->data
;
260 /* To perform delete, we just make it the min key, and extract. */
261 fibheap_replace_key (heap
, node
, FIBHEAPKEY_MIN
);
262 if (node
!= heap
->min
)
264 fprintf (stderr
, "Can't force minimum on fibheap.\n");
267 fibheap_extract_min (heap
);
274 fibheap_delete (fibheap_t heap
)
276 while (heap
->min
!= NULL
)
277 free (fibheap_extr_min_node (heap
));
282 /* Determine if HEAP is empty. */
284 fibheap_empty (fibheap_t heap
)
286 return heap
->nodes
== 0;
289 /* Extract the minimum node of the heap. */
291 fibheap_extr_min_node (fibheap_t heap
)
293 fibnode_t ret
= heap
->min
;
294 fibnode_t x
, y
, orig
;
296 /* Attach the child list of the minimum node to the root list of the heap.
297 If there is no child list, we don't do squat. */
298 for (x
= ret
->child
, orig
= NULL
; x
!= orig
&& x
!= NULL
; x
= y
)
304 fibheap_ins_root (heap
, x
);
307 /* Remove the old root. */
308 fibheap_rem_root (heap
, ret
);
311 /* If we are left with no nodes, then the min is NULL. */
312 if (heap
->nodes
== 0)
316 /* Otherwise, consolidate to find new minimum, as well as do the reorg
317 work that needs to be done. */
318 heap
->min
= ret
->right
;
319 fibheap_consolidate (heap
);
325 /* Insert NODE into the root list of HEAP. */
327 fibheap_ins_root (fibheap_t heap
, fibnode_t node
)
329 /* If the heap is currently empty, the new node becomes the singleton
330 circular root list. */
331 if (heap
->root
== NULL
)
339 /* Otherwise, insert it in the circular root list between the root
340 and it's right node. */
341 fibnode_insert_after (heap
->root
, node
);
344 /* Remove NODE from the rootlist of HEAP. */
346 fibheap_rem_root (fibheap_t heap
, fibnode_t node
)
348 if (node
->left
== node
)
351 heap
->root
= fibnode_remove (node
);
354 /* Consolidate the heap. */
356 fibheap_consolidate (fibheap_t heap
)
358 fibnode_t a
[1 + 8 * sizeof (long)];
366 D
= 1 + 8 * sizeof (long);
368 memset (a
, 0, sizeof (fibnode_t
) * D
);
370 while ((w
= heap
->root
) != NULL
)
373 fibheap_rem_root (heap
, w
);
378 if (fibheap_compare (heap
, x
, y
) > 0)
385 fibheap_link (heap
, y
, x
);
392 for (i
= 0; i
< D
; i
++)
395 fibheap_ins_root (heap
, a
[i
]);
396 if (heap
->min
== NULL
|| fibheap_compare (heap
, a
[i
], heap
->min
) < 0)
401 /* Make NODE a child of PARENT. */
403 fibheap_link (fibheap_t heap ATTRIBUTE_UNUSED
,
404 fibnode_t node
, fibnode_t parent
)
406 if (parent
->child
== NULL
)
407 parent
->child
= node
;
409 fibnode_insert_before (parent
->child
, node
);
410 node
->parent
= parent
;
415 /* Remove NODE from PARENT's child list. */
417 fibheap_cut (fibheap_t heap
, fibnode_t node
, fibnode_t parent
)
419 fibnode_remove (node
);
421 fibheap_ins_root (heap
, node
);
427 fibheap_cascading_cut (fibheap_t heap
, fibnode_t y
)
431 while ((z
= y
->parent
) != NULL
)
440 fibheap_cut (heap
, y
, z
);
447 fibnode_insert_after (fibnode_t a
, fibnode_t b
)
466 fibnode_remove (fibnode_t node
)
470 if (node
== node
->left
)
475 if (node
->parent
!= NULL
&& node
->parent
->child
== node
)
476 node
->parent
->child
= ret
;
478 node
->right
->left
= node
->left
;
479 node
->left
->right
= node
->right
;