Initializing size of hash table

If I know how many items are going to need to go in my hash table, how could I initialize it to a size that keeps the ratio of number of items to number of total elements at or below 2/3?

Oh yeah, the table size has to be a prime number!
Last edited on
Multiply the maximum expected number of elements by 1.5, and then round the result upwards to the next higher prime number.

Something like:
1
2
3
4
constexpr std::size_t max_size = 800 ;
std::size_t table_size =  max_size + max_size/2 ;
if( table_size%2 == 0 ) ++table_size ;
while( is_not_prime_number(table_size) ) table_size += 2 ;
Topic archived. No new replies allowed.