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)
34 #include "libiberty.h"
35 #include "splay-tree.h"
37 static void splay_tree_delete_helper
PARAMS((splay_tree
,
39 static void splay_tree_splay
PARAMS((splay_tree
,
41 static splay_tree_node splay_tree_splay_helper
47 static int splay_tree_foreach_helper
PARAMS((splay_tree
,
49 splay_tree_foreach_fn
,
52 /* Deallocate NODE (a member of SP), and all its sub-trees. */
55 splay_tree_delete_helper (sp
, node
)
62 splay_tree_delete_helper (sp
, node
->left
);
63 splay_tree_delete_helper (sp
, node
->right
);
66 (*sp
->delete_key
)(node
->key
);
68 (*sp
->delete_value
)(node
->value
);
73 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
74 and grandparent, respectively, of NODE. */
76 static splay_tree_node
77 splay_tree_splay_helper (sp
, key
, node
, parent
, grandparent
)
80 splay_tree_node
*node
;
81 splay_tree_node
*parent
;
82 splay_tree_node
*grandparent
;
84 splay_tree_node
*next
;
93 comparison
= (*sp
->comp
) (key
, n
->key
);
96 /* We've found the target. */
98 else if (comparison
< 0)
99 /* The target is to the left. */
102 /* The target is to the right. */
107 /* Continue down the tree. */
108 n
= splay_tree_splay_helper (sp
, key
, next
, node
, parent
);
110 /* The recursive call will change the place to which NODE
117 /* NODE is the root. We are done. */
120 /* First, handle the case where there is no grandparent (i.e.,
121 *PARENT is the root of the tree.) */
124 if (n
== (*parent
)->left
)
138 /* Next handle the cases where both N and *PARENT are left children,
139 or where both are right children. */
140 if (n
== (*parent
)->left
&& *parent
== (*grandparent
)->left
)
142 splay_tree_node p
= *parent
;
144 (*grandparent
)->left
= p
->right
;
145 p
->right
= *grandparent
;
151 else if (n
== (*parent
)->right
&& *parent
== (*grandparent
)->right
)
153 splay_tree_node p
= *parent
;
155 (*grandparent
)->right
= p
->left
;
156 p
->left
= *grandparent
;
163 /* Finally, deal with the case where N is a left child, but *PARENT
164 is a right child, or vice versa. */
165 if (n
== (*parent
)->left
)
167 (*parent
)->left
= n
->right
;
169 (*grandparent
)->right
= n
->left
;
170 n
->left
= *grandparent
;
176 (*parent
)->right
= n
->left
;
178 (*grandparent
)->left
= n
->right
;
179 n
->right
= *grandparent
;
185 /* Splay SP around KEY. */
188 splay_tree_splay (sp
, key
)
195 splay_tree_splay_helper (sp
, key
, &sp
->root
,
196 /*grandparent=*/0, /*parent=*/0);
199 /* Call FN, passing it the DATA, for every node below NODE, all of
200 which are from SP, following an in-order traversal. If FN every
201 returns a non-zero value, the iteration ceases immediately, and the
202 value is returned. Otherwise, this function returns 0. */
205 splay_tree_foreach_helper (sp
, node
, fn
, data
)
207 splay_tree_node node
;
208 splay_tree_foreach_fn fn
;
216 val
= splay_tree_foreach_helper (sp
, node
->left
, fn
, data
);
220 val
= (*fn
)(node
, data
);
224 return splay_tree_foreach_helper (sp
, node
->right
, fn
, data
);
227 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
228 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
232 splay_tree_new (compare_fn
, delete_key_fn
, delete_value_fn
)
233 splay_tree_compare_fn compare_fn
;
234 splay_tree_delete_key_fn delete_key_fn
;
235 splay_tree_delete_value_fn delete_value_fn
;
237 splay_tree sp
= (splay_tree
) xmalloc (sizeof (struct splay_tree
));
239 sp
->comp
= compare_fn
;
240 sp
->delete_key
= delete_key_fn
;
241 sp
->delete_value
= delete_value_fn
;
249 splay_tree_delete (sp
)
252 splay_tree_delete_helper (sp
, sp
->root
);
256 /* Insert a new node (associating KEY with DATA) into SP. If a
257 previous node with the indicated KEY exists, its data is replaced
258 with the new value. */
261 splay_tree_insert (sp
, key
, value
)
264 splay_tree_value value
;
268 splay_tree_splay (sp
, key
);
271 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
273 if (sp
->root
&& comparison
== 0)
275 /* If the root of the tree already has the indicated KEY, just
276 replace the value with VALUE. */
277 if (sp
->delete_value
)
278 (*sp
->delete_value
)(sp
->root
->value
);
279 sp
->root
->value
= value
;
283 /* Create a new node, and insert it at the root. */
284 splay_tree_node node
;
286 node
= (splay_tree_node
) xmalloc (sizeof (struct splay_tree_node
));
291 node
->left
= node
->right
= 0;
292 else if (comparison
< 0)
294 node
->left
= sp
->root
;
295 node
->right
= node
->left
->right
;
296 node
->left
->right
= 0;
300 node
->right
= sp
->root
;
301 node
->left
= node
->right
->left
;
302 node
->right
->left
= 0;
309 /* Lookup KEY in SP, returning VALUE if present, and NULL
313 splay_tree_lookup (sp
, key
)
317 splay_tree_splay (sp
, key
);
319 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
325 /* Call FN, passing it the DATA, for every node in SP, following an
326 in-order traversal. If FN every returns a non-zero value, the
327 iteration ceases immediately, and the value is returned.
328 Otherwise, this function returns 0. */
331 splay_tree_foreach (sp
, fn
, data
)
333 splay_tree_foreach_fn fn
;
336 return splay_tree_foreach_helper (sp
, sp
->root
, fn
, data
);