Lines Matching defs:hash

30  * Routines for manipulating hash tables
39 #include "hash.h"
43 struct hash {
94 hash_t *hash;
96 hash = xmalloc(sizeof (hash_t));
97 hash->h_buckets = xcalloc(sizeof (list_t *) * nbuckets);
98 hash->h_nbuckets = nbuckets;
99 hash->h_hashfn = hashfn ? hashfn : (int (*)())hash_def_hash;
100 hash->h_cmp = cmp ? cmp : (int (*)())hash_def_cmp;
102 return (hash);
106 hash_add(hash_t *hash, void *key)
108 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
110 list_add(&hash->h_buckets[bucket], key);
127 hash_remove_cb(void *key1, void *key2, hash_t *hash)
129 return (hash->h_cmp(key1, key2));
133 hash_remove(hash_t *hash, void *key)
135 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
137 (void) list_remove(&hash->h_buckets[bucket], key,
138 (int (*)())hash_remove_cb, hash);
142 hash_match(hash_t *hash, void *key, int (*fun)(void *, void *),
145 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
147 return (list_iter(hash->h_buckets[bucket], fun, private) < 0);
166 hash_find_iter(hash_t *hash, void *key, int (*fun)(void *, void *),
169 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
172 hd.hd_hash = hash;
177 return (list_iter(hash->h_buckets[bucket], (int (*)())hash_find_list_cb,
194 hash_find(hash_t *hash, void *key, void **value)
199 hd.hd_hash = hash;
203 ret = hash_match(hash, key, (int (*)())hash_find_first_cb, &hd);
211 hash_iter(hash_t *hash, int (*fun)(void *, void *), void *private)
217 for (i = 0; i < hash->h_nbuckets; i++) {
218 if (hash->h_buckets[i] != NULL) {
219 if ((cbrc = list_iter(hash->h_buckets[i], fun,
230 hash_count(hash_t *hash)
234 for (num = 0, i = 0; i < hash->h_nbuckets; i++)
235 num += list_count(hash->h_buckets[i]);
241 hash_free(hash_t *hash, void (*datafree)(void *, void *), void *private)
245 if (hash == NULL)
248 for (i = 0; i < hash->h_nbuckets; i++)
249 list_free(hash->h_buckets[i], datafree, private);
250 free(hash->h_buckets);
251 free(hash);
255 hash_stats(hash_t *hash, int verbose)
257 int min = list_count(hash->h_buckets[0]);
267 for (i = 1; i < hash->h_nbuckets; i++) {
268 count = list_count(hash->h_buckets[i]);
283 printf(" Buckets: %d\n", hash->h_nbuckets);
286 printf(" Average: %5.2f\n", (float)tot / (float)hash->h_nbuckets);