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 TAVL_H 00026 #define TAVL_H 1 00027 00028 #include <stddef.h> 00029 00030 /* Function types. */ 00031 typedef int tavl_comparison_func(const void *tavl_a, const void *tavl_b, 00032 void *tavl_param); 00033 typedef void tavl_item_func(void *tavl_item, void *tavl_param); 00034 typedef void *tavl_copy_func(void *tavl_item, void *tavl_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 tavl_allocator_default; 00048 void *tavl_malloc(struct libavl_allocator *, size_t); 00049 void tavl_free(struct libavl_allocator *, void *); 00050 00051 /* Maximum TAVL height. */ 00052 #ifndef TAVL_MAX_HEIGHT 00053 #define TAVL_MAX_HEIGHT 32 00054 #endif 00055 00056 /* Tree data structure. */ 00057 struct tavl_table 00058 { 00059 struct tavl_node *tavl_root; /* Tree's root. */ 00060 tavl_comparison_func *tavl_compare; /* Comparison function. */ 00061 void *tavl_param; /* Extra argument to |tavl_compare|. */ 00062 struct libavl_allocator *tavl_alloc; /* Memory allocator. */ 00063 size_t tavl_count; /* Number of items in tree. */ 00064 }; 00065 00066 /* Characterizes a link as a child pointer or a thread. */ 00067 enum tavl_tag 00068 { 00069 TAVL_CHILD, /* Child pointer. */ 00070 TAVL_THREAD /* Thread. */ 00071 }; 00072 00073 /* An TAVL tree node. */ 00074 struct tavl_node 00075 { 00076 struct tavl_node *tavl_link[2]; /* Subtrees. */ 00077 void *tavl_data; /* Pointer to data. */ 00078 unsigned char tavl_tag[2]; /* Tag fields. */ 00079 signed char tavl_balance; /* Balance factor. */ 00080 }; 00081 00082 /* TAVL traverser structure. */ 00083 struct tavl_traverser 00084 { 00085 struct tavl_table *tavl_table; /* Tree being traversed. */ 00086 struct tavl_node *tavl_node; /* Current node in tree. */ 00087 }; 00088 00089 /* Table functions. */ 00090 struct tavl_table *tavl_create(tavl_comparison_func *, void *, 00091 struct libavl_allocator *); 00092 struct tavl_table *tavl_copy(const struct tavl_table *, tavl_copy_func *, 00093 tavl_item_func *, struct libavl_allocator *); 00094 void tavl_destroy(struct tavl_table *, tavl_item_func *); 00095 void **tavl_probe(struct tavl_table *, void *); 00096 void *tavl_insert(struct tavl_table *, void *); 00097 void *tavl_replace(struct tavl_table *, void *); 00098 void *tavl_delete(struct tavl_table *, const void *); 00099 void *tavl_find(const struct tavl_table *, const void *); 00100 void tavl_assert_insert(struct tavl_table *, void *); 00101 void *tavl_assert_delete(struct tavl_table *, void *); 00102 00103 #define tavl_count(table) ((size_t) (table)->tavl_count) 00104 00105 /* Table traverser functions. */ 00106 void tavl_t_init(struct tavl_traverser *, struct tavl_table *); 00107 void *tavl_t_first(struct tavl_traverser *, struct tavl_table *); 00108 void *tavl_t_last(struct tavl_traverser *, struct tavl_table *); 00109 void *tavl_t_find(struct tavl_traverser *, struct tavl_table *, void *); 00110 void *tavl_t_insert(struct tavl_traverser *, struct tavl_table *, void *); 00111 void *tavl_t_copy(struct tavl_traverser *, const struct tavl_traverser *); 00112 void *tavl_t_next(struct tavl_traverser *); 00113 void *tavl_t_prev(struct tavl_traverser *); 00114 void *tavl_t_cur(struct tavl_traverser *); 00115 void *tavl_t_replace(struct tavl_traverser *, void *); 00116 00117 #endif /* tavl.h */