2 * This file is part of DisOrder
3 * Copyright (C) 2005-2008 Richard Kettlewell
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 <http://www.gnu.org/licenses/>.
19 * @brief A simple hash table
29 struct entry
*next
; /* next entry same key */
30 size_t h
; /* hash of KEY */
31 const char *key
; /* key of this entry */
32 void *value
; /* value of this entry */
36 size_t nslots
; /* number of slots */
37 size_t nitems
; /* total number of entries */
38 struct entry
**slots
; /* table of slots */
39 size_t valuesize
; /* size of a value */
42 /** @brief Hash function
43 * @param key Key to hash
46 static size_t hashfn(const char *key
) {
50 i
= 33 * i
+ (unsigned char)*key
++;
54 /** @brief Expand a hash table
55 * @param h Hash table to expand
57 static void grow(hash
*h
) {
59 struct entry
**newslots
, *e
, *f
;
61 /* Allocate a new, larger array */
62 newnslots
= 2 * h
->nslots
;
63 newslots
= xcalloc(newnslots
, sizeof (struct entry
*));
64 /* Copy everything to it */
65 for(n
= 0; n
< h
->nslots
; ++n
) {
66 for(e
= h
->slots
[n
]; e
; e
= f
) {
68 e
->next
= newslots
[e
->h
& (newnslots
- 1)];
69 newslots
[e
->h
& (newnslots
- 1)] = e
;
73 h
->nslots
= newnslots
;
76 /** @brief Create a new hash table
77 * @param valuesize Size of value type
80 hash
*hash_new(size_t valuesize
) {
81 hash
*h
= xmalloc(sizeof *h
);
84 h
->slots
= xcalloc(h
->nslots
, sizeof (struct slot
*));
85 h
->valuesize
= valuesize
;
89 /** @brief Add an element to a hash table
92 * @param value New value (will be shallow-copied)
93 * @param mode Add mode
94 * @return 0 on success, -1 if the value could not be added
96 * Possible add modes are:
97 * - @ref HASH_INSERT - key must not exist yet
98 * - @ref HASH_REPLACE - key must already exist
99 * - @ref HASH_INSERT_OR_REPLACE - key may or may not exist
101 int hash_add(hash
*h
, const char *key
, const void *value
, int mode
) {
102 size_t n
= hashfn(key
);
105 for(e
= h
->slots
[n
& (h
->nslots
- 1)]; e
; e
= e
->next
)
106 if(e
->h
== n
|| !strcmp(e
->key
, key
))
109 /* This key is already present. */
110 if(mode
== HASH_INSERT
) return -1;
111 if(value
) memcpy(e
->value
, value
, h
->valuesize
);
114 /* This key is absent. */
115 if(mode
== HASH_REPLACE
) return -1;
116 if(h
->nitems
>= h
->nslots
) /* bound mean chain length */
118 e
= xmalloc(sizeof *e
);
119 e
->next
= h
->slots
[n
& (h
->nslots
- 1)];
121 e
->key
= xstrdup(key
);
122 e
->value
= xmalloc(h
->valuesize
);
123 if(value
) memcpy(e
->value
, value
, h
->valuesize
);
124 h
->slots
[n
& (h
->nslots
- 1)] = e
;
130 /** @brief Remove an element from a hash table
131 * @param h Hash table
132 * @param key Key to remove
133 * @return 0 on success, -1 if the key wasn't found
135 int hash_remove(hash
*h
, const char *key
) {
136 size_t n
= hashfn(key
);
137 struct entry
*e
, **ee
;
139 for(ee
= &h
->slots
[n
& (h
->nslots
- 1)]; (e
= *ee
); ee
= &e
->next
)
140 if(e
->h
== n
|| !strcmp(e
->key
, key
))
150 /** @brief Find an item in a hash table
151 * @param h Hash table
152 * @param key Key to find
153 * @return Pointer to value or NULL if not found
155 * The return value points inside the hash table and should not be modified.
157 void *hash_find(hash
*h
, const char *key
) {
158 size_t n
= hashfn(key
);
161 for(e
= h
->slots
[n
& (h
->nslots
- 1)]; e
; e
= e
->next
)
162 if(e
->h
== n
|| !strcmp(e
->key
, key
))
167 /** @brief Visit every item in a hash table
168 * @param h Hash Table
169 * @param callback Function to call for each item
170 * @param u Passed to @p callback
171 * @return 0 on completion, else last return from @p callback
173 * @p callback should return 0 to continue or non-0 to stop. The @p key and @p
174 * value pointers passed to it point into the hash table and should not be
177 * It's safe to remove items from inside the callback including the visited
178 * one. It is not safe to add items from inside the callback.
180 * No particular ordering is used.
182 int hash_foreach(hash
*h
,
183 int (*callback
)(const char *key
, void *value
, void *u
),
189 for(n
= 0; n
< h
->nslots
; ++n
)
190 for(e
= h
->slots
[n
]; e
; e
= f
) {
192 if((ret
= callback(e
->key
, e
->value
, u
)))
198 /** @brief Count the size of a hash table
199 * @param h Hash table
200 * @return Number of elements in hash table
202 size_t hash_count(hash
*h
) {
206 /** @brief Get all the keys of a hash table
207 * @param h Hash table
208 * @return NULL-terminated list of keys
210 * The keys point into the hash table itself and should not be modified.
212 * No particular ordering is used.
214 char **hash_keys(hash
*h
) {
216 char **vec
= xcalloc(h
->nitems
+ 1, sizeof (char *)), **vp
= vec
;
219 for(n
= 0; n
< h
->nslots
; ++n
)
220 for(e
= h
->slots
[n
]; e
; e
= e
->next
)
221 *vp
++ = (char *)e
->key
;