avl.h 4.1 KB

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