From c02a55ba28ce7eb8e41479dbe2dab33acba7b3c5 Mon Sep 17 00:00:00 2001 From: yuqing Date: Tue, 13 Dec 2016 15:56:22 +0800 Subject: [PATCH] add function hash_get_prime_capacity --- HISTORY | 3 +++ src/hash.c | 36 ++++++++++++++---------------------- src/hash.h | 2 ++ 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/HISTORY b/HISTORY index d8ea127..622be89 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.33 2016-12-13 + * add function hash_get_prime_capacity + Version 1.32 2016-12-01 * downgrade log level from warning to debug diff --git a/src/hash.c b/src/hash.c index 47a0af9..62596fd 100644 --- a/src/hash.c +++ b/src/hash.c @@ -71,25 +71,30 @@ static int _hash_alloc_buckets(HashArray *pHash, const unsigned int old_capacity return 0; } -int hash_init_ex(HashArray *pHash, HashFunc hash_func, \ - const unsigned int capacity, const double load_factor, \ - const int64_t max_bytes, const bool bMallocValue) +unsigned int *hash_get_prime_capacity(const int capacity) { unsigned int *pprime; unsigned int *prime_end; - int result; - - memset(pHash, 0, sizeof(HashArray)); prime_end = prime_array + PRIME_ARRAY_SIZE; for (pprime = prime_array; pprime!=prime_end; pprime++) { if (*pprime > capacity) { - pHash->capacity = pprime; - break; + return pprime; } } + return NULL; +} + +int hash_init_ex(HashArray *pHash, HashFunc hash_func, \ + const unsigned int capacity, const double load_factor, \ + const int64_t max_bytes, const bool bMallocValue) +{ + int result; + + memset(pHash, 0, sizeof(HashArray)); + pHash->capacity = hash_get_prime_capacity(capacity); if (pHash->capacity == NULL) { return EINVAL; @@ -367,20 +372,7 @@ static int _rehash(HashArray *pHash) pOldCapacity = pHash->capacity; if (pHash->is_malloc_capacity) { - unsigned int *pprime; - unsigned int *prime_end; - - pHash->capacity = NULL; - - prime_end = prime_array + PRIME_ARRAY_SIZE; - for (pprime = prime_array; pprime!=prime_end; pprime++) - { - if (*pprime > *pOldCapacity) - { - pHash->capacity = pprime; - break; - } - } + pHash->capacity = hash_get_prime_capacity(*pOldCapacity); } else { diff --git a/src/hash.h b/src/hash.h index 9be6001..5791fc8 100644 --- a/src/hash.h +++ b/src/hash.h @@ -373,6 +373,8 @@ int CRC32_ex(void *key, const int key_len, \ hash_codes[0] = CRC32_FINAL(hash_codes[0]); \ +unsigned int *hash_get_prime_capacity(const int capacity); + #ifdef __cplusplus } #endif