@@ -917,7 +917,7 @@ namespace hud
917917 {
918918 if (count > max_slot_count_)
919919 {
920- grow_capacity (compute_max_count (count));
920+ resize (compute_max_count (count));
921921 }
922922 }
923923
@@ -1021,6 +1021,29 @@ namespace hud
10211021 }
10221022 }
10231023
1024+ constexpr void rehash (i32 count) const noexcept
1025+ {
1026+ // If we request 0
1027+ // and :
1028+ // - we have no allocation done, just return
1029+ // - we have allocation but no element, free allocation and reset internal state
1030+ if (count == 0 )
1031+ {
1032+ if (max_slot_count_ == 0 )
1033+ return ;
1034+ if (is_empty ())
1035+ reset_control_and_slot ();
1036+ }
1037+
1038+ // We request 0 or more and we have allocation and elements
1039+ // bitor is a faster way of doing `max` here. We will round up to the next
1040+ // power-of-2-minus-1, so bitor is good enough.
1041+ usize max_count = compute_max_count (count | min_capacity_for_count (count));
1042+ if (count == 0 || max_count > max_slot_count_)
1043+ resize (max_count);
1044+ return ;
1045+ }
1046+
10241047 [[nodiscard]]
10251048 constexpr const_iterator find (const key_type &key) const noexcept
10261049 {
@@ -1463,9 +1486,10 @@ namespace hud
14631486 [[nodiscard]] constexpr usize insert_no_construct (u64 h1, u8 h2) noexcept
14641487 {
14651488 // If we reach the load factor grow the table and retrieves the new slot, else use the given slot
1489+ // TODO : check rehash_and_grow_if_necessary and implement the SQuash DELETED branch
14661490 if (free_slot_before_grow () == 0 )
14671491 {
1468- grow_capacity (next_capacity ());
1492+ resize (next_capacity ());
14691493 }
14701494
14711495 // Find the first empty of deleted slot that can be used for this h1 hash
@@ -1478,9 +1502,9 @@ namespace hud
14781502 return slot_index;
14791503 }
14801504
1481- constexpr void grow_capacity (usize new_max_slot_count) noexcept
1505+ constexpr void resize (usize new_max_slot_count) noexcept
14821506 {
1483- hud::check (new_max_slot_count > max_slot_count_ && " Grow need a bigger value" );
1507+ // hud::check(new_max_slot_count > max_slot_count_ && "Grow need a bigger value");
14841508 hud::check (hud::bits::is_valid_power_of_two_mask (max_slot_count_) && " Not a mask" );
14851509
14861510 // Create the buffer with control and slots
@@ -1622,6 +1646,18 @@ namespace hud
16221646 capacity - capacity / 8 ;
16231647 }
16241648
1649+ /* * Compute the minimum capacity needed for the given `count` element that respect the load factor */
1650+ [[nodiscard]] constexpr usize min_capacity_for_count (usize count) noexcept
1651+ {
1652+ // `count*8/7`
1653+ if (group_type::SLOT_PER_GROUP == 8 && count == 7 )
1654+ {
1655+ // x+(x-1)/7 does not work when x==7.
1656+ return 8 ;
1657+ }
1658+ return count + static_cast <usize>((static_cast <i64 >(count) - 1 ) / 7 );
1659+ }
1660+
16251661 [[nodiscard]]
16261662 constexpr control_type *control_ptr_sentinel () const noexcept
16271663 {
0 commit comments