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
PARAMS((splay_tree
,
42 static void splay_tree_splay
PARAMS((splay_tree
,
44 static splay_tree_node splay_tree_splay_helper
50 static int splay_tree_foreach_helper
PARAMS((splay_tree
,
52 splay_tree_foreach_fn
,
55 /* Deallocate NODE (a member of SP), and all its sub-trees. */
58 splay_tree_delete_helper (sp
, node
)
62 splay_tree_node pending
= 0;
63 splay_tree_node active
= 0;
68 #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
69 #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
74 /* We use the "key" field to hold the "next" pointer. */
75 node
->key
= (splay_tree_key
)pending
;
76 pending
= (splay_tree_node
)node
;
78 /* Now, keep processing the pending list until there aren't any
79 more. This is a little more complicated than just recursing, but
80 it doesn't toast the stack for large trees. */
90 /* active points to a node which has its key and value
91 deallocated, we just need to process left and right. */
95 KDEL (active
->left
->key
);
96 VDEL (active
->left
->value
);
97 active
->left
->key
= (splay_tree_key
)pending
;
98 pending
= (splay_tree_node
)(active
->left
);
102 KDEL (active
->right
->key
);
103 VDEL (active
->right
->value
);
104 active
->right
->key
= (splay_tree_key
)pending
;
105 pending
= (splay_tree_node
)(active
->right
);
109 active
= (splay_tree_node
)(temp
->key
);
110 (*sp
->deallocate
) ((char*) temp
, sp
->allocate_data
);
117 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
118 and grandparent, respectively, of NODE. */
120 static splay_tree_node
121 splay_tree_splay_helper (sp
, key
, node
, parent
, grandparent
)
124 splay_tree_node
*node
;
125 splay_tree_node
*parent
;
126 splay_tree_node
*grandparent
;
128 splay_tree_node
*next
;
137 comparison
= (*sp
->comp
) (key
, n
->key
);
140 /* We've found the target. */
142 else if (comparison
< 0)
143 /* The target is to the left. */
146 /* The target is to the right. */
151 /* Continue down the tree. */
152 n
= splay_tree_splay_helper (sp
, key
, next
, node
, parent
);
154 /* The recursive call will change the place to which NODE
161 /* NODE is the root. We are done. */
164 /* First, handle the case where there is no grandparent (i.e.,
165 *PARENT is the root of the tree.) */
168 if (n
== (*parent
)->left
)
182 /* Next handle the cases where both N and *PARENT are left children,
183 or where both are right children. */
184 if (n
== (*parent
)->left
&& *parent
== (*grandparent
)->left
)
186 splay_tree_node p
= *parent
;
188 (*grandparent
)->left
= p
->right
;
189 p
->right
= *grandparent
;
195 else if (n
== (*parent
)->right
&& *parent
== (*grandparent
)->right
)
197 splay_tree_node p
= *parent
;
199 (*grandparent
)->right
= p
->left
;
200 p
->left
= *grandparent
;
207 /* Finally, deal with the case where N is a left child, but *PARENT
208 is a right child, or vice versa. */
209 if (n
== (*parent
)->left
)
211 (*parent
)->left
= n
->right
;
213 (*grandparent
)->right
= n
->left
;
214 n
->left
= *grandparent
;
220 (*parent
)->right
= n
->left
;
222 (*grandparent
)->left
= n
->right
;
223 n
->right
= *grandparent
;
229 /* Splay SP around KEY. */
232 splay_tree_splay (sp
, key
)
239 splay_tree_splay_helper (sp
, key
, &sp
->root
,
240 /*grandparent=*/0, /*parent=*/0);
243 /* Call FN, passing it the DATA, for every node below NODE, all of
244 which are from SP, following an in-order traversal. If FN every
245 returns a non-zero value, the iteration ceases immediately, and the
246 value is returned. Otherwise, this function returns 0. */
249 splay_tree_foreach_helper (sp
, node
, fn
, data
)
251 splay_tree_node node
;
252 splay_tree_foreach_fn fn
;
260 val
= splay_tree_foreach_helper (sp
, node
->left
, fn
, data
);
264 val
= (*fn
)(node
, data
);
268 return splay_tree_foreach_helper (sp
, node
->right
, fn
, data
);
272 /* An allocator and deallocator based on xmalloc. */
274 splay_tree_xmalloc_allocate (size
, data
)
276 void *data ATTRIBUTE_UNUSED
;
278 return (void *) xmalloc (size
);
282 splay_tree_xmalloc_deallocate (object
, data
)
284 void *data ATTRIBUTE_UNUSED
;
290 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
291 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
292 values. Use xmalloc to allocate the splay tree structure, and any
296 splay_tree_new (compare_fn
, delete_key_fn
, delete_value_fn
)
297 splay_tree_compare_fn compare_fn
;
298 splay_tree_delete_key_fn delete_key_fn
;
299 splay_tree_delete_value_fn delete_value_fn
;
301 return (splay_tree_new_with_allocator
302 (compare_fn
, delete_key_fn
, delete_value_fn
,
303 splay_tree_xmalloc_allocate
, splay_tree_xmalloc_deallocate
, 0));
307 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
308 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
312 splay_tree_new_with_allocator (compare_fn
, delete_key_fn
, delete_value_fn
,
313 allocate_fn
, deallocate_fn
, allocate_data
)
314 splay_tree_compare_fn compare_fn
;
315 splay_tree_delete_key_fn delete_key_fn
;
316 splay_tree_delete_value_fn delete_value_fn
;
317 splay_tree_allocate_fn allocate_fn
;
318 splay_tree_deallocate_fn deallocate_fn
;
321 splay_tree sp
= (splay_tree
) (*allocate_fn
) (sizeof (struct splay_tree_s
),
324 sp
->comp
= compare_fn
;
325 sp
->delete_key
= delete_key_fn
;
326 sp
->delete_value
= delete_value_fn
;
327 sp
->allocate
= allocate_fn
;
328 sp
->deallocate
= deallocate_fn
;
329 sp
->allocate_data
= allocate_data
;
337 splay_tree_delete (sp
)
340 splay_tree_delete_helper (sp
, sp
->root
);
341 (*sp
->deallocate
) ((char*) sp
, sp
->allocate_data
);
344 /* Insert a new node (associating KEY with DATA) into SP. If a
345 previous node with the indicated KEY exists, its data is replaced
346 with the new value. Returns the new node. */
349 splay_tree_insert (sp
, key
, value
)
352 splay_tree_value value
;
356 splay_tree_splay (sp
, key
);
359 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
361 if (sp
->root
&& comparison
== 0)
363 /* If the root of the tree already has the indicated KEY, just
364 replace the value with VALUE. */
365 if (sp
->delete_value
)
366 (*sp
->delete_value
)(sp
->root
->value
);
367 sp
->root
->value
= value
;
371 /* Create a new node, and insert it at the root. */
372 splay_tree_node node
;
374 node
= ((splay_tree_node
)
375 (*sp
->allocate
) (sizeof (struct splay_tree_node_s
),
381 node
->left
= node
->right
= 0;
382 else if (comparison
< 0)
384 node
->left
= sp
->root
;
385 node
->right
= node
->left
->right
;
386 node
->left
->right
= 0;
390 node
->right
= sp
->root
;
391 node
->left
= node
->right
->left
;
392 node
->right
->left
= 0;
401 /* Remove KEY from SP. It is not an error if it did not exist. */
404 splay_tree_remove (sp
, key
)
408 splay_tree_splay (sp
, key
);
410 if (sp
->root
&& (*sp
->comp
) (sp
->root
->key
, key
) == 0)
412 splay_tree_node left
, right
;
414 left
= sp
->root
->left
;
415 right
= sp
->root
->right
;
417 /* Delete the root node itself. */
418 if (sp
->delete_value
)
419 (*sp
->delete_value
) (sp
->root
->value
);
420 (*sp
->deallocate
) (sp
->root
, sp
->allocate_data
);
422 /* One of the children is now the root. Doesn't matter much
423 which, so long as we preserve the properties of the tree. */
428 /* If there was a right child as well, hang it off the
429 right-most leaf of the left child. */
442 /* Lookup KEY in SP, returning VALUE if present, and NULL
446 splay_tree_lookup (sp
, key
)
450 splay_tree_splay (sp
, key
);
452 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
458 /* Return the node in SP with the greatest key. */
464 splay_tree_node n
= sp
->root
;
475 /* Return the node in SP with the smallest key. */
481 splay_tree_node n
= sp
->root
;
492 /* Return the immediate predecessor KEY, or NULL if there is no
493 predecessor. KEY need not be present in the tree. */
496 splay_tree_predecessor (sp
, key
)
501 splay_tree_node node
;
503 /* If the tree is empty, there is certainly no predecessor. */
507 /* Splay the tree around KEY. That will leave either the KEY
508 itself, its predecessor, or its successor at the root. */
509 splay_tree_splay (sp
, key
);
510 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
512 /* If the predecessor is at the root, just return it. */
516 /* Otherwise, find the rightmost element of the left subtree. */
517 node
= sp
->root
->left
;
525 /* Return the immediate successor KEY, or NULL if there is no
526 successor. KEY need not be present in the tree. */
529 splay_tree_successor (sp
, key
)
534 splay_tree_node node
;
536 /* If the tree is empty, there is certainly no successor. */
540 /* Splay the tree around KEY. That will leave either the KEY
541 itself, its predecessor, or its successor at the root. */
542 splay_tree_splay (sp
, key
);
543 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
545 /* If the successor is at the root, just return it. */
549 /* Otherwise, find the leftmost element of the right subtree. */
550 node
= sp
->root
->right
;
558 /* Call FN, passing it the DATA, for every node in SP, following an
559 in-order traversal. If FN every returns a non-zero value, the
560 iteration ceases immediately, and the value is returned.
561 Otherwise, this function returns 0. */
564 splay_tree_foreach (sp
, fn
, data
)
566 splay_tree_foreach_fn fn
;
569 return splay_tree_foreach_helper (sp
, sp
->root
, fn
, data
);
572 /* Splay-tree comparison function, treating the keys as ints. */
575 splay_tree_compare_ints (k1
, k2
)
579 if ((int) k1
< (int) k2
)
581 else if ((int) k1
> (int) k2
)
587 /* Splay-tree comparison function, treating the keys as pointers. */
590 splay_tree_compare_pointers (k1
, k2
)
594 if ((char*) k1
< (char*) k2
)
596 else if ((char*) k1
> (char*) k2
)