Skip to content

Commit 274a992

Browse files
author
Julian LALU
committed
Improve hashmap and hashset
1 parent 1ec1bd5 commit 274a992

File tree

3 files changed

+149
-110
lines changed

3 files changed

+149
-110
lines changed

interface/core/containers/hashmap.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ namespace hud
6363
}
6464

6565
template<usize idx_to_reach>
66-
[[nodiscard]] friend constexpr decltype(auto) get(slot &&s) noexcept
66+
[[nodiscard]] friend constexpr auto get(slot &&s) noexcept
6767
{
6868
return hud::get<idx_to_reach>(hud::forward<slot>(s).element_);
6969
}
7070

7171
template<usize idx_to_reach>
72-
[[nodiscard]] friend constexpr decltype(auto) get(const slot &&s) noexcept
72+
[[nodiscard]] friend constexpr auto get(const slot &&s) noexcept
7373
{
7474
return hud::get<idx_to_reach>(hud::forward<const slot>(s).element_);
7575
}
@@ -191,4 +191,18 @@ namespace hud
191191

192192
} // namespace hud
193193

194+
namespace std
195+
{
196+
template<typename key_t, typename value_t>
197+
struct tuple_size<hud::details::hashmap::slot<key_t, value_t>>
198+
: hud::tuple_size<hud::details::hashmap::slot<key_t, value_t>>
199+
{
200+
};
201+
202+
template<std::size_t idx_to_reach, typename key_t, typename value_t>
203+
struct tuple_element<idx_to_reach, hud::details::hashmap::slot<key_t, value_t>>
204+
: hud::tuple_element<idx_to_reach, hud::details::hashmap::slot<key_t, value_t>>
205+
{
206+
};
207+
} // namespace std
194208
#endif // HD_INC_CORE_HASHMAP_H

interface/core/containers/hashset.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,6 @@ namespace hud
475475
HD_ASSUME(slot_ptr_ != nullptr);
476476
}
477477

478-
// constexpr iterator(const iterator<slot_type, false> &it) noexcept
479-
// : iterator(it.control_ptr_, it.slot_ptr_)
480-
// {
481-
// }
482-
483478
/**
484479
* Create a constant iterator from a non constant iterator.
485480
*/
@@ -553,13 +548,13 @@ namespace hud
553548
}
554549

555550
template<usize idx_to_reach>
556-
[[nodiscard]] friend constexpr decltype(auto) get(iterator &&s) noexcept
551+
[[nodiscard]] friend constexpr auto get(iterator &&s) noexcept
557552
{
558553
return get<idx_to_reach>(*(hud::forward<iterator>(s).slot_ptr_));
559554
}
560555

561556
template<usize idx_to_reach>
562-
[[nodiscard]] friend constexpr decltype(auto) get(const iterator &&s) noexcept
557+
[[nodiscard]] friend constexpr auto get(const iterator &&s) noexcept
563558
{
564559
return get<idx_to_reach>(*(hud::forward<const iterator>(s).slot_ptr_));
565560
}
@@ -1172,6 +1167,18 @@ namespace hud
11721167

11731168
namespace std
11741169
{
1170+
template<typename key_t>
1171+
struct tuple_size<hud::details::hashset::slot<key_t>>
1172+
: hud::tuple_size<hud::details::hashset::slot<key_t>>
1173+
{
1174+
};
1175+
1176+
template<std::size_t idx_to_reach, typename key_t>
1177+
struct tuple_element<idx_to_reach, hud::details::hashset::slot<key_t>>
1178+
: hud::tuple_element<idx_to_reach, hud::details::hashset::slot<key_t>>
1179+
{
1180+
};
1181+
11751182
template<typename slot_t, bool is_const>
11761183
struct tuple_size<hud::details::hashset::iterator<slot_t, is_const>>
11771184
: hud::tuple_size<hud::details::hashset::iterator<slot_t, is_const>>

test/hashmap/hashmap_iterator.cpp

Lines changed: 119 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ GTEST_TEST(hashmap, structure_binding)
8484
hud_assert_eq(second_c, 11);
8585
}
8686

87-
// auto is a reference
87+
// auto is a copy
8888
{
8989
hud::hashmap<i32, i64> map;
9090
map.add({1, 11});
@@ -100,116 +100,134 @@ GTEST_TEST(hashmap, structure_binding)
100100
hud_assert_eq(second, 111);
101101
auto [first_1, second_1] = map.find(1);
102102
hud_assert_eq(first_1, 1);
103+
hud_assert_eq(second_1, 11);
104+
}
105+
106+
// auto& is a reference
107+
{
108+
hud::hashmap<i32, i64> map;
109+
map.add({1, 11});
110+
map.add({2, 22});
111+
map.add({3, 33});
112+
map.add({4, 44});
113+
hud_assert_eq(map.count(), 4u);
114+
hud_assert_ge(map.max_count(), 4u);
115+
auto it = map.find(1);
116+
auto &[first, second] = it;
117+
hud_assert_eq(first, 1);
118+
second = 111;
119+
hud_assert_eq(second, 111);
120+
auto [first_1, second_1] = map.find(1);
121+
hud_assert_eq(first_1, 1);
103122
hud_assert_eq(second_1, 111);
104123
}
105124

