1 /* A splay-tree datatype.
2 Copyright (C) 1998-2021 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 */
42 #ifdef HAVE_INTTYPES_H
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 uintptr_t splay_tree_value
;
53 /* Forward declaration for a node in the tree. */
54 typedef struct splay_tree_node_s
*splay_tree_node
;
56 /* The type of a function which compares two splay-tree keys. The
57 function should return values as for qsort. */
58 typedef int (*splay_tree_compare_fn
) (splay_tree_key
, splay_tree_key
);
60 /* The type of a function used to deallocate any resources associated
61 with the key. If you provide this function, the splay tree
62 will take the ownership of the memory of the splay_tree_key arg
63 of splay_tree_insert. This function is called to release the keys
64 present in the tree when calling splay_tree_delete or splay_tree_remove.
65 If splay_tree_insert is called with a key equal to a key already
66 present in the tree, the old key and old value will be released. */
67 typedef void (*splay_tree_delete_key_fn
) (splay_tree_key
);
69 /* The type of a function used to deallocate any resources associated
70 with the value. If you provide this function, the memory of the
71 splay_tree_value arg of splay_tree_insert is managed similarly to
72 the splay_tree_key memory: see splay_tree_delete_key_fn. */
73 typedef void (*splay_tree_delete_value_fn
) (splay_tree_value
);
75 /* The type of a function used to iterate over the tree. */
76 typedef int (*splay_tree_foreach_fn
) (splay_tree_node
, void*);
78 /* The type of a function used to allocate memory for tree root and
79 node structures. The first argument is the number of bytes needed;
80 the second is a data pointer the splay tree functions pass through
81 to the allocator. This function must never return zero. */
82 typedef void *(*splay_tree_allocate_fn
) (int, void *);
84 /* The type of a function used to free memory allocated using the
85 corresponding splay_tree_allocate_fn. The first argument is the
86 memory to be freed; the latter is a data pointer the splay tree
87 functions pass through to the freer. */
88 typedef void (*splay_tree_deallocate_fn
) (void *, void *);
90 /* The nodes in the splay tree. */
91 struct splay_tree_node_s
{
96 splay_tree_value value
;
98 /* The left and right children, respectively. */
100 splay_tree_node right
;
103 /* The splay tree itself. */
104 struct splay_tree_s
{
105 /* The root of the tree. */
106 splay_tree_node root
;
108 /* The comparision function. */
109 splay_tree_compare_fn comp
;
111 /* The deallocate-key function. NULL if no cleanup is necessary. */
112 splay_tree_delete_key_fn delete_key
;
114 /* The deallocate-value function. NULL if no cleanup is necessary. */
115 splay_tree_delete_value_fn delete_value
;
117 /* Node allocate function. Takes allocate_data as a parameter. */
118 splay_tree_allocate_fn allocate
;
120 /* Free function for nodes and trees. Takes allocate_data as a parameter. */
121 splay_tree_deallocate_fn deallocate
;
123 /* Parameter for allocate/free functions. */
127 typedef struct splay_tree_s
*splay_tree
;
129 extern splay_tree
splay_tree_new (splay_tree_compare_fn
,
130 splay_tree_delete_key_fn
,
131 splay_tree_delete_value_fn
);
132 extern splay_tree
splay_tree_new_with_allocator (splay_tree_compare_fn
,
133 splay_tree_delete_key_fn
,
134 splay_tree_delete_value_fn
,
135 splay_tree_allocate_fn
,
136 splay_tree_deallocate_fn
,
138 extern splay_tree
splay_tree_new_typed_alloc (splay_tree_compare_fn
,
139 splay_tree_delete_key_fn
,
140 splay_tree_delete_value_fn
,
141 splay_tree_allocate_fn
,
142 splay_tree_allocate_fn
,
143 splay_tree_deallocate_fn
,
145 extern void splay_tree_delete (splay_tree
);
146 extern splay_tree_node
splay_tree_insert (splay_tree
,
149 extern void splay_tree_remove (splay_tree
, splay_tree_key
);
150 extern splay_tree_node
splay_tree_lookup (splay_tree
, splay_tree_key
);
151 extern splay_tree_node
splay_tree_predecessor (splay_tree
, splay_tree_key
);
152 extern splay_tree_node
splay_tree_successor (splay_tree
, splay_tree_key
);
153 extern splay_tree_node
splay_tree_max (splay_tree
);
154 extern splay_tree_node
splay_tree_min (splay_tree
);
155 extern int splay_tree_foreach (splay_tree
, splay_tree_foreach_fn
, void*);
156 extern int splay_tree_compare_ints (splay_tree_key
, splay_tree_key
);
157 extern int splay_tree_compare_pointers (splay_tree_key
, splay_tree_key
);
158 extern int splay_tree_compare_strings (splay_tree_key
, splay_tree_key
);
159 extern void splay_tree_delete_pointers (splay_tree_value
);
163 #endif /* __cplusplus */
165 #endif /* _SPLAY_TREE_H */