29#ifndef ANKERL_UNORDERED_DENSE_H
30#define ANKERL_UNORDERED_DENSE_H
33#define ANKERL_UNORDERED_DENSE_VERSION_MAJOR 4
34#define ANKERL_UNORDERED_DENSE_VERSION_MINOR 4
35#define ANKERL_UNORDERED_DENSE_VERSION_PATCH 0
40#define ANKERL_UNORDERED_DENSE_VERSION_CONCAT1(major, minor, patch) v##major##_##minor##_##patch
42#define ANKERL_UNORDERED_DENSE_VERSION_CONCAT(major, minor, patch) ANKERL_UNORDERED_DENSE_VERSION_CONCAT1(major, minor, patch)
43#define ANKERL_UNORDERED_DENSE_NAMESPACE \
44 ANKERL_UNORDERED_DENSE_VERSION_CONCAT( \
45 ANKERL_UNORDERED_DENSE_VERSION_MAJOR, ANKERL_UNORDERED_DENSE_VERSION_MINOR, ANKERL_UNORDERED_DENSE_VERSION_PATCH)
47#define ANKERL_UNORDERED_DENSE_CPP_VERSION __cplusplus
51# define ANKERL_UNORDERED_DENSE_PACK(decl) decl __attribute__((__packed__))
52#elif defined(_MSC_VER)
54# define ANKERL_UNORDERED_DENSE_PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop))
58#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
59# define ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() 1
61# define ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() 0
64# define ANKERL_UNORDERED_DENSE_NOINLINE __declspec(noinline)
66# define ANKERL_UNORDERED_DENSE_NOINLINE __attribute__((noinline))
70#if !defined(ANKERL_UNORDERED_DENSE_EXPORT)
71# define ANKERL_UNORDERED_DENSE_EXPORT
74#if ANKERL_UNORDERED_DENSE_CPP_VERSION < 201703L
75# error ankerl::unordered_dense requires C++17 or higher
81# include <initializer_list>
88# include <string_view>
90# include <type_traits>
93# if ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() == 0
97# if defined(__has_include)
98# if __has_include(<memory_resource>)
99# define ANKERL_UNORDERED_DENSE_PMR std::pmr
100# include <memory_resource>
101# elif __has_include(<experimental/memory_resource>)
102# define ANKERL_UNORDERED_DENSE_PMR std::experimental::pmr
103# include <experimental/memory_resource>
107# if defined(_MSC_VER) && defined(_M_X64)
109# pragma intrinsic(_umul128)
112# if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
113# define ANKERL_UNORDERED_DENSE_LIKELY(x) __builtin_expect(x, 1)
114# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) __builtin_expect(x, 0)
116# define ANKERL_UNORDERED_DENSE_LIKELY(x) (x)
117# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) (x)
120namespace ankerl::unordered_dense {
121inline namespace ANKERL_UNORDERED_DENSE_NAMESPACE {
125# if ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS()
129[[noreturn]]
inline ANKERL_UNORDERED_DENSE_NOINLINE
void on_error_key_not_found() {
130 throw std::out_of_range(
"ankerl::unordered_dense::map::at(): key not found");
132[[noreturn]]
inline ANKERL_UNORDERED_DENSE_NOINLINE
void on_error_bucket_overflow() {
133 throw std::overflow_error(
"ankerl::unordered_dense: reached max bucket size, cannot increase size");
135[[noreturn]]
inline ANKERL_UNORDERED_DENSE_NOINLINE
void on_error_too_many_elements() {
136 throw std::out_of_range(
"ankerl::unordered_dense::map::replace(): too many elements");
141[[noreturn]]
inline void on_error_key_not_found() {
144[[noreturn]]
inline void on_error_bucket_overflow() {
147[[noreturn]]
inline void on_error_too_many_elements() {
160namespace detail::wyhash {
162inline void mum(uint64_t* a, uint64_t* b) {
163# if defined(__SIZEOF_INT128__)
166 *a =
static_cast<uint64_t
>(r);
167 *b =
static_cast<uint64_t
>(r >> 64U);
168# elif defined(_MSC_VER) && defined(_M_X64)
169 *a = _umul128(*a, *b, b);
171 uint64_t ha = *a >> 32U;
172 uint64_t hb = *b >> 32U;
173 uint64_t la =
static_cast<uint32_t
>(*a);
174 uint64_t lb =
static_cast<uint32_t
>(*b);
177 uint64_t rh = ha * hb;
178 uint64_t rm0 = ha * lb;
179 uint64_t rm1 = hb * la;
180 uint64_t rl = la * lb;
181 uint64_t t = rl + (rm0 << 32U);
182 auto c =
static_cast<uint64_t
>(t < rl);
183 lo = t + (rm1 << 32U);
184 c +=
static_cast<uint64_t
>(lo < t);
185 hi = rh + (rm0 >> 32U) + (rm1 >> 32U) + c;
192[[nodiscard]]
inline auto mix(uint64_t a, uint64_t b) -> uint64_t {
198[[nodiscard]]
inline auto r8(
const uint8_t* p) -> uint64_t {
200 std::memcpy(&v, p, 8U);
204[[nodiscard]]
inline auto r4(
const uint8_t* p) -> uint64_t {
206 std::memcpy(&v, p, 4);
211[[nodiscard]]
inline auto r3(
const uint8_t* p,
size_t k) -> uint64_t {
212 return (
static_cast<uint64_t
>(p[0]) << 16U) | (
static_cast<uint64_t
>(p[k >> 1U]) << 8U) | p[k - 1];
215[[maybe_unused]] [[nodiscard]]
inline auto hash(
void const* key,
size_t len) -> uint64_t {
216 static constexpr auto secret = std::array{UINT64_C(0xa0761d6478bd642f),
217 UINT64_C(0xe7037ed1a0b428db),
218 UINT64_C(0x8ebc6af09c88c6e3),
219 UINT64_C(0x589965cc75374cc3)};
221 auto const* p =
static_cast<uint8_t const*
>(key);
222 uint64_t seed = secret[0];
225 if (ANKERL_UNORDERED_DENSE_LIKELY(len <= 16)) {
226 if (ANKERL_UNORDERED_DENSE_LIKELY(len >= 4)) {
227 a = (r4(p) << 32U) | r4(p + ((len >> 3U) << 2U));
228 b = (r4(p + len - 4) << 32U) | r4(p + len - 4 - ((len >> 3U) << 2U));
229 }
else if (ANKERL_UNORDERED_DENSE_LIKELY(len > 0)) {
238 if (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 48)) {
239 uint64_t see1 = seed;
240 uint64_t see2 = seed;
242 seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed);
243 see1 = mix(r8(p + 16) ^ secret[2], r8(p + 24) ^ see1);
244 see2 = mix(r8(p + 32) ^ secret[3], r8(p + 40) ^ see2);
247 }
while (ANKERL_UNORDERED_DENSE_LIKELY(i > 48));
250 while (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 16)) {
251 seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed);
259 return mix(secret[1] ^ len, mix(a ^ secret[1], b ^ seed));
262[[nodiscard]]
inline auto hash(uint64_t x) -> uint64_t {
263 return detail::wyhash::mix(x, UINT64_C(0x9E3779B97F4A7C15));
268ANKERL_UNORDERED_DENSE_EXPORT
template <
typename T,
typename Enable =
void>
270 auto operator()(T
const& obj)
const
271 noexcept(
noexcept(std::declval<std::hash<T>>().operator()(std::declval<T const&>()))) -> uint64_t {
272 return std::hash<T>{}(obj);
277struct hash<T, typename std::hash<T>::is_avalanching> {
278 using is_avalanching = void;
279 auto operator()(T
const& obj)
const
280 noexcept(
noexcept(std::declval<std::hash<T>>().operator()(std::declval<T const&>()))) -> uint64_t {
281 return std::hash<T>{}(obj);
285template <
typename CharT>
286struct hash<std::basic_string<CharT>> {
287 using is_avalanching = void;
288 auto operator()(std::basic_string<CharT>
const& str)
const noexcept -> uint64_t {
289 return detail::wyhash::hash(str.data(),
sizeof(CharT) * str.size());
293template <
typename CharT>
294struct hash<std::basic_string_view<CharT>> {
295 using is_avalanching = void;
296 auto operator()(std::basic_string_view<CharT>
const& sv)
const noexcept -> uint64_t {
297 return detail::wyhash::hash(sv.data(),
sizeof(CharT) * sv.size());
303 using is_avalanching = void;
304 auto operator()(T* ptr)
const noexcept -> uint64_t {
306 return detail::wyhash::hash(
reinterpret_cast<uintptr_t
>(ptr));
311struct hash<std::unique_ptr<T>> {
312 using is_avalanching = void;
313 auto operator()(std::unique_ptr<T>
const& ptr)
const noexcept -> uint64_t {
315 return detail::wyhash::hash(
reinterpret_cast<uintptr_t
>(ptr.get()));
320struct hash<std::shared_ptr<T>> {
321 using is_avalanching = void;
322 auto operator()(std::shared_ptr<T>
const& ptr)
const noexcept -> uint64_t {
324 return detail::wyhash::hash(
reinterpret_cast<uintptr_t
>(ptr.get()));
328template <
typename Enum>
329struct hash<Enum, typename std::enable_if<std::is_enum<Enum>::value>::type> {
330 using is_avalanching = void;
331 auto operator()(Enum e)
const noexcept -> uint64_t {
332 using underlying =
typename std::underlying_type_t<Enum>;
333 return detail::wyhash::hash(
static_cast<underlying
>(e));
337template <
typename... Args>
338struct tuple_hash_helper {
341 template <
typename Arg>
342 [[nodiscard]]
constexpr static auto to64(Arg
const& arg) -> uint64_t {
343 if constexpr (std::is_integral_v<Arg> || std::is_enum_v<Arg>) {
344 return static_cast<uint64_t
>(arg);
346 return hash<Arg>{}(arg);
350 [[nodiscard]]
static auto mix64(uint64_t state, uint64_t v) -> uint64_t {
351 return detail::wyhash::mix(state + v, uint64_t{0x9ddfea08eb382d69});
357 template <
typename T, std::size_t... Idx>
358 [[nodiscard]]
static auto calc_hash(T
const& t, std::index_sequence<Idx...>)
noexcept -> uint64_t {
360 ((h = mix64(h, to64(std::get<Idx>(t)))), ...);
365template <
typename... Args>
366struct hash<std::tuple<Args...>> : tuple_hash_helper<Args...> {
367 using is_avalanching = void;
368 auto operator()(std::tuple<Args...>
const& t)
const noexcept -> uint64_t {
369 return tuple_hash_helper<Args...>::calc_hash(t, std::index_sequence_for<Args...>{});
373template <
typename A,
typename B>
374struct hash<std::pair<A, B>> : tuple_hash_helper<A, B> {
375 using is_avalanching = void;
376 auto operator()(std::pair<A, B>
const& t)
const noexcept -> uint64_t {
377 return tuple_hash_helper<A, B>::calc_hash(t, std::index_sequence_for<A, B>{});
382# define ANKERL_UNORDERED_DENSE_HASH_STATICCAST(T) \
385 using is_avalanching = void; \
386 auto operator()(T const& obj) const noexcept -> uint64_t { \
387 return detail::wyhash::hash(static_cast<uint64_t>(obj)); \
391# if defined(__GNUC__) && !defined(__clang__)
392# pragma GCC diagnostic push
393# pragma GCC diagnostic ignored "-Wuseless-cast"
396ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
bool);
397ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char);
398ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
signed char);
399ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned char);
400# if ANKERL_UNORDERED_DENSE_CPP_VERSION >= 202002L && defined(__cpp_char8_t)
401ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char8_t);
403ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char16_t);
404ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char32_t);
405ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
wchar_t);
406ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
short);
407ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned short);
408ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
int);
409ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned int);
410ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
long);
411ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
long long);
412ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned long);
413ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned long long);
415# if defined(__GNUC__) && !defined(__clang__)
416# pragma GCC diagnostic pop
421namespace bucket_type {
424 static constexpr uint32_t dist_inc = 1U << 8U;
425 static constexpr uint32_t fingerprint_mask = dist_inc - 1;
427 uint32_t m_dist_and_fingerprint;
428 uint32_t m_value_idx;
431ANKERL_UNORDERED_DENSE_PACK(
struct big {
432 static constexpr uint32_t dist_inc = 1U << 8U;
433 static constexpr uint32_t fingerprint_mask = dist_inc - 1;
435 uint32_t m_dist_and_fingerprint;
444struct default_container_t {};
446template <
class Default,
class AlwaysVoid,
template <
class...>
class Op,
class... Args>
448 using value_t = std::false_type;
449 using type = Default;
452template <
class Default,
template <
class...>
class Op,
class... Args>
453struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
454 using value_t = std::true_type;
455 using type = Op<Args...>;
458template <
template <
class...>
class Op,
class... Args>
459using is_detected =
typename detail::detector<detail::nonesuch, void, Op, Args...>::value_t;
461template <
template <
class...>
class Op,
class... Args>
462constexpr bool is_detected_v = is_detected<Op, Args...>::value;
465using detect_avalanching =
typename T::is_avalanching;
468using detect_is_transparent =
typename T::is_transparent;
471using detect_iterator =
typename T::iterator;
474using detect_reserve =
decltype(std::declval<T&>().reserve(
size_t{}));
478template <
typename Mapped>
479constexpr bool is_map_v = !std::is_void_v<Mapped>;
482template <
typename Hash,
typename KeyEqual>
483constexpr bool is_transparent_v = is_detected_v<detect_is_transparent, Hash> && is_detected_v<detect_is_transparent, KeyEqual>;
486template <
typename From,
typename To1,
typename To2>
487constexpr bool is_neither_convertible_v = !std::is_convertible_v<From, To1> && !std::is_convertible_v<From, To2>;
490constexpr bool has_reserve = is_detected_v<detect_reserve, T>;
494struct base_table_type_map {
495 using mapped_type = T;
499struct base_table_type_set {};
508template <
typename T,
typename Allocator = std::allocator<T>,
size_t MaxSegmentSizeBytes = 4096>
509class segmented_vector {
510 template <
bool IsConst>
514 using allocator_type = Allocator;
515 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
516 using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
517 using difference_type =
typename std::allocator_traits<allocator_type>::difference_type;
518 using value_type = T;
519 using size_type = std::size_t;
520 using reference = T&;
521 using const_reference = T
const&;
522 using iterator = iter_t<false>;
523 using const_iterator = iter_t<true>;
526 using vec_alloc =
typename std::allocator_traits<Allocator>::template rebind_alloc<pointer>;
527 std::vector<pointer, vec_alloc> m_blocks{};
531 static constexpr auto num_bits_closest(
size_t max_val,
size_t s) ->
size_t {
533 while (s << (f + 1) <= max_val) {
539 using self_t = segmented_vector<T, Allocator, MaxSegmentSizeBytes>;
540 static constexpr auto num_bits = num_bits_closest(MaxSegmentSizeBytes,
sizeof(T));
541 static constexpr auto num_elements_in_block = 1U << num_bits;
542 static constexpr auto mask = num_elements_in_block - 1U;
547 template <
bool IsConst>
549 using ptr_t =
typename std::conditional_t<IsConst, segmented_vector::const_pointer const*, segmented_vector::pointer*>;
557 using difference_type = segmented_vector::difference_type;
558 using value_type = T;
559 using reference =
typename std::conditional_t<IsConst, value_type const&, value_type&>;
560 using pointer =
typename std::conditional_t<IsConst, segmented_vector::const_pointer, segmented_vector::pointer>;
561 using iterator_category = std::forward_iterator_tag;
563 iter_t() noexcept = default;
565 template <
bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type>
567 constexpr iter_t(iter_t<OtherIsConst> const& other) noexcept
568 : m_data(other.m_data)
569 , m_idx(other.m_idx) {}
571 constexpr iter_t(ptr_t data,
size_t idx) noexcept
575 template <bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type>
576 constexpr auto operator=(iter_t<OtherIsConst>
const& other)
noexcept -> iter_t& {
577 m_data = other.m_data;
582 constexpr auto operator++() noexcept -> iter_t& {
587 constexpr auto operator+(difference_type diff)
noexcept -> iter_t {
588 return {m_data,
static_cast<size_t>(
static_cast<difference_type
>(m_idx) + diff)};
591 template <
bool OtherIsConst>
592 constexpr auto operator-(iter_t<OtherIsConst>
const& other)
noexcept -> difference_type {
593 return static_cast<difference_type
>(m_idx) -
static_cast<difference_type
>(other.m_idx);
596 constexpr auto operator*() const noexcept -> reference {
597 return m_data[m_idx >> num_bits][m_idx & mask];
600 constexpr auto operator->() const noexcept -> pointer {
601 return &m_data[m_idx >> num_bits][m_idx & mask];
605 constexpr auto operator==(iter_t<O>
const& o)
const noexcept ->
bool {
606 return m_idx == o.m_idx;
610 constexpr auto operator!=(iter_t<O>
const& o)
const noexcept ->
bool {
611 return !(*
this == o);
616 void increase_capacity() {
617 auto ba = Allocator(m_blocks.get_allocator());
618 pointer block = std::allocator_traits<Allocator>::allocate(ba, num_elements_in_block);
619 m_blocks.push_back(block);
623 void append_everything_from(segmented_vector&& other) {
624 reserve(size() + other.size());
625 for (
auto&& o : other) {
626 emplace_back(std::move(o));
631 void append_everything_from(segmented_vector
const& other) {
632 reserve(size() + other.size());
633 for (
auto const& o : other) {
639 auto ba = Allocator(m_blocks.get_allocator());
640 for (
auto ptr : m_blocks) {
641 std::allocator_traits<Allocator>::deallocate(ba, ptr, num_elements_in_block);
645 [[nodiscard]]
static constexpr auto calc_num_blocks_for_capacity(
size_t capacity) {
646 return (capacity + num_elements_in_block - 1U) / num_elements_in_block;
650 segmented_vector() =
default;
653 segmented_vector(Allocator alloc)
654 : m_blocks(vec_alloc(alloc)) {}
656 segmented_vector(segmented_vector&& other, Allocator alloc)
657 : segmented_vector(alloc) {
658 *
this = std::move(other);
661 segmented_vector(segmented_vector
const& other, Allocator alloc)
662 : m_blocks(vec_alloc(alloc)) {
663 append_everything_from(other);
666 segmented_vector(segmented_vector&& other) noexcept
667 : segmented_vector(std::move(other), get_allocator()) {}
669 segmented_vector(segmented_vector
const& other) {
670 append_everything_from(other);
673 auto operator=(segmented_vector
const& other) -> segmented_vector& {
674 if (
this == &other) {
678 append_everything_from(other);
682 auto operator=(segmented_vector&& other)
noexcept -> segmented_vector& {
685 if (other.get_allocator() == get_allocator()) {
686 m_blocks = std::move(other.m_blocks);
687 m_size = std::exchange(other.m_size, {});
690 m_blocks = std::vector<pointer, vec_alloc>(vec_alloc(other.get_allocator()));
691 append_everything_from(std::move(other));
696 ~segmented_vector() {
701 [[nodiscard]]
constexpr auto size() const ->
size_t {
705 [[nodiscard]]
constexpr auto capacity() const ->
size_t {
706 return m_blocks.size() * num_elements_in_block;
710 [[nodiscard]]
constexpr auto operator[](
size_t i)
const noexcept -> T
const& {
711 return m_blocks[i >> num_bits][i & mask];
714 [[nodiscard]]
constexpr auto operator[](
size_t i)
noexcept -> T& {
715 return m_blocks[i >> num_bits][i & mask];
718 [[nodiscard]]
constexpr auto begin() -> iterator {
719 return {m_blocks.data(), 0U};
721 [[nodiscard]]
constexpr auto begin() const -> const_iterator {
722 return {m_blocks.data(), 0U};
724 [[nodiscard]]
constexpr auto cbegin() const -> const_iterator {
725 return {m_blocks.data(), 0U};
728 [[nodiscard]]
constexpr auto end() -> iterator {
729 return {m_blocks.data(), m_size};
731 [[nodiscard]]
constexpr auto end() const -> const_iterator {
732 return {m_blocks.data(), m_size};
734 [[nodiscard]]
constexpr auto cend() const -> const_iterator {
735 return {m_blocks.data(), m_size};
738 [[nodiscard]]
constexpr auto back() -> reference {
739 return operator[](m_size - 1);
741 [[nodiscard]]
constexpr auto back() const -> const_reference {
742 return operator[](m_size - 1);
750 [[nodiscard]]
auto empty()
const {
754 void reserve(
size_t new_capacity) {
755 m_blocks.reserve(calc_num_blocks_for_capacity(new_capacity));
756 while (new_capacity > capacity()) {
761 [[nodiscard]]
auto get_allocator() const -> allocator_type {
762 return allocator_type{m_blocks.get_allocator()};
765 template <
class... Args>
766 auto emplace_back(Args&&... args) -> reference {
767 if (m_size == capacity()) {
770 auto* ptr =
static_cast<void*
>(&operator[](m_size));
771 auto& ref = *
new (ptr) T(std::forward<Args>(args)...);
777 if constexpr (!std::is_trivially_destructible_v<T>) {
778 for (
size_t i = 0, s = size(); i < s; ++i) {
785 void shrink_to_fit() {
786 auto ba = Allocator(m_blocks.get_allocator());
787 auto num_blocks_required = calc_num_blocks_for_capacity(m_size);
788 while (m_blocks.size() > num_blocks_required) {
789 std::allocator_traits<Allocator>::deallocate(ba, m_blocks.back(), num_elements_in_block);
792 m_blocks.shrink_to_fit();
803 class AllocatorOrContainer,
805 class BucketContainer,
807class table :
public std::conditional_t<is_map_v<T>, base_table_type_map<T>, base_table_type_set> {
808 using underlying_value_type =
typename std::conditional_t<is_map_v<T>, std::pair<Key, T>, Key>;
809 using underlying_container_type = std::conditional_t<IsSegmented,
810 segmented_vector<underlying_value_type, AllocatorOrContainer>,
811 std::vector<underlying_value_type, AllocatorOrContainer>>;
814 using value_container_type = std::
815 conditional_t<is_detected_v<detect_iterator, AllocatorOrContainer>, AllocatorOrContainer, underlying_container_type>;
819 typename std::allocator_traits<typename value_container_type::allocator_type>::template rebind_alloc<Bucket>;
820 using default_bucket_container_type =
821 std::conditional_t<IsSegmented, segmented_vector<Bucket, bucket_alloc>, std::vector<Bucket, bucket_alloc>>;
823 using bucket_container_type = std::conditional_t<std::is_same_v<BucketContainer, detail::default_container_t>,
824 default_bucket_container_type,
827 static constexpr uint8_t initial_shifts = 64 - 2;
828 static constexpr float default_max_load_factor = 0.8F;
831 using key_type = Key;
832 using value_type =
typename value_container_type::value_type;
833 using size_type =
typename value_container_type::size_type;
834 using difference_type =
typename value_container_type::difference_type;
836 using key_equal = KeyEqual;
837 using allocator_type =
typename value_container_type::allocator_type;
838 using reference =
typename value_container_type::reference;
839 using const_reference =
typename value_container_type::const_reference;
840 using pointer =
typename value_container_type::pointer;
841 using const_pointer =
typename value_container_type::const_pointer;
842 using const_iterator =
typename value_container_type::const_iterator;
843 using iterator = std::conditional_t<is_map_v<T>,
typename value_container_type::iterator, const_iterator>;
844 using bucket_type = Bucket;
847 using value_idx_type =
decltype(Bucket::m_value_idx);
848 using dist_and_fingerprint_type =
decltype(Bucket::m_dist_and_fingerprint);
850 static_assert(std::is_trivially_destructible_v<Bucket>,
"assert there's no need to call destructor / std::destroy");
851 static_assert(std::is_trivially_copyable_v<Bucket>,
"assert we can just memset / memcpy");
853 value_container_type m_values{};
854 bucket_container_type m_buckets{};
855 size_t m_max_bucket_capacity = 0;
856 float m_max_load_factor = default_max_load_factor;
859 uint8_t m_shifts = initial_shifts;
861 [[nodiscard]]
auto next(value_idx_type bucket_idx)
const -> value_idx_type {
862 return ANKERL_UNORDERED_DENSE_UNLIKELY(bucket_idx + 1U == bucket_count())
864 :
static_cast<value_idx_type
>(bucket_idx + 1U);
868 [[nodiscard]]
static constexpr auto at(bucket_container_type& bucket,
size_t offset) -> Bucket& {
869 return bucket[offset];
872 [[nodiscard]]
static constexpr auto at(
const bucket_container_type& bucket,
size_t offset) ->
const Bucket& {
873 return bucket[offset];
877 [[nodiscard]]
static constexpr auto dist_inc(dist_and_fingerprint_type x) -> dist_and_fingerprint_type {
878 return static_cast<dist_and_fingerprint_type
>(x + Bucket::dist_inc);
881 [[nodiscard]]
static constexpr auto dist_dec(dist_and_fingerprint_type x) -> dist_and_fingerprint_type {
882 return static_cast<dist_and_fingerprint_type
>(x - Bucket::dist_inc);
886 template <
typename K>
887 [[nodiscard]]
constexpr auto mixed_hash(K
const& key)
const -> uint64_t {
888 if constexpr (is_detected_v<detect_avalanching, Hash>) {
890 if constexpr (
sizeof(
decltype(m_hash(key))) <
sizeof(uint64_t)) {
892 return m_hash(key) * UINT64_C(0x9ddfea08eb382d69);
899 return wyhash::hash(m_hash(key));
903 [[nodiscard]]
constexpr auto dist_and_fingerprint_from_hash(uint64_t hash)
const -> dist_and_fingerprint_type {
904 return Bucket::dist_inc | (
static_cast<dist_and_fingerprint_type
>(hash) & Bucket::fingerprint_mask);
907 [[nodiscard]]
constexpr auto bucket_idx_from_hash(uint64_t hash)
const -> value_idx_type {
908 return static_cast<value_idx_type
>(hash >> m_shifts);
911 [[nodiscard]]
static constexpr auto get_key(value_type
const& vt) -> key_type
const& {
912 if constexpr (is_map_v<T>) {
919 template <
typename K>
920 [[nodiscard]]
auto next_while_less(K
const& key)
const -> Bucket {
921 auto hash = mixed_hash(key);
922 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
923 auto bucket_idx = bucket_idx_from_hash(hash);
925 while (dist_and_fingerprint < at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
926 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
927 bucket_idx = next(bucket_idx);
929 return {dist_and_fingerprint, bucket_idx};
932 void place_and_shift_up(Bucket bucket, value_idx_type place) {
933 while (0 != at(m_buckets, place).m_dist_and_fingerprint) {
934 bucket = std::exchange(at(m_buckets, place), bucket);
935 bucket.m_dist_and_fingerprint = dist_inc(bucket.m_dist_and_fingerprint);
938 at(m_buckets, place) = bucket;
941 [[nodiscard]]
static constexpr auto calc_num_buckets(uint8_t shifts) ->
size_t {
942 return (std::min)(max_bucket_count(),
size_t{1} << (64U - shifts));
945 [[nodiscard]]
constexpr auto calc_shifts_for_size(
size_t s)
const -> uint8_t {
946 auto shifts = initial_shifts;
947 while (shifts > 0 &&
static_cast<size_t>(
static_cast<float>(calc_num_buckets(shifts)) * max_load_factor()) < s) {
954 void copy_buckets(table
const& other) {
958 allocate_buckets_from_shift();
961 m_shifts = other.m_shifts;
962 allocate_buckets_from_shift();
963 if constexpr (IsSegmented || !std::is_same_v<BucketContainer, default_container_t>) {
964 for (
auto i = 0UL; i < bucket_count(); ++i) {
965 at(m_buckets, i) = at(other.m_buckets, i);
968 std::memcpy(m_buckets.data(), other.m_buckets.data(),
sizeof(Bucket) * bucket_count());
976 [[nodiscard]]
auto is_full() const ->
bool {
977 return size() > m_max_bucket_capacity;
980 void deallocate_buckets() {
982 m_buckets.shrink_to_fit();
983 m_max_bucket_capacity = 0;
986 void allocate_buckets_from_shift() {
987 auto num_buckets = calc_num_buckets(m_shifts);
988 if constexpr (IsSegmented || !std::is_same_v<BucketContainer, default_container_t>) {
989 if constexpr (has_reserve<bucket_container_type>) {
990 m_buckets.reserve(num_buckets);
992 for (
size_t i = m_buckets.size(); i < num_buckets; ++i) {
993 m_buckets.emplace_back();
996 m_buckets.resize(num_buckets);
998 if (num_buckets == max_bucket_count()) {
1000 m_max_bucket_capacity = max_bucket_count();
1002 m_max_bucket_capacity =
static_cast<value_idx_type
>(
static_cast<float>(num_buckets) * max_load_factor());
1006 void clear_buckets() {
1007 if constexpr (IsSegmented || !std::is_same_v<BucketContainer, default_container_t>) {
1008 for (
auto&& e : m_buckets) {
1009 std::memset(&e, 0,
sizeof(e));
1012 std::memset(m_buckets.data(), 0,
sizeof(Bucket) * bucket_count());
1016 void clear_and_fill_buckets_from_values() {
1018 for (value_idx_type value_idx = 0, end_idx =
static_cast<value_idx_type
>(m_values.size()); value_idx < end_idx;
1020 auto const& key = get_key(m_values[value_idx]);
1021 auto [dist_and_fingerprint, bucket] = next_while_less(key);
1024 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket);
1028 void increase_size() {
1029 if (m_max_bucket_capacity == max_bucket_count()) {
1031 m_values.pop_back();
1032 on_error_bucket_overflow();
1035 if constexpr (!IsSegmented || std::is_same_v<BucketContainer, default_container_t>) {
1036 deallocate_buckets();
1038 allocate_buckets_from_shift();
1039 clear_and_fill_buckets_from_values();
1042 template <
typename Op>
1043 void do_erase(value_idx_type bucket_idx, Op handle_erased_value) {
1044 auto const value_idx_to_remove = at(m_buckets, bucket_idx).m_value_idx;
1047 auto next_bucket_idx = next(bucket_idx);
1048 while (at(m_buckets, next_bucket_idx).m_dist_and_fingerprint >= Bucket::dist_inc * 2) {
1049 at(m_buckets, bucket_idx) = {dist_dec(at(m_buckets, next_bucket_idx).m_dist_and_fingerprint),
1050 at(m_buckets, next_bucket_idx).m_value_idx};
1051 bucket_idx = std::exchange(next_bucket_idx, next(next_bucket_idx));
1053 at(m_buckets, bucket_idx) = {};
1054 handle_erased_value(std::move(m_values[value_idx_to_remove]));
1057 if (value_idx_to_remove != m_values.size() - 1) {
1059 auto& val = m_values[value_idx_to_remove];
1060 val = std::move(m_values.back());
1063 auto mh = mixed_hash(get_key(val));
1064 bucket_idx = bucket_idx_from_hash(mh);
1066 auto const values_idx_back =
static_cast<value_idx_type
>(m_values.size() - 1);
1067 while (values_idx_back != at(m_buckets, bucket_idx).m_value_idx) {
1068 bucket_idx = next(bucket_idx);
1070 at(m_buckets, bucket_idx).m_value_idx = value_idx_to_remove;
1072 m_values.pop_back();
1075 template <
typename K,
typename Op>
1076 auto do_erase_key(K&& key, Op handle_erased_value) ->
size_t {
1081 auto [dist_and_fingerprint, bucket_idx] = next_while_less(key);
1083 while (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
1084 !m_equal(key, get_key(m_values[at(m_buckets, bucket_idx).m_value_idx]))) {
1085 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1086 bucket_idx = next(bucket_idx);
1089 if (dist_and_fingerprint != at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
1092 do_erase(bucket_idx, handle_erased_value);
1096 template <
class K,
class M>
1097 auto do_insert_or_assign(K&& key, M&& mapped) -> std::pair<iterator, bool> {
1098 auto it_isinserted = try_emplace(std::forward<K>(key), std::forward<M>(mapped));
1099 if (!it_isinserted.second) {
1100 it_isinserted.first->second = std::forward<M>(mapped);
1102 return it_isinserted;
1105 template <
typename... Args>
1106 auto do_place_element(dist_and_fingerprint_type dist_and_fingerprint,
1107 value_idx_type bucket_idx,
1108 Args&&... args) -> std::pair<iterator, bool> {
1111 m_values.emplace_back(std::forward<Args>(args)...);
1113 auto value_idx =
static_cast<value_idx_type
>(m_values.size() - 1);
1114 if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) {
1117 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
1121 return {begin() +
static_cast<difference_type
>(value_idx),
true};
1124 template <
typename K,
typename... Args>
1125 auto do_try_emplace(K&& key, Args&&... args) -> std::pair<iterator, bool> {
1126 auto hash = mixed_hash(key);
1127 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1128 auto bucket_idx = bucket_idx_from_hash(hash);
1131 auto* bucket = &at(m_buckets, bucket_idx);
1132 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
1133 if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1134 return {begin() +
static_cast<difference_type
>(bucket->m_value_idx),
false};
1136 }
else if (dist_and_fingerprint > bucket->m_dist_and_fingerprint) {
1137 return do_place_element(dist_and_fingerprint,
1139 std::piecewise_construct,
1140 std::forward_as_tuple(std::forward<K>(key)),
1141 std::forward_as_tuple(std::forward<Args>(args)...));
1143 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1144 bucket_idx = next(bucket_idx);
1148 template <
typename K>
1149 auto do_find(K
const& key) -> iterator {
1150 if (ANKERL_UNORDERED_DENSE_UNLIKELY(empty())) {
1154 auto mh = mixed_hash(key);
1155 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(mh);
1156 auto bucket_idx = bucket_idx_from_hash(mh);
1157 auto* bucket = &at(m_buckets, bucket_idx);
1160 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint && m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1161 return begin() +
static_cast<difference_type
>(bucket->m_value_idx);
1163 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1164 bucket_idx = next(bucket_idx);
1165 bucket = &at(m_buckets, bucket_idx);
1167 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint && m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1168 return begin() +
static_cast<difference_type
>(bucket->m_value_idx);
1170 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1171 bucket_idx = next(bucket_idx);
1172 bucket = &at(m_buckets, bucket_idx);
1175 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
1176 if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1177 return begin() +
static_cast<difference_type
>(bucket->m_value_idx);
1179 }
else if (dist_and_fingerprint > bucket->m_dist_and_fingerprint) {
1182 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1183 bucket_idx = next(bucket_idx);
1184 bucket = &at(m_buckets, bucket_idx);
1188 template <
typename K>
1189 auto do_find(K
const& key)
const -> const_iterator {
1190 return const_cast<table*
>(
this)->do_find(key);
1193 template <
typename K,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1194 auto do_at(K
const& key) -> Q& {
1195 if (
auto it = find(key); ANKERL_UNORDERED_DENSE_LIKELY(end() != it)) {
1198 on_error_key_not_found();
1201 template <
typename K,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1202 auto do_at(K
const& key)
const -> Q
const& {
1203 return const_cast<table*
>(
this)->at(key);
1207 explicit table(
size_t bucket_count,
1208 Hash
const& hash = Hash(),
1209 KeyEqual
const& equal = KeyEqual(),
1210 allocator_type
const& alloc_or_container = allocator_type())
1211 : m_values(alloc_or_container)
1212 , m_buckets(alloc_or_container)
1215 if (0 != bucket_count) {
1216 reserve(bucket_count);
1218 allocate_buckets_from_shift();
1226 table(
size_t bucket_count, allocator_type
const& alloc)
1227 : table(bucket_count, Hash(), KeyEqual(), alloc) {}
1229 table(
size_t bucket_count, Hash
const& hash, allocator_type
const& alloc)
1230 : table(bucket_count, hash, KeyEqual(), alloc) {}
1232 explicit table(allocator_type
const& alloc)
1233 : table(0, Hash(), KeyEqual(), alloc) {}
1235 template <
class InputIt>
1236 table(InputIt first,
1238 size_type bucket_count = 0,
1239 Hash
const& hash = Hash(),
1240 KeyEqual
const& equal = KeyEqual(),
1241 allocator_type
const& alloc = allocator_type())
1242 : table(bucket_count, hash, equal, alloc) {
1243 insert(first, last);
1246 template <
class InputIt>
1247 table(InputIt first, InputIt last, size_type bucket_count, allocator_type
const& alloc)
1248 : table(first, last, bucket_count, Hash(), KeyEqual(), alloc) {}
1250 template <
class InputIt>
1251 table(InputIt first, InputIt last, size_type bucket_count, Hash
const& hash, allocator_type
const& alloc)
1252 : table(first, last, bucket_count, hash, KeyEqual(), alloc) {}
1254 table(table
const& other)
1255 : table(other, other.m_values.get_allocator()) {}
1257 table(table
const& other, allocator_type
const& alloc)
1258 : m_values(other.m_values, alloc)
1259 , m_max_load_factor(other.m_max_load_factor)
1260 , m_hash(other.m_hash)
1261 , m_equal(other.m_equal) {
1262 copy_buckets(other);
1265 table(table&& other) noexcept
1266 : table(std::move(other), other.m_values.get_allocator()) {}
1268 table(table&& other, allocator_type
const& alloc) noexcept
1270 *
this = std::move(other);
1273 table(std::initializer_list<value_type> ilist,
1274 size_t bucket_count = 0,
1275 Hash
const& hash = Hash(),
1276 KeyEqual
const& equal = KeyEqual(),
1277 allocator_type
const& alloc = allocator_type())
1278 : table(bucket_count, hash, equal, alloc) {
1282 table(std::initializer_list<value_type> ilist, size_type bucket_count, allocator_type
const& alloc)
1283 : table(ilist, bucket_count, Hash(), KeyEqual(), alloc) {}
1285 table(std::initializer_list<value_type> init, size_type bucket_count, Hash
const& hash, allocator_type
const& alloc)
1286 : table(init, bucket_count, hash, KeyEqual(), alloc) {}
1290 auto operator=(table
const& other) -> table& {
1291 if (&other !=
this) {
1292 deallocate_buckets();
1293 m_values = other.m_values;
1294 m_max_load_factor = other.m_max_load_factor;
1295 m_hash = other.m_hash;
1296 m_equal = other.m_equal;
1297 m_shifts = initial_shifts;
1298 copy_buckets(other);
1303 auto operator=(table&& other)
noexcept(
noexcept(std::is_nothrow_move_assignable_v<value_container_type> &&
1304 std::is_nothrow_move_assignable_v<Hash> &&
1305 std::is_nothrow_move_assignable_v<KeyEqual>)) -> table& {
1306 if (&other !=
this) {
1307 deallocate_buckets();
1308 m_values = std::move(other.m_values);
1309 other.m_values.clear();
1312 if (get_allocator() == other.get_allocator()) {
1313 m_buckets = std::move(other.m_buckets);
1314 other.m_buckets.clear();
1315 m_max_bucket_capacity = std::exchange(other.m_max_bucket_capacity, 0);
1316 m_shifts = std::exchange(other.m_shifts, initial_shifts);
1317 m_max_load_factor = std::exchange(other.m_max_load_factor, default_max_load_factor);
1318 m_hash = std::exchange(other.m_hash, {});
1319 m_equal = std::exchange(other.m_equal, {});
1320 other.allocate_buckets_from_shift();
1321 other.clear_buckets();
1325 m_max_load_factor = other.m_max_load_factor;
1328 copy_buckets(other);
1330 other.clear_buckets();
1331 m_hash = other.m_hash;
1332 m_equal = other.m_equal;
1339 auto operator=(std::initializer_list<value_type> ilist) -> table& {
1345 auto get_allocator() const noexcept -> allocator_type {
1346 return m_values.get_allocator();
1351 auto begin() noexcept -> iterator {
1352 return m_values.begin();
1355 auto begin() const noexcept -> const_iterator {
1356 return m_values.begin();
1359 auto cbegin() const noexcept -> const_iterator {
1360 return m_values.cbegin();
1363 auto end() noexcept -> iterator {
1364 return m_values.end();
1367 auto cend() const noexcept -> const_iterator {
1368 return m_values.cend();
1371 auto end() const noexcept -> const_iterator {
1372 return m_values.end();
1377 [[nodiscard]]
auto empty() const noexcept ->
bool {
1378 return m_values.empty();
1381 [[nodiscard]]
auto size() const noexcept ->
size_t {
1382 return m_values.size();
1385 [[nodiscard]]
static constexpr auto max_size() noexcept ->
size_t {
1386 if constexpr ((std::numeric_limits<value_idx_type>::max)() == (std::numeric_limits<size_t>::max)()) {
1387 return size_t{1} << (
sizeof(value_idx_type) * 8 - 1);
1389 return size_t{1} << (
sizeof(value_idx_type) * 8);
1400 auto insert(value_type
const& value) -> std::pair<iterator, bool> {
1401 return emplace(value);
1404 auto insert(value_type&& value) -> std::pair<iterator, bool> {
1405 return emplace(std::move(value));
1408 template <
class P, std::enable_if_t<std::is_constructible_v<value_type, P&&>,
bool> = true>
1409 auto insert(P&& value) -> std::pair<iterator, bool> {
1410 return emplace(std::forward<P>(value));
1413 auto insert(const_iterator , value_type
const& value) -> iterator {
1414 return insert(value).first;
1417 auto insert(const_iterator , value_type&& value) -> iterator {
1418 return insert(std::move(value)).first;
1421 template <
class P, std::enable_if_t<std::is_constructible_v<value_type, P&&>,
bool> = true>
1422 auto insert(const_iterator , P&& value) -> iterator {
1423 return insert(std::forward<P>(value)).first;
1426 template <
class InputIt>
1427 void insert(InputIt first, InputIt last) {
1428 while (first != last) {
1434 void insert(std::initializer_list<value_type> ilist) {
1435 insert(ilist.begin(), ilist.end());
1440 auto extract() && -> value_container_type {
1441 return std::move(m_values);
1446 auto replace(value_container_type&& container) {
1447 if (ANKERL_UNORDERED_DENSE_UNLIKELY(container.size() > max_size())) {
1448 on_error_too_many_elements();
1450 auto shifts = calc_shifts_for_size(container.size());
1451 if (0 == bucket_count() || shifts < m_shifts || container.get_allocator() != m_values.get_allocator()) {
1453 deallocate_buckets();
1454 allocate_buckets_from_shift();
1458 m_values = std::move(container);
1461 auto value_idx = value_idx_type{};
1464 while (value_idx !=
static_cast<value_idx_type
>(m_values.size())) {
1465 auto const& key = get_key(m_values[value_idx]);
1467 auto hash = mixed_hash(key);
1468 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1469 auto bucket_idx = bucket_idx_from_hash(hash);
1471 bool key_found =
false;
1473 auto const& bucket = at(m_buckets, bucket_idx);
1474 if (dist_and_fingerprint > bucket.m_dist_and_fingerprint) {
1477 if (dist_and_fingerprint == bucket.m_dist_and_fingerprint &&
1478 m_equal(key, get_key(m_values[bucket.m_value_idx]))) {
1482 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1483 bucket_idx = next(bucket_idx);
1487 if (value_idx !=
static_cast<value_idx_type
>(m_values.size() - 1)) {
1488 m_values[value_idx] = std::move(m_values.back());
1490 m_values.pop_back();
1492 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
1498 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1499 auto insert_or_assign(Key
const& key, M&& mapped) -> std::pair<iterator, bool> {
1500 return do_insert_or_assign(key, std::forward<M>(mapped));
1503 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1504 auto insert_or_assign(Key&& key, M&& mapped) -> std::pair<iterator, bool> {
1505 return do_insert_or_assign(std::move(key), std::forward<M>(mapped));
1508 template <
typename K,
1512 typename KE = KeyEqual,
1513 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1514 auto insert_or_assign(K&& key, M&& mapped) -> std::pair<iterator, bool> {
1515 return do_insert_or_assign(std::forward<K>(key), std::forward<M>(mapped));
1518 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1519 auto insert_or_assign(const_iterator , Key
const& key, M&& mapped) -> iterator {
1520 return do_insert_or_assign(key, std::forward<M>(mapped)).first;
1523 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1524 auto insert_or_assign(const_iterator , Key&& key, M&& mapped) -> iterator {
1525 return do_insert_or_assign(std::move(key), std::forward<M>(mapped)).first;
1528 template <
typename K,
1532 typename KE = KeyEqual,
1533 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1534 auto insert_or_assign(const_iterator , K&& key, M&& mapped) -> iterator {
1535 return do_insert_or_assign(std::forward<K>(key), std::forward<M>(mapped)).first;
1542 typename KE = KeyEqual,
1543 std::enable_if_t<!is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1544 auto emplace(K&& key) -> std::pair<iterator, bool> {
1545 auto hash = mixed_hash(key);
1546 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1547 auto bucket_idx = bucket_idx_from_hash(hash);
1549 while (dist_and_fingerprint <= at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
1550 if (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
1551 m_equal(key, m_values[at(m_buckets, bucket_idx).m_value_idx])) {
1553 return {begin() +
static_cast<difference_type
>(at(m_buckets, bucket_idx).m_value_idx),
false};
1555 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1556 bucket_idx = next(bucket_idx);
1560 return do_place_element(dist_and_fingerprint, bucket_idx, std::forward<K>(key));
1563 template <
class... Args>
1564 auto emplace(Args&&... args) -> std::pair<iterator, bool> {
1567 auto& key = get_key(m_values.emplace_back(std::forward<Args>(args)...));
1568 auto hash = mixed_hash(key);
1569 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1570 auto bucket_idx = bucket_idx_from_hash(hash);
1572 while (dist_and_fingerprint <= at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
1573 if (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
1574 m_equal(key, get_key(m_values[at(m_buckets, bucket_idx).m_value_idx]))) {
1575 m_values.pop_back();
1576 return {begin() +
static_cast<difference_type
>(at(m_buckets, bucket_idx).m_value_idx),
false};
1578 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1579 bucket_idx = next(bucket_idx);
1583 auto value_idx =
static_cast<value_idx_type
>(m_values.size() - 1);
1584 if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) {
1589 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
1591 return {begin() +
static_cast<difference_type
>(value_idx),
true};
1594 template <
class... Args>
1595 auto emplace_hint(const_iterator , Args&&... args) -> iterator {
1596 return emplace(std::forward<Args>(args)...).first;
1599 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1600 auto try_emplace(Key
const& key, Args&&... args) -> std::pair<iterator, bool> {
1601 return do_try_emplace(key, std::forward<Args>(args)...);
1604 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1605 auto try_emplace(Key&& key, Args&&... args) -> std::pair<iterator, bool> {
1606 return do_try_emplace(std::move(key), std::forward<Args>(args)...);
1609 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1610 auto try_emplace(const_iterator , Key
const& key, Args&&... args) -> iterator {
1611 return do_try_emplace(key, std::forward<Args>(args)...).first;
1614 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1615 auto try_emplace(const_iterator , Key&& key, Args&&... args) -> iterator {
1616 return do_try_emplace(std::move(key), std::forward<Args>(args)...).first;
1624 typename KE = KeyEqual,
1625 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE> && is_neither_convertible_v<K&&, iterator, const_iterator>,
1627 auto try_emplace(K&& key, Args&&... args) -> std::pair<iterator, bool> {
1628 return do_try_emplace(std::forward<K>(key), std::forward<Args>(args)...);
1636 typename KE = KeyEqual,
1637 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE> && is_neither_convertible_v<K&&, iterator, const_iterator>,
1639 auto try_emplace(const_iterator , K&& key, Args&&... args) -> iterator {
1640 return do_try_emplace(std::forward<K>(key), std::forward<Args>(args)...).first;
1643 auto erase(iterator it) -> iterator {
1644 auto hash = mixed_hash(get_key(*it));
1645 auto bucket_idx = bucket_idx_from_hash(hash);
1647 auto const value_idx_to_remove =
static_cast<value_idx_type
>(it - cbegin());
1648 while (at(m_buckets, bucket_idx).m_value_idx != value_idx_to_remove) {
1649 bucket_idx = next(bucket_idx);
1652 do_erase(bucket_idx, [](value_type&& ) {
1654 return begin() +
static_cast<difference_type
>(value_idx_to_remove);
1657 auto extract(iterator it) -> value_type {
1658 auto hash = mixed_hash(get_key(*it));
1659 auto bucket_idx = bucket_idx_from_hash(hash);
1661 auto const value_idx_to_remove =
static_cast<value_idx_type
>(it - cbegin());
1662 while (at(m_buckets, bucket_idx).m_value_idx != value_idx_to_remove) {
1663 bucket_idx = next(bucket_idx);
1666 auto tmp = std::optional<value_type>{};
1667 do_erase(bucket_idx, [&tmp](value_type&& val) {
1668 tmp = std::move(val);
1670 return std::move(tmp).value();
1673 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1674 auto erase(const_iterator it) -> iterator {
1675 return erase(begin() + (it - cbegin()));
1678 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1679 auto extract(const_iterator it) -> value_type {
1680 return extract(begin() + (it - cbegin()));
1683 auto erase(const_iterator first, const_iterator last) -> iterator {
1684 auto const idx_first = first - cbegin();
1685 auto const idx_last = last - cbegin();
1686 auto const first_to_last = std::distance(first, last);
1687 auto const last_to_end = std::distance(last, cend());
1690 auto const mid = idx_first + (std::min)(first_to_last, last_to_end);
1691 auto idx = idx_first;
1692 while (idx != mid) {
1693 erase(begin() + idx);
1699 while (idx != mid) {
1701 erase(begin() + idx);
1704 return begin() + idx_first;
1707 auto erase(Key
const& key) ->
size_t {
1708 return do_erase_key(key, [](value_type&& ) {
1712 auto extract(Key
const& key) -> std::optional<value_type> {
1713 auto tmp = std::optional<value_type>{};
1714 do_erase_key(key, [&tmp](value_type&& val) {
1715 tmp = std::move(val);
1720 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1721 auto erase(K&& key) ->
size_t {
1722 return do_erase_key(std::forward<K>(key), [](value_type&& ) {
1726 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1727 auto extract(K&& key) -> std::optional<value_type> {
1728 auto tmp = std::optional<value_type>{};
1729 do_erase_key(std::forward<K>(key), [&tmp](value_type&& val) {
1730 tmp = std::move(val);
1735 void swap(table& other)
noexcept(
noexcept(std::is_nothrow_swappable_v<value_container_type> &&
1736 std::is_nothrow_swappable_v<Hash> && std::is_nothrow_swappable_v<KeyEqual>)) {
1743 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1744 auto at(key_type
const& key) -> Q& {
1748 template <
typename K,
1751 typename KE = KeyEqual,
1752 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1753 auto at(K
const& key) -> Q& {
1757 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1758 auto at(key_type
const& key)
const -> Q
const& {
1762 template <
typename K,
1765 typename KE = KeyEqual,
1766 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1767 auto at(K
const& key)
const -> Q
const& {
1771 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1772 auto operator[](Key
const& key) -> Q& {
1773 return try_emplace(key).first->second;
1776 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1777 auto operator[](Key&& key) -> Q& {
1778 return try_emplace(std::move(key)).first->second;
1781 template <
typename K,
1784 typename KE = KeyEqual,
1785 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1786 auto operator[](K&& key) -> Q& {
1787 return try_emplace(std::forward<K>(key)).first->second;
1790 auto count(Key
const& key)
const ->
size_t {
1791 return find(key) == end() ? 0 : 1;
1794 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1795 auto count(K
const& key)
const ->
size_t {
1796 return find(key) == end() ? 0 : 1;
1799 auto find(Key
const& key) -> iterator {
1800 return do_find(key);
1803 auto find(Key
const& key)
const -> const_iterator {
1804 return do_find(key);
1807 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1808 auto find(K
const& key) -> iterator {
1809 return do_find(key);
1812 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1813 auto find(K
const& key)
const -> const_iterator {
1814 return do_find(key);
1817 auto contains(Key
const& key)
const ->
bool {
1818 return find(key) != end();
1821 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1822 auto contains(K
const& key)
const ->
bool {
1823 return find(key) != end();
1826 auto equal_range(Key
const& key) -> std::pair<iterator, iterator> {
1827 auto it = do_find(key);
1828 return {it, it == end() ? end() : it + 1};
1831 auto equal_range(
const Key& key)
const -> std::pair<const_iterator, const_iterator> {
1832 auto it = do_find(key);
1833 return {it, it == end() ? end() : it + 1};
1836 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1837 auto equal_range(K
const& key) -> std::pair<iterator, iterator> {
1838 auto it = do_find(key);
1839 return {it, it == end() ? end() : it + 1};
1842 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1843 auto equal_range(K
const& key)
const -> std::pair<const_iterator, const_iterator> {
1844 auto it = do_find(key);
1845 return {it, it == end() ? end() : it + 1};
1850 auto bucket_count() const noexcept ->
size_t {
1851 return m_buckets.size();
1854 static constexpr auto max_bucket_count() noexcept ->
size_t {
1860 [[nodiscard]]
auto load_factor() const ->
float {
1861 return bucket_count() ?
static_cast<float>(size()) /
static_cast<float>(bucket_count()) : 0.0F;
1864 [[nodiscard]]
auto max_load_factor() const ->
float {
1865 return m_max_load_factor;
1868 void max_load_factor(
float ml) {
1869 m_max_load_factor = ml;
1870 if (bucket_count() != max_bucket_count()) {
1871 m_max_bucket_capacity =
static_cast<value_idx_type
>(
static_cast<float>(bucket_count()) * max_load_factor());
1875 void rehash(
size_t count) {
1876 count = (std::min)(count, max_size());
1877 auto shifts = calc_shifts_for_size((std::max)(count, size()));
1878 if (shifts != m_shifts) {
1880 deallocate_buckets();
1881 m_values.shrink_to_fit();
1882 allocate_buckets_from_shift();
1883 clear_and_fill_buckets_from_values();
1887 void reserve(
size_t capa) {
1888 capa = (std::min)(capa, max_size());
1889 if constexpr (has_reserve<value_container_type>) {
1891 m_values.reserve(capa);
1893 auto shifts = calc_shifts_for_size((std::max)(capa, size()));
1894 if (0 == bucket_count() || shifts < m_shifts) {
1896 deallocate_buckets();
1897 allocate_buckets_from_shift();
1898 clear_and_fill_buckets_from_values();
1904 auto hash_function() const -> hasher {
1908 auto key_eq() const -> key_equal {
1913 [[nodiscard]]
auto values() const noexcept -> value_container_type const& {
1919 friend auto operator==(table
const& a, table
const& b) ->
bool {
1923 if (a.size() != b.size()) {
1926 for (
auto const& b_entry : b) {
1927 auto it = a.find(get_key(b_entry));
1928 if constexpr (is_map_v<T>) {
1930 if (a.end() == it || !(b_entry.second == it->second)) {
1935 if (a.end() == it) {
1943 friend auto operator!=(table
const& a, table
const& b) ->
bool {
1950ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1952 class Hash = hash<Key>,
1953 class KeyEqual = std::equal_to<Key>,
1954 class AllocatorOrContainer = std::allocator<std::pair<Key, T>>,
1955 class Bucket = bucket_type::standard,
1956 class BucketContainer = detail::default_container_t>
1957using map = detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, false>;
1959ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1961 class Hash = hash<Key>,
1962 class KeyEqual = std::equal_to<Key>,
1963 class AllocatorOrContainer = std::allocator<std::pair<Key, T>>,
1964 class Bucket = bucket_type::standard,
1965 class BucketContainer = detail::default_container_t>
1966using segmented_map = detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, true>;
1968ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1969 class Hash = hash<Key>,
1970 class KeyEqual = std::equal_to<Key>,
1971 class AllocatorOrContainer = std::allocator<Key>,
1972 class Bucket = bucket_type::standard,
1973 class BucketContainer = detail::default_container_t>
1974using set = detail::table<Key, void, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, false>;
1976ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1977 class Hash = hash<Key>,
1978 class KeyEqual = std::equal_to<Key>,
1979 class AllocatorOrContainer = std::allocator<Key>,
1980 class Bucket = bucket_type::standard,
1981 class BucketContainer = detail::default_container_t>
1982using segmented_set = detail::table<Key, void, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, true>;
1984# if defined(ANKERL_UNORDERED_DENSE_PMR)
1988ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1990 class Hash = hash<Key>,
1991 class KeyEqual = std::equal_to<Key>,
1992 class Bucket = bucket_type::standard>
1993using map = detail::table<Key,
1997 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<std::pair<Key, T>>,
1999 detail::default_container_t,
2002ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2004 class Hash = hash<Key>,
2005 class KeyEqual = std::equal_to<Key>,
2006 class Bucket = bucket_type::standard>
2007using segmented_map = detail::table<Key,
2011 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<std::pair<Key, T>>,
2013 detail::default_container_t,
2016ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2017 class Hash = hash<Key>,
2018 class KeyEqual = std::equal_to<Key>,
2019 class Bucket = bucket_type::standard>
2020using set = detail::table<Key,
2024 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<Key>,
2026 detail::default_container_t,
2029ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2030 class Hash = hash<Key>,
2031 class KeyEqual = std::equal_to<Key>,
2032 class Bucket = bucket_type::standard>
2033using segmented_set = detail::table<Key,
2037 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<Key>,
2039 detail::default_container_t,
2058ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2062 class AllocatorOrContainer,
2065 class BucketContainer,
2069 ankerl::unordered_dense::detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, IsSegmented>&
2071 Pred pred) ->
size_t {
2072 using map_t = ankerl::unordered_dense::detail::
2073 table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, IsSegmented>;
2076 auto const old_size = map.size();
2077 auto idx = old_size;
2080 auto it = map.begin() +
static_cast<typename map_t::difference_type
>(idx);
2086 return old_size - map.size();