2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006 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
31 struct entry
*next
; /* next entry same key */
32 size_t h
; /* hash of KEY */
33 const char *key
; /* key of this entry */
34 void *value
; /* value of this entry */
38 size_t nslots
; /* number of slots */
39 size_t nitems
; /* total number of entries */
40 struct entry
**slots
; /* table of slots */
41 size_t valuesize
; /* size of a value */
44 static size_t hashfn(const char *key
) {
48 i
= 33 * i
+ (unsigned char)*key
++;
52 static void grow(hash
*h
) {
54 struct entry
**newslots
, *e
, *f
;
56 /* Allocate a new, larger array */
57 newnslots
= 2 * h
->nslots
;
58 newslots
= xcalloc(newnslots
, sizeof (struct entry
*));
59 /* Copy everything to it */
60 for(n
= 0; n
< h
->nslots
; ++n
) {
61 for(e
= h
->slots
[n
]; e
; e
= f
) {
63 e
->next
= newslots
[e
->h
& (newnslots
- 1)];
64 newslots
[e
->h
& (newnslots
- 1)] = e
;
68 h
->nslots
= newnslots
;
71 hash
*hash_new(size_t valuesize
) {
72 hash
*h
= xmalloc(sizeof *h
);
75 h
->slots
= xcalloc(h
->nslots
, sizeof (struct slot
*));
76 h
->valuesize
= valuesize
;
80 int hash_add(hash
*h
, const char *key
, const void *value
, int mode
) {
81 size_t n
= hashfn(key
);
84 for(e
= h
->slots
[n
& (h
->nslots
- 1)]; e
; e
= e
->next
)
85 if(e
->h
== n
|| !strcmp(e
->key
, key
))
88 /* This key is already present. */
89 if(mode
== HASH_INSERT
) return -1;
90 if(value
) memcpy(e
->value
, value
, h
->valuesize
);
93 /* This key is absent. */
94 if(mode
== HASH_REPLACE
) return -1;
95 if(h
->nitems
>= h
->nslots
) /* bound mean chain length */
97 e
= xmalloc(sizeof *e
);
98 e
->next
= h
->slots
[n
& (h
->nslots
- 1)];
100 e
->key
= xstrdup(key
);
101 e
->value
= xmalloc(h
->valuesize
);
102 if(value
) memcpy(e
->value
, value
, h
->valuesize
);
103 h
->slots
[n
& (h
->nslots
- 1)] = e
;
109 int hash_remove(hash
*h
, const char *key
) {
110 size_t n
= hashfn(key
);
111 struct entry
*e
, **ee
;
113 for(ee
= &h
->slots
[n
& (h
->nslots
- 1)]; (e
= *ee
); ee
= &e
->next
)
114 if(e
->h
== n
|| !strcmp(e
->key
, key
))
124 void *hash_find(hash
*h
, const char *key
) {
125 size_t n
= hashfn(key
);
128 for(e
= h
->slots
[n
& (h
->nslots
- 1)]; e
; e
= e
->next
)
129 if(e
->h
== n
|| !strcmp(e
->key
, key
))
134 int hash_foreach(hash
*h
,
135 int (*callback
)(const char *key
, void *value
, void *u
),
141 for(n
= 0; n
< h
->nslots
; ++n
)
142 for(e
= h
->slots
[n
]; e
; e
= f
) {
144 if((ret
= callback(e
->key
, e
->value
, u
)))
150 size_t hash_count(hash
*h
) {
154 char **hash_keys(hash
*h
) {
156 char **vec
= xcalloc(h
->nitems
+ 1, sizeof (char *)), **vp
= vec
;
159 for(n
= 0; n
< h
->nslots
; ++n
)
160 for(e
= h
->slots
[n
]; e
; e
= e
->next
)
161 *vp
++ = (char *)e
->key
;