Skip to content

Commit b6207c8

Browse files
author
Julian LALU
committed
Improve hashmap
1 parent f7a0ed0 commit b6207c8

2 files changed

Lines changed: 438 additions & 6 deletions

File tree

interface/core/containers/hashset.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ namespace hud
10211021
}
10221022
}
10231023

1024-
constexpr void rehash(i32 count) const noexcept
1024+
constexpr void rehash(i32 count) noexcept
10251025
{
10261026
// If we request 0
10271027
// and :
@@ -1032,16 +1032,18 @@ namespace hud
10321032
if (max_slot_count_ == 0)
10331033
return;
10341034
if (is_empty())
1035+
{
10351036
reset_control_and_slot();
1037+
return;
1038+
}
10361039
}
10371040

10381041
// We request 0 or more and we have allocation and elements
10391042
// bitor is a faster way of doing `max` here. We will round up to the next
10401043
// power-of-2-minus-1, so bitor is good enough.
1041-
usize max_count = compute_max_count(count | min_capacity_for_count(count));
1044+
usize max_count = compute_max_count(count | min_capacity_for_count(count_));
10421045
if (count == 0 || max_count > max_slot_count_)
10431046
resize(max_count);
1044-
return;
10451047
}
10461048

10471049
[[nodiscard]]
@@ -1086,7 +1088,7 @@ namespace hud
10861088
/** Checks whether the array is empty of not. */
10871089
[[nodiscard]] HD_FORCEINLINE constexpr bool is_empty() const noexcept
10881090
{
1089-
return count_ != 0;
1091+
return count_ == 0;
10901092
}
10911093

10921094
/** Retreives number of elements in the array. */
@@ -1631,7 +1633,7 @@ namespace hud
16311633
* Compute the maximum number of slots we should put into the table before a resizing rehash.
16321634
* Subtract the returned value with the number of slots `count()` to obtains the number of slots we can currently use before a resizing rehash.
16331635
*/
1634-
[[nodiscard]] constexpr usize max_slot_before_grow(usize capacity) noexcept
1636+
[[nodiscard]] constexpr usize max_slot_before_grow(usize capacity) const noexcept
16351637
{
16361638
// Current load factor is 7/8, this means we can resize when 7/8 slots are occupied
16371639
// A special case appear when group are 8 bytes width and `capacity` is 7 : 7−7/8=7, in this case, we return 6
@@ -1647,7 +1649,7 @@ namespace hud
16471649
}
16481650

16491651
/** 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
1652+
[[nodiscard]] constexpr usize min_capacity_for_count(usize count) const noexcept
16511653
{
16521654
// `count*8/7`
16531655
if (group_type::SLOT_PER_GROUP == 8 && count == 7)

0 commit comments

Comments
 (0)