utils_rbtree.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __UTILS_RBTREE_H__
  31. #define __UTILS_RBTREE_H__
  32. #include <stdio.h>
  33. #include <stdint.h>
  34. #include <stddef.h>
  35. #ifndef RB_ITER_MAX_HEIGHT
  36. #define RB_ITER_MAX_HEIGHT 64 // Tallest allowable tree to iterate
  37. #endif
  38. struct rb_node;
  39. struct rb_tree;
  40. typedef int (*rb_tree_node_cmp_f) (struct rb_tree *self, struct rb_node *a, struct rb_node *b);
  41. typedef void (*rb_tree_node_f) (struct rb_tree *self, struct rb_node *node);
  42. struct rb_node {
  43. int red; // Color red (1), black (0)
  44. struct rb_node *link[2]; // Link left [0] and right [1]
  45. void *value; // User provided, used indirectly via rb_tree_node_cmp_f.
  46. };
  47. struct rb_tree {
  48. struct rb_node *root;
  49. rb_tree_node_cmp_f cmp;
  50. size_t size;
  51. void *info; // User provided, not used by rb_tree.
  52. };
  53. struct rb_iter {
  54. struct rb_tree *tree;
  55. struct rb_node *node; // Current node
  56. struct rb_node *path[RB_ITER_MAX_HEIGHT]; // Traversal path
  57. size_t top; // Top of stack
  58. void *info; // User provided, not used by rb_iter.
  59. };
  60. int rb_tree_node_cmp_ptr_cb (struct rb_tree *self, struct rb_node *a, struct rb_node *b);
  61. void rb_tree_node_dealloc_cb (struct rb_tree *self, struct rb_node *node);
  62. struct rb_node *rb_node_alloc ();
  63. struct rb_node *rb_node_create (void *value);
  64. struct rb_node *rb_node_init (struct rb_node *self, void *value);
  65. void rb_node_dealloc (struct rb_node *self);
  66. struct rb_tree *rb_tree_alloc ();
  67. struct rb_tree *rb_tree_create (rb_tree_node_cmp_f cmp);
  68. struct rb_tree *rb_tree_init (struct rb_tree *self, rb_tree_node_cmp_f cmp);
  69. void rb_tree_dealloc (struct rb_tree *self, rb_tree_node_f node_cb);
  70. void *rb_tree_find (struct rb_tree *self, void *value);
  71. int rb_tree_insert (struct rb_tree *self, void *value);
  72. int rb_tree_remove (struct rb_tree *self, void *value);
  73. size_t rb_tree_size (struct rb_tree *self);
  74. int rb_tree_insert_node (struct rb_tree *self, struct rb_node *node);
  75. int rb_tree_remove_with_cb (struct rb_tree *self, void *value, rb_tree_node_f node_cb);
  76. int rb_tree_test (struct rb_tree *self, struct rb_node *root);
  77. struct rb_iter *rb_iter_alloc ();
  78. struct rb_iter *rb_iter_init ();
  79. struct rb_iter *rb_iter_create ();
  80. void rb_iter_dealloc (struct rb_iter *self);
  81. void *rb_iter_first (struct rb_iter *self, struct rb_tree *tree);
  82. void *rb_iter_last (struct rb_iter *self, struct rb_tree *tree);
  83. void *rb_iter_next (struct rb_iter *self);
  84. void *rb_iter_prev (struct rb_iter *self);
  85. #endif