Back to index

tetex-bin  3.0
avl.h
Go to the documentation of this file.
00001 /* Produced by texiweb from libavl.w on 2003/01/06 at 18:07. */
00002 
00003 /* libavl - library for manipulation of binary trees.
00004    Copyright (C) 1998-2002 Free Software Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU General Public License as
00008    published by the Free Software Foundation; either version 2 of the
00009    License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful, but
00012    WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00014    See the GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019    02111-1307, USA.
00020 
00021    The author may be contacted at <blp@gnu.org> on the Internet, or
00022    write to Ben Pfaff, Stanford University, Computer Science Dept., 353
00023    Serra Mall, Stanford CA 94305, USA.
00024 */
00025 
00026 #ifndef AVL_H
00027 #define AVL_H 1
00028 
00029 #include <stddef.h>
00030 #if defined(__cplusplus)
00031 extern "C" {
00032 #endif
00033 
00034 
00035 /* Function types. */
00036 typedef int avl_comparison_func (const void *avl_a, const void *avl_b,
00037                                  void *avl_param);
00038 typedef void avl_item_func (void *avl_item, void *avl_param);
00039 typedef void *avl_copy_func (void *avl_item, void *avl_param);
00040 
00041 #ifndef LIBAVL_ALLOCATOR
00042 #define LIBAVL_ALLOCATOR
00043 /* Memory allocator. */
00044 struct libavl_allocator
00045   {
00046     void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size);
00047     void (*libavl_free) (struct libavl_allocator *, void *libavl_block);
00048   };
00049 #endif
00050 
00051 /* Default memory allocator. */
00052 extern struct libavl_allocator avl_allocator_default;
00053 void *avl_malloc (struct libavl_allocator *, size_t);
00054 void avl_free (struct libavl_allocator *, void *);
00055 
00056 /* Maximum AVL height. */
00057 #ifndef AVL_MAX_HEIGHT
00058 #define AVL_MAX_HEIGHT 32
00059 #endif
00060 
00061 /* Tree data structure. */
00062 struct avl_table
00063   {
00064     struct avl_node *avl_root;          /* Tree's root. */
00065     avl_comparison_func *avl_compare;   /* Comparison function. */
00066     void *avl_param;                    /* Extra argument to |avl_compare|. */
00067     struct libavl_allocator *avl_alloc; /* Memory allocator. */
00068     size_t avl_count;                   /* Number of items in tree. */
00069     unsigned long avl_generation;       /* Generation number. */
00070   };
00071 
00072 /* An AVL tree node. */
00073 struct avl_node
00074   {
00075     struct avl_node *avl_link[2];  /* Subtrees. */
00076     void *avl_data;                /* Pointer to data. */
00077     signed char avl_balance;       /* Balance factor. */
00078   };
00079 
00080 /* AVL traverser structure. */
00081 struct avl_traverser
00082   {
00083     struct avl_table *avl_table;        /* Tree being traversed. */
00084     struct avl_node *avl_node;          /* Current node in tree. */
00085     struct avl_node *avl_stack[AVL_MAX_HEIGHT];
00086                                         /* All the nodes above |avl_node|. */
00087     size_t avl_height;                  /* Number of nodes in |avl_parent|. */
00088     unsigned long avl_generation;       /* Generation number. */
00089   };
00090 
00091 /* Table functions. */
00092 struct avl_table *avl_create (avl_comparison_func *, void *,
00093                               struct libavl_allocator *);
00094 struct avl_table *avl_copy (const struct avl_table *, avl_copy_func *,
00095                             avl_item_func *, struct libavl_allocator *);
00096 void avl_destroy (struct avl_table *, avl_item_func *);
00097 void **avl_probe (struct avl_table *, void *);
00098 void *avl_insert (struct avl_table *, void *);
00099 void *avl_replace (struct avl_table *, void *);
00100 void *avl_delete (struct avl_table *, const void *);
00101 void *avl_find (const struct avl_table *, const void *);
00102 void avl_assert_insert (struct avl_table *, void *);
00103 void *avl_assert_delete (struct avl_table *, void *);
00104 
00105 #define avl_count(table) ((size_t) (table)->avl_count)
00106 
00107 /* Table traverser functions. */
00108 void avl_t_init (struct avl_traverser *, struct avl_table *);
00109 void *avl_t_first (struct avl_traverser *, struct avl_table *);
00110 void *avl_t_last (struct avl_traverser *, struct avl_table *);
00111 void *avl_t_find (struct avl_traverser *, struct avl_table *, void *);
00112 void *avl_t_insert (struct avl_traverser *, struct avl_table *, void *);
00113 void *avl_t_copy (struct avl_traverser *, const struct avl_traverser *);
00114 void *avl_t_next (struct avl_traverser *);
00115 void *avl_t_prev (struct avl_traverser *);
00116 void *avl_t_cur (struct avl_traverser *);
00117 void *avl_t_replace (struct avl_traverser *, void *);
00118 
00119 #if defined(__cplusplus)
00120 }
00121 #endif  
00122 
00123 #endif /* avl.h */