Skip to content

Commit edf418c

Browse files
author
Julian LALU
committed
Improve Hashset
1 parent 5874c8a commit edf418c

File tree

1 file changed

+18
-57
lines changed

1 file changed

+18
-57
lines changed

interface/core/containers/hashset.h

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -973,14 +973,6 @@ namespace hud
973973
constexpr iterator find(K &&key) noexcept
974974
{
975975
return find_impl(forward_key(hud::forward<K>(key)));
976-
// if constexpr (is_hashable_and_comparable_v<K>)
977-
// {
978-
// return find_impl(hud::forward<K>(key));
979-
// }
980-
// else
981-
// {
982-
// return find_impl(key_type(hud::forward<K>(key)));
983-
// }
984976
}
985977

986978
constexpr void rehash(i32 count) noexcept
@@ -1039,14 +1031,6 @@ namespace hud
10391031
iterator try_emplace(K &&key, Args &&...args) noexcept
10401032
{
10411033
return try_emplace_impl(forward_key(hud::forward<K>(key)));
1042-
// if constexpr (is_hashable_and_comparable_v<K>)
1043-
// {
1044-
// return try_emplace_impl(hud::forward<K>(key));
1045-
// }
1046-
// else
1047-
// {
1048-
// return try_emplace_impl(key_type(hud::forward<K>(key)));
1049-
// }
10501034
}
10511035

10521036
constexpr void swap(hashset_impl &other) noexcept
@@ -1145,13 +1129,12 @@ namespace hud
11451129
requires(hud::is_constructible_v<slot_type, key_type, args_t...>)
11461130
constexpr iterator add(key_type &&key, args_t &&...args) noexcept
11471131
{
1148-
hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1149-
slot_type *slot_ptr {slot_ptr_ + res.first};
1132+
hud::pair<iterator, bool> res {find_or_insert_no_construct(key)};
11501133
if (res.second)
11511134
{
1152-
hud::memory::construct_object_at(slot_ptr, hud::move(key), hud::forward<args_t>(args)...);
1135+
hud::memory::construct_object_at(res.first.slot_ptr_, hud::move(key), hud::forward<args_t>(args)...);
11531136
}
1154-
return {control_ptr_ + res.first, slot_ptr};
1137+
return res.first;
11551138
}
11561139

11571140
/**
@@ -1165,13 +1148,12 @@ namespace hud
11651148
requires(hud::is_constructible_v<slot_type, const key_type &, args_t...>)
11661149
constexpr iterator add(const key_type &key, args_t &&...args) noexcept
11671150
{
1168-
hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1169-
slot_type *slot_ptr {slot_ptr_ + res.first};
1151+
hud::pair<iterator, bool> res {find_or_insert_no_construct(key)};
11701152
if (res.second)
11711153
{
1172-
hud::memory::construct_object_at(slot_ptr, key, hud::forward<args_t>(args)...);
1154+
hud::memory::construct_object_at(res.first.slot_ptr_, key, hud::forward<args_t>(args)...);
11731155
}
1174-
return {control_ptr_ + res.first, slot_ptr};
1156+
return res.first;
11751157
}
11761158

11771159
template<typename K>
@@ -1232,35 +1214,12 @@ namespace hud
12321214
template<typename key_tuple_t, typename value_tuple_t>
12331215
constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple) noexcept
12341216
{
1235-
// /**
1236-
// * If the tuple can't be used directly as a hashable/comparable key,
1237-
// * we unpack its elements and construct a key_type from them.
1238-
// */
1239-
// constexpr auto forward_key = []<usize... indices_key>(
1240-
// key_tuple_t &&key_tuple,
1241-
// hud::index_sequence<indices_key...>
1242-
// ) -> decltype(auto)
1243-
// {
1244-
// if constexpr (is_hashable_and_comparable_v<key_tuple_t>)
1245-
// {
1246-
// static_assert(hud::is_constructible_v<key_type, decltype(hud::get<indices_key>(key_tuple))...>, "key_type is hashable and comparable with the given tuple but cannot be constructed from its values. ");
1247-
// return hud::forward<key_tuple_t>(key_tuple);
1248-
// }
1249-
// else
1250-
// {
1251-
// static_assert(hud::is_constructible_v<key_type, decltype(hud::get<indices_key>(key_tuple))...>, "key_type is neither hashable nor comparable with the given tuple, and cannot be constructed from its values. "
1252-
// "Ensure that hud::equal and hud::hash support hud::tuple<...&&>&&, or provide a constructor for key_type that accepts the tuple elements.");
1253-
// return key_type(hud::get<indices_key>(key_tuple)...);
1254-
// }
1255-
// };
1256-
1257-
hud::pair<usize, bool> res = find_or_insert_no_construct(forward_key(hud::forward<key_tuple_t>(key_tuple)));
1258-
slot_type *slot_ptr {slot_ptr_ + res.first};
1217+
hud::pair<iterator, bool> res = find_or_insert_no_construct(forward_key(hud::forward<key_tuple_t>(key_tuple)));
12591218
if (res.second)
12601219
{
1261-
hud::memory::construct_object_at(slot_ptr, hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple));
1220+
hud::memory::construct_object_at(res.first.slot_ptr_, hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple));
12621221
}
1263-
return {control_ptr_ + res.first, slot_ptr};
1222+
return res.first;
12641223
}
12651224

