Skip to content

Commit 588ae14

Browse files
author
Julian LALU
committed
Improve hashmap
1 parent e5e95d5 commit 588ae14

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

interface/core/containers/hashset.h

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

test/hashmap/hashmap_remove.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ namespace hud_test
88
using super = hud_test::non_bitwise_type;
99
using super::super;
1010

11+
#if defined(HD_COMPILER_GCC)
1112
virtual constexpr ~collided_key() noexcept
1213
{
1314
super::~super();
1415
}
16+
#endif
1517
};
1618
} // namespace hud_test
1719

test/hashmap/hashmap_reserve.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ GTEST_TEST(hashmap, reserve_empty_to_size_allocate_only)
5555
reserve_ok &= map.slack() >= 0;
5656
reserve_ok &= (map.allocator().allocation_count() == (hud::is_constant_evaluated() ? 2 : 1));
5757
reserve_ok &= map.allocator().free_count() == 0;
58-
for (const auto &[key, value] : map)
59-
{
60-
reserve_ok = false;
61-
}
6258
return reserve_ok;
6359
};
6460

0 commit comments

Comments
 (0)