add function hash_get_prime_capacity
parent
ec6f0b8711
commit
c02a55ba28
3
HISTORY
3
HISTORY
|
|
@ -1,4 +1,7 @@
|
||||||
|
|
||||||
|
Version 1.33 2016-12-13
|
||||||
|
* add function hash_get_prime_capacity
|
||||||
|
|
||||||
Version 1.32 2016-12-01
|
Version 1.32 2016-12-01
|
||||||
* downgrade log level from warning to debug
|
* downgrade log level from warning to debug
|
||||||
|
|
||||||
|
|
|
||||||
36
src/hash.c
36
src/hash.c
|
|
@ -71,25 +71,30 @@ static int _hash_alloc_buckets(HashArray *pHash, const unsigned int old_capacity
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hash_init_ex(HashArray *pHash, HashFunc hash_func, \
|
unsigned int *hash_get_prime_capacity(const int capacity)
|
||||||
const unsigned int capacity, const double load_factor, \
|
|
||||||
const int64_t max_bytes, const bool bMallocValue)
|
|
||||||
{
|
{
|
||||||
unsigned int *pprime;
|
unsigned int *pprime;
|
||||||
unsigned int *prime_end;
|
unsigned int *prime_end;
|
||||||
int result;
|
|
||||||
|
|
||||||
memset(pHash, 0, sizeof(HashArray));
|
|
||||||
prime_end = prime_array + PRIME_ARRAY_SIZE;
|
prime_end = prime_array + PRIME_ARRAY_SIZE;
|
||||||
for (pprime = prime_array; pprime!=prime_end; pprime++)
|
for (pprime = prime_array; pprime!=prime_end; pprime++)
|
||||||
{
|
{
|
||||||
if (*pprime > capacity)
|
if (*pprime > capacity)
|
||||||
{
|
{
|
||||||
pHash->capacity = pprime;
|
return pprime;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
if (pHash->capacity == NULL)
|
||||||
{
|
{
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
@ -367,20 +372,7 @@ static int _rehash(HashArray *pHash)
|
||||||
pOldCapacity = pHash->capacity;
|
pOldCapacity = pHash->capacity;
|
||||||
if (pHash->is_malloc_capacity)
|
if (pHash->is_malloc_capacity)
|
||||||
{
|
{
|
||||||
unsigned int *pprime;
|
pHash->capacity = hash_get_prime_capacity(*pOldCapacity);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -373,6 +373,8 @@ int CRC32_ex(void *key, const int key_len, \
|
||||||
hash_codes[0] = CRC32_FINAL(hash_codes[0]); \
|
hash_codes[0] = CRC32_FINAL(hash_codes[0]); \
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int *hash_get_prime_capacity(const int capacity);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue