Run renamed .inc files by clang-format for the first time

This commit is contained in:
Lassi Kortela 2019-08-09 17:23:10 +03:00
parent 4128fbc535
commit 09c6368668
2 changed files with 148 additions and 146 deletions

View File

@ -4,145 +4,147 @@
include this file and call HTIMPL to generate an implementation include this file and call HTIMPL to generate an implementation
*/ */
#define hash_size(h) ((h)->size/2) #define hash_size(h) ((h)->size / 2)
// compute empirical max-probe for a given size // compute empirical max-probe for a given size
#define max_probe(size) ((size)<=(HT_N_INLINE*2) ? (HT_N_INLINE/2) : (size)>>3) #define max_probe(size) \
((size) <= (HT_N_INLINE * 2) ? (HT_N_INLINE / 2) : (size) >> 3)
#define HTIMPL(HTNAME, HFUNC, EQFUNC) \ #define HTIMPL(HTNAME, HFUNC, EQFUNC) \
static void **HTNAME##_lookup_bp(htable_t *h, void *key) \ static void **HTNAME##_lookup_bp(htable_t *h, void *key) \
{ \ { \
uint_t hv; \ uint_t hv; \
size_t i, orig, index, iter; \ size_t i, orig, index, iter; \
size_t newsz, sz = hash_size(h); \ size_t newsz, sz = hash_size(h); \
size_t maxprobe = max_probe(sz); \ size_t maxprobe = max_probe(sz); \
void **tab = h->table; \ void **tab = h->table; \
void **ol; \ void **ol; \
\ \
hv = HFUNC((uptrint_t)key); \ hv = HFUNC((uptrint_t)key); \
retry_bp: \ retry_bp: \
iter = 0; \ iter = 0; \
index = (index_t)(hv & (sz-1)) * 2; \ index = (index_t)(hv & (sz - 1)) * 2; \
sz *= 2; \ sz *= 2; \
orig = index; \ orig = index; \
\ \
do { \ do { \
if (tab[index+1] == HT_NOTFOUND) { \ if (tab[index + 1] == HT_NOTFOUND) { \
tab[index] = key; \ tab[index] = key; \
return &tab[index+1]; \ return &tab[index + 1]; \
} \ } \
\ \
if (EQFUNC(key, tab[index])) \ if (EQFUNC(key, tab[index])) \
return &tab[index+1]; \ return &tab[index + 1]; \
\ \
index = (index+2) & (sz-1); \ index = (index + 2) & (sz - 1); \
iter++; \ iter++; \
if (iter > maxprobe) \ if (iter > maxprobe) \
break; \ break; \
} while (index != orig); \ } while (index != orig); \
\ \
/* table full */ \ /* table full */ \
/* quadruple size, rehash, retry the insert */ \ /* quadruple size, rehash, retry the insert */ \
/* it's important to grow the table really fast; otherwise we waste */ \ /* 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; \ /* lots of time rehashing all the keys over and over. */ \
ol = h->table; \ sz = h->size; \
if (sz >= (1<<19) || (sz <= (1<<8))) \ ol = h->table; \
newsz = sz<<1; \ if (sz >= (1 << 19) || (sz <= (1 << 8))) \
else if (sz <= HT_N_INLINE) \ newsz = sz << 1; \
newsz = HT_N_INLINE; \ else if (sz <= HT_N_INLINE) \
else \ newsz = HT_N_INLINE; \
newsz = sz<<2; \ else \
/*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \ newsz = sz << 2; \
tab = (void**)LLT_ALLOC(newsz*sizeof(void*)); \ /*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \
if (tab == NULL) \ tab = (void **)LLT_ALLOC(newsz * sizeof(void *)); \
return NULL; \ if (tab == NULL) \
for(i=0; i < newsz; i++) \ return NULL; \
tab[i] = HT_NOTFOUND; \ for (i = 0; i < newsz; i++) \
h->table = tab; \ tab[i] = HT_NOTFOUND; \
h->size = newsz; \ h->table = tab; \
for(i=0; i < sz; i+=2) { \ h->size = newsz; \
if (ol[i+1] != HT_NOTFOUND) { \ for (i = 0; i < sz; i += 2) { \
(*HTNAME##_lookup_bp(h, ol[i])) = ol[i+1]; \ if (ol[i + 1] != HT_NOTFOUND) { \
} \ (*HTNAME##_lookup_bp(h, ol[i])) = ol[i + 1]; \
} \ } \
if (ol != &h->_space[0]) \ } \
LLT_FREE(ol); \ if (ol != &h->_space[0]) \
\ LLT_FREE(ol); \
sz = hash_size(h); \ \
maxprobe = max_probe(sz); \ sz = hash_size(h); \
tab = h->table; \ maxprobe = max_probe(sz); \
\ tab = h->table; \
goto retry_bp; \ \
\ goto retry_bp; \
return NULL; \ \
} \ return NULL; \
\ } \
void HTNAME##_put(htable_t *h, void *key, void *val) \ \
{ \ void HTNAME##_put(htable_t *h, void *key, void *val) \
void **bp = HTNAME##_lookup_bp(h, key); \ { \
\ void **bp = HTNAME##_lookup_bp(h, key); \
*bp = val; \ \
} \ *bp = val; \
\ } \
void **HTNAME##_bp(htable_t *h, void *key) \ \
{ \ void **HTNAME##_bp(htable_t *h, void *key) \
return HTNAME##_lookup_bp(h, key); \ { \
} \ 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 */ \ /* returns bp if key is in hash, otherwise NULL */ \
static void **HTNAME##_peek_bp(htable_t *h, void *key) \ /* if return is non-NULL and *bp == HT_NOTFOUND then key was deleted */ \
{ \ static void **HTNAME##_peek_bp(htable_t *h, void *key) \
size_t sz = hash_size(h); \ { \
size_t maxprobe = max_probe(sz); \ size_t sz = hash_size(h); \
void **tab = h->table; \ size_t maxprobe = max_probe(sz); \
size_t index = (index_t)(HFUNC((uptrint_t)key) & (sz-1)) * 2; \ void **tab = h->table; \
sz *= 2; \ size_t index = (index_t)(HFUNC((uptrint_t)key) & (sz - 1)) * 2; \
size_t orig = index; \ sz *= 2; \
size_t iter = 0; \ size_t orig = index; \
\ size_t iter = 0; \
do { \ \
if (tab[index] == HT_NOTFOUND) \ do { \
return NULL; \ if (tab[index] == HT_NOTFOUND) \
if (EQFUNC(key, tab[index])) \ return NULL; \
return &tab[index+1]; \ if (EQFUNC(key, tab[index])) \
\ return &tab[index + 1]; \
index = (index+2) & (sz-1); \ \
iter++; \ index = (index + 2) & (sz - 1); \
if (iter > maxprobe) \ iter++; \
break; \ if (iter > maxprobe) \
} while (index != orig); \ break; \
\ } while (index != orig); \
return NULL; \ \
} \ return NULL; \
\ } \
void *HTNAME##_get(htable_t *h, void *key) \ \
{ \ void *HTNAME##_get(htable_t *h, void *key) \
void **bp = HTNAME##_peek_bp(h, key); \ { \
if (bp == NULL) \ void **bp = HTNAME##_peek_bp(h, key); \
return HT_NOTFOUND; \ if (bp == NULL) \
return *bp; \ return HT_NOTFOUND; \
} \ return *bp; \
\ } \
int HTNAME##_has(htable_t *h, void *key) \ \
{ \ int HTNAME##_has(htable_t *h, void *key) \
return (HTNAME##_get(h,key) != HT_NOTFOUND); \ { \
} \ return (HTNAME##_get(h, key) != HT_NOTFOUND); \
\ } \
int HTNAME##_remove(htable_t *h, void *key) \ \
{ \ int HTNAME##_remove(htable_t *h, void *key) \
void **bp = HTNAME##_peek_bp(h, key); \ { \
if (bp != NULL) { \ void **bp = HTNAME##_peek_bp(h, key); \
*bp = HT_NOTFOUND; \ if (bp != NULL) { \
return 1; \ *bp = HT_NOTFOUND; \
} \ return 1; \
return 0; \ } \
} \ return 0; \
\ } \
void HTNAME##_adjoin(htable_t *h, void *key, void *val) \ \
{ \ void HTNAME##_adjoin(htable_t *h, void *key, void *val) \
void **bp = HTNAME##_lookup_bp(h, key); \ { \
if (*bp == HT_NOTFOUND) \ void **bp = HTNAME##_lookup_bp(h, key); \
*bp = val; \ if (*bp == HT_NOTFOUND) \
} *bp = val; \
}

View File

@ -2,13 +2,13 @@
#include "htable.h" #include "htable.h"
#define HTPROT(HTNAME) \ #define HTPROT(HTNAME) \
void *HTNAME##_get(htable_t *h, void *key); \ void *HTNAME##_get(htable_t *h, void *key); \
void HTNAME##_put(htable_t *h, void *key, void *val); \ void HTNAME##_put(htable_t *h, void *key, void *val); \
void HTNAME##_adjoin(htable_t *h, void *key, void *val); \ void HTNAME##_adjoin(htable_t *h, void *key, void *val); \
int HTNAME##_has(htable_t *h, void *key); \ int HTNAME##_has(htable_t *h, void *key); \
int HTNAME##_remove(htable_t *h, void *key); \ int HTNAME##_remove(htable_t *h, void *key); \
void **HTNAME##_bp(htable_t *h, void *key); void **HTNAME##_bp(htable_t *h, void *key);
// return value, or HT_NOTFOUND if key not found // return value, or HT_NOTFOUND if key not found