gl_linkedhash_list.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Sequential list data type implemented by a hash table with a linked list.
  2. Copyright (C) 2006, 2008-2013 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 <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "gl_linkedhash_list.h"
  17. #include <stdint.h> /* for SIZE_MAX */
  18. #include <stdlib.h>
  19. #include "xsize.h"
  20. #ifndef uintptr_t
  21. # define uintptr_t unsigned long
  22. #endif
  23. #define WITH_HASHTABLE 1
  24. /* -------------------------- gl_list_t Data Type -------------------------- */
  25. /* Generic hash-table code. */
  26. #include "gl_anyhash_list1.h"
  27. /* Generic linked list code. */
  28. #include "gl_anylinked_list1.h"
  29. /* Generic hash-table code. */
  30. #include "gl_anyhash_list2.h"
  31. /* Resize the hash table if needed, after list->count was incremented. */
  32. static void
  33. hash_resize_after_add (gl_list_t list)
  34. {
  35. size_t count = list->count;
  36. size_t estimate = xsum (count, count / 2); /* 1.5 * count */
  37. if (estimate > list->table_size)
  38. hash_resize (list, estimate);
  39. }
  40. /* Add a node to the hash table structure. */
  41. static void
  42. add_to_bucket (gl_list_t list, gl_list_node_t node)
  43. {
  44. size_t bucket = node->h.hashcode % list->table_size;
  45. node->h.hash_next = list->table[bucket];
  46. list->table[bucket] = &node->h;
  47. }
  48. /* Tell all compilers that the return value is 0. */
  49. #define add_to_bucket(list,node) ((add_to_bucket) (list, node), 0)
  50. /* Remove a node from the hash table structure. */
  51. static void
  52. remove_from_bucket (gl_list_t list, gl_list_node_t node)
  53. {
  54. size_t bucket = node->h.hashcode % list->table_size;
  55. gl_hash_entry_t *p;
  56. for (p = &list->table[bucket]; ; p = &(*p)->hash_next)
  57. {
  58. if (*p == &node->h)
  59. {
  60. *p = node->h.hash_next;
  61. break;
  62. }
  63. if (*p == NULL)
  64. /* node is not in the right bucket. Did the hash codes
  65. change inadvertently? */
  66. abort ();
  67. }
  68. }
  69. /* Generic linked list code. */
  70. #include "gl_anylinked_list2.h"
  71. const struct gl_list_implementation gl_linkedhash_list_implementation =
  72. {
  73. gl_linked_nx_create_empty,
  74. gl_linked_nx_create,
  75. gl_linked_size,
  76. gl_linked_node_value,
  77. gl_linked_node_nx_set_value,
  78. gl_linked_next_node,
  79. gl_linked_previous_node,
  80. gl_linked_get_at,
  81. gl_linked_nx_set_at,
  82. gl_linked_search_from_to,
  83. gl_linked_indexof_from_to,
  84. gl_linked_nx_add_first,
  85. gl_linked_nx_add_last,
  86. gl_linked_nx_add_before,
  87. gl_linked_nx_add_after,
  88. gl_linked_nx_add_at,
  89. gl_linked_remove_node,
  90. gl_linked_remove_at,
  91. gl_linked_remove,
  92. gl_linked_list_free,
  93. gl_linked_iterator,
  94. gl_linked_iterator_from_to,
  95. gl_linked_iterator_next,
  96. gl_linked_iterator_free,
  97. gl_linked_sortedlist_search,
  98. gl_linked_sortedlist_search_from_to,
  99. gl_linked_sortedlist_indexof,
  100. gl_linked_sortedlist_indexof_from_to,
  101. gl_linked_sortedlist_nx_add,
  102. gl_linked_sortedlist_remove
  103. };