From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id mTWFJUCN5mh/PyQAWB0awg (envelope-from ) for ; Wed, 08 Oct 2025 12:11:44 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=nOZFXeOn; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 909631E047; Wed, 08 Oct 2025 12:11:44 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id F33EF1E047 for ; Wed, 08 Oct 2025 12:11:41 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8A4013858434 for ; Wed, 8 Oct 2025 16:11:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A4013858434 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=nOZFXeOn Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id A80D93858D33 for ; Wed, 8 Oct 2025 16:10:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A80D93858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A80D93858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1759939854; cv=none; b=siHF4mHR1qLAnAgqs3VE4BmqFneyHl5j5kxhCmzRbWqFuOcpXqPvuJwsvInuO3vMFIwvtBW34NJmZhKDbRsBSCnIXsWDytkQji39Px99c2f4NyX/Y3aZxFCebtcye/FG/M3IdO514yGufLKDj1wlc50ORSe/2OD418h5czKb4AY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1759939854; c=relaxed/simple; bh=Sq6WLxkyAWSyGgoBLDqcbxX46XvWdOko2hGCCJxdnJ0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LE7FZTwasMm8jo3fHPxlQvLJXOxLBUihOWNRMyxlZA2AY4daZDyVVybfxNNFQAAtW3uIlNpfIV7PwybohsWIJ0wZQFkoBOTQNkDZW/Jlf9BjrPlTmz2ojCxcxXwR0lL7s2nNs11ODw3NOyWHbHb65bFxonfw0ZGUSoBkeyzVFeY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A80D93858D33 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 598GAmVD004797 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 8 Oct 2025 12:10:53 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 598GAmVD004797 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=oct2025; t=1759939853; bh=L+JstPF2wo9icSafKWl3ffmvT1R2KenpAE5HDce3q7o=; h=From:To:Cc:Subject:Date:From; b=nOZFXeOnlCzKEZKkbhF5gNFKUlLqXUiv1QyAxbIIgVunr+8A57HPS7rMfrgSfxQf8 0GFDJfIYZjsiOfRUCpHxr2DZ7jo1iKFtUqd+8daBi5CUp1kxVrSnSAR8knNKMkec0m l1WfhhdkmBzISrM6DEhdfp+vL2zYzc8Znw/q3xFR4zQ0iWeZDHPD25Gt8X3koU8RNQ 1IFsJqKoto413qv9FeNkcqC9qnN7/0wQtr0SPlJeFrkAw+BcmRR9RG0enzxudxg+8A GHv99Iv8eL/JdCw+tfJCjFQmRVc0d0/F0UxUY8qZEaw/HwjANHP+e1s4VkxPZsPAQ0 I1JLO57w+Oe/w== Received: by simark.ca (Postfix) id 995EA1E047; Wed, 08 Oct 2025 12:10:48 -0400 (EDT) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] gdbsupport: bump unordered_dense library to 4.6.0 Date: Wed, 8 Oct 2025 12:10:37 -0400 Message-ID: <20251008161047.1381033-1-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Wed, 8 Oct 2025 16:10:48 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Simon Marchi This version brings a fix made by Pedro [1] to fix compilation on some Windows systems. [1] https://github.com/martinus/unordered_dense/pull/132 Change-Id: I5cedec0e644074e2274346ecc1c73e5be00f84b0 --- gdbsupport/unordered_dense.h | 479 +++++++++++++++++++++++++---------- 1 file changed, 343 insertions(+), 136 deletions(-) diff --git a/gdbsupport/unordered_dense.h b/gdbsupport/unordered_dense.h index 73dc9ace732a..64766f254fcd 100644 --- a/gdbsupport/unordered_dense.h +++ b/gdbsupport/unordered_dense.h @@ -1,12 +1,12 @@ ///////////////////////// ankerl::unordered_dense::{map, set} ///////////////////////// // A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion. -// Version 4.4.0 +// Version 4.6.0 // https://github.com/martinus/unordered_dense // // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2022-2023 Martin Leitner-Ankerl +// Copyright (c) 2022-2024 Martin Leitner-Ankerl // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -31,7 +31,7 @@ // see https://semver.org/spec/v2.0.0.html #define ANKERL_UNORDERED_DENSE_VERSION_MAJOR 4 // NOLINT(cppcoreguidelines-macro-usage) incompatible API changes -#define ANKERL_UNORDERED_DENSE_VERSION_MINOR 4 // NOLINT(cppcoreguidelines-macro-usage) backwards compatible functionality +#define ANKERL_UNORDERED_DENSE_VERSION_MINOR 6 // NOLINT(cppcoreguidelines-macro-usage) backwards compatible functionality #define ANKERL_UNORDERED_DENSE_VERSION_PATCH 0 // NOLINT(cppcoreguidelines-macro-usage) backwards compatible bug fixes // API versioning with inline namespace, see https://www.foonathan.net/2018/11/inline-namespaces/ @@ -70,6 +70,17 @@ # define ANKERL_UNORDERED_DENSE_NOINLINE __attribute__((noinline)) #endif +#if defined(__clang__) && defined(__has_attribute) +# if __has_attribute(__no_sanitize__) +# define ANKERL_UNORDERED_DENSE_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK \ + __attribute__((__no_sanitize__("unsigned-integer-overflow"))) +# endif +#endif + +#if !defined(ANKERL_UNORDERED_DENSE_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK) +# define ANKERL_UNORDERED_DENSE_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK +#endif + // defined in unordered_dense.cpp #if !defined(ANKERL_UNORDERED_DENSE_EXPORT) # define ANKERL_UNORDERED_DENSE_EXPORT @@ -98,8 +109,27 @@ # include // for abort # endif -# if defined(__has_include) -# if __has_include() +// includes , which fails to compile if +// targeting GCC >= 13 with the (rewritten) win32 thread model, and +// targeting Windows earlier than Vista (0x600). GCC predefines +// _REENTRANT when using the 'posix' model, and doesn't when using the +// 'win32' model. +# if defined __MINGW64__ && defined __GNUC__ && __GNUC__ >= 13 && !defined _REENTRANT +// _WIN32_WINNT is guaranteed to be defined here because of the +// inclusion above. +# ifndef _WIN32_WINNT +# error "_WIN32_WINNT not defined" +# endif +# if _WIN32_WINNT < 0x600 +# define ANKERL_MEMORY_RESOURCE_IS_BAD() 1 // NOLINT(cppcoreguidelines-macro-usage) +# endif +# endif +# ifndef ANKERL_MEMORY_RESOURCE_IS_BAD +# define ANKERL_MEMORY_RESOURCE_IS_BAD() 0 // NOLINT(cppcoreguidelines-macro-usage) +# endif + +# if defined(__has_include) && !defined(ANKERL_UNORDERED_DENSE_DISABLE_PMR) +# if __has_include() && !ANKERL_MEMORY_RESOURCE_IS_BAD() # define ANKERL_UNORDERED_DENSE_PMR std::pmr // NOLINT(cppcoreguidelines-macro-usage) # include // for polymorphic_allocator # elif __has_include() @@ -113,12 +143,23 @@ # pragma intrinsic(_umul128) # endif -# if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) -# define ANKERL_UNORDERED_DENSE_LIKELY(x) __builtin_expect(x, 1) // NOLINT(cppcoreguidelines-macro-usage) -# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) __builtin_expect(x, 0) // NOLINT(cppcoreguidelines-macro-usage) +# if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) && ANKERL_UNORDERED_DENSE_CPP_VERSION >= 202002L +# define ANKERL_UNORDERED_DENSE_LIKELY_ATTR [[likely]] // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR [[unlikely]] // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_LIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage) # else -# define ANKERL_UNORDERED_DENSE_LIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage) -# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_LIKELY_ATTR // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR // NOLINT(cppcoreguidelines-macro-usage) + +# if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) +# define ANKERL_UNORDERED_DENSE_LIKELY(x) __builtin_expect(x, 1) // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) __builtin_expect(x, 0) // NOLINT(cppcoreguidelines-macro-usage) +# else +# define ANKERL_UNORDERED_DENSE_LIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage) +# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage) +# endif + # endif namespace ankerl::unordered_dense { @@ -226,36 +267,44 @@ inline void mum(uint64_t* a, uint64_t* b) { uint64_t seed = secret[0]; uint64_t a{}; uint64_t b{}; - if (ANKERL_UNORDERED_DENSE_LIKELY(len <= 16)) { - if (ANKERL_UNORDERED_DENSE_LIKELY(len >= 4)) { - a = (r4(p) << 32U) | r4(p + ((len >> 3U) << 2U)); - b = (r4(p + len - 4) << 32U) | r4(p + len - 4 - ((len >> 3U) << 2U)); - } else if (ANKERL_UNORDERED_DENSE_LIKELY(len > 0)) { - a = r3(p, len); - b = 0; - } else { - a = 0; - b = 0; + if (ANKERL_UNORDERED_DENSE_LIKELY(len <= 16)) + ANKERL_UNORDERED_DENSE_LIKELY_ATTR { + if (ANKERL_UNORDERED_DENSE_LIKELY(len >= 4)) + ANKERL_UNORDERED_DENSE_LIKELY_ATTR { + a = (r4(p) << 32U) | r4(p + ((len >> 3U) << 2U)); + b = (r4(p + len - 4) << 32U) | r4(p + len - 4 - ((len >> 3U) << 2U)); + } + else if (ANKERL_UNORDERED_DENSE_LIKELY(len > 0)) + ANKERL_UNORDERED_DENSE_LIKELY_ATTR { + a = r3(p, len); + b = 0; + } + else { + a = 0; + b = 0; + } } - } else { + else { size_t i = len; - if (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 48)) { - uint64_t see1 = seed; - uint64_t see2 = seed; - do { + if (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 48)) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { + uint64_t see1 = seed; + uint64_t see2 = seed; + do { + seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed); + see1 = mix(r8(p + 16) ^ secret[2], r8(p + 24) ^ see1); + see2 = mix(r8(p + 32) ^ secret[3], r8(p + 40) ^ see2); + p += 48; + i -= 48; + } while (ANKERL_UNORDERED_DENSE_LIKELY(i > 48)); + seed ^= see1 ^ see2; + } + while (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 16)) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed); - see1 = mix(r8(p + 16) ^ secret[2], r8(p + 24) ^ see1); - see2 = mix(r8(p + 32) ^ secret[3], r8(p + 40) ^ see2); - p += 48; - i -= 48; - } while (ANKERL_UNORDERED_DENSE_LIKELY(i > 48)); - seed ^= see1 ^ see2; - } - while (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 16)) { - seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed); - i -= 16; - p += 16; - } + i -= 16; + p += 16; + } a = r8(p + i - 16); b = r8(p + i - 8); } @@ -267,7 +316,7 @@ inline void mum(uint64_t* a, uint64_t* b) { return detail::wyhash::mix(x, UINT64_C(0x9E3779B97F4A7C15)); } -} /* namespace detail::wyhash */ +} // namespace detail::wyhash ANKERL_UNORDERED_DENSE_EXPORT template struct hash { @@ -277,6 +326,15 @@ struct hash { } }; +template +struct hash::is_avalanching> { + using is_avalanching = void; + auto operator()(T const& obj) const noexcept(noexcept(std::declval>().operator()(std::declval()))) + -> uint64_t { + return std::hash{}(obj); + } +}; + template struct hash> { using is_avalanching = void; @@ -321,10 +379,10 @@ struct hash> { }; template -struct hash::value>::type> { +struct hash>> { using is_avalanching = void; auto operator()(Enum e) const noexcept -> uint64_t { - using underlying = typename std::underlying_type_t; + using underlying = std::underlying_type_t; return detail::wyhash::hash(static_cast(e)); } }; @@ -342,7 +400,8 @@ struct tuple_hash_helper { } } - [[nodiscard]] static auto mix64(uint64_t state, uint64_t v) -> uint64_t { + [[nodiscard]] ANKERL_UNORDERED_DENSE_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static auto mix64(uint64_t state, uint64_t v) + -> uint64_t { return detail::wyhash::mix(state + v, uint64_t{0x9ddfea08eb382d69}); } @@ -350,7 +409,7 @@ struct tuple_hash_helper { // not, we hash the object and use this for the array. Size of the array is known at compile time, and memcpy is optimized // away, so filling the buffer is highly efficient. Finally, call wyhash with this buffer. template - [[nodiscard]] static auto calc_hash(T const& t, std::index_sequence) noexcept -> uint64_t { + [[nodiscard]] static auto calc_hash(T const& t, std::index_sequence /*unused*/) noexcept -> uint64_t { auto h = uint64_t{}; ((h = mix64(h, to64(std::get(t)))), ...); return h; @@ -431,11 +490,12 @@ ANKERL_UNORDERED_DENSE_PACK(struct big { size_t m_value_idx; // index into the m_values vector. }); -} /* namespace bucket_type */ +} // namespace bucket_type namespace detail { struct nonesuch {}; +struct default_container_t {}; template class Op, class... Args> struct detector { @@ -492,7 +552,7 @@ struct base_table_type_map { // base type for set doesn't have mapped_type struct base_table_type_set {}; -} /* namespace detail */ +} // namespace detail // Very much like std::deque, but faster for indexing (in most cases). As of now this doesn't implement the full std::vector // API, but merely what's necessary to work as an underlying container for ankerl::unordered_dense::{map, set}. @@ -540,7 +600,7 @@ class segmented_vector { */ template class iter_t { - using ptr_t = typename std::conditional_t; + using ptr_t = std::conditional_t; ptr_t m_data{}; size_t m_idx{}; @@ -549,14 +609,14 @@ class segmented_vector { public: using difference_type = segmented_vector::difference_type; - using value_type = T; - using reference = typename std::conditional_t; - using pointer = typename std::conditional_t; + using value_type = segmented_vector::value_type; + using reference = std::conditional_t; + using pointer = std::conditional_t; using iterator_category = std::forward_iterator_tag; iter_t() noexcept = default; - template ::type> + template > // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions) constexpr iter_t(iter_t const& other) noexcept : m_data(other.m_data) @@ -566,7 +626,7 @@ class segmented_vector { : m_data(data) , m_idx(idx) {} - template ::type> + template > constexpr auto operator=(iter_t const& other) noexcept -> iter_t& { m_data = other.m_data; m_idx = other.m_idx; @@ -578,12 +638,43 @@ class segmented_vector { return *this; } - constexpr auto operator+(difference_type diff) noexcept -> iter_t { + constexpr auto operator++(int) noexcept -> iter_t { + iter_t prev(*this); + this->operator++(); + return prev; + } + + constexpr auto operator--() noexcept -> iter_t& { + --m_idx; + return *this; + } + + constexpr auto operator--(int) noexcept -> iter_t { + iter_t prev(*this); + this->operator--(); + return prev; + } + + [[nodiscard]] constexpr auto operator+(difference_type diff) const noexcept -> iter_t { return {m_data, static_cast(static_cast(m_idx) + diff)}; } + constexpr auto operator+=(difference_type diff) noexcept -> iter_t& { + m_idx += diff; + return *this; + } + + [[nodiscard]] constexpr auto operator-(difference_type diff) const noexcept -> iter_t { + return {m_data, static_cast(static_cast(m_idx) - diff)}; + } + + constexpr auto operator-=(difference_type diff) noexcept -> iter_t& { + m_idx -= diff; + return *this; + } + template - constexpr auto operator-(iter_t const& other) noexcept -> difference_type { + [[nodiscard]] constexpr auto operator-(iter_t const& other) const noexcept -> difference_type { return static_cast(m_idx) - static_cast(other.m_idx); } @@ -596,14 +687,34 @@ class segmented_vector { } template - constexpr auto operator==(iter_t const& o) const noexcept -> bool { + [[nodiscard]] constexpr auto operator==(iter_t const& o) const noexcept -> bool { return m_idx == o.m_idx; } template - constexpr auto operator!=(iter_t const& o) const noexcept -> bool { + [[nodiscard]] constexpr auto operator!=(iter_t const& o) const noexcept -> bool { return !(*this == o); } + + template + [[nodiscard]] constexpr auto operator<(iter_t const& o) const noexcept -> bool { + return m_idx < o.m_idx; + } + + template + [[nodiscard]] constexpr auto operator>(iter_t const& o) const noexcept -> bool { + return o < *this; + } + + template + [[nodiscard]] constexpr auto operator<=(iter_t const& o) const noexcept -> bool { + return !(o < *this); + } + + template + [[nodiscard]] constexpr auto operator>=(iter_t const& o) const noexcept -> bool { + return !(*this < o); + } }; // slow path: need to allocate a new segment every once in a while @@ -614,7 +725,7 @@ class segmented_vector { } // Moves everything from other - void append_everything_from(segmented_vector&& other) { + void append_everything_from(segmented_vector&& other) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved) reserve(size() + other.size()); for (auto&& o : other) { emplace_back(std::move(o)); @@ -640,6 +751,15 @@ class segmented_vector { return (capacity + num_elements_in_block - 1U) / num_elements_in_block; } + void resize_shrink(size_t new_size) { + if constexpr (!std::is_trivially_destructible_v) { + for (size_t ix = new_size; ix < m_size; ++ix) { + operator[](ix).~T(); + } + } + m_size = new_size; + } + public: segmented_vector() = default; @@ -658,7 +778,7 @@ class segmented_vector { } segmented_vector(segmented_vector&& other) noexcept - : segmented_vector(std::move(other), get_allocator()) {} + : segmented_vector(std::move(other), other.get_allocator()) {} segmented_vector(segmented_vector const& other) { append_everything_from(other); @@ -752,6 +872,30 @@ class segmented_vector { } } + void resize(size_t const count) { + if (count < m_size) { + resize_shrink(count); + } else if (count > m_size) { + size_t const new_elems = count - m_size; + reserve(count); + for (size_t ix = 0; ix < new_elems; ++ix) { + emplace_back(); + } + } + } + + void resize(size_t const count, value_type const& value) { + if (count < m_size) { + resize_shrink(count); + } else if (count > m_size) { + size_t const new_elems = count - m_size; + reserve(count); + for (size_t ix = 0; ix < new_elems; ++ix) { + emplace_back(value); + } + } + } + [[nodiscard]] auto get_allocator() const -> allocator_type { return allocator_type{m_blocks.get_allocator()}; } @@ -796,9 +940,10 @@ template class table : public std::conditional_t, base_table_type_map, base_table_type_set> { - using underlying_value_type = typename std::conditional_t, std::pair, Key>; + using underlying_value_type = std::conditional_t, std::pair, Key>; using underlying_container_type = std::conditional_t, std::vector>; @@ -810,7 +955,12 @@ class table : public std::conditional_t, base_table_type_map, bas private: using bucket_alloc = typename std::allocator_traits::template rebind_alloc; - using bucket_alloc_traits = std::allocator_traits; + using default_bucket_container_type = + std::conditional_t, std::vector>; + + using bucket_container_type = std::conditional_t, + default_bucket_container_type, + BucketContainer>; static constexpr uint8_t initial_shifts = 64 - 2; // 2^(64-m_shift) number of buckets static constexpr float default_max_load_factor = 0.8F; @@ -839,9 +989,7 @@ class table : public std::conditional_t, base_table_type_map, bas static_assert(std::is_trivially_copyable_v, "assert we can just memset / memcpy"); value_container_type m_values{}; // Contains all the key-value pairs in one densely stored container. No holes. - using bucket_pointer = typename std::allocator_traits::pointer; - bucket_pointer m_buckets{}; - size_t m_num_buckets = 0; + bucket_container_type m_buckets{}; size_t m_max_bucket_capacity = 0; float m_max_load_factor = default_max_load_factor; Hash m_hash{}; @@ -849,14 +997,21 @@ class table : public std::conditional_t, base_table_type_map, bas uint8_t m_shifts = initial_shifts; [[nodiscard]] auto next(value_idx_type bucket_idx) const -> value_idx_type { - return ANKERL_UNORDERED_DENSE_UNLIKELY(bucket_idx + 1U == m_num_buckets) - ? 0 - : static_cast(bucket_idx + 1U); + if (ANKERL_UNORDERED_DENSE_UNLIKELY(bucket_idx + 1U == bucket_count())) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { + return 0; + } + + return static_cast(bucket_idx + 1U); } // Helper to access bucket through pointer types - [[nodiscard]] static constexpr auto at(bucket_pointer bucket_ptr, size_t offset) -> Bucket& { - return *(bucket_ptr + static_cast::difference_type>(offset)); + [[nodiscard]] static constexpr auto at(bucket_container_type& bucket, size_t offset) -> Bucket& { + return bucket[offset]; + } + + [[nodiscard]] static constexpr auto at(const bucket_container_type& bucket, size_t offset) -> const Bucket& { + return bucket[offset]; } // use the dist_inc and dist_dec functions so that uint16_t types work without warning @@ -946,7 +1101,13 @@ class table : public std::conditional_t, base_table_type_map, bas } else { m_shifts = other.m_shifts; allocate_buckets_from_shift(); - std::memcpy(m_buckets, other.m_buckets, sizeof(Bucket) * bucket_count()); + if constexpr (IsSegmented || !std::is_same_v) { + for (auto i = 0UL; i < bucket_count(); ++i) { + at(m_buckets, i) = at(other.m_buckets, i); + } + } else { + std::memcpy(m_buckets.data(), other.m_buckets.data(), sizeof(Bucket) * bucket_count()); + } } } @@ -958,30 +1119,38 @@ class table : public std::conditional_t, base_table_type_map, bas } void deallocate_buckets() { - auto ba = bucket_alloc(m_values.get_allocator()); - if (nullptr != m_buckets) { - bucket_alloc_traits::deallocate(ba, m_buckets, bucket_count()); - m_buckets = nullptr; - } - m_num_buckets = 0; + m_buckets.clear(); + m_buckets.shrink_to_fit(); m_max_bucket_capacity = 0; } void allocate_buckets_from_shift() { - auto ba = bucket_alloc(m_values.get_allocator()); - m_num_buckets = calc_num_buckets(m_shifts); - m_buckets = bucket_alloc_traits::allocate(ba, m_num_buckets); - if (m_num_buckets == max_bucket_count()) { + auto num_buckets = calc_num_buckets(m_shifts); + if constexpr (IsSegmented || !std::is_same_v) { + if constexpr (has_reserve) { + m_buckets.reserve(num_buckets); + } + for (size_t i = m_buckets.size(); i < num_buckets; ++i) { + m_buckets.emplace_back(); + } + } else { + m_buckets.resize(num_buckets); + } + if (num_buckets == max_bucket_count()) { // reached the maximum, make sure we can use each bucket m_max_bucket_capacity = max_bucket_count(); } else { - m_max_bucket_capacity = static_cast(static_cast(m_num_buckets) * max_load_factor()); + m_max_bucket_capacity = static_cast(static_cast(num_buckets) * max_load_factor()); } } void clear_buckets() { - if (m_buckets != nullptr) { - std::memset(&*m_buckets, 0, sizeof(Bucket) * bucket_count()); + if constexpr (IsSegmented || !std::is_same_v) { + for (auto&& e : m_buckets) { + std::memset(&e, 0, sizeof(e)); + } + } else { + std::memset(m_buckets.data(), 0, sizeof(Bucket) * bucket_count()); } } @@ -1004,7 +1173,9 @@ class table : public std::conditional_t, base_table_type_map, bas on_error_bucket_overflow(); } --m_shifts; - deallocate_buckets(); + if constexpr (!IsSegmented || std::is_same_v) { + deallocate_buckets(); + } allocate_buckets_from_shift(); clear_and_fill_buckets_from_values(); } @@ -1043,7 +1214,7 @@ class table : public std::conditional_t, base_table_type_map, bas } template - auto do_erase_key(K&& key, Op handle_erased_value) -> size_t { + auto do_erase_key(K&& key, Op handle_erased_value) -> size_t { // NOLINT(cppcoreguidelines-missing-std-forward) if (empty()) { return 0; } @@ -1080,9 +1251,11 @@ class table : public std::conditional_t, base_table_type_map, bas m_values.emplace_back(std::forward(args)...); auto value_idx = static_cast(m_values.size() - 1); - if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) { - increase_size(); - } else { + if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { + increase_size(); + } + else { place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx); } @@ -1116,9 +1289,10 @@ class table : public std::conditional_t, base_table_type_map, bas template auto do_find(K const& key) -> iterator { - if (ANKERL_UNORDERED_DENSE_UNLIKELY(empty())) { - return end(); - } + if (ANKERL_UNORDERED_DENSE_UNLIKELY(empty())) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { + return end(); + } auto mh = mixed_hash(key); auto dist_and_fingerprint = dist_and_fingerprint_from_hash(mh); @@ -1161,9 +1335,10 @@ class table : public std::conditional_t, base_table_type_map, bas template , bool> = true> auto do_at(K const& key) -> Q& { - if (auto it = find(key); ANKERL_UNORDERED_DENSE_LIKELY(end() != it)) { - return it->second; - } + if (auto it = find(key); ANKERL_UNORDERED_DENSE_LIKELY(end() != it)) + ANKERL_UNORDERED_DENSE_LIKELY_ATTR { + return it->second; + } on_error_key_not_found(); } @@ -1178,6 +1353,7 @@ class table : public std::conditional_t, base_table_type_map, bas KeyEqual const& equal = KeyEqual(), allocator_type const& alloc_or_container = allocator_type()) : m_values(alloc_or_container) + , m_buckets(alloc_or_container) , m_hash(hash) , m_equal(equal) { if (0 != bucket_count) { @@ -1253,12 +1429,7 @@ class table : public std::conditional_t, base_table_type_map, bas table(std::initializer_list init, size_type bucket_count, Hash const& hash, allocator_type const& alloc) : table(init, bucket_count, hash, KeyEqual(), alloc) {} - ~table() { - if (nullptr != m_buckets) { - auto ba = bucket_alloc(m_values.get_allocator()); - bucket_alloc_traits::deallocate(ba, m_buckets, bucket_count()); - } - } + ~table() = default; auto operator=(table const& other) -> table& { if (&other != this) { @@ -1283,8 +1454,8 @@ class table : public std::conditional_t, base_table_type_map, bas // we can only reuse m_buckets when both maps have the same allocator! if (get_allocator() == other.get_allocator()) { - m_buckets = std::exchange(other.m_buckets, nullptr); - m_num_buckets = std::exchange(other.m_num_buckets, 0); + m_buckets = std::move(other.m_buckets); + other.m_buckets.clear(); m_max_bucket_capacity = std::exchange(other.m_max_bucket_capacity, 0); m_shifts = std::exchange(other.m_shifts, initial_shifts); m_max_load_factor = std::exchange(other.m_max_load_factor, default_max_load_factor); @@ -1417,11 +1588,12 @@ class table : public std::conditional_t, base_table_type_map, bas // nonstandard API: // Discards the internally held container and replaces it with the one passed. Erases non-unique elements. auto replace(value_container_type&& container) { - if (ANKERL_UNORDERED_DENSE_UNLIKELY(container.size() > max_size())) { - on_error_too_many_elements(); - } + if (ANKERL_UNORDERED_DENSE_UNLIKELY(container.size() > max_size())) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { + on_error_too_many_elements(); + } auto shifts = calc_shifts_for_size(container.size()); - if (0 == m_num_buckets || shifts < m_shifts || container.get_allocator() != m_values.get_allocator()) { + if (0 == bucket_count() || shifts < m_shifts || container.get_allocator() != m_values.get_allocator()) { m_shifts = shifts; deallocate_buckets(); allocate_buckets_from_shift(); @@ -1554,10 +1726,12 @@ class table : public std::conditional_t, base_table_type_map, bas // value is new, place the bucket and shift up until we find an empty spot auto value_idx = static_cast(m_values.size() - 1); - if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) { - // increase_size just rehashes all the data we have in m_values - increase_size(); - } else { + if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) + ANKERL_UNORDERED_DENSE_UNLIKELY_ATTR { + // increase_size just rehashes all the data we have in m_values + increase_size(); + } + else { // place element and shift up until we find an empty spot place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx); } @@ -1622,7 +1796,7 @@ class table : public std::conditional_t, base_table_type_map, bas bucket_idx = next(bucket_idx); } - do_erase(bucket_idx, [](value_type&& /*unused*/) { + do_erase(bucket_idx, [](value_type const& /*unused*/) { }); return begin() + static_cast(value_idx_to_remove); } @@ -1678,7 +1852,7 @@ class table : public std::conditional_t, base_table_type_map, bas } auto erase(Key const& key) -> size_t { - return do_erase_key(key, [](value_type&& /*unused*/) { + return do_erase_key(key, [](value_type const& /*unused*/) { }); } @@ -1692,7 +1866,7 @@ class table : public std::conditional_t, base_table_type_map, bas template , bool> = true> auto erase(K&& key) -> size_t { - return do_erase_key(std::forward(key), [](value_type&& /*unused*/) { + return do_erase_key(std::forward(key), [](value_type const& /*unused*/) { }); } @@ -1821,7 +1995,7 @@ class table : public std::conditional_t, base_table_type_map, bas // bucket interface /////////////////////////////////////////////////////// auto bucket_count() const noexcept -> size_t { // NOLINT(modernize-use-nodiscard) - return m_num_buckets; + return m_buckets.size(); } static constexpr auto max_bucket_count() noexcept -> size_t { // NOLINT(modernize-use-nodiscard) @@ -1840,7 +2014,7 @@ class table : public std::conditional_t, base_table_type_map, bas void max_load_factor(float ml) { m_max_load_factor = ml; - if (m_num_buckets != max_bucket_count()) { + if (bucket_count() != max_bucket_count()) { m_max_bucket_capacity = static_cast(static_cast(bucket_count()) * max_load_factor()); } } @@ -1864,7 +2038,7 @@ class table : public std::conditional_t, base_table_type_map, bas m_values.reserve(capa); } auto shifts = calc_shifts_for_size((std::max)(capa, size())); - if (0 == m_num_buckets || shifts < m_shifts) { + if (0 == bucket_count() || shifts < m_shifts) { m_shifts = shifts; deallocate_buckets(); allocate_buckets_from_shift(); @@ -1918,37 +2092,41 @@ class table : public std::conditional_t, base_table_type_map, bas } }; -} /* namespace detail */ +} // namespace detail ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class AllocatorOrContainer = std::allocator>, - class Bucket = bucket_type::standard> -using map = detail::table; + class Bucket = bucket_type::standard, + class BucketContainer = detail::default_container_t> +using map = detail::table; ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class AllocatorOrContainer = std::allocator>, - class Bucket = bucket_type::standard> -using segmented_map = detail::table; + class Bucket = bucket_type::standard, + class BucketContainer = detail::default_container_t> +using segmented_map = detail::table; ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class AllocatorOrContainer = std::allocator, - class Bucket = bucket_type::standard> -using set = detail::table; + class Bucket = bucket_type::standard, + class BucketContainer = detail::default_container_t> +using set = detail::table; ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class AllocatorOrContainer = std::allocator, - class Bucket = bucket_type::standard> -using segmented_set = detail::table; + class Bucket = bucket_type::standard, + class BucketContainer = detail::default_container_t> +using segmented_set = detail::table; # if defined(ANKERL_UNORDERED_DENSE_PMR) @@ -1959,31 +2137,56 @@ ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class Bucket = bucket_type::standard> -using map = - detail::table>, Bucket, false>; +using map = detail::table>, + Bucket, + detail::default_container_t, + false>; ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class Bucket = bucket_type::standard> -using segmented_map = - detail::table>, Bucket, true>; +using segmented_map = detail::table>, + Bucket, + detail::default_container_t, + true>; ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class Bucket = bucket_type::standard> -using set = detail::table, Bucket, false>; +using set = detail::table, + Bucket, + detail::default_container_t, + false>; ANKERL_UNORDERED_DENSE_EXPORT template , class KeyEqual = std::equal_to, class Bucket = bucket_type::standard> -using segmented_set = - detail::table, Bucket, true>; +using segmented_set = detail::table, + Bucket, + detail::default_container_t, + true>; -} /* namespace pmr */ +} // namespace pmr # endif @@ -1992,8 +2195,8 @@ using segmented_set = // deduction guides for alias templates are only possible since C++20 // see https://en.cppreference.com/w/cpp/language/class_template_argument_deduction -} /* namespace ANKERL_UNORDERED_DENSE_NAMESPACE */ -} /* namespace ankerl::unordered_dense */ +} // namespace ANKERL_UNORDERED_DENSE_NAMESPACE +} // namespace ankerl::unordered_dense // std extensions ///////////////////////////////////////////////////////////// @@ -2006,11 +2209,15 @@ ANKERL_UNORDERED_DENSE_EXPORT template // NOLINTNEXTLINE(cert-dcl58-cpp) -auto erase_if(ankerl::unordered_dense::detail::table& map, - Pred pred) -> size_t { - using map_t = ankerl::unordered_dense::detail::table; +auto erase_if( + ankerl::unordered_dense::detail::table& + map, + Pred pred) -> size_t { + using map_t = ankerl::unordered_dense::detail:: + table; // going back to front because erase() invalidates the end iterator auto const old_size = map.size(); @@ -2026,7 +2233,7 @@ auto erase_if(ankerl::unordered_dense::detail::table