1 /* A splay-tree datatype.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; see the file COPYING. If not, write to the Free
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 For an easily readable description of splay-trees, see:
23 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
24 Algorithms. Harper-Collins, Inc. 1991. */
26 #if defined (IN_GCC) || defined (HAVE_CONFIG_H)
35 #include "libiberty.h"
37 extern char* xmalloc ();
39 #include "splay-tree.h"
41 static void splay_tree_delete_helper
PARAMS((splay_tree
,
43 static void splay_tree_splay
PARAMS((splay_tree
,
45 static splay_tree_node splay_tree_splay_helper
51 static int splay_tree_foreach_helper
PARAMS((splay_tree
,
53 splay_tree_foreach_fn
,
56 /* Deallocate NODE (a member of SP), and all its sub-trees. */
59 splay_tree_delete_helper (sp
, node
)
66 splay_tree_delete_helper (sp
, node
->left
);
67 splay_tree_delete_helper (sp
, node
->right
);
70 (*sp
->delete_key
)(node
->key
);
72 (*sp
->delete_value
)(node
->value
);
77 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
78 and grandparent, respectively, of NODE. */
80 static splay_tree_node
81 splay_tree_splay_helper (sp
, key
, node
, parent
, grandparent
)
84 splay_tree_node
*node
;
85 splay_tree_node
*parent
;
86 splay_tree_node
*grandparent
;
88 splay_tree_node
*next
;
97 comparison
= (*sp
->comp
) (key
, n
->key
);
100 /* We've found the target. */
102 else if (comparison
< 0)
103 /* The target is to the left. */
106 /* The target is to the right. */
111 /* Continue down the tree. */
112 n
= splay_tree_splay_helper (sp
, key
, next
, node
, parent
);
114 /* The recursive call will change the place to which NODE
121 /* NODE is the root. We are done. */
124 /* First, handle the case where there is no grandparent (i.e.,
125 *PARENT is the root of the tree.) */
128 if (n
== (*parent
)->left
)
142 /* Next handle the cases where both N and *PARENT are left children,
143 or where both are right children. */
144 if (n
== (*parent
)->left
&& *parent
== (*grandparent
)->left
)
146 splay_tree_node p
= *parent
;
148 (*grandparent
)->left
= p
->right
;
149 p
->right
= *grandparent
;
155 else if (n
== (*parent
)->right
&& *parent
== (*grandparent
)->right
)
157 splay_tree_node p
= *parent
;
159 (*grandparent
)->right
= p
->left
;
160 p
->left
= *grandparent
;
167 /* Finally, deal with the case where N is a left child, but *PARENT
168 is a right child, or vice versa. */
169 if (n
== (*parent
)->left
)
171 (*parent
)->left
= n
->right
;
173 (*grandparent
)->right
= n
->left
;
174 n
->left
= *grandparent
;
180 (*parent
)->right
= n
->left
;
182 (*grandparent
)->left
= n
->right
;
183 n
->right
= *grandparent
;
189 /* Splay SP around KEY. */
192 splay_tree_splay (sp
, key
)
199 splay_tree_splay_helper (sp
, key
, &sp
->root
,
200 /*grandparent=*/0, /*parent=*/0);
203 /* Call FN, passing it the DATA, for every node below NODE, all of
204 which are from SP, following an in-order traversal. If FN every
205 returns a non-zero value, the iteration ceases immediately, and the
206 value is returned. Otherwise, this function returns 0. */
209 splay_tree_foreach_helper (sp
, node
, fn
, data
)
211 splay_tree_node node
;
212 splay_tree_foreach_fn fn
;
220 val
= splay_tree_foreach_helper (sp
, node
->left
, fn
, data
);
224 val
= (*fn
)(node
, data
);
228 return splay_tree_foreach_helper (sp
, node
->right
, fn
, data
);
231 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
232 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
236 splay_tree_new (compare_fn
, delete_key_fn
, delete_value_fn
)
237 splay_tree_compare_fn compare_fn
;
238 splay_tree_delete_key_fn delete_key_fn
;
239 splay_tree_delete_value_fn delete_value_fn
;
241 splay_tree sp
= (splay_tree
) xmalloc (sizeof (struct splay_tree
));
243 sp
->comp
= compare_fn
;
244 sp
->delete_key
= delete_key_fn
;
245 sp
->delete_value
= delete_value_fn
;
253 splay_tree_delete (sp
)
256 splay_tree_delete_helper (sp
, sp
->root
);
260 /* Insert a new node (associating KEY with DATA) into SP. If a
261 previous node with the indicated KEY exists, its data is replaced
262 with the new value. */
265 splay_tree_insert (sp
, key
, value
)
268 splay_tree_value value
;
272 splay_tree_splay (sp
, key
);
275 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
277 if (sp
->root
&& comparison
== 0)
279 /* If the root of the tree already has the indicated KEY, just
280 replace the value with VALUE. */
281 if (sp
->delete_value
)
282 (*sp
->delete_value
)(sp
->root
->value
);
283 sp
->root
->value
= value
;
287 /* Create a new node, and insert it at the root. */
288 splay_tree_node node
;
290 node
= (splay_tree_node
) xmalloc (sizeof (struct splay_tree_node
));
295 node
->left
= node
->right
= 0;
296 else if (comparison
< 0)
298 node
->left
= sp
->root
;
299 node
->right
= node
->left
->right
;
300 node
->left
->right
= 0;
304 node
->right
= sp
->root
;
305 node
->left
= node
->right
->left
;
306 node
->right
->left
= 0;
313 /* Lookup KEY in SP, returning VALUE if present, and NULL
317 splay_tree_lookup (sp
, key
)
321 splay_tree_splay (sp
, key
);
323 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
329 /* Call FN, passing it the DATA, for every node in SP, following an
330 in-order traversal. If FN every returns a non-zero value, the
331 iteration ceases immediately, and the value is returned.
332 Otherwise, this function returns 0. */
335 splay_tree_foreach (sp
, fn
, data
)
337 splay_tree_foreach_fn fn
;
340 return splay_tree_foreach_helper (sp
, sp
->root
, fn
, data
);