tavl.h 4.1 KB

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