Skip to content

Commit c5a9e54

Browse files
author
Julian LALU
committed
Update hasmap
1 parent d15b071 commit c5a9e54

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed

interface/core/containers/hashset.h

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,44 +1002,6 @@ namespace hud
10021002
reset_control_and_slot();
10031003
}
10041004

1005-
/**
1006-
* Insert a key in the hashset.
1007-
* @param key The key associated with the `value`
1008-
* @param args List of arguments pass to `value_type` constructor after the `key` itself
1009-
* @return Iterator to the `value`
1010-
*/
1011-
template<typename... args_t>
1012-
requires(hud::is_constructible_v<slot_type, key_type, args_t...>)
1013-
constexpr iterator add(key_type &&key, args_t &&...args) noexcept
1014-
{
1015-
hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1016-
slot_type *slot_ptr {slot_ptr_ + res.first};
1017-
if (res.second)
1018-
{
1019-
hud::memory::construct_object_at(slot_ptr, hud::move(key), hud::forward<args_t>(args)...);
1020-
}
1021-
return {control_ptr_ + res.first, slot_ptr};
1022-
}
1023-
1024-
/**
1025-
* Insert a key in the hashset.
1026-
* @param key The key associated with the `value`
1027-
* @param args List of arguments pass to `value_type` constructor after the `key` itself
1028-
* @return Iterator to the `value`
1029-
*/
1030-
template<typename... args_t>
1031-
requires(hud::is_constructible_v<slot_type, const key_type &, args_t...>)
1032-
constexpr iterator add(const key_type &key, args_t &&...args) noexcept
1033-
{
1034-
hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1035-
slot_type *slot_ptr {slot_ptr_ + res.first};
1036-
if (res.second)
1037-
{
1038-
hud::memory::construct_object_at(slot_ptr, key, hud::forward<args_t>(args)...);
1039-
}
1040-
return {control_ptr_ + res.first, slot_ptr};
1041-
}
1042-
10431005
/** Find a key and return an iterator to the value. */
10441006
template<typename K>
10451007
[[nodiscard]]
@@ -1210,6 +1172,45 @@ namespace hud
12101172
return const_iterator {control_ptr_sentinel()};
12111173
}
12121174

1175+
protected:
1176+
/**
1177+
* Insert a key in the hashset and pass
1178+
* @param key The key associated with the `value`
1179+
* @param args List of arguments pass to `value_type` constructor after the `key` itself
1180+
* @return Iterator to the `value`
1181+
*/
1182+
template<typename... args_t>
1183+
requires(hud::is_constructible_v<slot_type, key_type, args_t...>)
1184+
constexpr iterator add(key_type &&key, args_t &&...args) noexcept
1185+
{
1186+
hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1187+
slot_type *slot_ptr {slot_ptr_ + res.first};
1188+
if (res.second)
1189+
{
1190+
hud::memory::construct_object_at(slot_ptr, hud::move(key), hud::forward<args_t>(args)...);
1191+
}
1192+
return {control_ptr_ + res.first, slot_ptr};
1193+
}
1194+
1195+
/**
1196+
* Insert a key in the hashset.
1197+
* @param key The key associated with the `value`
1198+
* @param args List of arguments pass to `value_type` constructor after the `key` itself
1199+
* @return Iterator to the `value`
1200+
*/
1201+
template<typename... args_t>
1202+
requires(hud::is_constructible_v<slot_type, const key_type &, args_t...>)
1203+
constexpr iterator add(const key_type &key, args_t &&...args) noexcept
1204+
{
1205+
hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1206+
slot_type *slot_ptr {slot_ptr_ + res.first};
1207+
if (res.second)
1208+
{
1209+
hud::memory::construct_object_at(slot_ptr, key, hud::forward<args_t>(args)...);
1210+
}
1211+
return {control_ptr_ + res.first, slot_ptr};
1212+
}
1213+
12131214
private:
12141215
template<typename u_key_type>
12151216
[[nodiscard]] constexpr u64 compute_hash(const u_key_type &key) noexcept
@@ -1902,11 +1903,6 @@ namespace hud
19021903

19031904
} // namespace details::hashset
19041905

1905-
// using hashset_default_hasher = details::hashset::default_hasher;
1906-
1907-
// template<typename element_t>
1908-
// using hashset_default_key_equal = details::hashset::default_equal<element_t>;
1909-
19101906
using hashset_default_allocator = details::hashset::default_allocator;
19111907

19121908
template<

test/hashmap/hashmap_add.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4977,4 +4977,27 @@ GTEST_TEST(hashmap, add_pair_by_move_construct_non_bitwise_move_constructible_di
49774977
hud_assert_true(std::get<8>(second_element_result));
49784978
}
49794979
}
4980+
}
4981+
4982+
GTEST_TEST(hashmap, add_by_piecewise_construct_bitwise_same_type)
4983+
{
4984+
using key_type = i32;
4985+
using value_type = i64;
4986+
using hashmap_type = hud::hashmap<key_type, value_type, hud::hash_64<key_type>, hud::equal<key_type>, hud_test::allocator_watcher<1>>;
4987+
4988+
const auto test = []()
4989+
{
4990+
constexpr usize reserved_size = 2;
4991+
hashmap_type map;
4992+
map.reserve(reserved_size);
4993+
// map.add(hud::tag_piecewise_construct, hud::forward_as_tuple(1), hud::forward_as_tuple(2));
4994+
};
4995+
4996+
// Non constant
4997+
{
4998+
}
4999+
5000+
// Constant
5001+
{
5002+
}
49805003
}

0 commit comments

Comments
 (0)