avl.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: LGPL-3.0-or-later
  2. #ifndef _AVL_H
  3. #define _AVL_H 1
  4. #include "../libnetdata.h"
  5. /* Maximum AVL tree height. */
  6. #ifndef AVL_MAX_HEIGHT
  7. #define AVL_MAX_HEIGHT 92
  8. #endif
  9. #if defined(AVL_LOCK_WITH_RWLOCK)
  10. #define AVL_LOCK_INITIALIZER NETDATA_RWLOCK_INITIALIZER
  11. #else
  12. #define AVL_LOCK_INITIALIZER NETDATA_RW_SPINLOCK_INITIALIZER
  13. #endif
  14. /* Data structures */
  15. /* One element of the AVL tree */
  16. typedef struct avl_element {
  17. struct avl_element *avl_link[2]; /* Subtrees. */
  18. signed char avl_balance; /* Balance factor. */
  19. } avl_t;
  20. typedef struct __attribute__((packed)) avl_element_packed {
  21. struct avl_element *avl_link[2]; /* Subtrees. */
  22. signed char avl_balance; /* Balance factor. */
  23. } avl_t_packed;
  24. /* An AVL tree */
  25. typedef struct avl_tree_type {
  26. avl_t *root;
  27. int (*compar)(void *a, void *b);
  28. } avl_tree_type;
  29. typedef struct avl_tree_lock {
  30. avl_tree_type avl_tree;
  31. #if defined(AVL_LOCK_WITH_RWLOCK)
  32. netdata_rwlock_t rwlock;
  33. #else
  34. RW_SPINLOCK rwlock;
  35. #endif
  36. } avl_tree_lock;
  37. /* Public methods */
  38. /* Insert element a into the AVL tree t
  39. * returns the added element a, or a pointer the
  40. * element that is equal to a (as returned by t->compar())
  41. * a is linked directly to the tree, so it has to
  42. * be properly allocated by the caller.
  43. */
  44. avl_t *avl_insert_lock(avl_tree_lock *tree, avl_t *item) NEVERNULL WARNUNUSED;
  45. avl_t *avl_insert(avl_tree_type *tree, avl_t *item) NEVERNULL WARNUNUSED;
  46. /* Remove an element a from the AVL tree t
  47. * returns a pointer to the removed element
  48. * or NULL if an element equal to a is not found
  49. * (equal as returned by t->compar())
  50. */
  51. avl_t *avl_remove_lock(avl_tree_lock *tree, avl_t *item) WARNUNUSED;
  52. avl_t *avl_remove(avl_tree_type *tree, avl_t *item) WARNUNUSED;
  53. /* Find the element into the tree that equal to a
  54. * (equal as returned by t->compar())
  55. * returns NULL is no element is equal to a
  56. */
  57. avl_t *avl_search_lock(avl_tree_lock *tree, avl_t *item);
  58. avl_t *avl_search(avl_tree_type *tree, avl_t *item);
  59. /* Initialize the avl_tree_lock
  60. */
  61. void avl_init_lock(avl_tree_lock *tree, int (*compar)(void *a, void *b));
  62. void avl_init(avl_tree_type *tree, int (*compar)(void *a, void *b));
  63. /* Destroy the avl_tree_lock locks
  64. */
  65. void avl_destroy_lock(avl_tree_lock *tree);
  66. int avl_traverse_lock(avl_tree_lock *tree, int (*callback)(void *entry, void *data), void *data);
  67. int avl_traverse(avl_tree_type *tree, int (*callback)(void *entry, void *data), void *data);
  68. #endif /* avl.h */