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
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 static size_t hashfn(const char *key
) {
46 i
= 33 * i
+ (unsigned char)*key
++;
50 static void grow(hash
*h
) {
52 struct entry
**newslots
, *e
, *f
;
54 /* Allocate a new, larger array */
55 newnslots
= 2 * h
->nslots
;
56 newslots
= xcalloc(newnslots
, sizeof (struct entry
*));
57 /* Copy everything to it */
58 for(n
= 0; n
< h
->nslots
; ++n
) {
59 for(e
= h
->slots
[n
]; e
; e
= f
) {
61 e
->next
= newslots
[e
->h
& (newnslots
- 1)];
62 newslots
[e
->h
& (newnslots
- 1)] = e
;
66 h
->nslots
= newnslots
;
69 hash
*hash_new(size_t valuesize
) {
70 hash
*h
= xmalloc(sizeof *h
);
73 h
->slots
= xcalloc(h
->nslots
, sizeof (struct slot
*));
74 h
->valuesize
= valuesize
;
78 int hash_add(hash
*h
, const char *key
, const void *value
, int mode
) {
79 size_t n
= hashfn(key
);
82 for(e
= h
->slots
[n
& (h
->nslots
- 1)]; e
; e
= e
->next
)
83 if(e
->h
== n
|| !strcmp(e
->key
, key
))
86 /* This key is already present. */
87 if(mode
== HASH_INSERT
) return -1;
88 if(value
) memcpy(e
->value
, value
, h
->valuesize
);
91 /* This key is absent. */
92 if(mode
== HASH_REPLACE
) return -1;
93 if(h
->nitems
>= h
->nslots
) /* bound mean chain length */
95 e
= xmalloc(sizeof *e
);
96 e
->next
= h
->slots
[n
& (h
->nslots
- 1)];
98 e
->key
= xstrdup(key
);
99 e
->value
= xmalloc(h
->valuesize
);
100 if(value
) memcpy(e
->value
, value
, h
->valuesize
);
101 h
->slots
[n
& (h
->nslots
- 1)] = e
;
107 int hash_remove(hash
*h
, const char *key
) {
108 size_t n
= hashfn(key
);
109 struct entry
*e
, **ee
;
111 for(ee
= &h
->slots
[n
& (h
->nslots
- 1)]; (e
= *ee
); ee
= &e
->next
)
112 if(e
->h
== n
|| !strcmp(e
->key
, key
))
122 void *hash_find(hash
*h
, const char *key
) {
123 size_t n
= hashfn(key
);
126 for(e
= h
->slots
[n
& (h
->nslots
- 1)]; e
; e
= e
->next
)
127 if(e
->h
== n
|| !strcmp(e
->key
, key
))
132 int hash_foreach(hash
*h
,
133 int (*callback
)(const char *key
, void *value
, void *u
),
139 for(n
= 0; n
< h
->nslots
; ++n
)
140 for(e
= h
->slots
[n
]; e
; e
= f
) {
142 if((ret
= callback(e
->key
, e
->value
, u
)))
148 size_t hash_count(hash
*h
) {
152 char **hash_keys(hash
*h
) {
154 char **vec
= xcalloc(h
->nitems
+ 1, sizeof (char *)), **vp
= vec
;
157 for(n
= 0; n
< h
->nslots
; ++n
)
158 for(e
= h
->slots
[n
]; e
; e
= e
->next
)
159 *vp
++ = (char *)e
->key
;