diff --git a/hashset/src/main.c b/hashset/src/main.c index 0d3d1b7..bdfc635 100644 --- a/hashset/src/main.c +++ b/hashset/src/main.c @@ -276,43 +276,46 @@ static int test_function_3(hash_set_t *const hash_set) random_t random; random_init(&random); - for (;;) + for (size_t r = 0U; r < 3U; ++r) { - const uint64_t rnd = random_next(&random) & UINT64_C(0x3FFFFFFFFFFFFFF); - const errno_t error = hash_set_insert(hash_set, rnd); - if (error) + for (;;) { - if (error != EEXIST) + const uint64_t rnd = random_next(&random) & UINT64_C(0x3FFFFFFFFFFFFFF); + const errno_t error = hash_set_insert(hash_set, rnd); + if (error) { - printf("Insert operation has failed! (error: %d)\n", error); - return EXIT_FAILURE; + if (error != EEXIST) + { + printf("Insert operation has failed! (error: %d)\n", error); + return EXIT_FAILURE; + } + else + { + PRINT_SET_INFO(3); + printf("Collision detected! [%016llX]\n", rnd); + break; + } } - else + if (!(++spinner & 0x7F)) { - PRINT_SET_INFO(3); - printf("Collision detected! [%016llX]\n", rnd); - break; + const clock_t clock_now = clock(); + if ((clock_now < last_update) || (clock_now >= last_update + CLOCKS_PER_SEC)) + { + PRINT_SET_INFO(3); + last_update = clock_now; + } } } - if (!(++spinner & 0x7F)) + + PRINT_SET_INFO(3); + + if (hash_set_clear(hash_set)) { - const clock_t clock_now = clock(); - if ((clock_now < last_update) || (clock_now >= last_update + CLOCKS_PER_SEC)) - { - PRINT_SET_INFO(3); - last_update = clock_now; - } + puts("Clear operation has failed!"); + return EXIT_FAILURE; } } - PRINT_SET_INFO(3); - - if (hash_set_clear(hash_set)) - { - puts("Clear operation has failed!"); - return EXIT_FAILURE; - } - PRINT_SET_INFO(3); puts("---------"); @@ -323,13 +326,15 @@ static int test_function_3(hash_set_t *const hash_set) /* TEST #4 */ /* ========================================================================= */ +#define LIMIT (UINT64_MAX >> 2) + static int test_function_4(hash_set_t *const hash_set) { size_t capacity, valid, deleted, limit; uint8_t spinner = 0U; clock_t last_update = clock(); - for (uint64_t value = 0U; value < ((uint64_t)INT32_MAX); ++value) + for (uint64_t value = 0U; value < LIMIT; ++value) { const errno_t error = hash_set_insert(hash_set, value); if (error) @@ -349,7 +354,7 @@ static int test_function_4(hash_set_t *const hash_set) } } - for (uint64_t value = 0U; value < ((uint64_t)INT32_MAX); ++value) + for (uint64_t value = 0U; value < LIMIT; ++value) { const errno_t error = hash_set_remove(hash_set, value); if (error) diff --git a/libhashset/src/hash_set.c b/libhashset/src/hash_set.c index f184d8a..d85b2a5 100644 --- a/libhashset/src/hash_set.c +++ b/libhashset/src/hash_set.c @@ -68,7 +68,7 @@ static FORCE_INLINE size_t div_ceil(const size_t value, const size_t divisor) static FORCE_INLINE size_t round_sz(const double d) { - + return ((!isnan(d)) && (d >= 0.0)) ? ((d + 0.5 >= ((double)SIZE_MAX)) ? SIZE_MAX : ((size_t)(d + 0.5))) : 0U; } @@ -136,7 +136,7 @@ static INLINE uint64_t hash_compute(const uint64_t i, const uint64_t value) /* Allocation */ /* ------------------------------------------------- */ -static INLINE void zero_memory(void* const addr, const size_t count, const size_t size) +static INLINE void zero_memory(void *const addr, const size_t count, const size_t size) { memset(addr, 0, safe_mult(count, size)); } @@ -170,7 +170,7 @@ static INLINE bool_t alloc_data(struct _hash_set_data *const data, const size_t return TRUE; } -static INLINE void free_data(struct _hash_set_data* const data) +static INLINE void free_data(struct _hash_set_data *const data) { if (data) { @@ -195,7 +195,7 @@ static INLINE void set_flag(uint8_t *const flags, const size_t index) flags[index / 8U] |= UINT8_C(1) << (index % 8U); } -static INLINE void clear_flag(uint8_t* const flags, const size_t index) +static INLINE void clear_flag(uint8_t *const flags, const size_t index) { flags[index / 8U] &= ~(UINT8_C(1) << (index % 8U)); } @@ -261,10 +261,12 @@ static INLINE void put_value(struct _hash_set_data *const data, const size_t ind static size_t INLINE compute_limit(const size_t capacity, const double load_factor) { size_t limit = round_sz(capacity * load_factor); + while (capacity && (limit >= capacity)) { limit = safe_decr(limit); } + return limit; }