Fix specification header include.
[gnulib.git] / lib / gl_linkedhash_list.c
1 /* Sequential list data type implemented by a hash table with a linked list.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification.  */
24 #include "gl_linkedhash_list.h"
25
26 #include <stdlib.h>
27
28 #include "xalloc.h"
29 #include "xsize.h"
30 #include "size_max.h"
31
32 #ifndef uintptr_t
33 # define uintptr_t unsigned long
34 #endif
35
36 #define WITH_HASHTABLE 1
37
38 /* -------------------------- gl_list_t Data Type -------------------------- */
39
40 /* Generic hash-table code.  */
41 #include "gl_anyhash_list1.h"
42
43 /* Generic linked list code.  */
44 #include "gl_anylinked_list1.h"
45
46 /* Generic hash-table code.  */
47 #include "gl_anyhash_list2.h"
48
49 /* Resize the hash table if needed, after list->count was incremented.  */
50 static inline void
51 hash_resize_after_add (gl_list_t list)
52 {
53   size_t count = list->count;
54   size_t estimate = xsum (count, count / 2); /* 1.5 * count */
55   if (estimate > list->table_size)
56     hash_resize (list, estimate);
57 }
58
59 /* Add a node to the hash table structure.  */
60 static inline void
61 add_to_bucket (gl_list_t list, gl_list_node_t node)
62 {
63   size_t index = node->h.hashcode % list->table_size;
64
65   node->h.hash_next = list->table[index];
66   list->table[index] = &node->h;
67 }
68
69 /* Remove a node from the hash table structure.  */
70 static inline void
71 remove_from_bucket (gl_list_t list, gl_list_node_t node)
72 {
73   size_t index = node->h.hashcode % list->table_size;
74   gl_hash_entry_t *p;
75
76   for (p = &list->table[index]; ; p = &(*p)->hash_next)
77     {
78       if (*p == &node->h)
79         {
80           *p = node->h.hash_next;
81           break;
82         }
83       if (*p == NULL)
84         /* node is not in the right bucket.  Did the hash codes
85            change inadvertently?  */
86         abort ();
87     }
88 }
89
90 /* Generic linked list code.  */
91 #include "gl_anylinked_list2.h"
92
93
94 const struct gl_list_implementation gl_linkedhash_list_implementation =
95   {
96     gl_linked_create_empty,
97     gl_linked_create,
98     gl_linked_size,
99     gl_linked_node_value,
100     gl_linked_next_node,
101     gl_linked_previous_node,
102     gl_linked_get_at,
103     gl_linked_set_at,
104     gl_linked_search,
105     gl_linked_indexof,
106     gl_linked_add_first,
107     gl_linked_add_last,
108     gl_linked_add_before,
109     gl_linked_add_after,
110     gl_linked_add_at,
111     gl_linked_remove_node,
112     gl_linked_remove_at,
113     gl_linked_remove,
114     gl_linked_list_free,
115     gl_linked_iterator,
116     gl_linked_iterator_from_to,
117     gl_linked_iterator_next,
118     gl_linked_iterator_free,
119     gl_linked_sortedlist_search,
120     gl_linked_sortedlist_indexof,
121     gl_linked_sortedlist_add,
122     gl_linked_sortedlist_remove
123   };