106-
// // Ref do modify hash value
107-
// {
108-
// hud::hashmap<i32, hud_test::non_bitwise_copy_assignable_type> map;
109-
// map.add({1, 11});
110-
// map.add({2, 22});
111-
// map.add({3, 33});
112-
// map.add({4, 44});
113-
// hud_assert_eq(map.count(), 4u);
114-
// hud_assert_ge(map.max_count(), 4u);
115-
// auto it = map.find(4);
116-
// auto &[first, second] = *it;
117-
// hud_assert_eq(first, 4);
118-
// second = 444;
119-
// hud_assert_eq(second, 444);
120-
// auto it_1 = map.find(4);
121-
// auto &[first_1, second_1] = *it_1;
122-
// hud_assert_eq(first_1, 4);
123-
// hud_assert_eq(second_1, 444);
124-
// }
125+
// auto&& is a reference
126+
{
127+
hud::hashmap<i32, i64> map;
128+
map.add({1, 11});
129+
map.add({2, 22});
130+
map.add({3, 33});
131+
map.add({4, 44});
132+
hud_assert_eq(map.count(), 4u);
133+
hud_assert_ge(map.max_count(), 4u);
134+
auto it = map.find(1);
135+
auto &[first, second] = it;
136+
hud_assert_eq(first, 1);
137+
second = 111;
138+
hud_assert_eq(second, 111);
139+
auto [first_1, second_1] = map.find(1);
140+
hud_assert_eq(first_1, 1);
141+
hud_assert_eq(second_1, 111);
142+
}
125143

