Skip to content

Commit a91e0bb

Browse files
committed
Merge branch 'rs/khash-alloc-cleanup'
Code clean-up. * rs/khash-alloc-cleanup: khash: clarify that allocations never fail
2 parents 8eb90d3 + 5632e83 commit a91e0bb

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

khash.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static const double __ac_HASH_UPPER = 0.77;
7474
void kh_destroy_##name(kh_##name##_t *h); \
7575
void kh_clear_##name(kh_##name##_t *h); \
7676
khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
77-
int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
77+
void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
7878
khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
7979
void kh_del_##name(kh_##name##_t *h, khint_t x);
8080

@@ -116,7 +116,7 @@ static const double __ac_HASH_UPPER = 0.77;
116116
return __ac_iseither(h->flags, i)? h->n_buckets : i; \
117117
} else return 0; \
118118
} \
119-
SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
119+
SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
120120
{ /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
121121
khint32_t *new_flags = NULL; \
122122
khint_t j = 1; \
@@ -126,7 +126,6 @@ static const double __ac_HASH_UPPER = 0.77;
126126
if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
127127
else { /* hash table size to be changed (shrink or expand); rehash */ \
128128
ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
129-
if (!new_flags) return -1; \
130129
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
131130
if (h->n_buckets < new_n_buckets) { /* expand */ \
132131
REALLOC_ARRAY(h->keys, new_n_buckets); \
@@ -173,18 +172,15 @@ static const double __ac_HASH_UPPER = 0.77;
173172
h->n_occupied = h->size; \
174173
h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
175174
} \
176-
return 0; \
177175
} \
178176
SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
179177
{ \
180178
khint_t x; \
181179
if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
182180
if (h->n_buckets > (h->size<<1)) { \
183-
if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
184-
*ret = -1; return h->n_buckets; \
185-
} \
186-
} else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
187-
*ret = -1; return h->n_buckets; \
181+
kh_resize_##name(h, h->n_buckets - 1); /* clear "deleted" elements */ \
182+
} else { \
183+
kh_resize_##name(h, h->n_buckets + 1); /* expand the hash table */ \
188184
} \
189185
} /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
190186
{ \

0 commit comments

Comments
 (0)