gl_anytreehash_list2.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Sequential list data type implemented by a hash table with 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. /* Common code of gl_avltreehash_list.c and gl_rbtreehash_list.c. */
  15. static gl_list_node_t
  16. gl_tree_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
  17. const void *elt)
  18. {
  19. if (!(start_index <= end_index
  20. && end_index <= (list->root != NULL ? list->root->branch_size : 0)))
  21. /* Invalid arguments. */
  22. abort ();
  23. {
  24. size_t hashcode =
  25. (list->base.hashcode_fn != NULL
  26. ? list->base.hashcode_fn (elt)
  27. : (size_t)(uintptr_t) elt);
  28. size_t bucket = hashcode % list->table_size;
  29. gl_listelement_equals_fn equals = list->base.equals_fn;
  30. gl_hash_entry_t entry;
  31. if (list->base.allow_duplicates)
  32. {
  33. for (entry = list->table[bucket]; entry != NULL; entry = entry->hash_next)
  34. if (entry->hashcode == hashcode)
  35. {
  36. if (((struct gl_multiple_nodes *) entry)->magic == MULTIPLE_NODES_MAGIC)
  37. {
  38. /* An entry representing multiple nodes. */
  39. gl_oset_t nodes = ((struct gl_multiple_nodes *) entry)->nodes;
  40. /* The first node is interesting. */
  41. gl_list_node_t node = gl_oset_first (nodes);
  42. if (equals != NULL ? equals (elt, node->value) : elt == node->value)
  43. {
  44. /* All nodes in the entry are equal to the given ELT. */
  45. if (start_index == 0)
  46. {
  47. /* We have to return only the one at the minimal
  48. position, and this is the first one in the ordered
  49. set. */
  50. if (end_index == list->root->branch_size
  51. || node_position (node) < end_index)
  52. return node;
  53. }
  54. else
  55. {
  56. /* We have to return only the one at the minimal
  57. position >= start_index. */
  58. const void *nodes_elt;
  59. if (gl_oset_search_atleast (nodes,
  60. compare_position_threshold,
  61. (void *)(uintptr_t)start_index,
  62. &nodes_elt))
  63. {
  64. node = (gl_list_node_t) nodes_elt;
  65. if (end_index == list->root->branch_size
  66. || node_position (node) < end_index)
  67. return node;
  68. }
  69. }
  70. break;
  71. }
  72. }
  73. else
  74. {
  75. /* An entry representing a single node. */
  76. gl_list_node_t node = (struct gl_list_node_impl *) entry;
  77. if (equals != NULL ? equals (elt, node->value) : elt == node->value)
  78. {
  79. bool position_in_bounds;
  80. if (start_index == 0 && end_index == list->root->branch_size)
  81. position_in_bounds = true;
  82. else
  83. {
  84. size_t position = node_position (node);
  85. position_in_bounds =
  86. (position >= start_index && position < end_index);
  87. }
  88. if (position_in_bounds)
  89. return node;
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. else
  96. {
  97. /* If no duplicates are allowed, multiple nodes are not needed. */
  98. for (entry = list->table[bucket]; entry != NULL; entry = entry->hash_next)
  99. if (entry->hashcode == hashcode)
  100. {
  101. gl_list_node_t node = (struct gl_list_node_impl *) entry;
  102. if (equals != NULL ? equals (elt, node->value) : elt == node->value)
  103. {
  104. bool position_in_bounds;
  105. if (start_index == 0 && end_index == list->root->branch_size)
  106. position_in_bounds = true;
  107. else
  108. {
  109. size_t position = node_position (node);
  110. position_in_bounds =
  111. (position >= start_index && position < end_index);
  112. }
  113. if (position_in_bounds)
  114. return node;
  115. break;
  116. }
  117. }
  118. }
  119. return NULL;
  120. }
  121. }
  122. static size_t
  123. gl_tree_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
  124. const void *elt)
  125. {
  126. gl_list_node_t node =
  127. gl_tree_search_from_to (list, start_index, end_index, elt);
  128. if (node != NULL)
  129. return node_position (node);
  130. else
  131. return (size_t)(-1);
  132. }
  133. static void
  134. gl_tree_list_free (gl_list_t list)
  135. {
  136. if (list->base.allow_duplicates)
  137. {
  138. /* Free the ordered sets in the hash buckets. */
  139. size_t i;
  140. for (i = list->table_size; i > 0; )
  141. {
  142. gl_hash_entry_t entry = list->table[--i];
  143. while (entry != NULL)
  144. {
  145. gl_hash_entry_t next = entry->hash_next;
  146. if (((struct gl_multiple_nodes *) entry)->magic == MULTIPLE_NODES_MAGIC)
  147. {
  148. gl_oset_t nodes = ((struct gl_multiple_nodes *) entry)->nodes;
  149. gl_oset_free (nodes);
  150. free (entry);
  151. }
  152. entry = next;
  153. }
  154. }
  155. }
  156. /* Iterate across all elements in post-order. */
  157. {
  158. gl_list_node_t node = list->root;
  159. iterstack_t stack;
  160. iterstack_item_t *stack_ptr = &stack[0];
  161. for (;;)
  162. {
  163. /* Descend on left branch. */
  164. for (;;)
  165. {
  166. if (node == NULL)
  167. break;
  168. stack_ptr->node = node;
  169. stack_ptr->rightp = false;
  170. node = node->left;
  171. stack_ptr++;
  172. }
  173. /* Climb up again. */
  174. for (;;)
  175. {
  176. if (stack_ptr == &stack[0])
  177. goto done_iterate;
  178. stack_ptr--;
  179. node = stack_ptr->node;
  180. if (!stack_ptr->rightp)
  181. break;
  182. /* Free the current node. */
  183. if (list->base.dispose_fn != NULL)
  184. list->base.dispose_fn (node->value);
  185. free (node);
  186. }
  187. /* Descend on right branch. */
  188. stack_ptr->rightp = true;
  189. node = node->right;
  190. stack_ptr++;
  191. }
  192. }
  193. done_iterate:
  194. free (list->table);
  195. free (list);
  196. }