12661225
private:
@@ -1282,7 +1241,7 @@ namespace hud
12821241
slot_type *slot_that_match_h2 {slot_ptr_ + slot_index_that_match_h2};
12831242
if (key_equal_(slot_that_match_h2->key(), hud::forward<K>(key))) [[likely]]
12841243
{
1285-
return {control_ptr_ + slot_index_that_match_h2, slot_that_match_h2};
1244+
return iterator {control_ptr_ + slot_index_that_match_h2, slot_that_match_h2};
12861245
}
12871246
}
12881247

@@ -1635,7 +1594,7 @@ namespace hud
16351594

16361595
template<typename K>
16371596
[[nodiscard]]
1638-
constexpr hud::pair<usize, bool> find_or_insert_no_construct(K &&key) noexcept
1597+
constexpr hud::pair<iterator, bool> find_or_insert_no_construct(K &&key) noexcept
16391598
{
16401599
if constexpr (is_hashable_and_comparable_v<K>)
16411600
{
@@ -1653,7 +1612,7 @@ namespace hud
16531612
* If not found insert the key but do not construct the value.
16541613
*/
16551614
template<typename K>
1656-
[[nodiscard]] constexpr hud::pair<usize, bool> find_or_insert_no_construct_impl(K &&key) noexcept
1615+
[[nodiscard]] constexpr hud::pair<iterator, bool> find_or_insert_no_construct_impl(K &&key) noexcept
16571616
{
16581617
u64 hash {compute_hash(key)};
16591618
u64 h1(H1(hash));
@@ -1670,7 +1629,10 @@ namespace hud
16701629
slot_type *slot_that_match_h2 {slot_ptr_ + slot_index_that_match_h2};
16711630
if (key_equal_(slot_that_match_h2->key(), key)) [[likely]]
16721631
{
1673-
return {slot_index_that_match_h2, false};
1632+
return {
1633+
iterator {control_ptr_ + slot_index_that_match_h2, slot_that_match_h2},
1634+
false
1635+
};
16741636
}
16751637
}
16761638

@@ -1688,7 +1650,7 @@ namespace hud
16881650
}
16891651

16901652
/** Insert a slot index associated with the given h2 hash. */
1691-
[[nodiscard]] constexpr usize insert_no_construct(u64 h1, u8 h2) noexcept
1653+
[[nodiscard]] constexpr iterator insert_no_construct(u64 h1, u8 h2) noexcept
16921654
{
16931655
// If we reach the load factor grow the table and retrieves the new slot, else use the given slot
16941656
if (free_slot_before_grow() == 0)
@@ -1702,8 +1664,7 @@ namespace hud
17021664
control::set(control_ptr_, slot_index, h2, max_slot_count_);
17031665

17041666
free_slot_before_grow_--;
1705-
1706-
return slot_index;
1667+
return iterator {control_ptr_ + slot_index, slot_ptr_ + slot_index}; // slot_index;
17071668
}
17081669

17091670
constexpr void resize(usize new_max_slot_count) noexcept

0 commit comments

Comments
 (0)