Skip to content

Commit d2885ba

Browse files
Copilotjll63
andcommitted
Fix table size calculation and documentation for 10% waste
Co-authored-by: jll63 <[email protected]>
1 parent 7e44f68 commit d2885ba

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

include/boost/openmethod/policies/minimal_perfect_hash.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace policies {
4949
//! determine values for `M` and `N` that result in a minimal perfect hash
5050
//! function for the set of registered type_ids. This means that the hash
5151
//! function is collision-free and the codomain is approximately the size of
52-
//! the domain, resulting in a dense range [0, n-1] for n inputs.
52+
//! the domain, resulting in a dense range [0, 1.1*n-1] for n inputs.
5353
//!
5454
//! Unlike @ref fast_perfect_hash, which uses a hash table of size 2^k
5555
//! (typically larger than needed) and may have unused slots, this policy
@@ -194,10 +194,8 @@ void minimal_perfect_hash::fn<Registry>::initialize(
194194
}
195195

196196
// Table size is N * 1.1 to allow up to 10% waste (makes finding hash easier)
197-
table_size = N + N / 10;
198-
if (table_size == N && N > 0) {
199-
table_size = N + 1; // Ensure at least 1 extra slot for N > 0
200-
}
197+
// Use (N * 11 + 9) / 10 to ensure proper rounding up for small N
198+
table_size = (N * 11 + 9) / 10;
201199

202200
if (table_size == 0) {
203201
shift = 0;

0 commit comments

Comments
 (0)