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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 struct entry *next; /* next entry same key */
33 size_t h; /* hash of KEY */
34 const char *key; /* key of this entry */
35 void *value; /* value of this entry */
39 size_t nslots; /* number of slots */
40 size_t nitems; /* total number of entries */
41 struct entry **slots; /* table of slots */
42 size_t valuesize; /* size of a value */
45 static size_t hashfn(const char *key) {
49 i = 33 * i + (unsigned char)*key++;
53 static void grow(hash *h) {
55 struct entry **newslots, *e, *f;
57 /* Allocate a new, larger array */
58 newnslots = 2 * h->nslots;
59 newslots = xcalloc(newnslots, sizeof (struct entry *));
60 /* Copy everything to it */
61 for(n = 0; n < h->nslots; ++n) {
62 for(e = h->slots[n]; e; e = f) {
64 e->next = newslots[e->h & (newnslots - 1)];
65 newslots[e->h & (newnslots - 1)] = e;
69 h->nslots = newnslots;
72 hash *hash_new(size_t valuesize) {
73 hash *h = xmalloc(sizeof *h);
76 h->slots = xcalloc(h->nslots, sizeof (struct slot *));
77 h->valuesize = valuesize;
81 int hash_add(hash *h, const char *key, const void *value, int mode) {
82 size_t n = hashfn(key);
85 for(e = h->slots[n & (h->nslots - 1)]; e; e = e->next)
86 if(e->h == n || !strcmp(e->key, key))
89 /* This key is already present. */
90 if(mode == HASH_INSERT) return -1;
91 if(value) memcpy(e->value, value, h->valuesize);
94 /* This key is absent. */
95 if(mode == HASH_REPLACE) return -1;
96 if(h->nitems >= h->nslots) /* bound mean chain length */
98 e = xmalloc(sizeof *e);
99 e->next = h->slots[n & (h->nslots - 1)];
101 e->key = xstrdup(key);
102 e->value = xmalloc(h->valuesize);
103 if(value) memcpy(e->value, value, h->valuesize);
104 h->slots[n & (h->nslots - 1)] = e;
110 int hash_remove(hash *h, const char *key) {
111 size_t n = hashfn(key);
112 struct entry *e, **ee;
114 for(ee = &h->slots[n & (h->nslots - 1)]; (e = *ee); ee = &e->next)
115 if(e->h == n || !strcmp(e->key, key))
125 void *hash_find(hash *h, const char *key) {
126 size_t n = hashfn(key);
129 for(e = h->slots[n & (h->nslots - 1)]; e; e = e->next)
130 if(e->h == n || !strcmp(e->key, key))
135 int hash_foreach(hash *h,
136 int (*callback)(const char *key, void *value, void *u),
142 for(n = 0; n < h->nslots; ++n)
143 for(e = h->slots[n]; e; e = f) {
145 if((ret = callback(e->key, e->value, u)))
151 size_t hash_count(hash *h) {
155 char **hash_keys(hash *h) {
157 char **vec = xcalloc(h->nitems + 1, sizeof (char *)), **vp = vec;
160 for(n = 0; n < h->nslots; ++n)
161 for(e = h->slots[n]; e; e = e->next)
162 *vp++ = (char *)e->key;