avl_tree.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef TINC_AVL_TREE_H
  2. #define TINC_AVL_TREE_H
  3. /*
  4. avl_tree.h -- header file for avl_tree.c
  5. Copyright (C) 1998 Michael H. Buselli
  6. 2000-2005 Ivo Timmermans,
  7. 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
  8. 2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License along
  18. with this program; if not, write to the Free Software Foundation, Inc.,
  19. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
  21. Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
  22. instead of depths, to add the ->next and ->prev and to generally obfuscate
  23. the code. Mail me if you found a bug.
  24. Cleaned up and incorporated some of the ideas from the red-black tree
  25. library for inclusion into tinc (https://www.tinc-vpn.org/) by
  26. Guus Sliepen <guus@tinc-vpn.org>.
  27. */
  28. #ifndef AVL_DEPTH
  29. #ifndef AVL_COUNT
  30. #define AVL_DEPTH
  31. #endif
  32. #endif
  33. typedef struct avl_node_t {
  34. /* Linked list part */
  35. struct avl_node_t *next;
  36. struct avl_node_t *prev;
  37. /* Tree part */
  38. struct avl_node_t *parent;
  39. struct avl_node_t *left;
  40. struct avl_node_t *right;
  41. #ifdef AVL_COUNT
  42. unsigned int count;
  43. #endif
  44. #ifdef AVL_DEPTH
  45. unsigned char depth;
  46. #endif
  47. /* Payload */
  48. void *data;
  49. } avl_node_t;
  50. typedef int (*avl_compare_t)(const void *data1, const void *data2);
  51. typedef void (*avl_action_t)(const void *data);
  52. typedef void (*avl_action_node_t)(const avl_node_t *node);
  53. typedef struct avl_tree_t {
  54. /* Linked list part */
  55. avl_node_t *head;
  56. avl_node_t *tail;
  57. /* Tree part */
  58. avl_node_t *root;
  59. avl_compare_t compare;
  60. avl_action_t delete;
  61. } avl_tree_t;
  62. /* (De)constructors */
  63. extern avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete);
  64. extern void avl_free_tree(avl_tree_t *tree);
  65. extern avl_node_t *avl_alloc_node(void);
  66. extern void avl_free_node(avl_tree_t *tree, avl_node_t *node);
  67. /* Insertion and deletion */
  68. extern avl_node_t *avl_insert(avl_tree_t *tree, void *data);
  69. extern avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node);
  70. extern void avl_insert_top(avl_tree_t *tree, avl_node_t *node);
  71. extern void avl_insert_before(avl_tree_t *tree, avl_node_t *before, avl_node_t *node);
  72. extern void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node);
  73. extern avl_node_t *avl_unlink(avl_tree_t *tree, void *data);
  74. extern void avl_unlink_node(avl_tree_t *tree, avl_node_t *node);
  75. extern void avl_delete(avl_tree_t *tree, void *data);
  76. extern void avl_delete_node(avl_tree_t *tree, avl_node_t *node);
  77. /* Fast tree cleanup */
  78. extern void avl_delete_tree(avl_tree_t *tree);
  79. /* Searching */
  80. extern void *avl_search(const avl_tree_t *tree, const void *data);
  81. extern void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result);
  82. extern void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data);
  83. extern void *avl_search_closest_greater(const avl_tree_t *tree, const void *data);
  84. extern avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data);
  85. extern avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data, int *result);
  86. extern avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree, const void *data);
  87. extern avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree, const void *data);
  88. /* Tree walking */
  89. extern void avl_foreach(const avl_tree_t *tree, avl_action_t action);
  90. extern void avl_foreach_node(const avl_tree_t *tree, avl_action_t action);
  91. /* Indexing */
  92. #ifdef AVL_COUNT
  93. extern unsigned int avl_count(const avl_tree_t *tree);
  94. extern avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index);
  95. extern unsigned int avl_index(const avl_node_t *node);
  96. #endif
  97. #ifdef AVL_DEPTH
  98. extern unsigned int avl_depth(const avl_tree_t *tree);
  99. #endif
  100. #endif