Skip to content

Commit c1c21a8

Browse files
committed
Update hashset tests
1 parent 5913fce commit c1c21a8

File tree

11 files changed

+9438
-59
lines changed

11 files changed

+9438
-59
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"cmake.configureArgs": [
3-
//"-DSANITIZER:BOOL=ON" // ou undefined, thread, leak, etc.
3+
// "-DSANITIZER:BOOL=ON"
44
]
55
}

interface/core/containers/hashset.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ namespace hud
17801780
/** Compute the size of the allocation needed for the given slot count. */
17811781
[[nodiscard]] constexpr usize current_allocation_size() const noexcept
17821782
{
1783-
const usize control_size {max_slot_count_ + 1 + control::COUNT_CLONED_BYTE};
1783+
const usize control_size {control_size_for_max_count(max_slot_count_)};
17841784
const uptr aligned_control_size {hud::memory::align_address(control_size, sizeof(slot_type))};
17851785
return aligned_control_size + max_slot_count_ * sizeof(slot_type);
17861786
}
@@ -1880,11 +1880,9 @@ namespace hud
18801880

18811881
constexpr usize allocate_control_and_slot(usize max_slot_count) noexcept
18821882
{
1883-
const usize ctrl_size {control_size_for_max_count(max_slot_count)};
1883+
const usize control_size {control_size_for_max_count(max_slot_count)};
18841884
const usize slot_size {max_slot_count * sizeof(slot_type)};
1885-
1886-
const uptr aligned_control_size {hud::memory::align_address(ctrl_size, sizeof(slot_type))};
1887-
const usize aligned_allocation_size {aligned_control_size + slot_size};
1885+
const uptr aligned_control_size {hud::memory::align_address(control_size, sizeof(slot_type))};
18881886

18891887
if (hud::is_constant_evaluated())
18901888
{
@@ -1893,32 +1891,33 @@ namespace hud
18931891
}
18941892
else
18951893
{
1894+
const usize aligned_allocation_size {aligned_control_size + slot_size};
18961895
control_ptr_ = allocator_.template allocate<control_type>(aligned_allocation_size).data();
1897-
slot_ptr_ = reinterpret_cast<slot_type *>(hud::memory::align_address(reinterpret_cast<const uptr>(control_ptr_ + ctrl_size), alignof(slot_type)));
1896+
slot_ptr_ = reinterpret_cast<slot_type *>(hud::memory::align_address(reinterpret_cast<const uptr>(control_ptr_ + control_size), alignof(slot_type)));
18981897
hud::check(hud::memory::is_pointer_aligned(slot_ptr_, alignof(slot_type)));
18991898
}
1900-
return ctrl_size;
1899+
return control_size;
19011900
}
19021901

19031902
constexpr void free_control_and_slot(control_type *control_ptr, slot_type *slot_ptr, usize max_slot_count) noexcept
19041903
{
19051904
if (max_slot_count > 0)
19061905
{
1906+
const usize control_size {control_size_for_max_count(max_slot_count)};
1907+
const usize slot_size {max_slot_count * sizeof(slot_type)};
1908+
const uptr aligned_control_size {hud::memory::align_address(control_size, sizeof(slot_type))};
1909+
19071910
// In a constant-evaluated context, bit_cast cannot be used with pointers
19081911
// and allocation is done in two separate allocation
19091912
if (hud::is_constant_evaluated())
19101913
{
1911-
// Control size is the number of slot + sentinel + number of cloned bytes
1912-
const usize control_size {control_size_for_max_count(max_slot_count)};
1913-
const usize slot_size {max_slot_count * sizeof(slot_type)};
1914-
const uptr aligned_control_size {hud::memory::align_address(control_size, sizeof(slot_type))};
1915-
19161914
allocator_.template free<control_type>({control_ptr, aligned_control_size});
19171915
allocator_.template free<slot_type>({slot_ptr, slot_size});
19181916
}
19191917
else
19201918
{
1921-
allocator_.template free<slot_type>({hud::bit_cast<slot_type *>(control_ptr), current_allocation_size()});
1919+
const usize aligned_allocation_size {aligned_control_size + slot_size};
1920+
allocator_.template free<control_type>({control_ptr, aligned_allocation_size});
19221921
}
19231922
}
19241923
}
@@ -2146,10 +2145,20 @@ namespace hud
21462145
return false;
21472146
}
21482147

2149-
for (const auto &elem : left)
2148+
// Speed of find is dependent of the max_slot_count_
2149+
// We want to find in the smallest max_slot_count and iterate on the bigger only once
2150+
const hashset<key_t, hasher_t, key_equal_t, allocator_t> *biggest_capacity = &left;
2151+
const hashset<key_t, hasher_t, key_equal_t, allocator_t> *smallest_capacity = &right;
2152+
if (smallest_capacity->max_count() > biggest_capacity->max_count())
2153+
{
2154+
hud::swap(biggest_capacity, smallest_capacity);
2155+
}
2156+
2157+
// Iterate over biggest capacity and find in the smallest each elements
2158+
for (const auto &elem : *biggest_capacity)
21502159
{
2151-
const auto &it = right.find(elem.key());
2152-
if (it == right.end() && !(it->value() == elem.value()))
2160+
const auto &it = smallest_capacity->find(elem.key());
2161+
if (it == smallest_capacity->end())
21532162
{
21542163
return false;
21552164
}

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ set( src
8585
hashmap/hashmap_remove.cpp
8686
hashmap/hashmap_shrink_to_fit.cpp
8787
hashset/hashset_add.cpp
88+
hashset/hashset_clear_shrink.cpp
89+
hashset/hashset_clear.cpp
90+
hashset/hashset_comparison.cpp
8891
hashset/hashset_constructors.cpp
92+
hashset/hashset_contains.cpp
93+
hashset/hashset_copy_assign_operator.cpp
8994
hashset/hashset_iterator.cpp
9095
hashset/hashset_misc.cpp
9196
i128/i128_assign.cpp

test/hashmap/hashmap_clear_shrink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ GTEST_TEST(hashmap, clear_shrink_non_trivially_destructible_empty_map)
148148
{
149149
using key_type = hud_test::non_bitwise_type;
150150
using value_type = hud_test::non_bitwise_type;
151-
hud::hashmap<key_type, value_type, hud::hash_64<key_type>, hud::equal<value_type>, hud_test::allocator_watcher<1>> map;
151+
hud::hashmap<key_type, value_type, hud::hash_64<key_type>, hud::equal<key_type>, hud_test::allocator_watcher<1>> map;
152152
constexpr usize COUNT = 256;
153153
map.reserve(COUNT);
154154
map.clear_shrink();

0 commit comments

Comments
 (0)