1 /* Produced by texiweb from libavl.w. */ 2 3 /* libavl - library for manipulation of binary trees. 4 Copyright (C) 2004-2014 Han The Thanh, <thanh@pdftex.org> 5 Modified for pdfTeX, contact pdfTeX maintainers with any problems or 6 questions, not the original authors. 7 8 Copyright (C) 1998-2002, 2004 Free Software Foundation, Inc. 9 10 This program is free software; you can redistribute it and/or 11 modify it under the terms of the GNU General Public License as 12 published by the Free Software Foundation; either version 2 of the 13 License, or (at your option) any later version. 14 15 This program is distributed in the hope that it will be useful, but 16 WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 18 See the GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23 02111-1307, USA. 24 25 Original author: Ben Pfaff. 26 */ 27 28 #ifndef AVL_H 29 #define AVL_H 1 30 31 #include <stddef.h> 32 33 /* Function types. */ 34 typedef int avl_comparison_func (const void *avl_a, const void *avl_b, 35 void *avl_param); 36 typedef void avl_item_func (void *avl_item, void *avl_param); 37 typedef void *avl_copy_func (void *avl_item, void *avl_param); 38 39 #ifndef LIBAVL_ALLOCATOR 40 #define LIBAVL_ALLOCATOR 41 /* Memory allocator. */ 42 struct libavl_allocator 43 { 44 void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size); 45 void (*libavl_free) (struct libavl_allocator *, void *libavl_block); 46 }; 47 #endif 48 49 /* Default memory allocator. */ 50 extern struct libavl_allocator avl_allocator_default; 51 void *avl_malloc (struct libavl_allocator *, size_t); 52 void avl_free (struct libavl_allocator *, void *); 53 54 /* Maximum AVL height. */ 55 #ifndef AVL_MAX_HEIGHT 56 #define AVL_MAX_HEIGHT 32 57 #endif 58 59 /* Tree data structure. */ 60 struct avl_table 61 { 62 struct avl_node *avl_root; /* Tree's root. */ 63 avl_comparison_func *avl_compare; /* Comparison function. */ 64 void *avl_param; /* Extra argument to |avl_compare|. */ 65 struct libavl_allocator *avl_alloc; /* Memory allocator. */ 66 size_t avl_count; /* Number of items in tree. */ 67 unsigned long avl_generation; /* Generation number. */ 68 }; 69 70 /* An AVL tree node. */ 71 struct avl_node 72 { 73 struct avl_node *avl_link[2]; /* Subtrees. */ 74 void *avl_data; /* Pointer to data. */ 75 signed char avl_balance; /* Balance factor. */ 76 }; 77 78 /* AVL traverser structure. */ 79 struct avl_traverser 80 { 81 struct avl_table *avl_table; /* Tree being traversed. */ 82 struct avl_node *avl_node; /* Current node in tree. */ 83 struct avl_node *avl_stack[AVL_MAX_HEIGHT]; 84 /* All the nodes above |avl_node|. */ 85 size_t avl_height; /* Number of nodes in |avl_parent|. */ 86 unsigned long avl_generation; /* Generation number. */ 87 }; 88 89 /* Table functions. */ 90 struct avl_table *avl_create (avl_comparison_func *, void *, 91 struct libavl_allocator *); 92 struct avl_table *avl_copy (const struct avl_table *, avl_copy_func *, 93 avl_item_func *, struct libavl_allocator *); 94 void avl_destroy (struct avl_table *, avl_item_func *); 95 void **avl_probe (struct avl_table *, void *); 96 void *avl_insert (struct avl_table *, void *); 97 void *avl_replace (struct avl_table *, void *); 98 void *avl_delete (struct avl_table *, const void *); 99 void *avl_find (const struct avl_table *, const void *); 100 void avl_assert_insert (struct avl_table *, void *); 101 void *avl_assert_delete (struct avl_table *, void *); 102 103 #define avl_count(table) ((size_t) (table)->avl_count) 104 105 /* Table traverser functions. */ 106 void avl_t_init (struct avl_traverser *, struct avl_table *); 107 void *avl_t_first (struct avl_traverser *, struct avl_table *); 108 void *avl_t_last (struct avl_traverser *, struct avl_table *); 109 void *avl_t_find (struct avl_traverser *, struct avl_table *, void *); 110 void *avl_t_insert (struct avl_traverser *, struct avl_table *, void *); 111 void *avl_t_copy (struct avl_traverser *, const struct avl_traverser *); 112 void *avl_t_next (struct avl_traverser *); 113 void *avl_t_prev (struct avl_traverser *); 114 void *avl_t_cur (struct avl_traverser *); 115 void *avl_t_replace (struct avl_traverser *, void *); 116 117 #endif /* avl.h */ 118