tavl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. The author may be contacted at <blp@gnu.org> on the Internet, or
  17. as Ben Pfaff, 12167 Airport Rd, DeWitt MI 48820, USA through more
  18. mundane means.
  19. */
  20. #ifndef TAVL_H
  21. #define TAVL_H 1
  22. #include <stddef.h>
  23. /* Function types. */
  24. typedef int tavl_comparison_func(const void *tavl_a, const void *tavl_b,
  25. void *tavl_param);
  26. typedef void tavl_item_func(void *tavl_item, void *tavl_param);
  27. typedef void *tavl_copy_func(void *tavl_item, void *tavl_param);
  28. #ifndef LIBAVL_ALLOCATOR
  29. #define LIBAVL_ALLOCATOR
  30. /* Memory allocator. */
  31. struct libavl_allocator
  32. {
  33. void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size);
  34. void (*libavl_free) (struct libavl_allocator *, void *libavl_block);
  35. };
  36. #endif
  37. /* Default memory allocator. */
  38. extern struct libavl_allocator tavl_allocator_default;
  39. void *tavl_malloc(struct libavl_allocator *, size_t);
  40. void tavl_free(struct libavl_allocator *, void *);
  41. /* Maximum TAVL height. */
  42. #ifndef TAVL_MAX_HEIGHT
  43. #define TAVL_MAX_HEIGHT 32
  44. #endif
  45. /* Tree data structure. */
  46. struct tavl_table
  47. {
  48. struct tavl_node *tavl_root; /* Tree's root. */
  49. tavl_comparison_func *tavl_compare; /* Comparison function. */
  50. void *tavl_param; /* Extra argument to |tavl_compare|. */
  51. struct libavl_allocator *tavl_alloc; /* Memory allocator. */
  52. size_t tavl_count; /* Number of items in tree. */
  53. };
  54. /* Characterizes a link as a child pointer or a thread. */
  55. enum tavl_tag
  56. {
  57. TAVL_CHILD, /* Child pointer. */
  58. TAVL_THREAD /* Thread. */
  59. };
  60. /* An TAVL tree node. */
  61. struct tavl_node
  62. {
  63. struct tavl_node *tavl_link[2]; /* Subtrees. */
  64. void *tavl_data; /* Pointer to data. */
  65. unsigned char tavl_tag[2]; /* Tag fields. */
  66. signed char tavl_balance; /* Balance factor. */
  67. };
  68. /* TAVL traverser structure. */
  69. struct tavl_traverser
  70. {
  71. struct tavl_table *tavl_table; /* Tree being traversed. */
  72. struct tavl_node *tavl_node; /* Current node in tree. */
  73. };
  74. /* Table functions. */
  75. struct tavl_table *tavl_create(tavl_comparison_func *, void *,
  76. struct libavl_allocator *);
  77. struct tavl_table *tavl_copy(const struct tavl_table *, tavl_copy_func *,
  78. tavl_item_func *, struct libavl_allocator *);
  79. void tavl_destroy(struct tavl_table *, tavl_item_func *);
  80. void **tavl_probe(struct tavl_table *, void *);
  81. void *tavl_insert(struct tavl_table *, void *);
  82. void *tavl_replace(struct tavl_table *, void *);
  83. void *tavl_delete(struct tavl_table *, const void *);
  84. void *tavl_find(const struct tavl_table *, const void *);
  85. void tavl_assert_insert(struct tavl_table *, void *);
  86. void *tavl_assert_delete(struct tavl_table *, void *);
  87. #define tavl_count(table) ((size_t) (table)->tavl_count)
  88. /* Table traverser functions. */
  89. void tavl_t_init(struct tavl_traverser *, struct tavl_table *);
  90. void *tavl_t_first(struct tavl_traverser *, struct tavl_table *);
  91. void *tavl_t_last(struct tavl_traverser *, struct tavl_table *);
  92. void *tavl_t_find(struct tavl_traverser *, struct tavl_table *, void *);
  93. void *tavl_t_insert(struct tavl_traverser *, struct tavl_table *, void *);
  94. void *tavl_t_copy(struct tavl_traverser *, const struct tavl_traverser *);
  95. void *tavl_t_next(struct tavl_traverser *);
  96. void *tavl_t_prev(struct tavl_traverser *);
  97. void *tavl_t_cur(struct tavl_traverser *);
  98. void *tavl_t_replace(struct tavl_traverser *, void *);
  99. #endif /* tavl.h */