gl_rbtreehash_list.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Sequential list data type implemented by a hash table with a binary tree.
  2. Copyright (C) 2006, 2008-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_rbtreehash_list.h"
  17. #include <stdint.h> /* for uintptr_t, SIZE_MAX */
  18. #include <stdlib.h>
  19. #include "gl_rbtree_oset.h"
  20. #include "xsize.h"
  21. #define WITH_HASHTABLE 1
  22. /* Which kind of binary trees to use for ordered sets. Quite arbitrary. */
  23. #define OSET_TREE_FLAVOR GL_RBTREE_OSET
  24. /* -------------------------- gl_list_t Data Type -------------------------- */
  25. /* Generic hash-table code: Type definitions. */
  26. #include "gl_anyhash1.h"
  27. /* Generic red-black tree code: Type definitions. */
  28. #include "gl_anyrbtree_list1.h"
  29. /* Generic hash-table code: Low-level code. */
  30. #define CONTAINER_T gl_list_t
  31. #define CONTAINER_COUNT(list) \
  32. ((list)->root != NULL ? (list)->root->branch_size : 0)
  33. #include "gl_anyhash2.h"
  34. /* Generic binary tree code: Type definitions. */
  35. #include "gl_anytree_list1.h"
  36. /* Hash-table with binary tree code: Handling of hash buckets. */
  37. #include "gl_anytreehash_list1.h"
  38. /* Generic red-black tree code: Insertion/deletion algorithms. */
  39. #include "gl_anyrbtree_list2.h"
  40. /* Generic binary tree code: Functions taking advantage of the hash table. */
  41. #include "gl_anytreehash_list2.h"
  42. /* Generic binary tree code: All other functions. */
  43. #include "gl_anytree_list2.h"
  44. /* For debugging. */
  45. static unsigned int
  46. check_invariants (gl_list_node_t node, gl_list_node_t parent)
  47. {
  48. unsigned int left_blackheight =
  49. (node->left != NULL ? check_invariants (node->left, node) : 0);
  50. unsigned int right_blackheight =
  51. (node->right != NULL ? check_invariants (node->right, node) : 0);
  52. if (!(node->parent == parent))
  53. abort ();
  54. if (!(node->branch_size
  55. == (node->left != NULL ? node->left->branch_size : 0)
  56. + 1 + (node->right != NULL ? node->right->branch_size : 0)))
  57. abort ();
  58. if (!(node->color == BLACK || node->color == RED))
  59. abort ();
  60. if (parent == NULL && !(node->color == BLACK))
  61. abort ();
  62. if (!(left_blackheight == right_blackheight))
  63. abort ();
  64. return left_blackheight + (node->color == BLACK ? 1 : 0);
  65. }
  66. void
  67. gl_rbtreehash_list_check_invariants (gl_list_t list)
  68. {
  69. if (list->root != NULL)
  70. check_invariants (list->root, NULL);
  71. }
  72. const struct gl_list_implementation gl_rbtreehash_list_implementation =
  73. {
  74. gl_tree_nx_create_empty,
  75. gl_tree_nx_create,
  76. gl_tree_size,
  77. gl_tree_node_value,
  78. gl_tree_node_nx_set_value,
  79. gl_tree_next_node,
  80. gl_tree_previous_node,
  81. gl_tree_get_at,
  82. gl_tree_nx_set_at,
  83. gl_tree_search_from_to,
  84. gl_tree_indexof_from_to,
  85. gl_tree_nx_add_first,
  86. gl_tree_nx_add_last,
  87. gl_tree_nx_add_before,
  88. gl_tree_nx_add_after,
  89. gl_tree_nx_add_at,
  90. gl_tree_remove_node,
  91. gl_tree_remove_at,
  92. gl_tree_remove,
  93. gl_tree_list_free,
  94. gl_tree_iterator,
  95. gl_tree_iterator_from_to,
  96. gl_tree_iterator_next,
  97. gl_tree_iterator_free,
  98. gl_tree_sortedlist_search,
  99. gl_tree_sortedlist_search_from_to,
  100. gl_tree_sortedlist_indexof,
  101. gl_tree_sortedlist_indexof_from_to,
  102. gl_tree_sortedlist_nx_add,
  103. gl_tree_sortedlist_remove
  104. };