gl_anytreehash_list1.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /* Hash table entry representing the same value at more than one position. */
  16. struct gl_multiple_nodes
  17. {
  18. struct gl_hash_entry h; /* hash table entry fields; must be first */
  19. void *magic; /* used to distinguish from single node */
  20. gl_oset_t nodes; /* set of nodes, sorted by position */
  21. };
  22. /* A value that cannot occur at the corresponding field (->left) in
  23. gl_list_node_impl. */
  24. #define MULTIPLE_NODES_MAGIC (void *) -1
  25. /* Returns the position of the given node in the tree. */
  26. static size_t _GL_ATTRIBUTE_PURE
  27. node_position (gl_list_node_t node)
  28. {
  29. size_t position = 0;
  30. if (node->left != NULL)
  31. position += node->left->branch_size;
  32. for (;;)
  33. {
  34. gl_list_node_t parent = node->parent;
  35. if (parent == NULL)
  36. return position;
  37. /* position is now relative to the subtree of node. */
  38. if (parent->right == node)
  39. {
  40. position += 1;
  41. if (parent->left != NULL)
  42. position += parent->left->branch_size;
  43. }
  44. /* position is now relative to the subtree of parent. */
  45. node = parent;
  46. }
  47. }
  48. /* Compares two nodes by their position in the tree. */
  49. static int _GL_ATTRIBUTE_PURE
  50. compare_by_position (const void *x1, const void *x2)
  51. {
  52. gl_list_node_t node1 = (gl_list_node_t) x1;
  53. gl_list_node_t node2 = (gl_list_node_t) x2;
  54. size_t position1 = node_position (node1);
  55. size_t position2 = node_position (node2);
  56. return (position1 > position2 ? 1 :
  57. position1 < position2 ? -1 : 0);
  58. }
  59. /* Compares a node's position in the tree with a given threshold. */
  60. static bool _GL_ATTRIBUTE_PURE
  61. compare_position_threshold (const void *x, const void *threshold)
  62. {
  63. gl_list_node_t node = (gl_list_node_t) x;
  64. size_t position = node_position (node);
  65. return (position >= (uintptr_t)threshold);
  66. }
  67. /* Returns the first element of a non-empty ordered set of nodes. */
  68. static gl_list_node_t
  69. gl_oset_first (gl_oset_t set)
  70. {
  71. gl_oset_iterator_t iter = gl_oset_iterator (set);
  72. const void *first;
  73. if (!gl_oset_iterator_next (&iter, &first))
  74. abort ();
  75. gl_oset_iterator_free (&iter);
  76. return (gl_list_node_t) first;
  77. }
  78. /* Adds a node to the hash table structure.
  79. If duplicates are allowed, this function performs in average time
  80. O((log n)^2): gl_oset_nx_add may need to add an element to an ordered set
  81. of size O(n), needing O(log n) comparison function calls. The comparison
  82. function is compare_by_position, which is O(log n) worst-case.
  83. If duplicates are forbidden, this function is O(1).
  84. Return 0 upon success, -1 upon out-of-memory. */
  85. static int
  86. add_to_bucket (gl_list_t list, gl_list_node_t new_node)
  87. {
  88. size_t bucket = new_node->h.hashcode % list->table_size;
  89. /* If no duplicates are allowed, multiple nodes are not needed. */
  90. if (list->base.allow_duplicates)
  91. {
  92. size_t hashcode = new_node->h.hashcode;
  93. const void *value = new_node->value;
  94. gl_listelement_equals_fn equals = list->base.equals_fn;
  95. gl_hash_entry_t *entryp;
  96. for (entryp = &list->table[bucket]; *entryp != NULL; entryp = &(*entryp)->hash_next)
  97. {
  98. gl_hash_entry_t entry = *entryp;
  99. if (entry->hashcode == hashcode)
  100. {
  101. if (((struct gl_multiple_nodes *) entry)->magic == MULTIPLE_NODES_MAGIC)
  102. {
  103. /* An entry representing multiple nodes. */
  104. gl_oset_t nodes = ((struct gl_multiple_nodes *) entry)->nodes;
  105. /* Only the first node is interesting. */
  106. gl_list_node_t node = gl_oset_first (nodes);
  107. if (equals != NULL ? equals (value, node->value) : value == node->value)
  108. {
  109. /* Found already multiple nodes with the same value.
  110. Add the new_node to it. */
  111. return gl_oset_nx_add (nodes, new_node);
  112. }
  113. }
  114. else
  115. {
  116. /* An entry representing a single node. */
  117. gl_list_node_t node = (struct gl_list_node_impl *) entry;
  118. if (equals != NULL ? equals (value, node->value) : value == node->value)
  119. {
  120. /* Found already a node with the same value. Turn it
  121. into an ordered set, and add new_node to it. */
  122. gl_oset_t nodes;
  123. struct gl_multiple_nodes *multi_entry;
  124. nodes =
  125. gl_oset_nx_create_empty (OSET_TREE_FLAVOR,
  126. compare_by_position, NULL);
  127. if (nodes == NULL)
  128. return -1;
  129. if (gl_oset_nx_add (nodes, node) < 0)
  130. goto fail;
  131. if (gl_oset_nx_add (nodes, new_node) < 0)
  132. goto fail;
  133. multi_entry =
  134. (struct gl_multiple_nodes *) malloc (sizeof (struct gl_multiple_nodes));
  135. if (multi_entry == NULL)
  136. goto fail;
  137. multi_entry->h.hash_next = entry->hash_next;
  138. multi_entry->h.hashcode = entry->hashcode;
  139. multi_entry->magic = MULTIPLE_NODES_MAGIC;
  140. multi_entry->nodes = nodes;
  141. *entryp = &multi_entry->h;
  142. return 0;
  143. fail:
  144. gl_oset_free (nodes);
  145. return -1;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. /* If no duplicates are allowed, multiple nodes are not needed. */
  152. new_node->h.hash_next = list->table[bucket];
  153. list->table[bucket] = &new_node->h;
  154. return 0;
  155. }
  156. /* Tell GCC that the likely return value is 0. */
  157. #define add_to_bucket(list,node) \
  158. __builtin_expect ((add_to_bucket) (list, node), 0)
  159. /* Removes a node from the hash table structure.
  160. If duplicates are allowed, this function performs in average time
  161. O((log n)^2): gl_oset_remove may need to remove an element from an ordered
  162. set of size O(n), needing O(log n) comparison function calls. The
  163. comparison function is compare_by_position, which is O(log n) worst-case.
  164. If duplicates are forbidden, this function is O(1) on average. */
  165. static void
  166. remove_from_bucket (gl_list_t list, gl_list_node_t old_node)
  167. {
  168. size_t bucket = old_node->h.hashcode % list->table_size;
  169. if (list->base.allow_duplicates)
  170. {
  171. size_t hashcode = old_node->h.hashcode;
  172. const void *value = old_node->value;
  173. gl_listelement_equals_fn equals = list->base.equals_fn;
  174. gl_hash_entry_t *entryp;
  175. for (entryp = &list->table[bucket]; ; entryp = &(*entryp)->hash_next)
  176. {
  177. gl_hash_entry_t entry = *entryp;
  178. if (entry == &old_node->h)
  179. {
  180. /* Found old_node as a single node in the bucket. Remove it. */
  181. *entryp = old_node->h.hash_next;
  182. break;
  183. }
  184. if (entry == NULL)
  185. /* node is not in the right bucket. Did the hash codes
  186. change inadvertently? */
  187. abort ();
  188. if (((struct gl_multiple_nodes *) entry)->magic == MULTIPLE_NODES_MAGIC
  189. && entry->hashcode == hashcode)
  190. {
  191. /* An entry representing multiple nodes. */
  192. gl_oset_t nodes = ((struct gl_multiple_nodes *) entry)->nodes;
  193. /* Only the first node is interesting. */
  194. gl_list_node_t node = gl_oset_first (nodes);
  195. if (equals != NULL ? equals (value, node->value) : value == node->value)
  196. {
  197. /* Found multiple nodes with the same value.
  198. old_node must be one of them. Remove it. */
  199. if (!gl_oset_remove (nodes, old_node))
  200. abort ();
  201. if (gl_oset_size (nodes) == 1)
  202. {
  203. /* Replace a one-element set with a single node. */
  204. node = gl_oset_first (nodes);
  205. node->h.hash_next = entry->hash_next;
  206. *entryp = &node->h;
  207. gl_oset_free (nodes);
  208. free (entry);
  209. }
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. else
  216. {
  217. /* If no duplicates are allowed, multiple nodes are not needed. */
  218. gl_hash_entry_t *entryp;
  219. for (entryp = &list->table[bucket]; ; entryp = &(*entryp)->hash_next)
  220. {
  221. if (*entryp == &old_node->h)
  222. {
  223. *entryp = old_node->h.hash_next;
  224. break;
  225. }
  226. if (*entryp == NULL)
  227. /* node is not in the right bucket. Did the hash codes
  228. change inadvertently? */
  229. abort ();
  230. }
  231. }
  232. }
  233. /* Builds up the hash table during initialization: Stores all the nodes of
  234. list->root in the hash table.
  235. Returns 0 upon success, -1 upon out-of-memory. */
  236. static int
  237. add_nodes_to_buckets (gl_list_t list)
  238. {
  239. /* Iterate across all nodes. */
  240. gl_list_node_t node = list->root;
  241. iterstack_t stack;
  242. iterstack_item_t *stack_ptr = &stack[0];
  243. for (;;)
  244. {
  245. /* Descend on left branch. */
  246. for (;;)
  247. {
  248. if (node == NULL)
  249. break;
  250. stack_ptr->node = node;
  251. stack_ptr->rightp = false;
  252. node = node->left;
  253. stack_ptr++;
  254. }
  255. /* Climb up again. */
  256. for (;;)
  257. {
  258. if (stack_ptr == &stack[0])
  259. goto done;
  260. stack_ptr--;
  261. if (!stack_ptr->rightp)
  262. break;
  263. }
  264. node = stack_ptr->node;
  265. /* Add the current node to the hash table. */
  266. node->h.hashcode =
  267. (list->base.hashcode_fn != NULL
  268. ? list->base.hashcode_fn (node->value)
  269. : (size_t)(uintptr_t) node->value);
  270. if (add_to_bucket (list, node) < 0)
  271. goto fail;
  272. /* Descend on right branch. */
  273. stack_ptr->rightp = true;
  274. node = node->right;
  275. stack_ptr++;
  276. }
  277. done:
  278. return 0;
  279. fail:
  280. /* Undo everything. */
  281. for (;;)
  282. {
  283. /* Descend on left branch. */
  284. stack_ptr->rightp = false;
  285. node = node->left;
  286. stack_ptr++;
  287. /* Descend on right branch. */
  288. for (;;)
  289. {
  290. if (node == NULL)
  291. break;
  292. stack_ptr->node = node;
  293. stack_ptr->rightp = true;
  294. node = node->right;
  295. stack_ptr++;
  296. }
  297. /* Climb up again. */
  298. for (;;)
  299. {
  300. if (stack_ptr == &stack[0])
  301. goto fail_done;
  302. stack_ptr--;
  303. if (stack_ptr->rightp)
  304. break;
  305. }
  306. node = stack_ptr->node;
  307. /* Remove the current node from the hash table. */
  308. remove_from_bucket (list, node);
  309. }
  310. fail_done:
  311. return -1;
  312. }
  313. /* Tell GCC that the likely return value is 0. */
  314. #if (__GNUC__ >= 3) || (__clang_major__ >= 4)
  315. # define add_nodes_to_buckets(list) \
  316. __builtin_expect ((add_nodes_to_buckets) (list), 0)
  317. #endif