gl_rbtree_oset.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Ordered set data type implemented by a binary tree.
  2. Copyright (C) 2006-2007, 2009-2020 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. 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, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "gl_rbtree_oset.h"
  17. #include <stdlib.h>
  18. /* -------------------------- gl_oset_t Data Type -------------------------- */
  19. /* Parameterization of gl_rbtree_ordered.h. */
  20. #define CONTAINER_T gl_oset_t
  21. #define CONTAINER_IMPL gl_oset_impl
  22. #define CONTAINER_IMPL_BASE gl_oset_impl_base
  23. #define NODE_IMPL gl_oset_node_impl
  24. #define NODE_T gl_oset_node_t
  25. #define NODE_PAYLOAD_FIELDS \
  26. const void *value;
  27. #define NODE_PAYLOAD_PARAMS \
  28. const void *elt
  29. #define NODE_PAYLOAD_ASSIGN(node) \
  30. node->value = elt;
  31. #define NODE_PAYLOAD_DISPOSE(container, node) \
  32. if (container->base.dispose_fn != NULL) \
  33. container->base.dispose_fn (node->value);
  34. #include "gl_rbtree_ordered.h"
  35. /* Generic binary tree code. */
  36. #include "gl_anytree_oset.h"
  37. /* For debugging. */
  38. void
  39. gl_rbtree_oset_check_invariants (gl_oset_t set)
  40. {
  41. size_t counter = 0;
  42. if (set->root != NULL)
  43. check_invariants (set->root, NULL, &counter);
  44. if (!(set->count == counter))
  45. abort ();
  46. }
  47. const struct gl_oset_implementation gl_rbtree_oset_implementation =
  48. {
  49. gl_tree_nx_create_empty,
  50. gl_tree_size,
  51. gl_tree_search,
  52. gl_tree_search_atleast,
  53. gl_tree_nx_add,
  54. gl_tree_remove,
  55. gl_tree_update,
  56. gl_tree_oset_free,
  57. gl_tree_iterator,
  58. gl_tree_iterator_atleast,
  59. gl_tree_iterator_next,
  60. gl_tree_iterator_free
  61. };