avl.h 4.0 KB

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