2008-11-23 02:12:37 -05:00
|
|
|
//-*- mode:c -*-
|
|
|
|
|
|
|
|
/*
|
|
|
|
include this file and call HTIMPL to generate an implementation
|
|
|
|
*/
|
|
|
|
|
2019-08-09 10:23:10 -04:00
|
|
|
#define hash_size(h) ((h)->size / 2)
|
2008-11-23 02:12:37 -05:00
|
|
|
|
|
|
|
// compute empirical max-probe for a given size
|
2019-08-09 10:23:10 -04:00
|
|
|
#define max_probe(size) \
|
|
|
|
((size) <= (HT_N_INLINE * 2) ? (HT_N_INLINE / 2) : (size) >> 3)
|
2008-11-23 02:12:37 -05:00
|
|
|
|
2019-08-09 10:23:10 -04:00
|
|
|
#define HTIMPL(HTNAME, HFUNC, EQFUNC) \
|
2019-08-09 12:26:09 -04:00
|
|
|
static void **HTNAME##_lookup_bp(struct htable *h, void *key) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
2019-08-09 14:04:19 -04:00
|
|
|
uintptr_t hv; \
|
2019-08-09 10:23:10 -04:00
|
|
|
size_t i, orig, index, iter; \
|
|
|
|
size_t newsz, sz = hash_size(h); \
|
|
|
|
size_t maxprobe = max_probe(sz); \
|
|
|
|
void **tab = h->table; \
|
|
|
|
void **ol; \
|
|
|
|
\
|
2019-08-09 14:07:16 -04:00
|
|
|
hv = HFUNC((uintptr_t)key); \
|
2019-08-09 10:23:10 -04:00
|
|
|
retry_bp: \
|
|
|
|
iter = 0; \
|
2019-08-09 14:13:48 -04:00
|
|
|
index = (uintptr_t)(hv & (sz - 1)) * 2; \
|
2019-08-09 10:23:10 -04:00
|
|
|
sz *= 2; \
|
|
|
|
orig = index; \
|
|
|
|
\
|
|
|
|
do { \
|
|
|
|
if (tab[index + 1] == HT_NOTFOUND) { \
|
|
|
|
tab[index] = key; \
|
|
|
|
return &tab[index + 1]; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (EQFUNC(key, tab[index])) \
|
|
|
|
return &tab[index + 1]; \
|
|
|
|
\
|
|
|
|
index = (index + 2) & (sz - 1); \
|
|
|
|
iter++; \
|
|
|
|
if (iter > maxprobe) \
|
|
|
|
break; \
|
|
|
|
} while (index != orig); \
|
|
|
|
\
|
|
|
|
/* table full */ \
|
|
|
|
/* quadruple size, rehash, retry the insert */ \
|
|
|
|
/* it's important to grow the table really fast; otherwise we waste \
|
|
|
|
*/ \
|
|
|
|
/* lots of time rehashing all the keys over and over. */ \
|
|
|
|
sz = h->size; \
|
|
|
|
ol = h->table; \
|
|
|
|
if (sz >= (1 << 19) || (sz <= (1 << 8))) \
|
|
|
|
newsz = sz << 1; \
|
|
|
|
else if (sz <= HT_N_INLINE) \
|
|
|
|
newsz = HT_N_INLINE; \
|
|
|
|
else \
|
|
|
|
newsz = sz << 2; \
|
|
|
|
/*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \
|
|
|
|
tab = (void **)LLT_ALLOC(newsz * sizeof(void *)); \
|
|
|
|
if (tab == NULL) \
|
|
|
|
return NULL; \
|
|
|
|
for (i = 0; i < newsz; i++) \
|
|
|
|
tab[i] = HT_NOTFOUND; \
|
|
|
|
h->table = tab; \
|
|
|
|
h->size = newsz; \
|
|
|
|
for (i = 0; i < sz; i += 2) { \
|
|
|
|
if (ol[i + 1] != HT_NOTFOUND) { \
|
|
|
|
(*HTNAME##_lookup_bp(h, ol[i])) = ol[i + 1]; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (ol != &h->_space[0]) \
|
|
|
|
LLT_FREE(ol); \
|
|
|
|
\
|
|
|
|
sz = hash_size(h); \
|
|
|
|
maxprobe = max_probe(sz); \
|
|
|
|
tab = h->table; \
|
|
|
|
\
|
|
|
|
goto retry_bp; \
|
|
|
|
\
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
\
|
2019-08-09 12:26:09 -04:00
|
|
|
void HTNAME##_put(struct htable *h, void *key, void *val) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
void **bp = HTNAME##_lookup_bp(h, key); \
|
|
|
|
\
|
|
|
|
*bp = val; \
|
|
|
|
} \
|
|
|
|
\
|
2019-08-09 12:26:09 -04:00
|
|
|
void **HTNAME##_bp(struct htable *h, void *key) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
return HTNAME##_lookup_bp(h, key); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* returns bp if key is in hash, otherwise NULL */ \
|
|
|
|
/* if return is non-NULL and *bp == HT_NOTFOUND then key was deleted */ \
|
2019-08-09 12:26:09 -04:00
|
|
|
static void **HTNAME##_peek_bp(struct htable *h, void *key) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
size_t sz = hash_size(h); \
|
|
|
|
size_t maxprobe = max_probe(sz); \
|
|
|
|
void **tab = h->table; \
|
2019-08-09 14:13:48 -04:00
|
|
|
size_t index = (uintptr_t)(HFUNC((uintptr_t)key) & (sz - 1)) * 2; \
|
2019-08-09 10:23:10 -04:00
|
|
|
size_t orig = index; \
|
|
|
|
size_t iter = 0; \
|
|
|
|
\
|
2019-08-18 18:14:09 -04:00
|
|
|
sz *= 2; \
|
2019-08-09 10:23:10 -04:00
|
|
|
do { \
|
|
|
|
if (tab[index] == HT_NOTFOUND) \
|
|
|
|
return NULL; \
|
|
|
|
if (EQFUNC(key, tab[index])) \
|
|
|
|
return &tab[index + 1]; \
|
|
|
|
\
|
|
|
|
index = (index + 2) & (sz - 1); \
|
|
|
|
iter++; \
|
|
|
|
if (iter > maxprobe) \
|
|
|
|
break; \
|
|
|
|
} while (index != orig); \
|
|
|
|
\
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
\
|
2019-08-09 12:26:09 -04:00
|
|
|
void *HTNAME##_get(struct htable *h, void *key) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
void **bp = HTNAME##_peek_bp(h, key); \
|
|
|
|
if (bp == NULL) \
|
|
|
|
return HT_NOTFOUND; \
|
|
|
|
return *bp; \
|
|
|
|
} \
|
|
|
|
\
|
2019-08-09 12:26:09 -04:00
|
|
|
int HTNAME##_has(struct htable *h, void *key) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
return (HTNAME##_get(h, key) != HT_NOTFOUND); \
|
|
|
|
} \
|
|
|
|
\
|
2019-08-09 12:26:09 -04:00
|
|
|
int HTNAME##_remove(struct htable *h, void *key) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
void **bp = HTNAME##_peek_bp(h, key); \
|
|
|
|
if (bp != NULL) { \
|
|
|
|
*bp = HT_NOTFOUND; \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
\
|
2019-08-09 12:26:09 -04:00
|
|
|
void HTNAME##_adjoin(struct htable *h, void *key, void *val) \
|
2019-08-09 10:23:10 -04:00
|
|
|
{ \
|
|
|
|
void **bp = HTNAME##_lookup_bp(h, key); \
|
|
|
|
if (*bp == HT_NOTFOUND) \
|
|
|
|
*bp = val; \
|
|
|
|
}
|