Changeset 609 for branches/GNU/src/binutils/libiberty/splay-tree.c
- Timestamp:
- Aug 16, 2003, 6:59:22 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/binutils/libiberty/splay-tree.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r608 r609 1 1 /* A splay-tree datatype. 2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. 3 3 Contributed by Mark Mitchell (mark@markmitchell.com). 4 4 … … 71 71 (*sp->delete_value)(node->value); 72 72 73 free ((char*) node);73 (*sp->deallocate) ((char*) node, sp->allocate_data); 74 74 } 75 75 … … 228 228 } 229 229 230 231 /* An allocator and deallocator based on xmalloc. */ 232 static void * 233 splay_tree_xmalloc_allocate (size, data) 234 int size; 235 void *data ATTRIBUTE_UNUSED; 236 { 237 return (void *) xmalloc (size); 238 } 239 240 static void 241 splay_tree_xmalloc_deallocate (object, data) 242 void *object; 243 void *data ATTRIBUTE_UNUSED; 244 { 245 free (object); 246 } 247 248 230 249 /* Allocate a new splay tree, using COMPARE_FN to compare nodes, 231 250 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate 232 values. */ 251 values. Use xmalloc to allocate the splay tree structure, and any 252 nodes added. */ 233 253 234 254 splay_tree … … 238 258 splay_tree_delete_value_fn delete_value_fn; 239 259 { 240 splay_tree sp = (splay_tree) xmalloc (sizeof (struct splay_tree_s)); 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)); 263 } 264 265 266 /* Allocate a new splay tree, using COMPARE_FN to compare nodes, 267 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate 268 values. */ 269 270 splay_tree 271 splay_tree_new_with_allocator (compare_fn, delete_key_fn, delete_value_fn, 272 allocate_fn, deallocate_fn, allocate_data) 273 splay_tree_compare_fn compare_fn; 274 splay_tree_delete_key_fn delete_key_fn; 275 splay_tree_delete_value_fn delete_value_fn; 276 splay_tree_allocate_fn allocate_fn; 277 splay_tree_deallocate_fn deallocate_fn; 278 void *allocate_data; 279 { 280 splay_tree sp = (splay_tree) (*allocate_fn) (sizeof (struct splay_tree_s), 281 allocate_data); 241 282 sp->root = 0; 242 283 sp->comp = compare_fn; 243 284 sp->delete_key = delete_key_fn; 244 285 sp->delete_value = delete_value_fn; 286 sp->allocate = allocate_fn; 287 sp->deallocate = deallocate_fn; 288 sp->allocate_data = allocate_data; 245 289 246 290 return sp; … … 254 298 { 255 299 splay_tree_delete_helper (sp, sp->root); 256 free ((char*) sp);300 (*sp->deallocate) ((char*) sp, sp->allocate_data); 257 301 } 258 302 … … 287 331 splay_tree_node node; 288 332 289 node = (splay_tree_node) xmalloc (sizeof (struct splay_tree_node_s)); 333 node = ((splay_tree_node) 334 (*sp->allocate) (sizeof (struct splay_tree_node_s), 335 sp->allocate_data)); 290 336 node->key = key; 291 337 node->value = value; … … 331 377 if (sp->delete_value) 332 378 (*sp->delete_value) (sp->root->value); 333 free (sp->root);379 (*sp->deallocate) (sp->root, sp->allocate_data); 334 380 335 381 /* One of the children is now the root. Doesn't matter much … … 369 415 } 370 416 417 /* Return the node in SP with the greatest key. */ 418 419 splay_tree_node 420 splay_tree_max (sp) 421 splay_tree sp; 422 { 423 splay_tree_node n = sp->root; 424 425 if (!n) 426 return NULL; 427 428 while (n->right) 429 n = n->right; 430 431 return n; 432 } 433 434 /* Return the node in SP with the smallest key. */ 435 436 splay_tree_node 437 splay_tree_min (sp) 438 splay_tree sp; 439 { 440 splay_tree_node n = sp->root; 441 442 if (!n) 443 return NULL; 444 445 while (n->left) 446 n = n->left; 447 448 return n; 449 } 450 371 451 /* Return the immediate predecessor KEY, or NULL if there is no 372 452 predecessor. KEY need not be present in the tree. */ … … 403 483 404 484 /* Return the immediate successor KEY, or NULL if there is no 405 predecessor. KEY need not be present in the tree. */485 successor. KEY need not be present in the tree. */ 406 486 407 487 splay_tree_node … … 413 493 splay_tree_node node; 414 494 415 /* If the tree is empty, there is certainly no predecessor. */495 /* If the tree is empty, there is certainly no successor. */ 416 496 if (!sp->root) 417 497 return NULL; -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.