GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* Produced by texiweb from libavl.w on 2002/02/09 at 01:45. */ 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 00020 The author may be contacted at <blp@gnu.org> on the Internet, or 00021 as Ben Pfaff, 12167 Airport Rd, DeWitt MI 48820, USA through more 00022 mundane means. 00023 */ 00024 00025 #ifndef AVL_H 00026 #define AVL_H 1 00027 00028 #include <stddef.h> 00029 00030 /* Function types. */ 00031 typedef int avl_comparison_func(const void *avl_a, const void *avl_b, 00032 void *avl_param); 00033 typedef void avl_item_func(void *avl_item, void *avl_param); 00034 typedef void *avl_copy_func(void *avl_item, void *avl_param); 00035 00036 #ifndef LIBAVL_ALLOCATOR 00037 #define LIBAVL_ALLOCATOR 00038 /* Memory allocator. */ 00039 struct libavl_allocator 00040 { 00041 void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size); 00042 void (*libavl_free) (struct libavl_allocator *, void *libavl_block); 00043 }; 00044 #endif 00045 00046 /* Default memory allocator. */ 00047 extern struct libavl_allocator avl_allocator_default; 00048 void *avl_malloc(struct libavl_allocator *, size_t); 00049 void avl_free(struct libavl_allocator *, void *); 00050 00051 /* Maximum AVL height. */ 00052 #ifndef AVL_MAX_HEIGHT 00053 #define AVL_MAX_HEIGHT 32 00054 #endif 00055 00056 /* Tree data structure. */ 00057 struct avl_table 00058 { 00059 struct avl_node *avl_root; /* Tree's root. */ 00060 avl_comparison_func *avl_compare; /* Comparison function. */ 00061 void *avl_param; /* Extra argument to |avl_compare|. */ 00062 struct libavl_allocator *avl_alloc; /* Memory allocator. */ 00063 size_t avl_count; /* Number of items in tree. */ 00064 unsigned long avl_generation; /* Generation number. */ 00065 }; 00066 00067 /* An AVL tree node. */ 00068 struct avl_node 00069 { 00070 struct avl_node *avl_link[2]; /* Subtrees. */ 00071 void *avl_data; /* Pointer to data. */ 00072 signed char avl_balance; /* Balance factor. */ 00073 }; 00074 00075 /* AVL traverser structure. */ 00076 struct avl_traverser 00077 { 00078 struct avl_table *avl_table; /* Tree being traversed. */ 00079 struct avl_node *avl_node; /* Current node in tree. */ 00080 struct avl_node *avl_stack[AVL_MAX_HEIGHT]; 00081 /* All the nodes above |avl_node|. */ 00082 size_t avl_height; /* Number of nodes in |avl_parent|. */ 00083 unsigned long avl_generation; /* Generation number. */ 00084 }; 00085 00086 /* Table functions. */ 00087 struct avl_table *avl_create(avl_comparison_func *, void *, 00088 struct libavl_allocator *); 00089 struct avl_table *avl_copy(const struct avl_table *, avl_copy_func *, 00090 avl_item_func *, struct libavl_allocator *); 00091 void avl_destroy(struct avl_table *, avl_item_func *); 00092 void **avl_probe(struct avl_table *, void *); 00093 void *avl_insert(struct avl_table *, void *); 00094 void *avl_replace(struct avl_table *, void *); 00095 void *avl_delete(struct avl_table *, const void *); 00096 void *avl_find(const struct avl_table *, const void *); 00097 void avl_assert_insert(struct avl_table *, void *); 00098 void *avl_assert_delete(struct avl_table *, void *); 00099 00100 #define avl_count(table) ((size_t) (table)->avl_count) 00101 00102 /* Table traverser functions. */ 00103 void avl_t_init(struct avl_traverser *, struct avl_table *); 00104 void *avl_t_first(struct avl_traverser *, struct avl_table *); 00105 void *avl_t_last(struct avl_traverser *, struct avl_table *); 00106 void *avl_t_find(struct avl_traverser *, struct avl_table *, void *); 00107 void *avl_t_insert(struct avl_traverser *, struct avl_table *, void *); 00108 void *avl_t_copy(struct avl_traverser *, const struct avl_traverser *); 00109 void *avl_t_next(struct avl_traverser *); 00110 void *avl_t_prev(struct avl_traverser *); 00111 void *avl_t_cur(struct avl_traverser *); 00112 void *avl_t_replace(struct avl_traverser *, void *); 00113 00114 #endif /* avl.h */