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/>.
27 struct entry *next; /* next entry same key */
28 size_t h; /* hash of KEY */
29 const char *key; /* key of this entry */
30 void *value; /* value of this entry */
34 size_t nslots; /* number of slots */
35 size_t nitems; /* total number of entries */
36 struct entry **slots; /* table of slots */
37 size_t valuesize; /* size of a value */
40 static size_t hashfn(const char *key) {
44 i = 33 * i + (unsigned char)*key++;
48 static void grow(hash *h) {
50 struct entry **newslots, *e, *f;
52 /* Allocate a new, larger array */
53 newnslots = 2 * h->nslots;
54 newslots = xcalloc(newnslots, sizeof (struct entry *));
55 /* Copy everything to it */
56 for(n = 0; n < h->nslots; ++n) {
57 for(e = h->slots[n]; e; e = f) {
59 e->next = newslots[e->h & (newnslots - 1)];
60 newslots[e->h & (newnslots - 1)] = e;
64 h->nslots = newnslots;
67 hash *hash_new(size_t valuesize) {
68 hash *h = xmalloc(sizeof *h);
71 h->slots = xcalloc(h->nslots, sizeof (struct slot *));
72 h->valuesize = valuesize;
76 int hash_add(hash *h, const char *key, const void *value, int mode) {
77 size_t n = hashfn(key);
80 for(e = h->slots[n & (h->nslots - 1)]; e; e = e->next)
81 if(e->h == n || !strcmp(e->key, key))
84 /* This key is already present. */
85 if(mode == HASH_INSERT) return -1;
86 if(value) memcpy(e->value, value, h->valuesize);
89 /* This key is absent. */
90 if(mode == HASH_REPLACE) return -1;
91 if(h->nitems >= h->nslots) /* bound mean chain length */
93 e = xmalloc(sizeof *e);
94 e->next = h->slots[n & (h->nslots - 1)];
96 e->key = xstrdup(key);
97 e->value = xmalloc(h->valuesize);
98 if(value) memcpy(e->value, value, h->valuesize);
99 h->slots[n & (h->nslots - 1)] = e;
105 int hash_remove(hash *h, const char *key) {
106 size_t n = hashfn(key);
107 struct entry *e, **ee;
109 for(ee = &h->slots[n & (h->nslots - 1)]; (e = *ee); ee = &e->next)
110 if(e->h == n || !strcmp(e->key, key))
120 void *hash_find(hash *h, const char *key) {
121 size_t n = hashfn(key);
124 for(e = h->slots[n & (h->nslots - 1)]; e; e = e->next)
125 if(e->h == n || !strcmp(e->key, key))
130 int hash_foreach(hash *h,
131 int (*callback)(const char *key, void *value, void *u),
137 for(n = 0; n < h->nslots; ++n)
138 for(e = h->slots[n]; e; e = f) {
140 if((ret = callback(e->key, e->value, u)))
146 size_t hash_count(hash *h) {
150 char **hash_keys(hash *h) {
152 char **vec = xcalloc(h->nitems + 1, sizeof (char *)), **vp = vec;
155 for(n = 0; n < h->nslots; ++n)
156 for(e = h->slots[n]; e; e = e->next)
157 *vp++ = (char *)e->key;