Skip to content

Commit 031a07a

Browse files
author
Julian LALU
committed
Fix hashmap tests
1 parent bb7a1d1 commit 031a07a

File tree

3 files changed

+46
-63
lines changed

3 files changed

+46
-63
lines changed

interface/core/containers/hashmap.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,9 @@ namespace hud
9797
};
9898

9999
template<typename key_t>
100-
struct default_equal
101-
: hud::equal<key_t>
102-
{
103-
};
100+
using default_equal = hud::equal<key_t>;
104101

105-
struct default_allocator
106-
: hud::heap_allocator
107-
{
108-
};
102+
using default_allocator = hud::heap_allocator;
109103

110104
} // namespace details::hashmap
111105

interface/core/containers/hashset.h

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,9 @@ namespace hud
9696
};
9797

9898
template<typename key_t>
99-
struct default_equal
100-
: hud::equal<key_t>
101-
{
102-
};
99+
using default_equal = hud::equal<key_t>;
103100

104-
struct default_allocator
105-
: hud::heap_allocator
106-
{
107-
};
101+
using default_allocator = hud::heap_allocator;
108102

109103
/** Retrives the H1 (57 first bit) of a hash. */
110104
[[nodiscard]]
@@ -579,55 +573,50 @@ namespace hud
579573
explicit constexpr hashset_impl() noexcept = default;
580574

581575
constexpr explicit hashset_impl(const allocator_type &allocator) noexcept
582-
requires(hud::allocator_traits<allocator_type>::copy_when_container_copy::value)
583-
: allocator_(allocator)
584-
{
585-
}
586-
587-
constexpr explicit hashset_impl(const allocator_type &allocator) noexcept
588-
requires(!hud::allocator_traits<allocator_type>::copy_when_container_copy::value)
589-
: hashset_impl()
576+
: allocator_(hud::allocator_traits<allocator_type>::copy_when_container_copy::value ? allocator : allocator_type {})
590577
{
591578
}
592579

593580
constexpr explicit hashset_impl(const hashset_impl &other) noexcept
594-
: allocator_(other.allocator())
581+
: allocator_(hud::allocator_traits<allocator_type>::copy_when_container_copy::value ? other.allocator() : allocator_type {})
595582
, max_slot_count_(other.max_count())
596583
, count_(other.count())
597584
, free_slot_before_grow_(max_slot_before_grow(max_slot_count_) - count_)
598585
{
599586

600-
// Allocate the buffer that will contain controls and aligned slots
601-
// In a constant-evaluated context, bit_cast cannot be used with pointers
602-
// To satisfy the compiler, allocate controls and slots in two separate allocations
603-
usize control_size = allocate_control_and_slot(max_slot_count_);
604-
605-
// If constant evaluated context
606-
// loop through all slot and construct them regardless of the trivially constructible ( Maybe only for control_ptr_ ) like like grow_capacity
607-
// In a non constant evaluated context
608-
// If type is trivially copy constructible, just memcpy control and slot
609-
// else do like grow_capacity
610-
if (hud::is_constant_evaluated() || hud::is_trivially_copy_constructible_v<slot_type>)
587+
if (max_slot_count_ > 0)
611588
{
612-
// Set control to empty ending with sentinel
613-
hud::memory::set(control_ptr_, control_size, empty_byte);
614-
control_ptr_[max_slot_count_] = sentinel_byte;
615-
616-
// Copy slots to newly allocated buffer
617-
for (auto &slot : other)
589+
// Allocate the buffer that will contain controls and aligned slots
590+
// In a constant-evaluated context, bit_cast cannot be used with pointers
591+
// To satisfy the compiler, allocate controls and slots in two separate allocations
592+
usize control_size = allocate_control_and_slot(max_slot_count_);
593+
594+
// If constant evaluated context
595+
// loop through all slot and construct them regardless of the trivially constructible ( Maybe only for control_ptr_ ) like like grow_capacity
596+
// In a non constant evaluated context
597+
// If type is trivially copy constructible, just memcpy control and slot
598+
// else do like grow_capacity
599+
if (hud::is_constant_evaluated() || hud::is_trivially_copy_constructible_v<slot_type>)
618600
{
619-
// Compute the hash
620-
u64 hash = hasher_type {}(slot.key());
621-
// Find H1 slot index
622-
u64 h1 = H1(hash);
623-
usize slot_index = find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1);
624-
// Save h2 in control h1 index
625-
control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
626-
// Copy slot
627-
hud::memory::construct_at(slot_ptr_ + slot_index, slot);
601+
// Set control to empty ending with sentinel
602+
hud::memory::set(control_ptr_, control_size, empty_byte);
603+
control_ptr_[max_slot_count_] = sentinel_byte;
604+
605+
// Copy slots to newly allocated buffer
606+
for (auto &slot : other)
607+
{
608+
// Compute the hash
609+
u64 hash = hasher_type {}(slot.key());
610+
// Find H1 slot index
611+
u64 h1 = H1(hash);
612+
usize slot_index = find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1);
613+
// Save h2 in control h1 index
614+
control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
615+
// Copy slot
616+
hud::memory::construct_at(slot_ptr_ + slot_index, slot);
617+
}
628618
}
629619
}
630-
631620
// Set control to empty ending with sentinel only if type is not trivially copyable
632621
// Int he case of trivially copyable type we just memcpy control and slots
633622
// if (!hud::is_trivially_copy_constructible_v<slot_type>)

test/hashmap/hashmap_copy_constructors.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ GTEST_TEST(hashmap, copy_construct_bitwise_copy_constructible_same_type_same_all
5858
}
5959

6060
return std::tuple {
61-
copy.count() == copied.count(), // 0
62-
copy.max_count() == copied.max_count(), // 1
63-
all_keys_and_values_copied, // 2
64-
copy.allocator().allocation_count() == (hud::is_constant_evaluated() ? 2 : 1), // 3
65-
copy.allocator().free_count() == 0 // 4
61+
copy.count() == copied.count(), // 0
62+
copy.max_count() == copied.max_count(), // 1
63+
all_keys_and_values_copied, // 2
64+
copy.allocator().allocation_count() == (initializer.size() > 0 ? (hud::is_constant_evaluated() ? 4 : 2) : 0), // 3
65+
copy.allocator().free_count() == 0 // 4
6666
};
6767
};
6868

@@ -86,12 +86,12 @@ GTEST_TEST(hashmap, copy_construct_bitwise_copy_constructible_same_type_same_all
8686
// Constant
8787
{
8888

89-
// constexpr auto result_empty = test_default_allocator({});
90-
// hud_assert_true(std::get<0>(result_empty));
91-
// hud_assert_true(std::get<1>(result_empty));
92-
// hud_assert_true(std::get<2>(result_empty));
93-
// hud_assert_true(std::get<3>(result_empty));
94-
// hud_assert_true(std::get<4>(result_empty));
89+
constexpr auto result_empty = test_default_allocator({});
90+
hud_assert_true(std::get<0>(result_empty));
91+
hud_assert_true(std::get<1>(result_empty));
92+
hud_assert_true(std::get<2>(result_empty));
93+
hud_assert_true(std::get<3>(result_empty));
94+
hud_assert_true(std::get<4>(result_empty));
9595

9696
constexpr auto result = test_default_allocator(TEST_VALUES);
9797
hud_assert_true(std::get<0>(result));

0 commit comments

Comments
 (0)