1 /* A splay-tree datatype.
2 Copyright 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* For an easily readable description of splay-trees, see:
24 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
25 Algorithms. Harper-Collins, Inc. 1991.
27 The major feature of splay trees is that all basic tree operations
28 are amortized O(log n) time for a tree with n nodes. */
35 #endif /* __cplusplus */
41 #ifndef ATTRIBUTE_UNUSED
42 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
43 #endif /* ATTRIBUTE_UNUSED */
45 /* Use typedefs for the key and data types to facilitate changing
46 these types, if necessary. These types should be sufficiently wide
47 that any pointer or scalar can be cast to these types, and then
48 cast back, without loss of precision. */
49 typedef unsigned long int splay_tree_key
;
50 typedef unsigned long int splay_tree_value
;
52 /* Forward declaration for a node in the tree. */
53 typedef struct splay_tree_node_s
*splay_tree_node
;
55 /* The type of a function which compares two splay-tree keys. The
56 function should return values as for qsort. */
57 typedef int (*splay_tree_compare_fn
) (splay_tree_key
, splay_tree_key
);
59 /* The type of a function used to deallocate any resources associated
61 typedef void (*splay_tree_delete_key_fn
) (splay_tree_key
);
63 /* The type of a function used to deallocate any resources associated
65 typedef void (*splay_tree_delete_value_fn
) (splay_tree_value
);
67 /* The type of a function used to iterate over the tree. */
68 typedef int (*splay_tree_foreach_fn
) (splay_tree_node
, void*);
70 /* The type of a function used to allocate memory for tree root and
71 node structures. The first argument is the number of bytes needed;
72 the second is a data pointer the splay tree functions pass through
73 to the allocator. This function must never return zero. */
74 typedef void *(*splay_tree_allocate_fn
) (int, void *);
76 /* The type of a function used to free memory allocated using the
77 corresponding splay_tree_allocate_fn. The first argument is the
78 memory to be freed; the latter is a data pointer the splay tree
79 functions pass through to the freer. */
80 typedef void (*splay_tree_deallocate_fn
) (void *, void *);
82 /* The nodes in the splay tree. */
83 struct splay_tree_node_s
GTY(())
86 splay_tree_key
GTY ((use_param1
)) key
;
89 splay_tree_value
GTY ((use_param2
)) value
;
91 /* The left and right children, respectively. */
92 splay_tree_node
GTY ((use_params
)) left
;
93 splay_tree_node
GTY ((use_params
)) right
;
96 /* The splay tree itself. */
97 struct splay_tree_s
GTY(())
99 /* The root of the tree. */
100 splay_tree_node
GTY ((use_params
)) root
;
102 /* The comparision function. */
103 splay_tree_compare_fn comp
;
105 /* The deallocate-key function. NULL if no cleanup is necessary. */
106 splay_tree_delete_key_fn delete_key
;
108 /* The deallocate-value function. NULL if no cleanup is necessary. */
109 splay_tree_delete_value_fn delete_value
;
111 /* Allocate/free functions, and a data pointer to pass to them. */
112 splay_tree_allocate_fn allocate
;
113 splay_tree_deallocate_fn deallocate
;
114 void * GTY((skip
)) allocate_data
;
117 typedef struct splay_tree_s
*splay_tree
;
119 extern splay_tree
splay_tree_new (splay_tree_compare_fn
,
120 splay_tree_delete_key_fn
,
121 splay_tree_delete_value_fn
);
122 extern splay_tree
splay_tree_new_with_allocator (splay_tree_compare_fn
,
123 splay_tree_delete_key_fn
,
124 splay_tree_delete_value_fn
,
125 splay_tree_allocate_fn
,
126 splay_tree_deallocate_fn
,
128 extern void splay_tree_delete (splay_tree
);
129 extern splay_tree_node
splay_tree_insert (splay_tree
,
132 extern void splay_tree_remove (splay_tree
, splay_tree_key
);
133 extern splay_tree_node
splay_tree_lookup (splay_tree
, splay_tree_key
);
134 extern splay_tree_node
splay_tree_predecessor (splay_tree
, splay_tree_key
);
135 extern splay_tree_node
splay_tree_successor (splay_tree
, splay_tree_key
);
136 extern splay_tree_node
splay_tree_max (splay_tree
);
137 extern splay_tree_node
splay_tree_min (splay_tree
);
138 extern int splay_tree_foreach (splay_tree
, splay_tree_foreach_fn
, void*);
139 extern int splay_tree_compare_ints (splay_tree_key
, splay_tree_key
);
140 extern int splay_tree_compare_pointers (splay_tree_key
, splay_tree_key
);
144 #endif /* __cplusplus */
146 #endif /* _SPLAY_TREE_H */