binutils-gdb/libiberty/fibheap.c

487 lines
11 KiB
C
Raw Normal View History

2001-08-21 23:19:19 +08:00
/* A Fibonacci heap datatype.
Update the libiberty sources with the latest patches found in the master sources. 2017-01-02 Richard Biener <rguenther@suse.de> PR lto/83452 * simple-object-elf.c (simple_object_elf_copy_lto_debug_section): Do not use UNDEF locals for removed symbols but instead just define them in the first prevailing section and with no name. Use the same gnu_lto_v1 name for all removed globals we promote to WEAK UNDEFs so hpux can use a stub to provide this symbol. Clear sh_info and sh_link in removed sections. 2017-10-30 Richard Biener <rguenther@suse.de> PR lto/82757 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Strip two leading _s from the __gnu_lto_* symbols. 2017-10-24 Alan Modra <amodra@gmail.com> PR lto/82687 PR lto/82575 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Only make __gnu_lto symbols hidden. 2017-10-20 Alan Modra <amodra@gmail.com> PR lto/82575 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Make discarded non-local symbols weak and hidden. 2017-10-18 Jakub Jelinek <jakub@redhat.com> PR lto/82598 * simple-object.c (handle_lto_debug_sections): Copy over also .note.GNU-stack section with unchanged name. * simple-object-elf.c (SHF_EXECINSTR): Define. (simple_object_elf_copy_lto_debug_section): Drop SHF_EXECINSTR bit on .note.GNU-stack section. 2017-09-25 Nathan Sidwell <nathan@acm.org> PR demangler/82195 * cp-demangle.c (d_encoding): Strip return type when name is a LOCAL_NAME. (d_local_name): Strip return type of enclosing TYPED_NAME. * testsuite/demangle-expected: Add and adjust tests. 2017-09-21 Nathan Sidwell <nathan@acm.org> PR demangler/82195 * cp-demangle.c (d_name): Revert addition of 'toplevel' parm. (has_return_type): Recurse for DEMANGLE_COMPONENT_LOCAL_NAME. (d_encoding): Revert d_name change. Use is_fnqual_component_type to strip modifiers that do not belong. (d_special_name, d_class_enum_type): Revert d_name call change. (d_expresion_1): Commonize DEMANGLE_COMPONENT_UNARY building. (d_local_name): Revert parsing of a function type. (d_print_comp_inner): An inner LOCAL_NAME might contain a TEMPLATE. * testsuite/demangle-expected: Add & adjust tests
2018-01-10 21:57:48 +08:00
Copyright (C) 1998-2018 Free Software Foundation, Inc.
2001-08-21 23:19:19 +08:00
Contributed by Daniel Berlin (dan@cgsoftware.com).
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
2001-08-21 23:19:19 +08:00
2001-08-22 10:12:15 +08:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIMITS_H
2001-08-21 23:19:19 +08:00
#include <limits.h>
2001-08-22 10:12:15 +08:00
#endif
#ifdef HAVE_STDLIB_H
2001-08-21 23:19:19 +08:00
#include <stdlib.h>
2001-08-22 10:12:15 +08:00
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
2001-08-21 23:19:19 +08:00
#include "libiberty.h"
#include "fibheap.h"
2001-08-22 10:12:15 +08:00
#define FIBHEAPKEY_MIN LONG_MIN
2005-03-28 10:09:01 +08:00
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);
2001-08-21 23:19:19 +08:00
#define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b)
2005-03-28 10:09:01 +08:00
static fibnode_t fibnode_remove (fibnode_t);
2001-08-21 23:19:19 +08:00
2001-08-22 10:12:15 +08:00
2001-08-21 23:19:19 +08:00
/* Create a new fibonacci heap. */
fibheap_t
2005-03-28 10:09:01 +08:00
fibheap_new (void)
2001-08-21 23:19:19 +08:00
{
2001-08-23 06:55:38 +08:00
return (fibheap_t) xcalloc (1, sizeof (struct fibheap));
2001-08-22 10:12:15 +08:00
}
/* Create a new fibonacci heap node. */
2001-08-23 06:55:38 +08:00
static fibnode_t
2005-03-28 10:09:01 +08:00
fibnode_new (void)
2001-08-22 10:12:15 +08:00
{
2001-08-23 06:55:38 +08:00
fibnode_t node;
2001-08-22 10:12:15 +08:00
2002-09-20 21:45:20 +08:00
node = (fibnode_t) xcalloc (1, sizeof *node);
2001-08-23 06:55:38 +08:00
node->left = node;
node->right = node;
2001-08-22 10:12:15 +08:00
2001-08-23 06:55:38 +08:00
return node;
2001-08-22 10:12:15 +08:00
}
static inline int
2005-03-28 10:09:01 +08:00
fibheap_compare (fibheap_t heap ATTRIBUTE_UNUSED, fibnode_t a, fibnode_t b)
2001-08-22 10:12:15 +08:00
{
if (a->key < b->key)
return -1;
if (a->key > b->key)
return 1;
return 0;
}
static inline int
2005-03-28 10:09:01 +08:00
fibheap_comp_data (fibheap_t heap, fibheapkey_t key, void *data, fibnode_t b)
2001-08-21 23:19:19 +08:00
{
2001-08-22 10:12:15 +08:00
struct fibnode a;
a.key = key;
a.data = data;
return fibheap_compare (heap, &a, b);
2001-08-21 23:19:19 +08:00
}
/* Insert DATA, with priority KEY, into HEAP. */
fibnode_t
2005-03-28 10:09:01 +08:00
fibheap_insert (fibheap_t heap, fibheapkey_t key, void *data)
2001-08-21 23:19:19 +08:00
{
fibnode_t node;
2001-08-22 10:12:15 +08:00
2001-08-23 06:55:38 +08:00
/* Create the new node. */
node = fibnode_new ();
2001-08-22 10:12:15 +08:00
2001-08-21 23:19:19 +08:00
/* Set the node's data. */
node->data = data;
node->key = key;
/* Insert it into the root list. */
fibheap_ins_root (heap, node);
2001-08-22 10:12:15 +08:00
/* If their was no minimum, or this key is less than the min,
it's the new min. */
2001-08-21 23:19:19 +08:00
if (heap->min == NULL || node->key < heap->min->key)
heap->min = node;
heap->nodes++;
return node;
}
/* Return the data of the minimum node (if we know it). */
void *
2005-03-28 10:09:01 +08:00
fibheap_min (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
/* If there is no min, we can't easily return it. */
if (heap->min == NULL)
return NULL;
return heap->min->data;
}
/* Return the key of the minimum node (if we know it). */
fibheapkey_t
2005-03-28 10:09:01 +08:00
fibheap_min_key (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
/* If there is no min, we can't easily return it. */
if (heap->min == NULL)
return 0;
return heap->min->key;
}
/* Union HEAPA and HEAPB into a new heap. */
fibheap_t
2005-03-28 10:09:01 +08:00
fibheap_union (fibheap_t heapa, fibheap_t heapb)
2001-08-21 23:19:19 +08:00
{
2001-08-22 10:12:15 +08:00
fibnode_t a_root, b_root, temp;
2001-08-21 23:19:19 +08:00
/* If one of the heaps is empty, the union is just the other heap. */
2001-08-22 10:12:15 +08:00
if ((a_root = heapa->root) == NULL)
2001-08-21 23:19:19 +08:00
{
2001-08-22 10:12:15 +08:00
free (heapa);
return heapb;
}
if ((b_root = heapb->root) == NULL)
{
free (heapb);
return heapa;
2001-08-21 23:19:19 +08:00
}
2001-08-22 10:12:15 +08:00
2001-08-21 23:19:19 +08:00
/* Merge them to the next nodes on the opposite chain. */
2001-08-22 10:12:15 +08:00
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;
2001-08-21 23:19:19 +08:00
heapa->nodes += heapb->nodes;
/* And set the new minimum, if it's changed. */
if (fibheap_compare (heapa, heapb->min, heapa->min) < 0)
heapa->min = heapb->min;
free (heapb);
return heapa;
}
/* Extract the data of the minimum node from HEAP. */
void *
2005-03-28 10:09:01 +08:00
fibheap_extract_min (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
fibnode_t z;
2001-08-22 10:12:15 +08:00
void *ret = NULL;
2001-08-21 23:19:19 +08:00
/* If we don't have a min set, it means we have no nodes. */
if (heap->min != NULL)
{
/* Otherwise, extract the min node, free the node, and return the
node's data. */
z = fibheap_extr_min_node (heap);
ret = z->data;
free (z);
}
return ret;
}
/* Replace both the KEY and the DATA associated with NODE. */
void *
2005-03-28 10:09:01 +08:00
fibheap_replace_key_data (fibheap_t heap, fibnode_t node,
fibheapkey_t key, void *data)
2001-08-21 23:19:19 +08:00
{
void *odata;
2003-09-07 07:51:12 +08:00
fibheapkey_t okey;
2001-08-21 23:19:19 +08:00
fibnode_t y;
/* If we wanted to, we could actually do a real increase by redeleting and
inserting. However, this would require O (log n) time. So just bail out
for now. */
if (fibheap_comp_data (heap, key, data, node) > 0)
return NULL;
odata = node->data;
okey = node->key;
node->data = data;
node->key = key;
y = node->parent;
2009-05-29 11:01:15 +08:00
/* 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)
2001-08-21 23:19:19 +08:00
return odata;
/* These two compares are specifically <= 0 to make sure that in the case
of equality, a node we replaced the data on, becomes the new min. This
is needed so that delete's call to extractmin gets the right node. */
if (y != NULL && fibheap_compare (heap, node, y) <= 0)
{
fibheap_cut (heap, node, y);
fibheap_cascading_cut (heap, y);
}
if (fibheap_compare (heap, node, heap->min) <= 0)
heap->min = node;
return odata;
}
2001-08-22 10:12:15 +08:00
/* Replace the DATA associated with NODE. */
void *
2005-03-28 10:09:01 +08:00
fibheap_replace_data (fibheap_t heap, fibnode_t node, void *data)
2001-08-22 10:12:15 +08:00
{
return fibheap_replace_key_data (heap, node, node->key, data);
}
/* Replace the KEY associated with NODE. */
fibheapkey_t
2005-03-28 10:09:01 +08:00
fibheap_replace_key (fibheap_t heap, fibnode_t node, fibheapkey_t key)
2001-08-22 10:12:15 +08:00
{
int okey = node->key;
fibheap_replace_key_data (heap, node, key, node->data);
return okey;
}
2001-08-21 23:19:19 +08:00
/* Delete NODE from HEAP. */
void *
2005-03-28 10:09:01 +08:00
fibheap_delete_node (fibheap_t heap, fibnode_t node)
2001-08-21 23:19:19 +08:00
{
2001-08-22 10:12:15 +08:00
void *ret = node->data;
2001-08-21 23:19:19 +08:00
/* To perform delete, we just make it the min key, and extract. */
2001-08-22 10:12:15 +08:00
fibheap_replace_key (heap, node, FIBHEAPKEY_MIN);
2009-05-29 11:01:15 +08:00
if (node != heap->min)
{
fprintf (stderr, "Can't force minimum on fibheap.\n");
abort ();
}
2001-08-21 23:19:19 +08:00
fibheap_extract_min (heap);
2001-08-22 10:12:15 +08:00
return ret;
2001-08-21 23:19:19 +08:00
}
/* Delete HEAP. */
void
2005-03-28 10:09:01 +08:00
fibheap_delete (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
while (heap->min != NULL)
free (fibheap_extr_min_node (heap));
free (heap);
}
/* Determine if HEAP is empty. */
int
2005-03-28 10:09:01 +08:00
fibheap_empty (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
return heap->nodes == 0;
}
/* Extract the minimum node of the heap. */
static fibnode_t
2005-03-28 10:09:01 +08:00
fibheap_extr_min_node (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
2001-08-22 10:12:15 +08:00
fibnode_t ret = heap->min;
2001-08-21 23:19:19 +08:00
fibnode_t x, y, orig;
/* 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. */
2001-08-22 10:12:15 +08:00
for (x = ret->child, orig = NULL; x != orig && x != NULL; x = y)
2001-08-21 23:19:19 +08:00
{
if (orig == NULL)
orig = x;
y = x->right;
x->parent = NULL;
fibheap_ins_root (heap, x);
}
2001-08-22 10:12:15 +08:00
2001-08-21 23:19:19 +08:00
/* Remove the old root. */
fibheap_rem_root (heap, ret);
heap->nodes--;
2001-08-22 10:12:15 +08:00
2001-08-21 23:19:19 +08:00
/* If we are left with no nodes, then the min is NULL. */
if (heap->nodes == 0)
heap->min = NULL;
else
{
/* Otherwise, consolidate to find new minimum, as well as do the reorg
work that needs to be done. */
heap->min = ret->right;
fibheap_consolidate (heap);
}
return ret;
}
/* Insert NODE into the root list of HEAP. */
static void
2005-03-28 10:09:01 +08:00
fibheap_ins_root (fibheap_t heap, fibnode_t node)
2001-08-21 23:19:19 +08:00
{
/* If the heap is currently empty, the new node becomes the singleton
circular root list. */
if (heap->root == NULL)
{
heap->root = node;
node->left = node;
node->right = node;
return;
}
2001-08-22 10:12:15 +08:00
/* Otherwise, insert it in the circular root list between the root
and it's right node. */
2001-08-21 23:19:19 +08:00
fibnode_insert_after (heap->root, node);
}
/* Remove NODE from the rootlist of HEAP. */
static void
2005-03-28 10:09:01 +08:00
fibheap_rem_root (fibheap_t heap, fibnode_t node)
2001-08-21 23:19:19 +08:00
{
if (node->left == node)
heap->root = NULL;
else
heap->root = fibnode_remove (node);
}
/* Consolidate the heap. */
static void
2005-03-28 10:09:01 +08:00
fibheap_consolidate (fibheap_t heap)
2001-08-21 23:19:19 +08:00
{
fibnode_t a[1 + 8 * sizeof (long)];
fibnode_t w;
fibnode_t y;
fibnode_t x;
int i;
int d;
int D;
D = 1 + 8 * sizeof (long);
memset (a, 0, sizeof (fibnode_t) * D);
while ((w = heap->root) != NULL)
{
x = w;
fibheap_rem_root (heap, w);
d = x->degree;
while (a[d] != NULL)
{
y = a[d];
if (fibheap_compare (heap, x, y) > 0)
{
fibnode_t temp;
temp = x;
x = y;
y = temp;
}
fibheap_link (heap, y, x);
a[d] = NULL;
d++;
}
a[d] = x;
}
heap->min = NULL;
for (i = 0; i < D; i++)
if (a[i] != NULL)
{
fibheap_ins_root (heap, a[i]);
if (heap->min == NULL || fibheap_compare (heap, a[i], heap->min) < 0)
heap->min = a[i];
}
}
/* Make NODE a child of PARENT. */
static void
2005-03-28 10:09:01 +08:00
fibheap_link (fibheap_t heap ATTRIBUTE_UNUSED,
fibnode_t node, fibnode_t parent)
2001-08-21 23:19:19 +08:00
{
if (parent->child == NULL)
parent->child = node;
else
fibnode_insert_before (parent->child, node);
node->parent = parent;
parent->degree++;
node->mark = 0;
}
/* Remove NODE from PARENT's child list. */
static void
2005-03-28 10:09:01 +08:00
fibheap_cut (fibheap_t heap, fibnode_t node, fibnode_t parent)
2001-08-21 23:19:19 +08:00
{
fibnode_remove (node);
parent->degree--;
fibheap_ins_root (heap, node);
node->parent = NULL;
node->mark = 0;
}
static void
2005-03-28 10:09:01 +08:00
fibheap_cascading_cut (fibheap_t heap, fibnode_t y)
2001-08-21 23:19:19 +08:00
{
fibnode_t z;
while ((z = y->parent) != NULL)
{
if (y->mark == 0)
{
y->mark = 1;
return;
}
else
{
fibheap_cut (heap, y, z);
y = z;
}
}
}
static void
2005-03-28 10:09:01 +08:00
fibnode_insert_after (fibnode_t a, fibnode_t b)
2001-08-21 23:19:19 +08:00
{
if (a == a->right)
{
a->right = b;
a->left = b;
b->right = a;
b->left = a;
}
else
{
b->right = a->right;
a->right->left = b;
a->right = b;
b->left = a;
}
}
static fibnode_t
2005-03-28 10:09:01 +08:00
fibnode_remove (fibnode_t node)
2001-08-21 23:19:19 +08:00
{
fibnode_t ret;
if (node == node->left)
ret = NULL;
else
ret = node->left;
if (node->parent != NULL && node->parent->child == node)
node->parent->child = ret;
node->right->left = node->left;
node->left->right = node->right;
node->parent = NULL;
node->left = node;
node->right = node;
return ret;
}