36 #define ZHASH_FACTOR_CRITICAL 2 39 #define ZHASH_FACTOR_REALLOC 4 46 uint32_t(*
hash)(
const void *a);
49 int(*
equals)(
const void *a,
const void *b);
58 uint32_t(*
hash)(
const void *a),
int(*
equals)(
const void *a,
const void*b),
71 if ((nentries & (nentries - 1)) != 0) {
73 while (nentries < _nentries)
92 uint32_t(*
hash)(
const void *a),
int(*
equals)(
const void *a,
const void *b))
119 uint32_t code = zh->
hash(key);
120 uint32_t entry_idx = code & (zh->
nentries - 1);
124 if (zh->
equals(key, this_key)) {
129 entry_idx = (entry_idx + 1) & (zh->
nentries - 1);
139 memcpy(out_value, tmp, zh->
valuesz);
146 int zhash_put(
zhash_t *zh,
const void *key,
const void *value,
void *oldkey,
void *oldvalue)
148 uint32_t code = zh->
hash(key);
149 uint32_t entry_idx = code & (zh->
nentries - 1);
155 if (zh->
equals(key, this_key)) {
158 memcpy(oldkey, this_key, zh->
keysz);
160 memcpy(oldvalue, this_value, zh->
valuesz);
161 memcpy(this_key, key, zh->
keysz);
162 memcpy(this_value, value, zh->
valuesz);
167 entry_idx = (entry_idx + 1) & (zh->
nentries - 1);
181 for (
int idx = 0; idx < zh->
nentries; idx++) {
186 if (
zhash_put(newhash, this_key, this_value, NULL, NULL))
193 memcpy(&tmp, zh,
sizeof(
zhash_t));
194 memcpy(zh, newhash,
sizeof(
zhash_t));
195 memcpy(newhash, &tmp,
sizeof(
zhash_t));
204 uint32_t code = zh->
hash(key);
205 uint32_t entry_idx = code & (zh->
nentries - 1);
211 if (zh->
equals(key, this_key)) {
213 memcpy(old_key, this_key, zh->
keysz);
215 memcpy(old_value, this_value, zh->
valuesz);
223 entry_idx = (entry_idx + 1) & (zh->
nentries - 1);
227 char *tmp = malloc(
sizeof(
char)*zh->
entrysz);
242 entry_idx = (entry_idx + 1) & (zh->
nentries - 1);
254 for (
int entry_idx = 0; entry_idx < zh->
nentries; entry_idx++) {
258 if (
zhash_put(newhash, this_key, this_value, NULL, NULL))
301 *((
void**) outkey) = this_key;
302 if (outvalue != NULL)
303 *((
void**) outvalue) = this_value;
314 void *outkeyp, *outvaluep;
320 memcpy(outkey, outkeyp, zh->
keysz);
321 if (outvalue != NULL)
322 memcpy(outvalue, outvaluep, zh->
valuesz);
339 char *tmp = malloc(
sizeof(
char)*zh->
entrysz);
349 entry_idx = (entry_idx + 1) & (zh->
nentries - 1);
383 void *p = *(
void**) key;
414 void *p = *(
void**) value;
458 uint32_t a = *((uint32_t*) _a);
467 uint32_t a = *((uint32_t*) _a);
468 uint32_t b = *((uint32_t*) _b);
477 uint64_t a = *((uint64_t*) _a);
478 return (uint32_t) (a ^ (a >> 32));
486 uint64_t a = *((uint64_t*) _a);
487 uint64_t b = *((uint64_t*) _b);
507 uint32_t
hash = ip.
i;
519 const void * ptra = * (
void**)a;
520 const void * ptrb = * (
void**)b;
530 char *a = * (
char**)_a;
531 char *b = * (
char**)_b;
533 return !strcmp(a, b);
540 char *a = * (
char**)_a;
545 hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
556 for (
int entry_idx = 0; entry_idx < zh->
nentries; entry_idx++) {
558 memcpy(&k, &zh->
entries[entry_idx * zh->
entrysz + 1],
sizeof(
char*));
560 printf(
"%d: %d, %s => %s\n", entry_idx, zh->
entries[entry_idx * zh->
entrysz], k, v);
int(* equals)(const void *a, const void *b)
void zhash_map_keys(zhash_t *zh, void(*f)())
uint32_t zhash_uint32_hash(const void *_a)
int zhash_get(const zhash_t *zh, const void *key, void *out_value)
#define ZHASH_FACTOR_REALLOC
uint32_t zhash_str_hash(const void *_a)
uint32_t zhash_uint64_hash(const void *_a)
int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue)
int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue)
int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue)
uint32_t(* hash)(const void *a)
void zhash_map_values(zhash_t *zh, void(*f)())
void zhash_vmap_values(zhash_t *zh, void(*f)())
zhash_t * zhash_create_capacity(size_t keysz, size_t valuesz, uint32_t(*hash)(const void *a), int(*equals)(const void *a, const void *b), int capacity)
int zhash_uint32_equals(const void *_a, const void *_b)
static zarray_t * zarray_create(size_t el_sz)
int zhash_uint64_equals(const void *_a, const void *_b)
#define ZHASH_FACTOR_CRITICAL
void zhash_clear(zhash_t *zh)
void zhash_iterator_init(zhash_t *zh, zhash_iterator_t *zit)
void zhash_iterator_init_const(const zhash_t *zh, zhash_iterator_t *zit)
zhash_t * zhash_copy(const zhash_t *zh)
int zhash_size(const zhash_t *zh)
void zhash_vmap_keys(zhash_t *zh, void(*f)())
int zhash_contains(const zhash_t *zh, const void *key)
void zhash_destroy(zhash_t *zh)
zarray_t * zhash_keys(const zhash_t *zh)
int zhash_remove(zhash_t *zh, const void *key, void *old_key, void *old_value)
zhash_t * zhash_create(size_t keysz, size_t valuesz, uint32_t(*hash)(const void *a), int(*equals)(const void *a, const void *b))
int zhash_ptr_equals(const void *a, const void *b)
void zhash_debug(zhash_t *zh)
zarray_t * zhash_values(const zhash_t *zh)
int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_value)
void zhash_iterator_remove(zhash_iterator_t *zit)
int zhash_str_equals(const void *_a, const void *_b)
static void zarray_add(zarray_t *za, const void *p)
uint32_t zhash_ptr_hash(const void *a)