1
1
/* A splay-tree datatype.
2
Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
3
Contributed by Mark Mitchell (mark@markmitchell.com).
5
5
This file is part of GNU CC.
225
227
return splay_tree_foreach_helper (sp, node->right, fn, data);
231
/* An allocator and deallocator based on xmalloc. */
233
splay_tree_xmalloc_allocate (size, data)
235
void *data ATTRIBUTE_UNUSED;
237
return (void *) xmalloc (size);
241
splay_tree_xmalloc_deallocate (object, data)
243
void *data ATTRIBUTE_UNUSED;
249
/* Allocate a new splay tree, using COMPARE_FN to compare nodes,
250
DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
251
values. Use xmalloc to allocate the splay tree structure, and any
255
splay_tree_new (compare_fn, delete_key_fn, delete_value_fn)
256
splay_tree_compare_fn compare_fn;
257
splay_tree_delete_key_fn delete_key_fn;
258
splay_tree_delete_value_fn delete_value_fn;
260
return (splay_tree_new_with_allocator
261
(compare_fn, delete_key_fn, delete_value_fn,
262
splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
228
266
/* Allocate a new splay tree, using COMPARE_FN to compare nodes,
229
267
DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
233
splay_tree_new (compare_fn, delete_key_fn, delete_value_fn)
271
splay_tree_new_with_allocator (compare_fn, delete_key_fn, delete_value_fn,
272
allocate_fn, deallocate_fn, allocate_data)
234
273
splay_tree_compare_fn compare_fn;
235
274
splay_tree_delete_key_fn delete_key_fn;
236
275
splay_tree_delete_value_fn delete_value_fn;
276
splay_tree_allocate_fn allocate_fn;
277
splay_tree_deallocate_fn deallocate_fn;
238
splay_tree sp = (splay_tree) xmalloc (sizeof (struct splay_tree_s));
280
splay_tree sp = (splay_tree) (*allocate_fn) (sizeof (struct splay_tree_s),
240
283
sp->comp = compare_fn;
241
284
sp->delete_key = delete_key_fn;
242
285
sp->delete_value = delete_value_fn;
286
sp->allocate = allocate_fn;
287
sp->deallocate = deallocate_fn;
288
sp->allocate_data = allocate_data;
328
376
/* Delete the root node itself. */
329
377
if (sp->delete_value)
330
378
(*sp->delete_value) (sp->root->value);
379
(*sp->deallocate) (sp->root, sp->allocate_data);
333
381
/* One of the children is now the root. Doesn't matter much
334
382
which, so long as we preserve the properties of the tree. */
417
/* Return the node in SP with the greatest key. */
423
splay_tree_node n = sp->root;
434
/* Return the node in SP with the smallest key. */
440
splay_tree_node n = sp->root;
451
/* Return the immediate predecessor KEY, or NULL if there is no
452
predecessor. KEY need not be present in the tree. */
455
splay_tree_predecessor (sp, key)
460
splay_tree_node node;
462
/* If the tree is empty, there is certainly no predecessor. */
466
/* Splay the tree around KEY. That will leave either the KEY
467
itself, its predecessor, or its successor at the root. */
468
splay_tree_splay (sp, key);
469
comparison = (*sp->comp)(sp->root->key, key);
471
/* If the predecessor is at the root, just return it. */
475
/* Otherwise, find the rightmost element of the left subtree. */
476
node = sp->root->left;
484
/* Return the immediate successor KEY, or NULL if there is no
485
successor. KEY need not be present in the tree. */
488
splay_tree_successor (sp, key)
493
splay_tree_node node;
495
/* If the tree is empty, there is certainly no successor. */
499
/* Splay the tree around KEY. That will leave either the KEY
500
itself, its predecessor, or its successor at the root. */
501
splay_tree_splay (sp, key);
502
comparison = (*sp->comp)(sp->root->key, key);
504
/* If the successor is at the root, just return it. */
508
/* Otherwise, find the leftmost element of the right subtree. */
509
node = sp->root->right;
369
517
/* Call FN, passing it the DATA, for every node in SP, following an
370
518
in-order traversal. If FN every returns a non-zero value, the
371
519
iteration ceases immediately, and the value is returned.