tavl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 TAVL_H
  19. #define TAVL_H 1
  20. #include <stddef.h>
  21. /* Function types. */
  22. typedef int tavl_comparison_func(const void *tavl_a, const void *tavl_b,
  23. void *tavl_param);
  24. typedef void tavl_item_func(void *tavl_item, void *tavl_param);
  25. typedef void *tavl_copy_func(void *tavl_item, void *tavl_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 tavl_allocator_default;
  37. void *tavl_malloc(struct libavl_allocator *, size_t);
  38. void tavl_free(struct libavl_allocator *, void *);
  39. /* Maximum TAVL height. */
  40. #ifndef TAVL_MAX_HEIGHT
  41. #define TAVL_MAX_HEIGHT 92
  42. #endif
  43. /* Tree data structure. */
  44. struct tavl_table
  45. {
  46. struct tavl_node *tavl_root; /* Tree's root. */
  47. tavl_comparison_func *tavl_compare; /* Comparison function. */
  48. void *tavl_param; /* Extra argument to |tavl_compare|. */
  49. struct libavl_allocator *tavl_alloc; /* Memory allocator. */
  50. size_t tavl_count; /* Number of items in tree. */
  51. };
  52. /* Characterizes a link as a child pointer or a thread. */
  53. enum tavl_tag
  54. {
  55. TAVL_CHILD, /* Child pointer. */
  56. TAVL_THREAD /* Thread. */
  57. };
  58. /* An TAVL tree node. */
  59. struct tavl_node
  60. {
  61. struct tavl_node *tavl_link[2]; /* Subtrees. */
  62. void *tavl_data; /* Pointer to data. */
  63. unsigned char tavl_tag[2]; /* Tag fields. */
  64. signed char tavl_balance; /* Balance factor. */
  65. };
  66. /* TAVL traverser structure. */
  67. struct tavl_traverser
  68. {
  69. struct tavl_table *tavl_table; /* Tree being traversed. */
  70. struct tavl_node *tavl_node; /* Current node in tree. */
  71. };
  72. /* Table functions. */
  73. struct tavl_table *tavl_create(tavl_comparison_func *, void *,
  74. struct libavl_allocator *);
  75. struct tavl_table *tavl_copy(const struct tavl_table *, tavl_copy_func *,
  76. tavl_item_func *, struct libavl_allocator *);
  77. void tavl_destroy(struct tavl_table *, tavl_item_func *);
  78. void **tavl_probe(struct tavl_table *, void *);
  79. void *tavl_insert(struct tavl_table *, void *);
  80. void *tavl_replace(struct tavl_table *, void *);
  81. void *tavl_delete(struct tavl_table *, const void *);
  82. void *tavl_find(const struct tavl_table *, const void *);
  83. void tavl_assert_insert(struct tavl_table *, void *);
  84. void *tavl_assert_delete(struct tavl_table *, void *);
  85. #define tavl_count(table) ((size_t) (table)->tavl_count)
  86. /* Table traverser functions. */
  87. void tavl_t_init(struct tavl_traverser *, struct tavl_table *);
  88. void *tavl_t_first(struct tavl_traverser *, struct tavl_table *);
  89. void *tavl_t_last(struct tavl_traverser *, struct tavl_table *);
  90. void *tavl_t_find(struct tavl_traverser *, struct tavl_table *, void *);
  91. void *tavl_t_insert(struct tavl_traverser *, struct tavl_table *, void *);
  92. void *tavl_t_copy(struct tavl_traverser *, const struct tavl_traverser *);
  93. void *tavl_t_next(struct tavl_traverser *);
  94. void *tavl_t_prev(struct tavl_traverser *);
  95. void *tavl_t_cur(struct tavl_traverser *);
  96. void *tavl_t_replace(struct tavl_traverser *, void *);
  97. #endif /* tavl.h */