126-
// // Loop
127-
// {
128-
// const auto test = []()
129-
// {
130-
// using map_type = hud::hashmap<i32, i64>;
131-
// map_type map {
132-
// {1, 11},
133-
// {2, 22},
134-
// {3, 33},
135-
// {4, 44}
136-
// };
137-
// const map_type const_map {
138-
// {1, 11},
139-
// {2, 22},
140-
// {3, 33},
141-
// {4, 44}
142-
// };
144+
// Loop
145+
{
146+
const auto test = []()
147+
{
148+
using map_type = hud::hashmap<i32, i64>;
149+
map_type map {
150+
{1, 11},
151+
{2, 22},
152+
{3, 33},
153+
{4, 44}
154+
};
155+
const map_type const_map {
156+
{1, 11},
157+
{2, 22},
158+
{3, 33},
159+
{4, 44}
160+
};
143161

144-
// map_type::element_type result[4];
145-
// usize index = 0;
146-
// for (const auto &[first, second] : map)
147-
// {
148-
// result[index++] = {first, second};
149-
// }
162+
hud::pair<map_type::key_type, map_type::value_type> result[4];
163+
usize index = 0;
164+
for (const auto &[first, second] : map)
165+
{
166+
result[index++] = {first, second};
167+
}
150168

151-
// map_type::element_type const_result[4];
152-
// index = 0;
153-
// for (const auto &[first, second] : const_map)
154-
// {
155-
// const_result[index++] = {first, second};
156-
// }
157-
// i32 pair_1_11_index = hud::find_index_if(result, 4, [](const auto &pair)
158-
// { return pair.first == 1 && pair.second == 11; });
159-
// i32 pair_2_22_index = hud::find_index_if(result, 4, [](const auto &pair)
160-
// { return pair.first == 2 && pair.second == 22; });
161-
// i32 pair_3_33_index = hud::find_index_if(result, 4, [](const auto &pair)
162-
// { return pair.first == 3 && pair.second == 33; });
163-
// i32 pair_4_44_index = hud::find_index_if(result, 4, [](const auto &pair)
164-
// { return pair.first == 4 && pair.second == 44; });
169+
hud::pair<map_type::key_type, map_type::value_type> const_result[4];
170+
index = 0;
171+
for (const auto &[first, second] : const_map)
172+
{
173+
const_result[index++] = {first, second};
174+
}
175+
i32 pair_1_11_index = hud::find_index_if(result, 4, [](const auto &pair)
176+
{ return pair.first == 1 && pair.second == 11; });
177+
i32 pair_2_22_index = hud::find_index_if(result, 4, [](const auto &pair)
178+
{ return pair.first == 2 && pair.second == 22; });
179+
i32 pair_3_33_index = hud::find_index_if(result, 4, [](const auto &pair)
180+
{ return pair.first == 3 && pair.second == 33; });
181+
i32 pair_4_44_index = hud::find_index_if(result, 4, [](const auto &pair)
182+
{ return pair.first == 4 && pair.second == 44; });
165183

166-
// i32 const_pair_1_11_index = hud::find_index_if(const_result, 4, [](const auto &pair)
167-
// { return pair.first == 1 && pair.second == 11; });
168-
// i32 const_pair_2_22_index = hud::find_index_if(const_result, 4, [](const auto &pair)
169-
// { return pair.first == 2 && pair.second == 22; });
170-
// i32 const_pair_3_33_index = hud::find_index_if(const_result, 4, [](const auto &pair)
171-
// { return pair.first == 3 && pair.second == 33; });
172-
// i32 const_pair_4_44_index = hud::find_index_if(const_result, 4, [](const auto &pair)
173-
// { return pair.first == 4 && pair.second == 44; });
184+
i32 const_pair_1_11_index = hud::find_index_if(const_result, 4, [](const auto &pair)
185+
{ return pair.first == 1 && pair.second == 11; });
186+
i32 const_pair_2_22_index = hud::find_index_if(const_result, 4, [](const auto &pair)
187+
{ return pair.first == 2 && pair.second == 22; });
188+
i32 const_pair_3_33_index = hud::find_index_if(const_result, 4, [](const auto &pair)
189+
{ return pair.first == 3 && pair.second == 33; });
190+
i32 const_pair_4_44_index = hud::find_index_if(const_result, 4, [](const auto &pair)
191+
{ return pair.first == 4 && pair.second == 44; });
174192

175-
// return std::tuple {
176-
// pair_1_11_index != -1,
177-
// pair_2_22_index != -1,
178-
// pair_3_33_index != -1,
179-
// pair_4_44_index != -1,
180-
// const_pair_1_11_index != -1,
181-
// const_pair_2_22_index != -1,
182-
// const_pair_3_33_index != -1,
183-
// const_pair_4_44_index != -1,
184-
// };
185-
// };
193+
return std::tuple {
194+
pair_1_11_index != -1,
195+
pair_2_22_index != -1,
196+
pair_3_33_index != -1,
197+
pair_4_44_index != -1,
198+
const_pair_1_11_index != -1,
199+
const_pair_2_22_index != -1,
200+
const_pair_3_33_index != -1,
201+
const_pair_4_44_index != -1,
202+
};
203+
};
186204

187-
// // Non constant
188-
// {
189-
// const auto result = test();
190-
// hud_assert_true(std::get<0>(result));
191-
// hud_assert_true(std::get<1>(result));
192-
// hud_assert_true(std::get<2>(result));
193-
// hud_assert_true(std::get<3>(result));
194-
// hud_assert_true(std::get<4>(result));
195-
// hud_assert_true(std::get<5>(result));
196-
// hud_assert_true(std::get<6>(result));
197-
// hud_assert_true(std::get<7>(result));
198-
// }
205+
// Non constant
206+
{
207+
const auto result = test();
208+
hud_assert_true(std::get<0>(result));
209+
hud_assert_true(std::get<1>(result));
210+
hud_assert_true(std::get<2>(result));
211+
hud_assert_true(std::get<3>(result));
212+
hud_assert_true(std::get<4>(result));
213+
hud_assert_true(std::get<5>(result));
214+
hud_assert_true(std::get<6>(result));
215+
hud_assert_true(std::get<7>(result));
216+
}
199217

200-
// // Constant
201-
// {
202-
// constexpr auto result = test();
203-
// hud_assert_true(std::get<0>(result));
204-
// hud_assert_true(std::get<1>(result));
205-
// hud_assert_true(std::get<2>(result));
206-
// hud_assert_true(std::get<3>(result));
207-
// hud_assert_true(std::get<4>(result));
208-
// hud_assert_true(std::get<5>(result));
209-
// hud_assert_true(std::get<6>(result));
210-
// hud_assert_true(std::get<7>(result));
211-
// }
212-
// }
218+
// Constant
219+
{
220+
constexpr auto result = test();
221+
hud_assert_true(std::get<0>(result));
222+
hud_assert_true(std::get<1>(result));
223+
hud_assert_true(std::get<2>(result));
224+
hud_assert_true(std::get<3>(result));
225+
hud_assert_true(std::get<4>(result));
226+
hud_assert_true(std::get<5>(result));
227+
hud_assert_true(std::get<6>(result));
228+
hud_assert_true(std::get<7>(result));
229+
}
230+
}
213231
}
214232

215233
GTEST_TEST(hashmap, range_for_loop)

0 commit comments

Comments
 (0)