OSDN Git Service

2004-06-29 Frank Ch. Eigler <fche@redhat.com>
[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.
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 #ifndef _SPLAY_TREE_H
32 #define _SPLAY_TREE_H
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37
38 #define PARAMS(X) X
39 #define PTR  void *
40 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
41
42 #ifndef GTY
43 #define GTY(X)
44 #endif
45
46 /* Use typedefs for the key and data types to facilitate changing
47    these types, if necessary.  These types should be sufficiently wide
48    that any pointer or scalar can be cast to these types, and then
49    cast back, without loss of precision.  */
50 typedef uintptr_t splay_tree_key;
51 typedef void *splay_tree_value;
52
53 /* Forward declaration for a node in the tree.  */
54 typedef struct splay_tree_node_s *splay_tree_node;
55
56 /* The type of a function used to iterate over the tree.  */
57 typedef int (*splay_tree_foreach_fn) PARAMS((splay_tree_node, void*));
58
59 /* The type of a function used to allocate memory for tree root and
60    node structures.  The first argument is the number of bytes needed;
61    the second is a data pointer the splay tree functions pass through
62    to the allocator.  This function must never return zero.  */
63 typedef PTR (*splay_tree_allocate_fn) PARAMS((int, void *));
64
65 /* The type of a function used to free memory allocated using the
66    corresponding splay_tree_allocate_fn.  The first argument is the
67    memory to be freed; the latter is a data pointer the splay tree
68    functions pass through to the freer.  */
69 typedef void (*splay_tree_deallocate_fn) PARAMS((void *, void *));
70
71 /* The nodes in the splay tree.  */
72 struct splay_tree_node_s GTY(())
73 {
74   /* The key.  */
75   splay_tree_key GTY ((use_param1)) key;
76
77   /* The value.  */
78   splay_tree_value GTY ((use_param2)) value;
79
80   /* The left and right children, respectively.  */
81   splay_tree_node GTY ((use_params)) left;
82   splay_tree_node GTY ((use_params)) right;
83 };
84
85 /* The splay tree itself.  */
86 struct splay_tree_s GTY(())
87 {
88   /* The root of the tree.  */
89   splay_tree_node GTY ((use_params)) root;
90
91   /* Allocate/free functions, and a data pointer to pass to them.  */
92   splay_tree_allocate_fn allocate;
93   splay_tree_deallocate_fn deallocate;
94   PTR GTY((skip)) allocate_data;
95
96   /* The last key value for which the tree has been splayed, but not
97      since modified.  */
98   splay_tree_key GTY ((use_param1)) last_splayed_key;
99   int last_splayed_key_p;
100 };
101 typedef struct splay_tree_s *splay_tree;
102
103 extern splay_tree splay_tree_new        PARAMS((void));
104 extern void splay_tree_delete           PARAMS((splay_tree));
105 extern splay_tree_node splay_tree_insert          
106                                         PARAMS((splay_tree,
107                                                 splay_tree_key,
108                                                 splay_tree_value));
109 extern void splay_tree_remove           PARAMS((splay_tree,
110                                                 splay_tree_key));
111 extern splay_tree_node splay_tree_lookup   
112                                         PARAMS((splay_tree,
113                                                 splay_tree_key));
114 extern splay_tree_node splay_tree_predecessor
115                                         PARAMS((splay_tree,
116                                                 splay_tree_key));
117 extern splay_tree_node splay_tree_successor
118                                         PARAMS((splay_tree,
119                                                 splay_tree_key));
120 extern splay_tree_node splay_tree_max
121                                         PARAMS((splay_tree));
122 extern splay_tree_node splay_tree_min
123                                         PARAMS((splay_tree));
124 extern int splay_tree_foreach           PARAMS((splay_tree,
125                                                 splay_tree_foreach_fn,
126                                                 void*));
127 extern int splay_tree_compare_ints      PARAMS((splay_tree_key,
128                                                 splay_tree_key));
129 extern int splay_tree_compare_pointers  PARAMS((splay_tree_key,
130                                                 splay_tree_key));
131                                                
132 #ifdef __cplusplus
133 }
134 #endif /* __cplusplus */
135
136 #endif /* _SPLAY_TREE_H */