OSDN Git Service

742eb4bb888f27962eb776580bf5006e47aa9faa
[pf3gnuchains/gcc-fork.git] / libmudflap / splay-tree.h
1 /* A splay-tree datatype.  
2    Copyright 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Mark Mitchell (mark@markmitchell.com).
4    Adapted for libmudflap from libiberty by Frank Ch. Eigler <fche@redhat.com>.
5
6 This file is part of GCC.
7    
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* For an easily readable description of splay-trees, see:
24
25      Lewis, Harry R. and Denenberg, Larry.  Data Structures and Their
26      Algorithms.  Harper-Collins, Inc.  1991.  
27
28    The major feature of splay trees is that all basic tree operations
29    are amortized O(log n) time for a tree with n nodes.  
30
31    This version has been further modified to periodically rebalance
32    the entire tree, should degenerate access patterns result in a very
33    lopsided tree.
34 */
35
36 #ifndef _SPLAY_TREE_H
37 #define _SPLAY_TREE_H
38
39 /* Use typedefs for the key and data types to facilitate changing
40    these types, if necessary.  These types should be sufficiently wide
41    that any pointer or scalar can be cast to these types, and then
42    cast back, without loss of precision.  */
43 typedef uintptr_t splay_tree_key;
44 typedef void *splay_tree_value;
45
46 /* Forward declaration for a node in the tree.  */
47 typedef struct splay_tree_node_s *splay_tree_node;
48
49 /* The type of a function used to iterate over the tree.  */
50 typedef int (*splay_tree_foreach_fn) (splay_tree_node, void *);
51
52 /* The nodes in the splay tree.  */
53 struct splay_tree_node_s
54 {
55   /* Data.  */
56   splay_tree_key key;
57   splay_tree_value value;
58   /* Children.  */
59   splay_tree_node left;
60   splay_tree_node right;
61 };
62
63 /* The splay tree itself.  */
64 struct splay_tree_s
65 {
66   /* The root of the tree.  */
67   splay_tree_node root;
68
69   /* The last key value for which the tree has been splayed, but not
70      since modified.  */
71   splay_tree_key last_splayed_key;
72   int last_splayed_key_p;
73
74   /* Statistics.  */
75   unsigned num_keys;
76
77   /* Traversal recursion control flags.  */
78   unsigned max_depth;
79   unsigned depth;
80   unsigned rebalance_p;
81 };
82 typedef struct splay_tree_s *splay_tree;
83
84 extern splay_tree splay_tree_new (void);
85 extern splay_tree_node splay_tree_insert (splay_tree, splay_tree_key, splay_tree_value);
86 extern void splay_tree_remove (splay_tree, splay_tree_key);
87 extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key);
88 extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key);
89 extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key);
90 extern splay_tree_node splay_tree_max (splay_tree);
91 extern splay_tree_node splay_tree_min (splay_tree);
92 extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void *);
93 extern void splay_tree_rebalance (splay_tree sp);
94
95
96 #endif /* _SPLAY_TREE_H */