1 /* A splay-tree datatype.
2 Copyright (C) 1998, 1999, 2000, 2001 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
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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. */
37 #include "libiberty.h"
38 #include "splay-tree.h"
40 static void splay_tree_delete_helper (splay_tree
, splay_tree_node
);
41 static void splay_tree_splay (splay_tree
, splay_tree_key
);
42 static splay_tree_node
splay_tree_splay_helper (splay_tree
,
47 static int splay_tree_foreach_helper (splay_tree
, splay_tree_node
,
48 splay_tree_foreach_fn
, void*);
50 /* Deallocate NODE (a member of SP), and all its sub-trees. */
53 splay_tree_delete_helper (splay_tree sp
, splay_tree_node node
)
55 splay_tree_node pending
= 0;
56 splay_tree_node active
= 0;
61 #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
62 #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
67 /* We use the "key" field to hold the "next" pointer. */
68 node
->key
= (splay_tree_key
)pending
;
69 pending
= (splay_tree_node
)node
;
71 /* Now, keep processing the pending list until there aren't any
72 more. This is a little more complicated than just recursing, but
73 it doesn't toast the stack for large trees. */
83 /* active points to a node which has its key and value
84 deallocated, we just need to process left and right. */
88 KDEL (active
->left
->key
);
89 VDEL (active
->left
->value
);
90 active
->left
->key
= (splay_tree_key
)pending
;
91 pending
= (splay_tree_node
)(active
->left
);
95 KDEL (active
->right
->key
);
96 VDEL (active
->right
->value
);
97 active
->right
->key
= (splay_tree_key
)pending
;
98 pending
= (splay_tree_node
)(active
->right
);
102 active
= (splay_tree_node
)(temp
->key
);
103 (*sp
->deallocate
) ((char*) temp
, sp
->allocate_data
);
110 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
111 and grandparent, respectively, of NODE. */
113 static splay_tree_node
114 splay_tree_splay_helper (splay_tree sp
, splay_tree_key key
,
115 splay_tree_node
*node
, splay_tree_node
*parent
,
116 splay_tree_node
*grandparent
)
118 splay_tree_node
*next
;
127 comparison
= (*sp
->comp
) (key
, n
->key
);
130 /* We've found the target. */
132 else if (comparison
< 0)
133 /* The target is to the left. */
136 /* The target is to the right. */
141 /* Continue down the tree. */
142 n
= splay_tree_splay_helper (sp
, key
, next
, node
, parent
);
144 /* The recursive call will change the place to which NODE
151 /* NODE is the root. We are done. */
154 /* First, handle the case where there is no grandparent (i.e.,
155 *PARENT is the root of the tree.) */
158 if (n
== (*parent
)->left
)
172 /* Next handle the cases where both N and *PARENT are left children,
173 or where both are right children. */
174 if (n
== (*parent
)->left
&& *parent
== (*grandparent
)->left
)
176 splay_tree_node p
= *parent
;
178 (*grandparent
)->left
= p
->right
;
179 p
->right
= *grandparent
;
185 else if (n
== (*parent
)->right
&& *parent
== (*grandparent
)->right
)
187 splay_tree_node p
= *parent
;
189 (*grandparent
)->right
= p
->left
;
190 p
->left
= *grandparent
;
197 /* Finally, deal with the case where N is a left child, but *PARENT
198 is a right child, or vice versa. */
199 if (n
== (*parent
)->left
)
201 (*parent
)->left
= n
->right
;
203 (*grandparent
)->right
= n
->left
;
204 n
->left
= *grandparent
;
210 (*parent
)->right
= n
->left
;
212 (*grandparent
)->left
= n
->right
;
213 n
->right
= *grandparent
;
219 /* Splay SP around KEY. */
222 splay_tree_splay (splay_tree sp
, splay_tree_key key
)
227 splay_tree_splay_helper (sp
, key
, &sp
->root
,
228 /*grandparent=*/0, /*parent=*/0);
231 /* Call FN, passing it the DATA, for every node below NODE, all of
232 which are from SP, following an in-order traversal. If FN every
233 returns a non-zero value, the iteration ceases immediately, and the
234 value is returned. Otherwise, this function returns 0. */
237 splay_tree_foreach_helper (splay_tree sp
, splay_tree_node node
,
238 splay_tree_foreach_fn fn
, void *data
)
245 val
= splay_tree_foreach_helper (sp
, node
->left
, fn
, data
);
249 val
= (*fn
)(node
, data
);
253 return splay_tree_foreach_helper (sp
, node
->right
, fn
, data
);
257 /* An allocator and deallocator based on xmalloc. */
259 splay_tree_xmalloc_allocate (int size
, void *data ATTRIBUTE_UNUSED
)
261 return (void *) xmalloc (size
);
265 splay_tree_xmalloc_deallocate (void *object
, void *data ATTRIBUTE_UNUSED
)
271 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
272 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
273 values. Use xmalloc to allocate the splay tree structure, and any
277 splay_tree_new (splay_tree_compare_fn compare_fn
,
278 splay_tree_delete_key_fn delete_key_fn
,
279 splay_tree_delete_value_fn delete_value_fn
)
281 return (splay_tree_new_with_allocator
282 (compare_fn
, delete_key_fn
, delete_value_fn
,
283 splay_tree_xmalloc_allocate
, splay_tree_xmalloc_deallocate
, 0));
287 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
288 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
292 splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn
,
293 splay_tree_delete_key_fn delete_key_fn
,
294 splay_tree_delete_value_fn delete_value_fn
,
295 splay_tree_allocate_fn allocate_fn
,
296 splay_tree_deallocate_fn deallocate_fn
,
299 splay_tree sp
= (splay_tree
) (*allocate_fn
) (sizeof (struct splay_tree_s
),
302 sp
->comp
= compare_fn
;
303 sp
->delete_key
= delete_key_fn
;
304 sp
->delete_value
= delete_value_fn
;
305 sp
->allocate
= allocate_fn
;
306 sp
->deallocate
= deallocate_fn
;
307 sp
->allocate_data
= allocate_data
;
315 splay_tree_delete (splay_tree sp
)
317 splay_tree_delete_helper (sp
, sp
->root
);
318 (*sp
->deallocate
) ((char*) sp
, sp
->allocate_data
);
321 /* Insert a new node (associating KEY with DATA) into SP. If a
322 previous node with the indicated KEY exists, its data is replaced
323 with the new value. Returns the new node. */
326 splay_tree_insert (splay_tree sp
, splay_tree_key key
, splay_tree_value value
)
330 splay_tree_splay (sp
, key
);
333 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
335 if (sp
->root
&& comparison
== 0)
337 /* If the root of the tree already has the indicated KEY, just
338 replace the value with VALUE. */
339 if (sp
->delete_value
)
340 (*sp
->delete_value
)(sp
->root
->value
);
341 sp
->root
->value
= value
;
345 /* Create a new node, and insert it at the root. */
346 splay_tree_node node
;
348 node
= ((splay_tree_node
)
349 (*sp
->allocate
) (sizeof (struct splay_tree_node_s
),
355 node
->left
= node
->right
= 0;
356 else if (comparison
< 0)
358 node
->left
= sp
->root
;
359 node
->right
= node
->left
->right
;
360 node
->left
->right
= 0;
364 node
->right
= sp
->root
;
365 node
->left
= node
->right
->left
;
366 node
->right
->left
= 0;
375 /* Remove KEY from SP. It is not an error if it did not exist. */
378 splay_tree_remove (splay_tree sp
, splay_tree_key key
)
380 splay_tree_splay (sp
, key
);
382 if (sp
->root
&& (*sp
->comp
) (sp
->root
->key
, key
) == 0)
384 splay_tree_node left
, right
;
386 left
= sp
->root
->left
;
387 right
= sp
->root
->right
;
389 /* Delete the root node itself. */
390 if (sp
->delete_value
)
391 (*sp
->delete_value
) (sp
->root
->value
);
392 (*sp
->deallocate
) (sp
->root
, sp
->allocate_data
);
394 /* One of the children is now the root. Doesn't matter much
395 which, so long as we preserve the properties of the tree. */
400 /* If there was a right child as well, hang it off the
401 right-most leaf of the left child. */
414 /* Lookup KEY in SP, returning VALUE if present, and NULL
418 splay_tree_lookup (splay_tree sp
, splay_tree_key key
)
420 splay_tree_splay (sp
, key
);
422 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
428 /* Return the node in SP with the greatest key. */
431 splay_tree_max (splay_tree sp
)
433 splay_tree_node n
= sp
->root
;
444 /* Return the node in SP with the smallest key. */
447 splay_tree_min (splay_tree sp
)
449 splay_tree_node n
= sp
->root
;
460 /* Return the immediate predecessor KEY, or NULL if there is no
461 predecessor. KEY need not be present in the tree. */
464 splay_tree_predecessor (splay_tree sp
, splay_tree_key key
)
467 splay_tree_node node
;
469 /* If the tree is empty, there is certainly no predecessor. */
473 /* Splay the tree around KEY. That will leave either the KEY
474 itself, its predecessor, or its successor at the root. */
475 splay_tree_splay (sp
, key
);
476 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
478 /* If the predecessor is at the root, just return it. */
482 /* Otherwise, find the rightmost element of the left subtree. */
483 node
= sp
->root
->left
;
491 /* Return the immediate successor KEY, or NULL if there is no
492 successor. KEY need not be present in the tree. */
495 splay_tree_successor (splay_tree sp
, splay_tree_key key
)
498 splay_tree_node node
;
500 /* If the tree is empty, there is certainly no successor. */
504 /* Splay the tree around KEY. That will leave either the KEY
505 itself, its predecessor, or its successor at the root. */
506 splay_tree_splay (sp
, key
);
507 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
509 /* If the successor is at the root, just return it. */
513 /* Otherwise, find the leftmost element of the right subtree. */
514 node
= sp
->root
->right
;
522 /* Call FN, passing it the DATA, for every node in SP, following an
523 in-order traversal. If FN every returns a non-zero value, the
524 iteration ceases immediately, and the value is returned.
525 Otherwise, this function returns 0. */
528 splay_tree_foreach (splay_tree sp
, splay_tree_foreach_fn fn
, void *data
)
530 return splay_tree_foreach_helper (sp
, sp
->root
, fn
, data
);
533 /* Splay-tree comparison function, treating the keys as ints. */
536 splay_tree_compare_ints (splay_tree_key k1
, splay_tree_key k2
)
538 if ((int) k1
< (int) k2
)
540 else if ((int) k1
> (int) k2
)
546 /* Splay-tree comparison function, treating the keys as pointers. */
549 splay_tree_compare_pointers (splay_tree_key k1
, splay_tree_key k2
)
551 if ((char*) k1
< (char*) k2
)
553 else if ((char*) k1
> (char*) k2
)