Lines Matching refs:hash

30  * Routines for manipulating hash tables
39 #include "hash.h"
43 struct hash {
95 hash_t *hash;
97 hash = xmalloc(sizeof (hash_t));
98 hash->h_buckets = xcalloc(sizeof (list_t *) * nbuckets);
99 hash->h_nbuckets = nbuckets;
100 hash->h_hashfn = hashfn ? hashfn : hash_def_hash;
101 hash->h_cmp = cmp ? cmp : hash_def_cmp;
103 return (hash);
107 hash_add(hash_t *hash, void *key)
109 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
111 list_add(&hash->h_buckets[bucket], key);
130 hash_t *hash = arg;
131 return (hash->h_cmp(key1, key2));
135 hash_remove(hash_t *hash, void *key)
137 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
139 (void) list_remove(&hash->h_buckets[bucket], key,
140 hash_remove_cb, hash);
144 hash_match(hash_t *hash, void *key, int (*fun)(void *, void *),
147 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
149 return (list_iter(hash->h_buckets[bucket], fun, private) < 0);
169 hash_find_iter(hash_t *hash, void *key, int (*fun)(void *, void *),
172 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
175 hd.hd_hash = hash;
180 return (list_iter(hash->h_buckets[bucket], hash_find_list_cb,
198 hash_find(hash_t *hash, void *key, void **value)
203 hd.hd_hash = hash;
207 ret = hash_match(hash, key, hash_find_first_cb, &hd);
215 hash_iter(hash_t *hash, int (*fun)(void *, void *), void *private)
221 for (i = 0; i < hash->h_nbuckets; i++) {
222 if (hash->h_buckets[i] != NULL) {
223 if ((cbrc = list_iter(hash->h_buckets[i], fun,
234 hash_count(hash_t *hash)
238 for (num = 0, i = 0; i < hash->h_nbuckets; i++)
239 num += list_count(hash->h_buckets[i]);
245 hash_free(hash_t *hash, void (*datafree)(void *, void *), void *private)
249 if (hash == NULL)
252 for (i = 0; i < hash->h_nbuckets; i++)
253 list_free(hash->h_buckets[i], datafree, private);
254 free(hash->h_buckets);
255 free(hash);
259 hash_stats(hash_t *hash, int verbose)
261 int min = list_count(hash->h_buckets[0]);
271 for (i = 1; i < hash->h_nbuckets; i++) {
272 count = list_count(hash->h_buckets[i]);
287 printf(" Buckets: %d\n", hash->h_nbuckets);
290 printf(" Average: %5.2f\n", (float)tot / (float)hash->h_nbuckets);