avl.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef AVL_WITHOUT_PTHREADS
  10. #include <pthread.h>
  11. // #define AVL_LOCK_WITH_MUTEX 1
  12. #ifdef AVL_LOCK_WITH_MUTEX
  13. #define AVL_LOCK_INITIALIZER NETDATA_MUTEX_INITIALIZER
  14. #else /* AVL_LOCK_WITH_MUTEX */
  15. #define AVL_LOCK_INITIALIZER NETDATA_RWLOCK_INITIALIZER
  16. #endif /* AVL_LOCK_WITH_MUTEX */
  17. #else /* AVL_WITHOUT_PTHREADS */
  18. #define AVL_LOCK_INITIALIZER
  19. #endif /* AVL_WITHOUT_PTHREADS */
  20. /* Data structures */
  21. /* One element of the AVL tree */
  22. typedef struct avl_element {
  23. struct avl_element *avl_link[2]; /* Subtrees. */
  24. signed char avl_balance; /* Balance factor. */
  25. } avl_t;
  26. /* An AVL tree */
  27. typedef struct avl_tree_type {
  28. avl_t *root;
  29. int (*compar)(void *a, void *b);
  30. } avl_tree_type;
  31. typedef struct avl_tree_lock {
  32. avl_tree_type avl_tree;
  33. #ifndef AVL_WITHOUT_PTHREADS
  34. #ifdef AVL_LOCK_WITH_MUTEX
  35. netdata_mutex_t mutex;
  36. #else /* AVL_LOCK_WITH_MUTEX */
  37. netdata_rwlock_t rwlock;
  38. #endif /* AVL_LOCK_WITH_MUTEX */
  39. #endif /* AVL_WITHOUT_PTHREADS */
  40. } avl_tree_lock;
  41. /* Public methods */
  42. /* Insert element a into the AVL tree t
  43. * returns the added element a, or a pointer the
  44. * element that is equal to a (as returned by t->compar())
  45. * a is linked directly to the tree, so it has to
  46. * be properly allocated by the caller.
  47. */
  48. avl_t *avl_insert_lock(avl_tree_lock *tree, avl_t *item) NEVERNULL WARNUNUSED;
  49. avl_t *avl_insert(avl_tree_type *tree, avl_t *item) NEVERNULL WARNUNUSED;
  50. /* Remove an element a from the AVL tree t
  51. * returns a pointer to the removed element
  52. * or NULL if an element equal to a is not found
  53. * (equal as returned by t->compar())
  54. */
  55. avl_t *avl_remove_lock(avl_tree_lock *tree, avl_t *item) WARNUNUSED;
  56. avl_t *avl_remove(avl_tree_type *tree, avl_t *item) WARNUNUSED;
  57. /* Find the element into the tree that equal to a
  58. * (equal as returned by t->compar())
  59. * returns NULL is no element is equal to a
  60. */
  61. avl_t *avl_search_lock(avl_tree_lock *tree, avl_t *item);
  62. avl_t *avl_search(avl_tree_type *tree, avl_t *item);
  63. /* Initialize the avl_tree_lock
  64. */
  65. void avl_init_lock(avl_tree_lock *tree, int (*compar)(void *a, void *b));
  66. void avl_init(avl_tree_type *tree, int (*compar)(void *a, void *b));
  67. /* Destroy the avl_tree_lock locks
  68. */
  69. void avl_destroy_lock(avl_tree_lock *tree);
  70. int avl_traverse_lock(avl_tree_lock *tree, int (*callback)(void *entry, void *data), void *data);
  71. int avl_traverse(avl_tree_type *tree, int (*callback)(void *entry, void *data), void *data);
  72. #endif /* avl.h */