gcc/libiberty/splay-tree.c

514 lines
12 KiB
C
Raw Normal View History

/* A splay-tree datatype.
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
Contributed by Mark Mitchell (mark@markmitchell.com).
1999-01-11 15:06:22 +01:00
This file is part of GNU CC.
1999-01-11 15:06:22 +01:00
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.
1999-01-11 15:06:22 +01:00
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.
1999-01-11 15:06:22 +01:00
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
1999-01-12 20:09:02 +01:00
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
1999-01-11 15:06:22 +01:00
/* For an easily readable description of splay-trees, see:
Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
Algorithms. Harper-Collins, Inc. 1991. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <stdio.h>
#include "libiberty.h"
#include "splay-tree.h"
static void splay_tree_delete_helper PARAMS((splay_tree,
splay_tree_node));
static void splay_tree_splay PARAMS((splay_tree,
splay_tree_key));
static splay_tree_node splay_tree_splay_helper
PARAMS((splay_tree,
splay_tree_key,
splay_tree_node*,
splay_tree_node*,
splay_tree_node*));
static int splay_tree_foreach_helper PARAMS((splay_tree,
splay_tree_node,
splay_tree_foreach_fn,
void*));
/* Deallocate NODE (a member of SP), and all its sub-trees. */
static void
splay_tree_delete_helper (sp, node)
splay_tree sp;
splay_tree_node node;
{
if (!node)
return;
splay_tree_delete_helper (sp, node->left);
splay_tree_delete_helper (sp, node->right);
if (sp->delete_key)
(*sp->delete_key)(node->key);
if (sp->delete_value)
(*sp->delete_value)(node->value);
free ((char*) node);
}
/* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
and grandparent, respectively, of NODE. */
static splay_tree_node
splay_tree_splay_helper (sp, key, node, parent, grandparent)
splay_tree sp;
splay_tree_key key;
splay_tree_node *node;
splay_tree_node *parent;
splay_tree_node *grandparent;
{
splay_tree_node *next;
splay_tree_node n;
int comparison;
n = *node;
if (!n)
return *parent;
comparison = (*sp->comp) (key, n->key);
if (comparison == 0)
/* We've found the target. */
next = 0;
else if (comparison < 0)
/* The target is to the left. */
next = &n->left;
else
/* The target is to the right. */
next = &n->right;
if (next)
{
/* Continue down the tree. */
n = splay_tree_splay_helper (sp, key, next, node, parent);
/* The recursive call will change the place to which NODE
points. */
if (*node != n)
return n;
}
if (!parent)
/* NODE is the root. We are done. */
return n;
/* First, handle the case where there is no grandparent (i.e.,
*PARENT is the root of the tree.) */
if (!grandparent)
{
if (n == (*parent)->left)
{
*node = n->right;
n->right = *parent;
}
else
{
*node = n->left;
n->left = *parent;
}
*parent = n;
return n;
}
/* Next handle the cases where both N and *PARENT are left children,
or where both are right children. */
if (n == (*parent)->left && *parent == (*grandparent)->left)
{
splay_tree_node p = *parent;
(*grandparent)->left = p->right;
p->right = *grandparent;
p->left = n->right;
n->right = p;
*grandparent = n;
return n;
}
else if (n == (*parent)->right && *parent == (*grandparent)->right)
{
splay_tree_node p = *parent;
(*grandparent)->right = p->left;
p->left = *grandparent;
p->right = n->left;
n->left = p;
*grandparent = n;
return n;
}
/* Finally, deal with the case where N is a left child, but *PARENT
is a right child, or vice versa. */
if (n == (*parent)->left)
{
(*parent)->left = n->right;
n->right = *parent;
(*grandparent)->right = n->left;
n->left = *grandparent;
*grandparent = n;
return n;
}
else
{
(*parent)->right = n->left;
n->left = *parent;
(*grandparent)->left = n->right;
n->right = *grandparent;
*grandparent = n;
return n;
}
}
/* Splay SP around KEY. */
static void
splay_tree_splay (sp, key)
splay_tree sp;
splay_tree_key key;
{
if (sp->root == 0)
return;
splay_tree_splay_helper (sp, key, &sp->root,
/*grandparent=*/0, /*parent=*/0);
}
/* Call FN, passing it the DATA, for every node below NODE, all of
which are from SP, following an in-order traversal. If FN every
returns a non-zero value, the iteration ceases immediately, and the
value is returned. Otherwise, this function returns 0. */
static int
splay_tree_foreach_helper (sp, node, fn, data)
splay_tree sp;
splay_tree_node node;
splay_tree_foreach_fn fn;
void* data;
{
int val;
if (!node)
return 0;
val = splay_tree_foreach_helper (sp, node->left, fn, data);
if (val)
return val;
val = (*fn)(node, data);
if (val)
return val;
return splay_tree_foreach_helper (sp, node->right, fn, data);
}
/* Allocate a new splay tree, using COMPARE_FN to compare nodes,
DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
values. */
splay_tree
splay_tree_new (compare_fn, delete_key_fn, delete_value_fn)
splay_tree_compare_fn compare_fn;
splay_tree_delete_key_fn delete_key_fn;
splay_tree_delete_value_fn delete_value_fn;
{
splay_tree sp = (splay_tree) xmalloc (sizeof (struct splay_tree_s));
sp->root = 0;
sp->comp = compare_fn;
sp->delete_key = delete_key_fn;
sp->delete_value = delete_value_fn;
return sp;
}
/* Deallocate SP. */
void
splay_tree_delete (sp)
splay_tree sp;
{
splay_tree_delete_helper (sp, sp->root);
free ((char*) sp);
}
/* Insert a new node (associating KEY with DATA) into SP. If a
previous node with the indicated KEY exists, its data is replaced
with the new value. Returns the new node. */
splay_tree_node
splay_tree_insert (sp, key, value)
splay_tree sp;
splay_tree_key key;
splay_tree_value value;
{
int comparison = 0;
splay_tree_splay (sp, key);
if (sp->root)
comparison = (*sp->comp)(sp->root->key, key);
if (sp->root && comparison == 0)
{
/* If the root of the tree already has the indicated KEY, just
replace the value with VALUE. */
if (sp->delete_value)
(*sp->delete_value)(sp->root->value);
sp->root->value = value;
}
else
{
/* Create a new node, and insert it at the root. */
splay_tree_node node;
node = (splay_tree_node) xmalloc (sizeof (struct splay_tree_node_s));
node->key = key;
node->value = value;
if (!sp->root)
node->left = node->right = 0;
else if (comparison < 0)
{
node->left = sp->root;
node->right = node->left->right;
node->left->right = 0;
}
else
{
node->right = sp->root;
node->left = node->right->left;
node->right->left = 0;
}
c-decl.c (c_expand_body): Don't generate RTL if flag_syntax_only. * c-decl.c (c_expand_body): Don't generate RTL if flag_syntax_only. (lang_mark_false_label_stack): Remove. * c-lex.c (init_c_lex): Add file_info_tree as GC root. Allocate <top level> string in GC area. (mark_splay_tree_node): New function. (mark_splay_tree): Likewise. * except.c (mark_eh_status): Only call lang_mark_false_label_stack if it exists. * ggc-callbacks.c (lang_mark_false_label_stack): Remove. * ggc-common.c (lang_mark_false_label_stack): Change type. * ggc.h (ggc_alloc_string): Add comment. (ggc_strdup): New function. * decl.c (lang_mark_false_label_stack): Remove. * lex.c (cp_mang_lang_type): Use ggc_alloc_cleared. * com.c (lang_mark_false_label_stack): Remove. Convert to GC. * Make-lang.in (s-java): Don't depend on ggc-callbacks.o. * Makefile.in (BACKEND): Don't include ggc-callbacks.o. (typeck.o): Depend on ggc.h. * class.c (add_method_1): Use GC functions for allocation. (init_class_processing): Register roots. * decl.c (ggc_p): Set to 1. (pending_local_decls): Make it static. (push_jvm_slot): Use GC functions for allocation. (init_decl_processing): Register roots. (give_name_to_locals): Use GC functions for allocation. (lang_mark_tree): New function. * java-tree.h (MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC): Use GC functions for allocation. * jcf-parse.c (jcf_parse_source): Use ggc_strdup. * lex.c (java_lex): Use build_string, rather than replicating it inline. * parse.y (goal): Add more roots. (mark_parser_ctxt): New function. * typeck.c: Include ggc.h. * splay-tree.c (splay_tree_insert): Fix formatting. From-SVN: r36687
2000-10-01 21:19:30 +02:00
sp->root = node;
}
return sp->root;
}
/* Remove KEY from SP. It is not an error if it did not exist. */
void
splay_tree_remove (sp, key)
splay_tree sp;
splay_tree_key key;
{
splay_tree_splay (sp, key);
if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
{
splay_tree_node left, right;
left = sp->root->left;
right = sp->root->right;
/* Delete the root node itself. */
if (sp->delete_value)
(*sp->delete_value) (sp->root->value);
free (sp->root);
/* One of the children is now the root. Doesn't matter much
which, so long as we preserve the properties of the tree. */
if (left)
{
sp->root = left;
/* If there was a right child as well, hang it off the
right-most leaf of the left child. */
if (right)
{
while (left->right)
left = left->right;
left->right = right;
}
}
else
sp->root = right;
}
}
/* Lookup KEY in SP, returning VALUE if present, and NULL
otherwise. */
splay_tree_node
splay_tree_lookup (sp, key)
splay_tree sp;
splay_tree_key key;
{
splay_tree_splay (sp, key);
if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
return sp->root;
else
return 0;
}
/* Return the node in SP with the greatest key. */
splay_tree_node
splay_tree_max (sp)
splay_tree sp;
{
splay_tree_node n = sp->root;
if (!n)
return NULL;
while (n->right)
n = n->right;
return n;
}
/* Return the node in SP with the smallest key. */
splay_tree_node
splay_tree_min (sp)
splay_tree sp;
{
splay_tree_node n = sp->root;
if (!n)
return NULL;
while (n->left)
n = n->left;
return n;
}
/* Return the immediate predecessor KEY, or NULL if there is no
predecessor. KEY need not be present in the tree. */
splay_tree_node
splay_tree_predecessor (sp, key)
splay_tree sp;
splay_tree_key key;
{
int comparison;
splay_tree_node node;
/* If the tree is empty, there is certainly no predecessor. */
if (!sp->root)
return NULL;
/* Splay the tree around KEY. That will leave either the KEY
itself, its predecessor, or its successor at the root. */
splay_tree_splay (sp, key);
comparison = (*sp->comp)(sp->root->key, key);
/* If the predecessor is at the root, just return it. */
if (comparison < 0)
return sp->root;
splay-tree.c (splay_tree_predecessor): Fix typo in comment. * splay-tree.c (splay_tree_predecessor): Fix typo in comment. Convert the C front-end to use function-at-a-time mode. * c-common.h: Include splay-tree.h. (C_DECLARED_LABEL_FLAG): New macro. (struct language_function): Add x_scope_stmt_stack and x_function_name_declared_p. (RECHAIN_STMTS): Move definition. (lang_statment_code_p): Likewise. (lang_expand_stmt): Likewise. (lang_expand_decl_stmt): New variable. (lang_expand_function_end): Likewise. (current_scope_stmt_stack): New function. (add_decl_stmt): Likewise. (add_scope_stmt): Likewise. (mark_stmt_tree): Likewise. (struct c_lang_decl): New structure. (DECL_SAVED_TREE): Define. (c_mark_lang_decl): New function. (c_expand_start_cond): Change prototype. (c_finish_then): New function. (c_finish_else): Likewise. (current_function_name_declared): Remove. (set_current_function_name_declared): Likewise. (mark_c_language_function): Declare. (case_compare): Likewise. (c_add_case_label): Likewise. (c_expand_expr): Likewise. (c_safe_from_p): Likewise. * c-common.c (lang_expand_function_end): New variable. (struct if_elt): Add if_stmt. (c_expand_start_cond): Add the if-statement to the statement-tree, rather than generating RTL. (c_finish_then): New function. (c_expand_start_else): Don't generate RTL. (c_finish_else): New function. (c_expand_expr_stmt): Don't generate RTL. (statement_code_p): Add SCOPE_STMT. (case_compare): New function. (c_add_case_label): Likewise. (mark_stmt_tree): Likewise. (c_mark_lang_decl): Likewise. (mark_c_language_function): Likewise. (c_expand_expr): Likewise. (c_safe_from_p): Likewise. * c-decl.c (c_stmt_tree): New variable (c_scope_stmt_stack): Likewise. (c_function_name_declared_p): Likewise. (lang_expand_expr_stmt): Remove. (poplevel): Don't call output_inline_function for nested functions. (pushdecl): Don't set DECL_CONTEXT for a local declaration of an `extern' function. (redeclaration_error_message): Change means of computing whether or not a function is nested. (lookup_label): Don't call label_rtx. (init_decl_processing): Add more GC roots. (start_decl): Add DECL_STMTs to the statement-tree, rather than calling rest_of_decl_compilation. (finish_decl): Don't call expand_decl. (store_parm_decls): Begin the statement-tree, but don't generate RTL. (finish_function): Tie off the statement-tree. Call c_expand_body if appropriate. (c_expand_body): New function. (push_c_function_context): Save more information. (pop_c_function_contxt): Likewise. (copy_lang_decl): Now that we use DECL_LANG_SPECIFIC, copy it. (lang_mark_tree): Mark it. (current_stmt_tree): Adjust. (current_scope_stmt_stack): New function. (do_case): Remove. (set_current_name_declared): Likewise. (c_begin_compound_stmt): Define. (c_expand_decl_stmt): Likewise. * c-lang.c: Include rtl.h and expr.h. (lang_init): Set more language-specific hooks. * c-lex.c: Include expr.h. * c-parse.in: Changes throughout to add statements to the statement-tree, rather than generating RTL after every statement. * c-semantics.c (lang_expand_decl_stmt): Define. (add_decl_stmt): New function. (add_scope_stmt): Likewise. (finish_stmt_tree): Tweak. (genrtl_expr_stmt): Likewise. (genrtl_decl_stmt): Handle local labels, and call lang_expand_decl_stmt if required. (genrtl_for_stmt): Fix line-number handling. (genrtl_case_label): Handle cleanups. (genrtl_asm_stmt): Don't call combine_strings. (genrtl_compound_stmt): Simplify. (expand_stmt): Handle SCOPE_STMTs. * c-tree.h (struct lang_decl): New structure. (C_DECLARED_LABEL_FLAG): Remove. (c_begin_compound_stmt): Declare. (c_expand_decl_stmt): Likewise. (c_expand_start_case): Rename to c_start_case. (c_finish_case): New function. * c-typeck.c (start_init): Tweak setting of constructor_incremental. (c_expand_asm_operands): Tweak error-handling. Add to the statement-tree. (c_expand_return): Add to the statement-tree. (c_expand_start_case): Rename to ... (c_start_case): ... this. (struct c_switch): New type. (switch_stack): New variable. (do_case): Simplify. (c_finish_case): New function. * dependence.c: Include expr.h. (enum dependence_type): Change spelling of enumerals. (check_node_dependence): Adjust. * expr.h (lang_safe_from_p): Declare. (safe_from_p): Likewise. * expr.c (lang_safe_from_p): New variable. (safe_from_p): Give it external linkage. Use lang_safe_from_p. * stmt.c (expand_expr_stmt): Avoid clobberring of last_expr_type. * toplev.c (rest_of_decl_compilation): Robustify. * tree.c (contains_placeholder_p): Likewise. * Makefile.in: Update dependencies. * objc/objc-act.h: Adjust calculation of value for dummy_tree_code. * objc/objc-act.c: Include rtl.h, expr.h, and c-common.h. (objc_expand_function_end): New function. (finish_method_def): Use it. (init_objc): Initialize more language-specific hooks. * objc/Make-lang.in: Update dependencies. * cp-tree.h (struct cp_language_function): Remove x_scope_stmt_stack and name_declared. (current_scope_stmt_stack): Remove. (function_name_declared_p): New macro. (struct lang_decl_flags): Use c_lang_decl as a base class. (context): Remove. (struct lang_decl): Replace saved_tree with context. (DECL_FRIEND_CONTEXT): Adjust accordingly. (SET_DECL_FRIEND_CONTEXT): Likewise. (DECL_VIRTUAL_CONTEXT): Likewise. (DECL_SAVED_TREE): Remove. (C_DECLARED_LABEL_FLAG): Likewise. (cplus_expand_expr_stmt): Don't declare. (add_decl_stmt): Likewise. (add_scope_stmt): Likewise. * decl.c (mark_stmt_tree): Remove. (case_compare): Likewise. (finish_case_label): Use c_add_case_label. (init_decl_processing): Set more language-specific hooks. (build_enumerator): Fix typo in comment. (cplus_expand_expr_stmt): Remove. (mark_lang_function): Use mark_c_language_function. (lang_mark_tree): Use c_mark_lang_decl. * decl2.c: Change order of inclusion. * except.c: Likewise. * expr.c (cplus_expand_expr): Remove handling of STMT_EXPR. Fall back on c_expand_expr. * friend.c: Include expr.h. * init.c: Change order of inclusion. * Makefile.in: Update dependencies. * lex.h (free_lang_decl_chain): Remove. * optimize.c (maybe_clone_body): Use function_name_declared_p. * pt.c (build_template_decl): Don't copy DECL_VIRTUAL_CONTEXT if it doesn't exist. (instantiate_decl): Use function_name_declared_p. * semantics.c (lang_expand_expr_stmt): Remove. (set_current_function_name_declared): Likewise. (current_function_name_declared): Likewise. (begin_compound_stmt): Use function_name_declared_p. (add_decl_stmt): Remove. (setup_vtbl_ptr): Use function_name_declared_p. (add_scope_stmt): Remove. (current_scope_stmt_stack): New function. (cp_expand_stmt): Don't handle SCOPE_STMTs. (expand_body): Use function_name_declared_p. * tree.c (cp_statement_code_p): Don't include SCOPE_STMT. * typeck.c: Change order of includes. (convert_sequence): Remove. From-SVN: r36464
2000-09-17 09:38:23 +02:00
/* Otherwise, find the leftmost element of the right subtree. */
node = sp->root->left;
if (node)
while (node->right)
node = node->right;
return node;
}
/* Return the immediate successor KEY, or NULL if there is no
predecessor. KEY need not be present in the tree. */
splay_tree_node
splay_tree_successor (sp, key)
splay_tree sp;
splay_tree_key key;
{
int comparison;
splay_tree_node node;
/* If the tree is empty, there is certainly no predecessor. */
if (!sp->root)
return NULL;
/* Splay the tree around KEY. That will leave either the KEY
itself, its predecessor, or its successor at the root. */
splay_tree_splay (sp, key);
comparison = (*sp->comp)(sp->root->key, key);
/* If the successor is at the root, just return it. */
if (comparison > 0)
return sp->root;
/* Otherwise, find the rightmost element of the left subtree. */
node = sp->root->right;
if (node)
while (node->left)
node = node->left;
return node;
}
/* Call FN, passing it the DATA, for every node in SP, following an
in-order traversal. If FN every returns a non-zero value, the
iteration ceases immediately, and the value is returned.
Otherwise, this function returns 0. */
int
splay_tree_foreach (sp, fn, data)
splay_tree sp;
splay_tree_foreach_fn fn;
void *data;
{
return splay_tree_foreach_helper (sp, sp->root, fn, data);
}
/* Splay-tree comparison function, treating the keys as ints. */
int
splay_tree_compare_ints (k1, k2)
splay_tree_key k1;
splay_tree_key k2;
{
if ((int) k1 < (int) k2)
return -1;
else if ((int) k1 > (int) k2)
return 1;
else
return 0;
}
/* Splay-tree comparison function, treating the keys as pointers. */
int
splay_tree_compare_pointers (k1, k2)
splay_tree_key k1;
splay_tree_key k2;
{
if ((char*) k1 < (char*) k2)
return -1;
else if ((char*) k1 > (char*) k2)
return 1;
else
return 0;
}