Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/libiberty/splay-tree.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* 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.
    33   Contributed by Mark Mitchell (mark@markmitchell.com).
    44
     
    7171    (*sp->delete_value)(node->value);
    7272
    73   free ((char*) node);
     73  (*sp->deallocate) ((char*) node, sp->allocate_data);
    7474}
    7575
     
    228228}
    229229
     230
     231/* An allocator and deallocator based on xmalloc.  */
     232static void *
     233splay_tree_xmalloc_allocate (size, data)
     234     int size;
     235     void *data ATTRIBUTE_UNUSED;
     236{
     237  return (void *) xmalloc (size);
     238}
     239
     240static void
     241splay_tree_xmalloc_deallocate (object, data)
     242     void *object;
     243     void *data ATTRIBUTE_UNUSED;
     244{
     245  free (object);
     246}
     247
     248
    230249/* Allocate a new splay tree, using COMPARE_FN to compare nodes,
    231250   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.  */
    233253
    234254splay_tree
     
    238258     splay_tree_delete_value_fn delete_value_fn;
    239259{
    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
     270splay_tree
     271splay_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);
    241282  sp->root = 0;
    242283  sp->comp = compare_fn;
    243284  sp->delete_key = delete_key_fn;
    244285  sp->delete_value = delete_value_fn;
     286  sp->allocate = allocate_fn;
     287  sp->deallocate = deallocate_fn;
     288  sp->allocate_data = allocate_data;
    245289
    246290  return sp;
     
    254298{
    255299  splay_tree_delete_helper (sp, sp->root);
    256   free ((char*) sp);
     300  (*sp->deallocate) ((char*) sp, sp->allocate_data);
    257301}
    258302
     
    287331      splay_tree_node node;
    288332     
    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));
    290336      node->key = key;
    291337      node->value = value;
     
    331377      if (sp->delete_value)
    332378        (*sp->delete_value) (sp->root->value);
    333       free (sp->root);
     379      (*sp->deallocate) (sp->root, sp->allocate_data);
    334380
    335381      /* One of the children is now the root.  Doesn't matter much
     
    369415}
    370416
     417/* Return the node in SP with the greatest key.  */
     418
     419splay_tree_node
     420splay_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
     436splay_tree_node
     437splay_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
    371451/* Return the immediate predecessor KEY, or NULL if there is no
    372452   predecessor.  KEY need not be present in the tree.  */
     
    403483
    404484/* 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.  */
    406486
    407487splay_tree_node
     
    413493  splay_tree_node node;
    414494
    415   /* If the tree is empty, there is certainly no predecessor.  */
     495  /* If the tree is empty, there is certainly no successor.  */
    416496  if (!sp->root)
    417497    return NULL;
Note: See TracChangeset for help on using the changeset viewer.