1 /* Map data type implemented by a hash table with a linked list.
2 Copyright (C) 2006, 2008-2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #include "gl_linkedhash_map.h"
23 #include <stdint.h> /* for SIZE_MAX */
29 # define uintptr_t unsigned long
32 /* --------------------------- gl_map_t Data Type --------------------------- */
34 #include "gl_anyhash1.h"
36 /* Concrete list node implementation, valid for this file only. */
37 struct gl_list_node_impl
39 struct gl_hash_entry h
; /* hash table entry fields; must be first */
40 struct gl_list_node_impl
*next
;
41 struct gl_list_node_impl
*prev
;
45 typedef struct gl_list_node_impl
* gl_list_node_t
;
47 /* Concrete gl_map_impl type, valid for this file only. */
50 struct gl_map_impl_base base
;
51 gl_mapkey_hashcode_fn hashcode_fn
;
52 /* A hash table: managed as an array of collision lists. */
53 struct gl_hash_entry
**table
;
55 /* A circular list anchored at root.
56 The first node is = root.next, the last node is = root.prev.
57 The root's value is unused. */
58 struct gl_list_node_impl root
;
59 /* Number of list nodes, excluding the root. */
63 #define CONTAINER_T gl_map_t
64 #define CONTAINER_COUNT(map) (map)->count
65 #include "gl_anyhash2.h"
67 /* If the symbol SIGNAL_SAFE_MAP is defined, the code is compiled in such
68 a way that a gl_map_t data structure may be used from within a signal
69 handler. The operations allowed in the signal handler are:
70 gl_map_iterator, gl_map_iterator_next, gl_map_iterator_free.
71 The map and node fields that are therefore accessed from the signal handler
73 map->root, node->next, node->value.
74 We are careful to make modifications to these fields only in an order
75 that maintains the consistency of the list data structure at any moment,
76 and we use 'volatile' assignments to prevent the compiler from reordering
78 #ifdef SIGNAL_SAFE_MAP
79 # define ASYNCSAFE(type) *(type volatile *)&
81 # define ASYNCSAFE(type)
84 /* --------------------------- gl_map_t Data Type --------------------------- */
87 gl_linkedhash_nx_create_empty (gl_map_implementation_t implementation
,
88 gl_mapkey_equals_fn equals_fn
,
89 gl_mapkey_hashcode_fn hashcode_fn
,
90 gl_mapkey_dispose_fn kdispose_fn
,
91 gl_mapvalue_dispose_fn vdispose_fn
)
93 struct gl_map_impl
*map
=
94 (struct gl_map_impl
*) malloc (sizeof (struct gl_map_impl
));
99 map
->base
.vtable
= implementation
;
100 map
->base
.equals_fn
= equals_fn
;
101 map
->base
.kdispose_fn
= kdispose_fn
;
102 map
->base
.vdispose_fn
= vdispose_fn
;
103 map
->hashcode_fn
= hashcode_fn
;
104 map
->table_size
= 11;
106 (gl_hash_entry_t
*) calloc (map
->table_size
, sizeof (gl_hash_entry_t
));
107 if (map
->table
== NULL
)
109 map
->root
.next
= &map
->root
;
110 map
->root
.prev
= &map
->root
;
120 static size_t _GL_ATTRIBUTE_PURE
121 gl_linkedhash_size (gl_map_t map
)
126 static bool _GL_ATTRIBUTE_PURE
127 gl_linkedhash_search (gl_map_t map
, const void *key
, const void **valuep
)
130 (map
->hashcode_fn
!= NULL
131 ? map
->hashcode_fn (key
)
132 : (size_t)(uintptr_t) key
);
133 size_t bucket
= hashcode
% map
->table_size
;
134 gl_mapkey_equals_fn equals
= map
->base
.equals_fn
;
136 /* Look for a match in the hash bucket. */
139 for (node
= (gl_list_node_t
) map
->table
[bucket
];
141 node
= (gl_list_node_t
) node
->h
.hash_next
)
142 if (node
->h
.hashcode
== hashcode
144 ? equals (key
, node
->key
)
147 *valuep
= node
->value
;
154 gl_linkedhash_nx_getput (gl_map_t map
, const void *key
, const void *value
,
155 const void **oldvaluep
)
158 (map
->hashcode_fn
!= NULL
159 ? map
->hashcode_fn (key
)
160 : (size_t)(uintptr_t) key
);
161 size_t bucket
= hashcode
% map
->table_size
;
162 gl_mapkey_equals_fn equals
= map
->base
.equals_fn
;
164 /* Look for a match in the hash bucket. */
168 for (node
= (gl_list_node_t
) map
->table
[bucket
];
170 node
= (gl_list_node_t
) node
->h
.hash_next
)
171 if (node
->h
.hashcode
== hashcode
173 ? equals (key
, node
->key
)
176 *oldvaluep
= node
->value
;
182 /* Allocate a new node. */
183 gl_list_node_t node
=
184 (struct gl_list_node_impl
*) malloc (sizeof (struct gl_list_node_impl
));
189 ASYNCSAFE(const void *) node
->key
= key
;
190 ASYNCSAFE(const void *) node
->value
= value
;
191 node
->h
.hashcode
= hashcode
;
193 /* Add node to the hash table. */
194 node
->h
.hash_next
= map
->table
[bucket
];
195 map
->table
[bucket
] = &node
->h
;
197 /* Add node to the map. */
198 ASYNCSAFE(gl_list_node_t
) node
->next
= &map
->root
;
199 node
->prev
= map
->root
.prev
;
200 ASYNCSAFE(gl_list_node_t
) node
->prev
->next
= node
;
201 map
->root
.prev
= node
;
204 hash_resize_after_add (map
);
210 gl_linkedhash_getremove (gl_map_t map
, const void *key
, const void **oldvaluep
)
213 (map
->hashcode_fn
!= NULL
214 ? map
->hashcode_fn (key
)
215 : (size_t)(uintptr_t) key
);
216 size_t bucket
= hashcode
% map
->table_size
;
217 gl_mapkey_equals_fn equals
= map
->base
.equals_fn
;
219 /* Look for the first match in the hash bucket. */
220 gl_list_node_t
*nodep
;
222 for (nodep
= (gl_list_node_t
*) &map
->table
[bucket
];
224 nodep
= (gl_list_node_t
*) &(*nodep
)->h
.hash_next
)
226 gl_list_node_t node
= *nodep
;
227 if (node
->h
.hashcode
== hashcode
229 ? equals (key
, node
->key
)
232 *oldvaluep
= node
->value
;
234 /* Remove node from the hash table. */
235 *nodep
= (gl_list_node_t
) node
->h
.hash_next
;
237 /* Remove node from the list. */
239 gl_list_node_t prev
= node
->prev
;
240 gl_list_node_t next
= node
->next
;
242 ASYNCSAFE(gl_list_node_t
) prev
->next
= next
;
247 if (map
->base
.kdispose_fn
!= NULL
)
248 map
->base
.kdispose_fn (node
->key
);
258 gl_linkedhash_free (gl_map_t map
)
260 gl_mapkey_dispose_fn kdispose
= map
->base
.kdispose_fn
;
261 gl_mapvalue_dispose_fn vdispose
= map
->base
.vdispose_fn
;
264 for (node
= map
->root
.next
; node
!= &map
->root
; )
266 gl_list_node_t next
= node
->next
;
267 if (vdispose
!= NULL
)
268 vdispose (node
->value
);
269 if (kdispose
!= NULL
)
270 kdispose (node
->key
);
278 /* ---------------------- gl_map_iterator_t Data Type ---------------------- */
280 /* Iterate through the list, not through the hash buckets, so that the order
281 in which the pairs are returned is predictable. */
283 static gl_map_iterator_t
284 gl_linkedhash_iterator (gl_map_t map
)
286 gl_map_iterator_t result
;
288 result
.vtable
= map
->base
.vtable
;
290 result
.p
= map
->root
.next
;
291 result
.q
= &map
->root
;
292 #if defined GCC_LINT || defined lint
302 gl_linkedhash_iterator_next (gl_map_iterator_t
*iterator
,
303 const void **keyp
, const void **valuep
)
305 if (iterator
->p
!= iterator
->q
)
307 gl_list_node_t node
= (gl_list_node_t
) iterator
->p
;
309 *valuep
= node
->value
;
310 iterator
->p
= node
->next
;
318 gl_linkedhash_iterator_free (gl_map_iterator_t
*iterator
)
323 const struct gl_map_implementation gl_linkedhash_map_implementation
=
325 gl_linkedhash_nx_create_empty
,
327 gl_linkedhash_search
,
328 gl_linkedhash_nx_getput
,
329 gl_linkedhash_getremove
,
331 gl_linkedhash_iterator
,
332 gl_linkedhash_iterator_next
,
333 gl_linkedhash_iterator_free