From fc8d745cc1c8d8943b31a095d1ea9dc1acabc44c Mon Sep 17 00:00:00 2001 From: Merry Date: Sun, 10 Jul 2022 10:00:57 +0100 Subject: [PATCH] container: hmap fixups --- .../mcl/container/detail/meta_byte_group.hpp | 26 +++++++++---------- include/mcl/container/detail/slot_union.hpp | 1 + include/mcl/container/hmap.hpp | 20 ++++++++------ include/mcl/container/ihmap.hpp | 2 +- include/mcl/hash/xmrx.hpp | 2 ++ 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/include/mcl/container/detail/meta_byte_group.hpp b/include/mcl/container/detail/meta_byte_group.hpp index 592d1f7..05f9ad1 100644 --- a/include/mcl/container/detail/meta_byte_group.hpp +++ b/include/mcl/container/detail/meta_byte_group.hpp @@ -134,7 +134,7 @@ struct meta_byte_group { bool is_all_empty_or_tombstone() const { - return match_empty_or_tombstone() const == 0xffff; + return match_empty_or_tombstone() == 0xffff; } meta_byte get(size_t index) const @@ -152,20 +152,20 @@ struct meta_byte_group { __m128i data; }; -# define MCL_HMAP_MATCH_META_BYTE_GROUP(MATCH, ...) \ - { \ - for (const u32 match_result{MATCH}; match_result != 0; match_result &= match_result - 1) { \ - const size_t match_index{static_cast(std::countr_zero(match_result))}; \ - __VA_ARGS__ \ - } \ +# define MCL_HMAP_MATCH_META_BYTE_GROUP(MATCH, ...) \ + { \ + for (u16 match_result{MATCH}; match_result != 0; match_result &= match_result - 1) { \ + const size_t match_index{static_cast(std::countr_zero(match_result))}; \ + __VA_ARGS__ \ + } \ } -# define MCL_HMAP_MATCH_META_BYTE_GROUP_EXCEPT_LAST(MATCH, ...) \ - { \ - for (const u32 match_result{(MATCH) & (0x7fff)}; match_result != 0; match_result &= match_result - 1) { \ - const size_t match_index{static_cast(std::countr_zero(match_result))}; \ - __VA_ARGS__ \ - } \ +# define MCL_HMAP_MATCH_META_BYTE_GROUP_EXCEPT_LAST(MATCH, ...) \ + { \ + for (u16 match_result{static_cast((MATCH) & (0x7fff))}; match_result != 0; match_result &= match_result - 1) { \ + const size_t match_index{static_cast(std::countr_zero(match_result))}; \ + __VA_ARGS__ \ + } \ } #else diff --git a/include/mcl/container/detail/slot_union.hpp b/include/mcl/container/detail/slot_union.hpp index e8337ba..5038503 100644 --- a/include/mcl/container/detail/slot_union.hpp +++ b/include/mcl/container/detail/slot_union.hpp @@ -9,6 +9,7 @@ namespace mcl::detail { template union slot_union { slot_union() {} + ~slot_union() {} ValueType value; }; diff --git a/include/mcl/container/hmap.hpp b/include/mcl/container/hmap.hpp index 0a2544c..f0616c8 100644 --- a/include/mcl/container/hmap.hpp +++ b/include/mcl/container/hmap.hpp @@ -16,8 +16,10 @@ #include "mcl/container/detail/slot_union.hpp" #include "mcl/hash/xmrx.hpp" #include "mcl/hint/assume.hpp" +#include "mcl/memory/overaligned_unique_ptr.hpp" namespace mcl { + template class hmap; @@ -127,13 +129,15 @@ public: using const_iterator = hmap_iterator; private: - using slot_type = detail::slot_union; - static_assert(!std::is_reference_v); - static_assert(!std::is_reference_v); - static constexpr size_t group_size{detail::meta_byte_group::max_group_size}; static constexpr size_t average_max_group_load{group_size - 2}; + using slot_type = detail::slot_union; + using slot_ptr = std::unique_ptr; + using meta_byte_ptr = overaligned_unique_ptr; + static_assert(!std::is_reference_v); + static_assert(!std::is_reference_v); + public: hmap() { @@ -501,8 +505,8 @@ private: // DEBUG_ASSERT(group_count != 0 && std::ispow2(group_count)); group_index_mask = group_count - 1; - mbs = std::unique_ptr{new (std::align_val_t(group_size)) detail::meta_byte[group_count * group_size + 1]}; - slots = std::unique_ptr{new slot_type[group_count * group_size]}; + mbs = make_overaligned_unique_ptr_array(group_count * group_size + 1); + slots = slot_ptr{new slot_type[group_count * group_size]}; clear_metadata(); } @@ -521,8 +525,8 @@ private: std::size_t group_index_mask; std::size_t empty_slots; std::size_t full_slots; - std::unique_ptr mbs; - std::unique_ptr slots; + meta_byte_ptr mbs; + slot_ptr slots; }; } // namespace mcl diff --git a/include/mcl/container/ihmap.hpp b/include/mcl/container/ihmap.hpp index 79360ea..b50229e 100644 --- a/include/mcl/container/ihmap.hpp +++ b/include/mcl/container/ihmap.hpp @@ -470,7 +470,7 @@ private: { group_type& g{groups[pos.group_index]}; - g.slots[pos.slot_index].value->~value_type(); + g.slots[pos.slot_index].value.~value_type(); --full_slots; if (g.meta.is_any_empty()) { diff --git a/include/mcl/hash/xmrx.hpp b/include/mcl/hash/xmrx.hpp index ebc0fb6..3ba38ca 100644 --- a/include/mcl/hash/xmrx.hpp +++ b/include/mcl/hash/xmrx.hpp @@ -4,6 +4,8 @@ // Reference: http://jonkagstrom.com/bit-mixer-construction/ +#pragma once + #include #include "mcl/bit/rotate.hpp"