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),
62 assert(equals != NULL);
70 int nentries = _nentries;
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);