hash table REFACTOR func for fixed size ht
diff --git a/src/hash_table.c b/src/hash_table.c
index 276ba2d..7b7ac14 100644
--- a/src/hash_table.c
+++ b/src/hash_table.c
@@ -835,3 +835,27 @@
{
return lyht_remove_with_resize_cb(ht, val_p, hash, NULL);
}
+
+uint32_t
+lyht_get_fixed_size(uint32_t item_count)
+{
+ uint32_t i, size = 0;
+
+ /* detect number of upper zero bits in the items' counter value ... */
+ for (i = (sizeof item_count * CHAR_BIT) - 1; i > 0; i--) {
+ size = item_count << i;
+ size = size >> i;
+ if (size == item_count) {
+ break;
+ }
+ }
+ assert(i);
+
+ /* ... and then we convert it to the position of the highest non-zero bit ... */
+ i = (sizeof item_count * CHAR_BIT) - i;
+
+ /* ... and by using it to shift 1 to the left we get the closest sufficient hash table size */
+ size = 1 << i;
+
+ return size;
+}