Skip to content

Commit

Permalink
htable: fix implementation of htable_reset
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Feb 13, 2019
1 parent 38d6247 commit 3bb8404
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/support/htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern "C" {

htable_t *htable_new(htable_t *h, size_t size)
{
if (size <= HT_N_INLINE/2) {
if (size <= HT_N_INLINE / 2) {
h->size = size = HT_N_INLINE;
h->table = &h->_space[0];
}
Expand All @@ -29,11 +29,12 @@ htable_t *htable_new(htable_t *h, size_t size)
size *= 2; // 2 pointers per key/value pair
size *= 2; // aim for 50% occupancy
h->size = size;
h->table = (void**)LLT_ALLOC(size*sizeof(void*));
h->table = (void**)LLT_ALLOC(size * sizeof(void*));
}
if (h->table == NULL) return NULL;
if (h->table == NULL)
return NULL;
size_t i;
for(i=0; i < size; i++)
for (i = 0; i < size; i++)
h->table[i] = HT_NOTFOUND;
return h;
}
Expand All @@ -48,15 +49,17 @@ void htable_free(htable_t *h)
void htable_reset(htable_t *h, size_t sz)
{
sz = next_power_of_two(sz);
if (h->size > sz*4 && h->size > HT_N_INLINE) {
size_t newsz = sz*4;
void **newtab = (void**)LLT_REALLOC(h->table, newsz*sizeof(void*));
h->size = newsz;
h->table = newtab;
if (h->size > sz * 4 && h->size > HT_N_INLINE) {
LLT_FREE(h->table);
h->table = NULL;
if (htable_new(h, sz) == NULL)
htable_new(h, 0);
}
else {
size_t i, hsz = h->size;
for (i = 0; i < hsz; i++)
h->table[i] = HT_NOTFOUND;
}
size_t i, hsz=h->size;
for(i=0; i < hsz; i++)
h->table[i] = HT_NOTFOUND;
}

#ifdef __cplusplus
Expand Down

0 comments on commit 3bb8404

Please sign in to comment.