gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / libiberty / fibheap.c
CommitLineData
8e777d6a 1/* A Fibonacci heap datatype.
533da483 2 Copyright (C) 1998-2020 Free Software Foundation, Inc.
8e777d6a
DD
3 Contributed by Daniel Berlin (dan@cgsoftware.com).
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
979c05d3
NC
19the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20Boston, MA 02110-1301, USA. */
8e777d6a 21
f01b59ed
DD
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#ifdef HAVE_LIMITS_H
8e777d6a 26#include <limits.h>
f01b59ed
DD
27#endif
28#ifdef HAVE_STDLIB_H
8e777d6a 29#include <stdlib.h>
f01b59ed
DD
30#endif
31#ifdef HAVE_STRING_H
32#include <string.h>
33#endif
8e777d6a
DD
34#include "libiberty.h"
35#include "fibheap.h"
36
37
f01b59ed
DD
38#define FIBHEAPKEY_MIN LONG_MIN
39
49b1fae4
DD
40static void fibheap_ins_root (fibheap_t, fibnode_t);
41static void fibheap_rem_root (fibheap_t, fibnode_t);
42static void fibheap_consolidate (fibheap_t);
43static void fibheap_link (fibheap_t, fibnode_t, fibnode_t);
44static void fibheap_cut (fibheap_t, fibnode_t, fibnode_t);
45static void fibheap_cascading_cut (fibheap_t, fibnode_t);
46static fibnode_t fibheap_extr_min_node (fibheap_t);
47static int fibheap_compare (fibheap_t, fibnode_t, fibnode_t);
48static int fibheap_comp_data (fibheap_t, fibheapkey_t, void *, fibnode_t);
49static fibnode_t fibnode_new (void);
50static void fibnode_insert_after (fibnode_t, fibnode_t);
8e777d6a 51#define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b)
49b1fae4 52static fibnode_t fibnode_remove (fibnode_t);
8e777d6a 53
f01b59ed 54\f
8e777d6a
DD
55/* Create a new fibonacci heap. */
56fibheap_t
49b1fae4 57fibheap_new (void)
8e777d6a 58{
f080c76d 59 return (fibheap_t) xcalloc (1, sizeof (struct fibheap));
f01b59ed
DD
60}
61
62/* Create a new fibonacci heap node. */
f080c76d 63static fibnode_t
49b1fae4 64fibnode_new (void)
f01b59ed 65{
f080c76d 66 fibnode_t node;
f01b59ed 67
585cc78f 68 node = (fibnode_t) xcalloc (1, sizeof *node);
f080c76d
DD
69 node->left = node;
70 node->right = node;
f01b59ed 71
f080c76d 72 return node;
f01b59ed
DD
73}
74
75static inline int
49b1fae4 76fibheap_compare (fibheap_t heap ATTRIBUTE_UNUSED, fibnode_t a, fibnode_t b)
f01b59ed
DD
77{
78 if (a->key < b->key)
79 return -1;
80 if (a->key > b->key)
81 return 1;
82 return 0;
83}
84
85static inline int
49b1fae4 86fibheap_comp_data (fibheap_t heap, fibheapkey_t key, void *data, fibnode_t b)
8e777d6a 87{
f01b59ed
DD
88 struct fibnode a;
89
90 a.key = key;
91 a.data = data;
92
93 return fibheap_compare (heap, &a, b);
8e777d6a
DD
94}
95
96/* Insert DATA, with priority KEY, into HEAP. */
97fibnode_t
49b1fae4 98fibheap_insert (fibheap_t heap, fibheapkey_t key, void *data)
8e777d6a
DD
99{
100 fibnode_t node;
f01b59ed 101
f080c76d
DD
102 /* Create the new node. */
103 node = fibnode_new ();
f01b59ed 104
8e777d6a
DD
105 /* Set the node's data. */
106 node->data = data;
107 node->key = key;
108
109 /* Insert it into the root list. */
110 fibheap_ins_root (heap, node);
111
f01b59ed
DD
112 /* If their was no minimum, or this key is less than the min,
113 it's the new min. */
8e777d6a
DD
114 if (heap->min == NULL || node->key < heap->min->key)
115 heap->min = node;
116
117 heap->nodes++;
118
119 return node;
120}
121
122/* Return the data of the minimum node (if we know it). */
123void *
49b1fae4 124fibheap_min (fibheap_t heap)
8e777d6a
DD
125{
126 /* If there is no min, we can't easily return it. */
127 if (heap->min == NULL)
128 return NULL;
129 return heap->min->data;
130}
131
132/* Return the key of the minimum node (if we know it). */
133fibheapkey_t
49b1fae4 134fibheap_min_key (fibheap_t heap)
8e777d6a
DD
135{
136 /* If there is no min, we can't easily return it. */
137 if (heap->min == NULL)
138 return 0;
139 return heap->min->key;
140}
141
142/* Union HEAPA and HEAPB into a new heap. */
143fibheap_t
49b1fae4 144fibheap_union (fibheap_t heapa, fibheap_t heapb)
8e777d6a 145{
f01b59ed 146 fibnode_t a_root, b_root, temp;
8e777d6a
DD
147
148 /* If one of the heaps is empty, the union is just the other heap. */
f01b59ed 149 if ((a_root = heapa->root) == NULL)
8e777d6a 150 {
f01b59ed
DD
151 free (heapa);
152 return heapb;
153 }
154 if ((b_root = heapb->root) == NULL)
155 {
156 free (heapb);
157 return heapa;
8e777d6a 158 }
f01b59ed 159
8e777d6a 160 /* Merge them to the next nodes on the opposite chain. */
f01b59ed
DD
161 a_root->left->right = b_root;
162 b_root->left->right = a_root;
163 temp = a_root->left;
164 a_root->left = b_root->left;
165 b_root->left = temp;
8e777d6a
DD
166 heapa->nodes += heapb->nodes;
167
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;
171
172 free (heapb);
173 return heapa;
174}
175
176/* Extract the data of the minimum node from HEAP. */
177void *
49b1fae4 178fibheap_extract_min (fibheap_t heap)
8e777d6a
DD
179{
180 fibnode_t z;
f01b59ed 181 void *ret = NULL;
8e777d6a 182
8e777d6a
DD
183 /* If we don't have a min set, it means we have no nodes. */
184 if (heap->min != NULL)
185 {
186 /* Otherwise, extract the min node, free the node, and return the
187 node's data. */
188 z = fibheap_extr_min_node (heap);
189 ret = z->data;
190 free (z);
191 }
192
193 return ret;
194}
195
8e777d6a
DD
196/* Replace both the KEY and the DATA associated with NODE. */
197void *
49b1fae4
DD
198fibheap_replace_key_data (fibheap_t heap, fibnode_t node,
199 fibheapkey_t key, void *data)
8e777d6a
DD
200{
201 void *odata;
aae66b9f 202 fibheapkey_t okey;
8e777d6a
DD
203 fibnode_t y;
204
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
207 for now. */
208 if (fibheap_comp_data (heap, key, data, node) > 0)
209 return NULL;
210
211 odata = node->data;
212 okey = node->key;
213 node->data = data;
214 node->key = key;
215 y = node->parent;
216
0dc69033
DD
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)
8e777d6a
DD
221 return odata;
222
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)
227 {
228 fibheap_cut (heap, node, y);
229 fibheap_cascading_cut (heap, y);
230 }
231
232 if (fibheap_compare (heap, node, heap->min) <= 0)
233 heap->min = node;
234
235 return odata;
236}
237
f01b59ed
DD
238/* Replace the DATA associated with NODE. */
239void *
49b1fae4 240fibheap_replace_data (fibheap_t heap, fibnode_t node, void *data)
f01b59ed
DD
241{
242 return fibheap_replace_key_data (heap, node, node->key, data);
243}
244
245/* Replace the KEY associated with NODE. */
246fibheapkey_t
49b1fae4 247fibheap_replace_key (fibheap_t heap, fibnode_t node, fibheapkey_t key)
f01b59ed
DD
248{
249 int okey = node->key;
250 fibheap_replace_key_data (heap, node, key, node->data);
251 return okey;
252}
253
8e777d6a
DD
254/* Delete NODE from HEAP. */
255void *
49b1fae4 256fibheap_delete_node (fibheap_t heap, fibnode_t node)
8e777d6a 257{
f01b59ed
DD
258 void *ret = node->data;
259
8e777d6a 260 /* To perform delete, we just make it the min key, and extract. */
f01b59ed 261 fibheap_replace_key (heap, node, FIBHEAPKEY_MIN);
0dc69033
DD
262 if (node != heap->min)
263 {
264 fprintf (stderr, "Can't force minimum on fibheap.\n");
265 abort ();
266 }
8e777d6a
DD
267 fibheap_extract_min (heap);
268
f01b59ed 269 return ret;
8e777d6a
DD
270}
271
272/* Delete HEAP. */
273void
49b1fae4 274fibheap_delete (fibheap_t heap)
8e777d6a
DD
275{
276 while (heap->min != NULL)
277 free (fibheap_extr_min_node (heap));
278
279 free (heap);
280}
281
282/* Determine if HEAP is empty. */
283int
49b1fae4 284fibheap_empty (fibheap_t heap)
8e777d6a
DD
285{
286 return heap->nodes == 0;
287}
288
8e777d6a
DD
289/* Extract the minimum node of the heap. */
290static fibnode_t
49b1fae4 291fibheap_extr_min_node (fibheap_t heap)
8e777d6a 292{
f01b59ed 293 fibnode_t ret = heap->min;
8e777d6a
DD
294 fibnode_t x, y, orig;
295
8e777d6a
DD
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. */
f01b59ed 298 for (x = ret->child, orig = NULL; x != orig && x != NULL; x = y)
8e777d6a
DD
299 {
300 if (orig == NULL)
301 orig = x;
302 y = x->right;
303 x->parent = NULL;
304 fibheap_ins_root (heap, x);
8e777d6a 305 }
f01b59ed 306
8e777d6a
DD
307 /* Remove the old root. */
308 fibheap_rem_root (heap, ret);
309 heap->nodes--;
f01b59ed 310
8e777d6a
DD
311 /* If we are left with no nodes, then the min is NULL. */
312 if (heap->nodes == 0)
313 heap->min = NULL;
314 else
315 {
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);
320 }
321
322 return ret;
323}
324
325/* Insert NODE into the root list of HEAP. */
326static void
49b1fae4 327fibheap_ins_root (fibheap_t heap, fibnode_t node)
8e777d6a
DD
328{
329 /* If the heap is currently empty, the new node becomes the singleton
330 circular root list. */
331 if (heap->root == NULL)
332 {
333 heap->root = node;
334 node->left = node;
335 node->right = node;
336 return;
337 }
f01b59ed
DD
338
339 /* Otherwise, insert it in the circular root list between the root
340 and it's right node. */
8e777d6a
DD
341 fibnode_insert_after (heap->root, node);
342}
343
344/* Remove NODE from the rootlist of HEAP. */
345static void
49b1fae4 346fibheap_rem_root (fibheap_t heap, fibnode_t node)
8e777d6a
DD
347{
348 if (node->left == node)
349 heap->root = NULL;
350 else
351 heap->root = fibnode_remove (node);
352}
353
354/* Consolidate the heap. */
355static void
49b1fae4 356fibheap_consolidate (fibheap_t heap)
8e777d6a
DD
357{
358 fibnode_t a[1 + 8 * sizeof (long)];
359 fibnode_t w;
360 fibnode_t y;
361 fibnode_t x;
362 int i;
363 int d;
364 int D;
365
366 D = 1 + 8 * sizeof (long);
367
368 memset (a, 0, sizeof (fibnode_t) * D);
369
370 while ((w = heap->root) != NULL)
371 {
372 x = w;
373 fibheap_rem_root (heap, w);
374 d = x->degree;
375 while (a[d] != NULL)
376 {
377 y = a[d];
378 if (fibheap_compare (heap, x, y) > 0)
379 {
380 fibnode_t temp;
381 temp = x;
382 x = y;
383 y = temp;
384 }
385 fibheap_link (heap, y, x);
386 a[d] = NULL;
387 d++;
388 }
389 a[d] = x;
390 }
391 heap->min = NULL;
392 for (i = 0; i < D; i++)
393 if (a[i] != NULL)
394 {
395 fibheap_ins_root (heap, a[i]);
396 if (heap->min == NULL || fibheap_compare (heap, a[i], heap->min) < 0)
397 heap->min = a[i];
398 }
399}
400
401/* Make NODE a child of PARENT. */
402static void
49b1fae4
DD
403fibheap_link (fibheap_t heap ATTRIBUTE_UNUSED,
404 fibnode_t node, fibnode_t parent)
8e777d6a
DD
405{
406 if (parent->child == NULL)
407 parent->child = node;
408 else
409 fibnode_insert_before (parent->child, node);
410 node->parent = parent;
411 parent->degree++;
412 node->mark = 0;
413}
414
415/* Remove NODE from PARENT's child list. */
416static void
49b1fae4 417fibheap_cut (fibheap_t heap, fibnode_t node, fibnode_t parent)
8e777d6a
DD
418{
419 fibnode_remove (node);
420 parent->degree--;
421 fibheap_ins_root (heap, node);
422 node->parent = NULL;
423 node->mark = 0;
424}
425
426static void
49b1fae4 427fibheap_cascading_cut (fibheap_t heap, fibnode_t y)
8e777d6a
DD
428{
429 fibnode_t z;
430
431 while ((z = y->parent) != NULL)
432 {
433 if (y->mark == 0)
434 {
435 y->mark = 1;
436 return;
437 }
438 else
439 {
440 fibheap_cut (heap, y, z);
441 y = z;
442 }
443 }
444}
445
8e777d6a 446static void
49b1fae4 447fibnode_insert_after (fibnode_t a, fibnode_t b)
8e777d6a
DD
448{
449 if (a == a->right)
450 {
451 a->right = b;
452 a->left = b;
453 b->right = a;
454 b->left = a;
455 }
456 else
457 {
458 b->right = a->right;
459 a->right->left = b;
460 a->right = b;
461 b->left = a;
462 }
463}
464
8e777d6a 465static fibnode_t
49b1fae4 466fibnode_remove (fibnode_t node)
8e777d6a
DD
467{
468 fibnode_t ret;
469
470 if (node == node->left)
471 ret = NULL;
472 else
473 ret = node->left;
474
475 if (node->parent != NULL && node->parent->child == node)
476 node->parent->child = ret;
477
478 node->right->left = node->left;
479 node->left->right = node->right;
480
481 node->parent = NULL;
482 node->left = node;
483 node->right = node;
484
485 return ret;
486}
This page took 0.774616 seconds and 4 git commands to generate.