Skip to content

Commit 0087bfb

Browse files
author
Julian LALU
committed
Improve hashmap and hashset
1 parent cbba28a commit 0087bfb

File tree

3 files changed

+263
-350
lines changed

3 files changed

+263
-350
lines changed

interface/core/containers/hashset.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ namespace hud
4646
}
4747

4848
template<usize idx_to_reach>
49-
[[nodiscard]] friend constexpr decltype(auto) get(slot &s) noexcept
49+
[[nodiscard]] friend constexpr key_type &get(slot &s) noexcept
5050
{
5151
static_assert(idx_to_reach == 0, "Index out of bound");
5252
return s.element_;
5353
}
5454

5555
template<usize idx_to_reach>
56-
[[nodiscard]] friend constexpr decltype(auto) get(const slot &s) noexcept
56+
[[nodiscard]] friend constexpr const key_type &get(const slot &s) noexcept
5757
{
5858
static_assert(idx_to_reach == 0, "Index out of bound");
5959
return s.element_;
@@ -1099,13 +1099,15 @@ namespace hud
10991099
{
11001100
}
11011101

1102-
constexpr hashset(std::initializer_list<element_t> list, const allocator_type &allocator = allocator_type()) noexcept
1102+
template<typename u_element_t>
1103+
requires(hud::is_constructible_v<slot_type, u_element_t>)
1104+
constexpr hashset(std::initializer_list<u_element_t> list, const allocator_type &allocator = allocator_type()) noexcept
11031105
: super(allocator)
11041106
{
11051107
reserve(list.size());
11061108
for (auto &value : list)
11071109
{
1108-
add(value);
1110+
add(hud::move(value));
11091111
}
11101112
}
11111113
};

test/hashmap/hashmap_iterator.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ GTEST_TEST(hashmap, structure_binding)
8686

8787
// auto is a copy
8888
{
89-
hud::hashmap<i32, i64> map;
89+
hud::hashmap<hud_test::non_bitwise_type, i64> map;
9090
map.add({1, 11});
9191
map.add({2, 22});
9292
map.add({3, 33});
@@ -95,6 +95,12 @@ GTEST_TEST(hashmap, structure_binding)
9595
hud_assert_ge(map.max_count(), 4u);
9696
auto [first, second] = *map.find(4);
9797
hud_assert_eq(first, 4);
98+
// Check key is a copy
99+
hud_assert_eq(first.constructor_count(), 1u);
100+
hud_assert_eq(first.copy_assign_count(), 0u);
101+
hud_assert_eq(first.copy_constructor_count(), 1u);
102+
hud_assert_eq(first.move_assign_count(), 0u);
103+
hud_assert_eq(first.move_constructor_count(), 1u);
98104
second = 444;
99105
hud_assert_eq(first, 4);
100106
hud_assert_eq(second, 444);
@@ -105,7 +111,7 @@ GTEST_TEST(hashmap, structure_binding)
105111

106112
// auto& is a reference
107113
{
108-
hud::hashmap<i32, i64> map;
114+
hud::hashmap<hud_test::non_bitwise_type, i64> map;
109115
map.add({1, 11});
110116
map.add({2, 22});
111117
map.add({3, 33});
@@ -114,6 +120,12 @@ GTEST_TEST(hashmap, structure_binding)
114120
hud_assert_ge(map.max_count(), 4u);
115121
auto &[first, second] = *map.find(4);
116122
hud_assert_eq(first, 4);
123+
// Check key is a reference
124+
hud_assert_eq(first.constructor_count(), 1u);
125+
hud_assert_eq(first.copy_assign_count(), 0u);
126+
hud_assert_eq(first.copy_constructor_count(), 0u);
127+
hud_assert_eq(first.move_assign_count(), 0u);
128+
hud_assert_eq(first.move_constructor_count(), 1u);
117129
second = 444;
118130
hud_assert_eq(second, 444);
119131
auto &[first_1, second_1] = *map.find(4);
@@ -123,20 +135,26 @@ GTEST_TEST(hashmap, structure_binding)
123135

124136
// auto&& is a reference
125137
{
126-
hud::hashmap<i32, i64> map;
138+
hud::hashmap<hud_test::non_bitwise_type, i64> map;
127139
map.add({1, 11});
128140
map.add({2, 22});
129141
map.add({3, 33});
130142
map.add({4, 44});
131143
hud_assert_eq(map.count(), 4u);
132144
hud_assert_ge(map.max_count(), 4u);
133-
auto &&[first, second] = *map.find(1);
134-
hud_assert_eq(first, 1);
135-
second = 111;
136-
hud_assert_eq(second, 111);
137-
auto &&[first_1, second_1] = *map.find(1);
138-
hud_assert_eq(first_1, 1);
139-
hud_assert_eq(second_1, 111);
145+
auto &&[first, second] = *map.find(4);
146+
hud_assert_eq(first, 4);
147+
// Check key is a reference
148+
hud_assert_eq(first.constructor_count(), 1u);
149+
hud_assert_eq(first.copy_assign_count(), 0u);
150+
hud_assert_eq(first.copy_constructor_count(), 0u);
151+
hud_assert_eq(first.move_assign_count(), 0u);
152+
hud_assert_eq(first.move_constructor_count(), 1u);
153+
second = 444;
154+
hud_assert_eq(second, 444);
155+
auto &&[first_1, second_1] = *map.find(4);
156+
hud_assert_eq(first_1, 4);
157+
hud_assert_eq(second_1, 444);
140158
}
141159

142160
// Loop

0 commit comments

Comments
 (0)