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. */
27 #include "libiberty.h"
29 #include "splay-tree.h"
31 static void splay_tree_delete_helper
PARAMS((splay_tree
,
33 static void splay_tree_splay
PARAMS((splay_tree
,
35 static splay_tree_node splay_tree_splay_helper
41 static int splay_tree_foreach_helper
PARAMS((splay_tree
,
43 splay_tree_foreach_fn
,
46 /* Deallocate NODE (a member of SP), and all its sub-trees. */
49 splay_tree_delete_helper (sp
, node
)
56 splay_tree_delete_helper (sp
, node
->left
);
57 splay_tree_delete_helper (sp
, node
->right
);
60 (*sp
->delete_key
)(node
->key
);
62 (*sp
->delete_value
)(node
->value
);
67 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
68 and grandparent, respectively, of NODE. */
70 static splay_tree_node
71 splay_tree_splay_helper (sp
, key
, node
, parent
, grandparent
)
74 splay_tree_node
*node
;
75 splay_tree_node
*parent
;
76 splay_tree_node
*grandparent
;
78 splay_tree_node
*next
;
87 comparison
= (*sp
->comp
) (key
, n
->key
);
90 /* We've found the target. */
92 else if (comparison
< 0)
93 /* The target is to the left. */
96 /* The target is to the right. */
101 /* Continue down the tree. */
102 n
= splay_tree_splay_helper (sp
, key
, next
, node
, parent
);
104 /* The recursive call will change the place to which NODE
111 /* NODE is the root. We are done. */
114 /* First, handle the case where there is no grandparent (i.e.,
115 *PARENT is the root of the tree.) */
118 if (n
== (*parent
)->left
)
132 /* Next handle the cases where both N and *PARENT are left children,
133 or where both are right children. */
134 if (n
== (*parent
)->left
&& *parent
== (*grandparent
)->left
)
136 splay_tree_node p
= *parent
;
138 (*grandparent
)->left
= p
->right
;
139 p
->right
= *grandparent
;
145 else if (n
== (*parent
)->right
&& *parent
== (*grandparent
)->right
)
147 splay_tree_node p
= *parent
;
149 (*grandparent
)->right
= p
->left
;
150 p
->left
= *grandparent
;
157 /* Finally, deal with the case where N is a left child, but *PARENT
158 is a right child, or vice versa. */
159 if (n
== (*parent
)->left
)
161 (*parent
)->left
= n
->right
;
163 (*grandparent
)->right
= n
->left
;
164 n
->left
= *grandparent
;
170 (*parent
)->right
= n
->left
;
172 (*grandparent
)->left
= n
->right
;
173 n
->right
= *grandparent
;
179 /* Splay SP around KEY. */
182 splay_tree_splay (sp
, key
)
189 splay_tree_splay_helper (sp
, key
, &sp
->root
,
190 /*grandparent=*/0, /*parent=*/0);
193 /* Call FN, passing it the DATA, for every node below NODE, all of
194 which are from SP, following an in-order traversal. If FN every
195 returns a non-zero value, the iteration ceases immediately, and the
196 value is returned. Otherwise, this function returns 0. */
199 splay_tree_foreach_helper (sp
, node
, fn
, data
)
201 splay_tree_node node
;
202 splay_tree_foreach_fn fn
;
210 val
= splay_tree_foreach_helper (sp
, node
->left
, fn
, data
);
214 val
= (*fn
)(node
, data
);
218 return splay_tree_foreach_helper (sp
, node
->right
, fn
, data
);
221 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
222 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
226 splay_tree_new (compare_fn
, delete_key_fn
, delete_value_fn
)
227 splay_tree_compare_fn compare_fn
;
228 splay_tree_delete_key_fn delete_key_fn
;
229 splay_tree_delete_value_fn delete_value_fn
;
231 splay_tree sp
= (splay_tree
) xmalloc (sizeof (struct splay_tree
));
233 sp
->comp
= compare_fn
;
234 sp
->delete_key
= delete_key_fn
;
235 sp
->delete_value
= delete_value_fn
;
243 splay_tree_delete (sp
)
246 splay_tree_delete_helper (sp
, sp
->root
);
250 /* Insert a new node (associating KEY with DATA) into SP. If a
251 previous node with the indicated KEY exists, its data is replaced
252 with the new value. */
255 splay_tree_insert (sp
, key
, value
)
258 splay_tree_value value
;
262 splay_tree_splay (sp
, key
);
265 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
267 if (sp
->root
&& comparison
== 0)
269 /* If the root of the tree already has the indicated KEY, just
270 replace the value with VALUE. */
271 if (sp
->delete_value
)
272 (*sp
->delete_value
)(sp
->root
->value
);
273 sp
->root
->value
= value
;
277 /* Create a new node, and insert it at the root. */
278 splay_tree_node node
;
280 node
= (splay_tree_node
) xmalloc (sizeof (struct splay_tree_node
));
285 node
->left
= node
->right
= 0;
286 else if (comparison
< 0)
288 node
->left
= sp
->root
;
289 node
->right
= node
->left
->right
;
290 node
->left
->right
= 0;
294 node
->right
= sp
->root
;
295 node
->left
= node
->right
->left
;
296 node
->right
->left
= 0;
303 /* Lookup KEY in SP, returning VALUE if present, and NULL
307 splay_tree_lookup (sp
, key
)
311 splay_tree_splay (sp
, key
);
313 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
319 /* Call FN, passing it the DATA, for every node in SP, following an
320 in-order traversal. If FN every returns a non-zero value, the
321 iteration ceases immediately, and the value is returned.
322 Otherwise, this function returns 0. */
325 splay_tree_foreach (sp
, fn
, data
)
327 splay_tree_foreach_fn fn
;
330 return splay_tree_foreach_helper (sp
, sp
->root
, fn
, data
);