Skip to content

Commit 2b1854e

Browse files
author
Julian LALU
committed
Improve hashmap
1 parent c3ab572 commit 2b1854e

4 files changed

Lines changed: 101 additions & 0 deletions

File tree

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ set( src
7070
hashmap/hashmap_constructors.cpp
7171
hashmap/hashmap_copy_assign_operator.cpp
7272
hashmap/hashmap_copy_constructors.cpp
73+
hashmap/hashmap_destructeur.cpp
74+
hashmap/hashmap_indexed_operator.cpp
7375
hashmap/hashmap_find.cpp
7476
hashmap/hashmap_comparison.cpp
7577
hashmap/hashmap_swap.cpp
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <core/containers/hashmap.h>
2+
#include "../misc/allocator_watcher.h"
3+
4+
GTEST_TEST(hashmap, destructor_call_elements_destructors)
5+
{
6+
7+
using key_type = hud_test::SetBoolToTrueWhenDestroyed;
8+
using value_type = hud_test::SetBoolToTrueWhenDestroyed;
9+
using hashmap_type = hud::hashmap<key_type, value_type>;
10+
11+
const auto test = []()
12+
{
13+
hashmap_type map;
14+
15+
const usize COUNT = 4;
16+
i32 *dtor_assigned_key_counter = nullptr;
17+
i32 *dtor_assigned_value_counter = nullptr;
18+
19+
dtor_assigned_key_counter = hud::memory::allocate_array<i32>(COUNT);
20+
hud::memory::set_memory_zero_safe(dtor_assigned_key_counter, COUNT * sizeof(i32));
21+
dtor_assigned_value_counter = hud::memory::allocate_array<i32>(COUNT);
22+
hud::memory::set_memory_zero_safe(dtor_assigned_value_counter, COUNT * sizeof(i32));
23+
24+
hud_test::LeakArrayGuard guard_assigned_key_counter(dtor_assigned_key_counter, COUNT);
25+
hud_test::LeakArrayGuard guard_assigned_value_counter(dtor_assigned_value_counter, COUNT);
26+
27+
bool all_keys_destructor_are_not_called = true;
28+
bool all_values_destructor_are_not_called = true;
29+
{
30+
hashmap_type map;
31+
map.reserve(COUNT);
32+
for (i32 i = 0; i < COUNT; i++)
33+
{
34+
map.add({i, dtor_assigned_key_counter + i}, {i, dtor_assigned_value_counter + i});
35+
}
36+
37+
// Ensure element's destructors are not called
38+
for (i32 i = 0; i < COUNT; i++)
39+
{
40+
// Should be accepted with is_transparent
41+
// const auto it = map.find(i);
42+
const auto it = map.find({i, dtor_assigned_key_counter + i});
43+
if (*(it->key().ptr()) != 0)
44+
{
45+
all_keys_destructor_are_not_called = false;
46+
}
47+
if ((*it->value().ptr()) != 0)
48+
{
49+
all_values_destructor_are_not_called = false;
50+
}
51+
}
52+
}
53+
54+
return std::tuple {
55+
all_keys_destructor_are_not_called, // 0
56+
all_values_destructor_are_not_called // 1
57+
};
58+
};
59+
60+
// Non constant
61+
{
62+
const auto result = test();
63+
hud_assert_true(std::get<0>(result));
64+
hud_assert_true(std::get<1>(result));
65+
}
66+
67+
// Constant
68+
{
69+
// constexpr auto result = test();
70+
// hud_assert_true(std::get<0>(result));
71+
// hud_assert_true(std::get<1>(result));
72+
}
73+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <core/containers/hashmap.h>

test/misc/set_bool_to_true_when_destroyed.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ namespace hud
154154
{
155155
return hud::hash_32<i32> {}(custom.id());
156156
}
157+
158+
[[nodiscard]] constexpr u32 operator()(const i32 custom) const
159+
{
160+
return hud::hash_32<i32> {}(custom);
161+
}
157162
};
158163

159164
template<>
@@ -163,7 +168,27 @@ namespace hud
163168
{
164169
return hud::hash_64<i32> {}(custom.id());
165170
}
171+
172+
[[nodiscard]] constexpr u32 operator()(const i32 custom) const
173+
{
174+
return hud::hash_32<i32> {}(custom);
175+
}
166176
};
167177

178+
// template<>
179+
// struct hash_32<string>
180+
// {
181+
// std::size_t operator()(const string &s) const
182+
// {
183+
// return ...;
184+
// }
185+
186+
// // hash_equal=true allows hashing string literals
187+
// std::size_t operator()(const char *s) const
188+
// {
189+
// return ...;
190+
// }
191+
// };
192+
168193
} // namespace hud
169194
#endif // HD_INC_MISC_SETBOOLTOTRUEWHENDESTROYED_H

0 commit comments

Comments
 (0)