31#ifndef ANKERL_UNORDERED_DENSE_H
32#define ANKERL_UNORDERED_DENSE_H
35#define ANKERL_UNORDERED_DENSE_VERSION_MAJOR 4
36#define ANKERL_UNORDERED_DENSE_VERSION_MINOR 5
37#define ANKERL_UNORDERED_DENSE_VERSION_PATCH 0
42#define ANKERL_UNORDERED_DENSE_VERSION_CONCAT1(major, minor, patch) v##major##_##minor##_##patch
44#define ANKERL_UNORDERED_DENSE_VERSION_CONCAT(major, minor, patch) ANKERL_UNORDERED_DENSE_VERSION_CONCAT1(major, minor, patch)
45#define ANKERL_UNORDERED_DENSE_NAMESPACE \
46 ANKERL_UNORDERED_DENSE_VERSION_CONCAT( \
47 ANKERL_UNORDERED_DENSE_VERSION_MAJOR, ANKERL_UNORDERED_DENSE_VERSION_MINOR, ANKERL_UNORDERED_DENSE_VERSION_PATCH)
49#if defined(_MSVC_LANG)
50# define ANKERL_UNORDERED_DENSE_CPP_VERSION _MSVC_LANG
52# define ANKERL_UNORDERED_DENSE_CPP_VERSION __cplusplus
57# define ANKERL_UNORDERED_DENSE_PACK(decl) decl __attribute__((__packed__))
58#elif defined(_MSC_VER)
60# define ANKERL_UNORDERED_DENSE_PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop))
64#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
65# define ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() 1
67# define ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() 0
70# define ANKERL_UNORDERED_DENSE_NOINLINE __declspec(noinline)
72# define ANKERL_UNORDERED_DENSE_NOINLINE __attribute__((noinline))
76#if !defined(ANKERL_UNORDERED_DENSE_EXPORT)
77# define ANKERL_UNORDERED_DENSE_EXPORT
80#if ANKERL_UNORDERED_DENSE_CPP_VERSION < 201703L
81# error ankerl::unordered_dense requires C++17 or higher
87# include <initializer_list>
94# include <string_view>
96# include <type_traits>
99# if ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() == 0
103# if defined(__has_include) && !defined(ANKERL_UNORDERED_DENSE_DISABLE_PMR)
104# if __has_include(<memory_resource>)
105# define ANKERL_UNORDERED_DENSE_PMR std::pmr
106# include <memory_resource>
107# elif __has_include(<experimental/memory_resource>)
108# define ANKERL_UNORDERED_DENSE_PMR std::experimental::pmr
109# include <experimental/memory_resource>
113# if defined(_MSC_VER) && defined(_M_X64)
115# pragma intrinsic(_umul128)
118# if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
119# define ANKERL_UNORDERED_DENSE_LIKELY(x) __builtin_expect(x, 1)
120# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) __builtin_expect(x, 0)
122# define ANKERL_UNORDERED_DENSE_LIKELY(x) (x)
123# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) (x)
126namespace ankerl::unordered_dense {
127inline namespace ANKERL_UNORDERED_DENSE_NAMESPACE {
131# if ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS()
135[[noreturn]]
inline ANKERL_UNORDERED_DENSE_NOINLINE
void on_error_key_not_found() {
136 throw std::out_of_range(
"ankerl::unordered_dense::map::at(): key not found");
138[[noreturn]]
inline ANKERL_UNORDERED_DENSE_NOINLINE
void on_error_bucket_overflow() {
139 throw std::overflow_error(
"ankerl::unordered_dense: reached max bucket size, cannot increase size");
141[[noreturn]]
inline ANKERL_UNORDERED_DENSE_NOINLINE
void on_error_too_many_elements() {
142 throw std::out_of_range(
"ankerl::unordered_dense::map::replace(): too many elements");
147[[noreturn]]
inline void on_error_key_not_found() {
150[[noreturn]]
inline void on_error_bucket_overflow() {
153[[noreturn]]
inline void on_error_too_many_elements() {
166namespace detail::wyhash {
168inline void mum(uint64_t* a, uint64_t* b) {
169# if defined(__SIZEOF_INT128__)
172 *a =
static_cast<uint64_t
>(r);
173 *b =
static_cast<uint64_t
>(r >> 64U);
174# elif defined(_MSC_VER) && defined(_M_X64)
175 *a = _umul128(*a, *b, b);
177 uint64_t ha = *a >> 32U;
178 uint64_t hb = *b >> 32U;
179 uint64_t la =
static_cast<uint32_t
>(*a);
180 uint64_t lb =
static_cast<uint32_t
>(*b);
183 uint64_t rh = ha * hb;
184 uint64_t rm0 = ha * lb;
185 uint64_t rm1 = hb * la;
186 uint64_t rl = la * lb;
187 uint64_t t = rl + (rm0 << 32U);
188 auto c =
static_cast<uint64_t
>(t < rl);
189 lo = t + (rm1 << 32U);
190 c +=
static_cast<uint64_t
>(lo < t);
191 hi = rh + (rm0 >> 32U) + (rm1 >> 32U) + c;
198[[nodiscard]]
inline auto mix(uint64_t a, uint64_t b) -> uint64_t {
204[[nodiscard]]
inline auto r8(
const uint8_t* p) -> uint64_t {
206 std::memcpy(&v, p, 8U);
210[[nodiscard]]
inline auto r4(
const uint8_t* p) -> uint64_t {
212 std::memcpy(&v, p, 4);
217[[nodiscard]]
inline auto r3(
const uint8_t* p,
size_t k) -> uint64_t {
218 return (
static_cast<uint64_t
>(p[0]) << 16U) | (
static_cast<uint64_t
>(p[k >> 1U]) << 8U) | p[k - 1];
221[[maybe_unused]] [[nodiscard]]
inline auto hash(
void const* key,
size_t len) -> uint64_t {
222 static constexpr auto secret = std::array{UINT64_C(0xa0761d6478bd642f),
223 UINT64_C(0xe7037ed1a0b428db),
224 UINT64_C(0x8ebc6af09c88c6e3),
225 UINT64_C(0x589965cc75374cc3)};
227 auto const* p =
static_cast<uint8_t const*
>(key);
228 uint64_t seed = secret[0];
231 if (ANKERL_UNORDERED_DENSE_LIKELY(len <= 16)) {
232 if (ANKERL_UNORDERED_DENSE_LIKELY(len >= 4)) {
233 a = (r4(p) << 32U) | r4(p + ((len >> 3U) << 2U));
234 b = (r4(p + len - 4) << 32U) | r4(p + len - 4 - ((len >> 3U) << 2U));
235 }
else if (ANKERL_UNORDERED_DENSE_LIKELY(len > 0)) {
244 if (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 48)) {
245 uint64_t see1 = seed;
246 uint64_t see2 = seed;
248 seed =
mix(r8(p) ^ secret[1], r8(p + 8) ^ seed);
249 see1 =
mix(r8(p + 16) ^ secret[2], r8(p + 24) ^ see1);
250 see2 =
mix(r8(p + 32) ^ secret[3], r8(p + 40) ^ see2);
253 }
while (ANKERL_UNORDERED_DENSE_LIKELY(i > 48));
256 while (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 16)) {
257 seed =
mix(r8(p) ^ secret[1], r8(p + 8) ^ seed);
265 return mix(secret[1] ^ len,
mix(a ^ secret[1], b ^ seed));
268[[nodiscard]]
inline auto hash(uint64_t x) -> uint64_t {
269 return detail::wyhash::mix(x, UINT64_C(0x9E3779B97F4A7C15));
274ANKERL_UNORDERED_DENSE_EXPORT
template <
typename T,
typename Enable =
void>
276 auto operator()(T
const& obj)
const noexcept(
noexcept(std::declval<std::hash<T>>().
operator()(std::declval<T const&>())))
278 return std::hash<T>{}(obj);
283struct hash<T, typename
std::hash<T>::is_avalanching> {
284 using is_avalanching = void;
285 auto operator()(T
const& obj)
const noexcept(
noexcept(std::declval<std::hash<T>>().
operator()(std::declval<T const&>())))
287 return std::hash<T>{}(obj);
291template <
typename CharT>
292struct hash<
std::basic_string<CharT>> {
293 using is_avalanching = void;
294 auto operator()(std::basic_string<CharT>
const& str)
const noexcept -> uint64_t {
295 return detail::wyhash::hash(str.data(),
sizeof(CharT) * str.size());
299template <
typename CharT>
300struct hash<
std::basic_string_view<CharT>> {
301 using is_avalanching = void;
302 auto operator()(std::basic_string_view<CharT>
const& sv)
const noexcept -> uint64_t {
303 return detail::wyhash::hash(sv.data(),
sizeof(CharT) * sv.size());
309 using is_avalanching = void;
310 auto operator()(T* ptr)
const noexcept -> uint64_t {
312 return detail::wyhash::hash(
reinterpret_cast<uintptr_t
>(ptr));
317struct hash<
std::unique_ptr<T>> {
318 using is_avalanching = void;
319 auto operator()(std::unique_ptr<T>
const& ptr)
const noexcept -> uint64_t {
321 return detail::wyhash::hash(
reinterpret_cast<uintptr_t
>(ptr.get()));
326struct hash<
std::shared_ptr<T>> {
327 using is_avalanching = void;
328 auto operator()(std::shared_ptr<T>
const& ptr)
const noexcept -> uint64_t {
330 return detail::wyhash::hash(
reinterpret_cast<uintptr_t
>(ptr.get()));
334template <
typename Enum>
335struct hash<Enum, typename
std::enable_if<std::is_enum<Enum>::value>::type> {
336 using is_avalanching = void;
337 auto operator()(Enum e)
const noexcept -> uint64_t {
338 using underlying =
typename std::underlying_type_t<Enum>;
339 return detail::wyhash::hash(
static_cast<underlying
>(e));
343template <
typename... Args>
344struct tuple_hash_helper {
347 template <
typename Arg>
348 [[nodiscard]]
constexpr static auto to64(Arg
const& arg) -> uint64_t {
349 if constexpr (std::is_integral_v<Arg> || std::is_enum_v<Arg>) {
350 return static_cast<uint64_t
>(arg);
352 return hash<Arg>{}(arg);
356 [[nodiscard]]
static auto mix64(uint64_t state, uint64_t v) -> uint64_t {
357 return detail::wyhash::mix(state + v, uint64_t{0x9ddfea08eb382d69});
363 template <
typename T,
size_t... Idx>
364 [[nodiscard]]
static auto calc_hash(T
const& t, std::index_sequence<Idx...>)
noexcept -> uint64_t {
366 ((h = mix64(h, to64(std::get<Idx>(t)))), ...);
371template <
typename... Args>
372struct hash<
std::tuple<Args...>> : tuple_hash_helper<Args...> {
373 using is_avalanching = void;
374 auto operator()(std::tuple<Args...>
const& t)
const noexcept -> uint64_t {
375 return tuple_hash_helper<Args...>::calc_hash(t, std::index_sequence_for<Args...>{});
379template <
typename A,
typename B>
380struct hash<
std::pair<A, B>> : tuple_hash_helper<A, B> {
381 using is_avalanching = void;
382 auto operator()(std::pair<A, B>
const& t)
const noexcept -> uint64_t {
383 return tuple_hash_helper<A, B>::calc_hash(t, std::index_sequence_for<A, B>{});
388# define ANKERL_UNORDERED_DENSE_HASH_STATICCAST(T) \
391 using is_avalanching = void; \
392 auto operator()(T const& obj) const noexcept -> uint64_t { \
393 return detail::wyhash::hash(static_cast<uint64_t>(obj)); \
397# if defined(__GNUC__) && !defined(__clang__)
398# pragma GCC diagnostic push
399# pragma GCC diagnostic ignored "-Wuseless-cast"
402ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
bool);
403ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char);
404ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
signed char);
405ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned char);
406# if ANKERL_UNORDERED_DENSE_CPP_VERSION >= 202002L && defined(__cpp_char8_t)
407ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char8_t);
409ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char16_t);
410ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
char32_t);
411ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
wchar_t);
412ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
short);
413ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned short);
414ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
int);
415ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned int);
416ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
long);
417ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
long long);
418ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned long);
419ANKERL_UNORDERED_DENSE_HASH_STATICCAST(
unsigned long long);
421# if defined(__GNUC__) && !defined(__clang__)
422# pragma GCC diagnostic pop
427namespace bucket_type {
430 static constexpr uint32_t dist_inc = 1U << 8U;
431 static constexpr uint32_t fingerprint_mask = dist_inc - 1;
433 uint32_t m_dist_and_fingerprint;
434 uint32_t m_value_idx;
437ANKERL_UNORDERED_DENSE_PACK(
struct big {
438 static constexpr uint32_t dist_inc = 1U << 8U;
439 static constexpr uint32_t fingerprint_mask = dist_inc - 1;
441 uint32_t m_dist_and_fingerprint;
450struct default_container_t {};
452template <
class Default,
class AlwaysVoid,
template <
class...>
class Op,
class... Args>
454 using value_t = std::false_type;
455 using type = Default;
458template <
class Default,
template <
class...>
class Op,
class... Args>
459struct detector<Default,
std::
void_t<Op<Args...>>, Op, Args...> {
460 using value_t = std::true_type;
461 using type = Op<Args...>;
464template <
template <
class...>
class Op,
class... Args>
465using is_detected =
typename detail::detector<detail::nonesuch, void, Op, Args...>::value_t;
467template <
template <
class...>
class Op,
class... Args>
468constexpr bool is_detected_v = is_detected<Op, Args...>::value;
471using detect_avalanching =
typename T::is_avalanching;
474using detect_is_transparent =
typename T::is_transparent;
477using detect_iterator =
typename T::iterator;
480using detect_reserve =
decltype(std::declval<T&>().reserve(
size_t{}));
484template <
typename Mapped>
485constexpr bool is_map_v = !std::is_void_v<Mapped>;
488template <
typename Hash,
typename KeyEqual>
489constexpr bool is_transparent_v = is_detected_v<detect_is_transparent, Hash> && is_detected_v<detect_is_transparent, KeyEqual>;
492template <
typename From,
typename To1,
typename To2>
493constexpr bool is_neither_convertible_v = !std::is_convertible_v<From, To1> && !std::is_convertible_v<From, To2>;
496constexpr bool has_reserve = is_detected_v<detect_reserve, T>;
500struct base_table_type_map {
501 using mapped_type = T;
505struct base_table_type_set {};
514template <
typename T,
typename Allocator = std::allocator<T>,
size_t MaxSegmentSizeBytes = 4096>
515class segmented_vector {
516 template <
bool IsConst>
520 using allocator_type = Allocator;
521 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
522 using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
523 using difference_type =
typename std::allocator_traits<allocator_type>::difference_type;
524 using value_type = T;
525 using size_type = size_t;
526 using reference = T&;
527 using const_reference = T
const&;
528 using iterator = iter_t<false>;
529 using const_iterator = iter_t<true>;
532 using vec_alloc =
typename std::allocator_traits<Allocator>::template rebind_alloc<pointer>;
533 std::vector<pointer, vec_alloc> m_blocks{};
537 static constexpr auto num_bits_closest(
size_t max_val,
size_t s) ->
size_t {
539 while (s << (f + 1) <= max_val) {
545 using self_t = segmented_vector<T, Allocator, MaxSegmentSizeBytes>;
546 static constexpr auto num_bits = num_bits_closest(MaxSegmentSizeBytes,
sizeof(T));
547 static constexpr auto num_elements_in_block = 1U << num_bits;
548 static constexpr auto mask = num_elements_in_block - 1U;
553 template <
bool IsConst>
555 using ptr_t =
typename std::conditional_t<IsConst, segmented_vector::const_pointer const*, segmented_vector::pointer*>;
563 using difference_type = segmented_vector::difference_type;
564 using value_type = T;
565 using reference =
typename std::conditional_t<IsConst, value_type const&, value_type&>;
566 using pointer =
typename std::conditional_t<IsConst, segmented_vector::const_pointer, segmented_vector::pointer>;
567 using iterator_category = std::forward_iterator_tag;
569 iter_t() noexcept = default;
571 template <
bool OtherIsConst, typename = typename
std::enable_if<IsConst && !OtherIsConst>::type>
573 constexpr iter_t(iter_t<OtherIsConst> const& other) noexcept
574 : m_data(other.m_data)
575 , m_idx(other.m_idx) {}
577 constexpr iter_t(ptr_t data,
size_t idx) noexcept
581 template <bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type>
582 constexpr auto operator=(iter_t<OtherIsConst>
const& other)
noexcept -> iter_t& {
583 m_data = other.m_data;
588 constexpr auto operator++() noexcept -> iter_t& {
593 constexpr auto operator++(
int)
noexcept -> iter_t {
599 constexpr auto operator+(difference_type diff)
noexcept -> iter_t {
600 return {m_data,
static_cast<size_t>(
static_cast<difference_type
>(m_idx) + diff)};
603 template <
bool OtherIsConst>
604 constexpr auto operator-(iter_t<OtherIsConst>
const& other)
noexcept -> difference_type {
605 return static_cast<difference_type
>(m_idx) -
static_cast<difference_type
>(other.m_idx);
608 constexpr auto operator*() const noexcept -> reference {
609 return m_data[m_idx >> num_bits][m_idx & mask];
612 constexpr auto operator->() const noexcept -> pointer {
613 return &m_data[m_idx >> num_bits][m_idx & mask];
617 constexpr auto operator==(iter_t<O>
const& o)
const noexcept ->
bool {
618 return m_idx == o.m_idx;
622 constexpr auto operator!=(iter_t<O>
const& o)
const noexcept ->
bool {
623 return !(*
this == o);
628 void increase_capacity() {
629 auto ba = Allocator(m_blocks.get_allocator());
630 pointer block = std::allocator_traits<Allocator>::allocate(ba, num_elements_in_block);
631 m_blocks.push_back(block);
635 void append_everything_from(segmented_vector&& other) {
636 reserve(
size() + other.size());
637 for (
auto&& o : other) {
638 emplace_back(std::move(o));
643 void append_everything_from(segmented_vector
const& other) {
644 reserve(
size() + other.size());
645 for (
auto const& o : other) {
651 auto ba = Allocator(m_blocks.get_allocator());
652 for (
auto ptr : m_blocks) {
653 std::allocator_traits<Allocator>::deallocate(ba, ptr, num_elements_in_block);
657 [[nodiscard]]
static constexpr auto calc_num_blocks_for_capacity(
size_t capacity) {
658 return (capacity + num_elements_in_block - 1U) / num_elements_in_block;
662 segmented_vector() =
default;
665 segmented_vector(Allocator alloc)
666 : m_blocks(vec_alloc(alloc)) {}
668 segmented_vector(segmented_vector&& other, Allocator alloc)
669 : segmented_vector(alloc) {
670 *
this = std::move(other);
673 segmented_vector(segmented_vector
const& other, Allocator alloc)
674 : m_blocks(vec_alloc(alloc)) {
675 append_everything_from(other);
678 segmented_vector(segmented_vector&& other) noexcept
679 : segmented_vector(std::move(other), get_allocator()) {}
681 segmented_vector(segmented_vector
const& other) {
682 append_everything_from(other);
685 auto operator=(segmented_vector
const& other) -> segmented_vector& {
686 if (
this == &other) {
690 append_everything_from(other);
694 auto operator=(segmented_vector&& other)
noexcept -> segmented_vector& {
697 if (other.get_allocator() == get_allocator()) {
698 m_blocks = std::move(other.m_blocks);
699 m_size = std::exchange(other.m_size, {});
702 m_blocks = std::vector<pointer, vec_alloc>(vec_alloc(other.get_allocator()));
703 append_everything_from(std::move(other));
708 ~segmented_vector() {
713 [[nodiscard]]
constexpr auto size() const ->
size_t {
717 [[nodiscard]]
constexpr auto capacity() const ->
size_t {
718 return m_blocks.size() * num_elements_in_block;
722 [[nodiscard]]
constexpr auto operator[](
size_t i)
const noexcept -> T
const& {
723 return m_blocks[i >> num_bits][i & mask];
726 [[nodiscard]]
constexpr auto operator[](
size_t i)
noexcept -> T& {
727 return m_blocks[i >> num_bits][i & mask];
730 [[nodiscard]]
constexpr auto begin() -> iterator {
731 return {m_blocks.data(), 0U};
733 [[nodiscard]]
constexpr auto begin() const -> const_iterator {
734 return {m_blocks.data(), 0U};
736 [[nodiscard]]
constexpr auto cbegin() const -> const_iterator {
737 return {m_blocks.data(), 0U};
740 [[nodiscard]]
constexpr auto end() -> iterator {
741 return {m_blocks.data(), m_size};
743 [[nodiscard]]
constexpr auto end() const -> const_iterator {
744 return {m_blocks.data(), m_size};
746 [[nodiscard]]
constexpr auto cend() const -> const_iterator {
747 return {m_blocks.data(), m_size};
750 [[nodiscard]]
constexpr auto back() -> reference {
751 return operator[](m_size - 1);
753 [[nodiscard]]
constexpr auto back() const -> const_reference {
754 return operator[](m_size - 1);
762 [[nodiscard]]
auto empty()
const {
766 void reserve(
size_t new_capacity) {
767 m_blocks.reserve(calc_num_blocks_for_capacity(new_capacity));
768 while (new_capacity > capacity()) {
773 [[nodiscard]]
auto get_allocator() const -> allocator_type {
774 return allocator_type{m_blocks.get_allocator()};
777 template <
class... Args>
778 auto emplace_back(Args&&... args) -> reference {
779 if (m_size == capacity()) {
782 auto* ptr =
static_cast<void*
>(&operator[](m_size));
783 auto& ref = *
new (ptr) T(std::forward<Args>(args)...);
789 if constexpr (!std::is_trivially_destructible_v<T>) {
790 for (
size_t i = 0, s =
size(); i < s; ++i) {
797 void shrink_to_fit() {
798 auto ba = Allocator(m_blocks.get_allocator());
799 auto num_blocks_required = calc_num_blocks_for_capacity(m_size);
800 while (m_blocks.size() > num_blocks_required) {
801 std::allocator_traits<Allocator>::deallocate(ba, m_blocks.back(), num_elements_in_block);
804 m_blocks.shrink_to_fit();
815 class AllocatorOrContainer,
817 class BucketContainer,
819class table :
public std::conditional_t<is_map_v<T>, base_table_type_map<T>, base_table_type_set> {
820 using underlying_value_type =
typename std::conditional_t<is_map_v<T>, std::pair<Key, T>, Key>;
821 using underlying_container_type = std::conditional_t<IsSegmented,
822 segmented_vector<underlying_value_type, AllocatorOrContainer>,
823 std::vector<underlying_value_type, AllocatorOrContainer>>;
826 using value_container_type = std::
827 conditional_t<is_detected_v<detect_iterator, AllocatorOrContainer>, AllocatorOrContainer, underlying_container_type>;
831 typename std::allocator_traits<typename value_container_type::allocator_type>::template rebind_alloc<Bucket>;
832 using default_bucket_container_type =
833 std::conditional_t<IsSegmented, segmented_vector<Bucket, bucket_alloc>, std::vector<Bucket, bucket_alloc>>;
835 using bucket_container_type = std::conditional_t<std::is_same_v<BucketContainer, detail::default_container_t>,
836 default_bucket_container_type,
839 static constexpr uint8_t initial_shifts = 64 - 2;
840 static constexpr float default_max_load_factor = 0.8F;
843 using key_type = Key;
844 using value_type =
typename value_container_type::value_type;
845 using size_type =
typename value_container_type::size_type;
846 using difference_type =
typename value_container_type::difference_type;
848 using key_equal = KeyEqual;
849 using allocator_type =
typename value_container_type::allocator_type;
850 using reference =
typename value_container_type::reference;
851 using const_reference =
typename value_container_type::const_reference;
852 using pointer =
typename value_container_type::pointer;
853 using const_pointer =
typename value_container_type::const_pointer;
854 using const_iterator =
typename value_container_type::const_iterator;
855 using iterator = std::conditional_t<is_map_v<T>,
typename value_container_type::iterator, const_iterator>;
856 using bucket_type = Bucket;
859 using value_idx_type =
decltype(Bucket::m_value_idx);
860 using dist_and_fingerprint_type =
decltype(Bucket::m_dist_and_fingerprint);
862 static_assert(std::is_trivially_destructible_v<Bucket>,
"assert there's no need to call destructor / std::destroy");
863 static_assert(std::is_trivially_copyable_v<Bucket>,
"assert we can just memset / memcpy");
865 value_container_type m_values{};
866 bucket_container_type m_buckets{};
867 size_t m_max_bucket_capacity = 0;
868 float m_max_load_factor = default_max_load_factor;
871 uint8_t m_shifts = initial_shifts;
873 [[nodiscard]]
auto next(value_idx_type bucket_idx)
const -> value_idx_type {
874 return ANKERL_UNORDERED_DENSE_UNLIKELY(bucket_idx + 1U == bucket_count())
876 :
static_cast<value_idx_type
>(bucket_idx + 1U);
880 [[nodiscard]]
static constexpr auto at(bucket_container_type& bucket,
size_t offset) -> Bucket& {
881 return bucket[offset];
884 [[nodiscard]]
static constexpr auto at(
const bucket_container_type& bucket,
size_t offset) ->
const Bucket& {
885 return bucket[offset];
889 [[nodiscard]]
static constexpr auto dist_inc(dist_and_fingerprint_type x) -> dist_and_fingerprint_type {
890 return static_cast<dist_and_fingerprint_type
>(x + Bucket::dist_inc);
893 [[nodiscard]]
static constexpr auto dist_dec(dist_and_fingerprint_type x) -> dist_and_fingerprint_type {
894 return static_cast<dist_and_fingerprint_type
>(x - Bucket::dist_inc);
898 template <
typename K>
899 [[nodiscard]]
constexpr auto mixed_hash(K
const& key)
const -> uint64_t {
900 if constexpr (is_detected_v<detect_avalanching, Hash>) {
902 if constexpr (
sizeof(
decltype(m_hash(key))) <
sizeof(uint64_t)) {
904 return m_hash(key) * UINT64_C(0x9ddfea08eb382d69);
911 return wyhash::hash(m_hash(key));
915 [[nodiscard]]
constexpr auto dist_and_fingerprint_from_hash(uint64_t hash)
const -> dist_and_fingerprint_type {
916 return Bucket::dist_inc | (
static_cast<dist_and_fingerprint_type
>(hash) & Bucket::fingerprint_mask);
919 [[nodiscard]]
constexpr auto bucket_idx_from_hash(uint64_t hash)
const -> value_idx_type {
920 return static_cast<value_idx_type
>(hash >> m_shifts);
923 [[nodiscard]]
static constexpr auto get_key(value_type
const& vt) -> key_type
const& {
924 if constexpr (is_map_v<T>) {
931 template <
typename K>
932 [[nodiscard]]
auto next_while_less(K
const& key)
const -> Bucket {
933 auto hash = mixed_hash(key);
934 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
935 auto bucket_idx = bucket_idx_from_hash(hash);
937 while (dist_and_fingerprint < at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
938 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
939 bucket_idx = next(bucket_idx);
941 return {dist_and_fingerprint, bucket_idx};
944 void place_and_shift_up(Bucket bucket, value_idx_type place) {
945 while (0 != at(m_buckets, place).m_dist_and_fingerprint) {
946 bucket = std::exchange(at(m_buckets, place), bucket);
947 bucket.m_dist_and_fingerprint = dist_inc(bucket.m_dist_and_fingerprint);
950 at(m_buckets, place) = bucket;
953 [[nodiscard]]
static constexpr auto calc_num_buckets(uint8_t shifts) ->
size_t {
954 return (std::min)(max_bucket_count(),
size_t{1} << (64U - shifts));
957 [[nodiscard]]
constexpr auto calc_shifts_for_size(
size_t s)
const -> uint8_t {
958 auto shifts = initial_shifts;
959 while (shifts > 0 &&
static_cast<size_t>(
static_cast<float>(calc_num_buckets(shifts)) * max_load_factor()) < s) {
966 void copy_buckets(table
const& other) {
970 allocate_buckets_from_shift();
973 m_shifts = other.m_shifts;
974 allocate_buckets_from_shift();
975 if constexpr (IsSegmented || !std::is_same_v<BucketContainer, default_container_t>) {
976 for (
auto i = 0UL; i < bucket_count(); ++i) {
977 at(m_buckets, i) = at(other.m_buckets, i);
980 std::memcpy(m_buckets.data(), other.m_buckets.data(),
sizeof(Bucket) * bucket_count());
988 [[nodiscard]]
auto is_full() const ->
bool {
989 return size() > m_max_bucket_capacity;
992 void deallocate_buckets() {
994 m_buckets.shrink_to_fit();
995 m_max_bucket_capacity = 0;
998 void allocate_buckets_from_shift() {
999 auto num_buckets = calc_num_buckets(m_shifts);
1000 if constexpr (IsSegmented || !std::is_same_v<BucketContainer, default_container_t>) {
1001 if constexpr (has_reserve<bucket_container_type>) {
1002 m_buckets.reserve(num_buckets);
1004 for (
size_t i = m_buckets.size(); i < num_buckets; ++i) {
1005 m_buckets.emplace_back();
1008 m_buckets.resize(num_buckets);
1010 if (num_buckets == max_bucket_count()) {
1012 m_max_bucket_capacity = max_bucket_count();
1014 m_max_bucket_capacity =
static_cast<value_idx_type
>(
static_cast<float>(num_buckets) * max_load_factor());
1018 void clear_buckets() {
1019 if constexpr (IsSegmented || !std::is_same_v<BucketContainer, default_container_t>) {
1020 for (
auto&& e : m_buckets) {
1021 std::memset(&e, 0,
sizeof(e));
1024 std::memset(m_buckets.data(), 0,
sizeof(Bucket) * bucket_count());
1028 void clear_and_fill_buckets_from_values() {
1030 for (value_idx_type value_idx = 0, end_idx =
static_cast<value_idx_type
>(m_values.size()); value_idx < end_idx;
1032 auto const& key = get_key(m_values[value_idx]);
1033 auto [dist_and_fingerprint, bucket] = next_while_less(key);
1036 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket);
1040 void increase_size() {
1041 if (m_max_bucket_capacity == max_bucket_count()) {
1043 m_values.pop_back();
1044 on_error_bucket_overflow();
1047 if constexpr (!IsSegmented || std::is_same_v<BucketContainer, default_container_t>) {
1048 deallocate_buckets();
1050 allocate_buckets_from_shift();
1051 clear_and_fill_buckets_from_values();
1054 template <
typename Op>
1055 void do_erase(value_idx_type bucket_idx, Op handle_erased_value) {
1056 auto const value_idx_to_remove = at(m_buckets, bucket_idx).m_value_idx;
1059 auto next_bucket_idx = next(bucket_idx);
1060 while (at(m_buckets, next_bucket_idx).m_dist_and_fingerprint >= Bucket::dist_inc * 2) {
1061 at(m_buckets, bucket_idx) = {dist_dec(at(m_buckets, next_bucket_idx).m_dist_and_fingerprint),
1062 at(m_buckets, next_bucket_idx).m_value_idx};
1063 bucket_idx = std::exchange(next_bucket_idx, next(next_bucket_idx));
1065 at(m_buckets, bucket_idx) = {};
1066 handle_erased_value(std::move(m_values[value_idx_to_remove]));
1069 if (value_idx_to_remove != m_values.size() - 1) {
1071 auto& val = m_values[value_idx_to_remove];
1072 val = std::move(m_values.back());
1075 auto mh = mixed_hash(get_key(val));
1076 bucket_idx = bucket_idx_from_hash(mh);
1078 auto const values_idx_back =
static_cast<value_idx_type
>(m_values.size() - 1);
1079 while (values_idx_back != at(m_buckets, bucket_idx).m_value_idx) {
1080 bucket_idx = next(bucket_idx);
1082 at(m_buckets, bucket_idx).m_value_idx = value_idx_to_remove;
1084 m_values.pop_back();
1087 template <
typename K,
typename Op>
1088 auto do_erase_key(K&& key, Op handle_erased_value) ->
size_t {
1093 auto [dist_and_fingerprint, bucket_idx] = next_while_less(key);
1095 while (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
1096 !m_equal(key, get_key(m_values[at(m_buckets, bucket_idx).m_value_idx]))) {
1097 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1098 bucket_idx = next(bucket_idx);
1101 if (dist_and_fingerprint != at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
1104 do_erase(bucket_idx, handle_erased_value);
1108 template <
class K,
class M>
1109 auto do_insert_or_assign(K&& key, M&& mapped) -> std::pair<iterator, bool> {
1110 auto it_isinserted = try_emplace(std::forward<K>(key), std::forward<M>(mapped));
1111 if (!it_isinserted.second) {
1112 it_isinserted.first->second = std::forward<M>(mapped);
1114 return it_isinserted;
1117 template <
typename... Args>
1118 auto do_place_element(dist_and_fingerprint_type dist_and_fingerprint, value_idx_type bucket_idx, Args&&... args)
1119 -> std::pair<iterator, bool> {
1122 m_values.emplace_back(std::forward<Args>(args)...);
1124 auto value_idx =
static_cast<value_idx_type
>(m_values.size() - 1);
1125 if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) {
1128 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
1132 return {begin() +
static_cast<difference_type
>(value_idx),
true};
1135 template <
typename K,
typename... Args>
1136 auto do_try_emplace(K&& key, Args&&... args) -> std::pair<iterator, bool> {
1137 auto hash = mixed_hash(key);
1138 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1139 auto bucket_idx = bucket_idx_from_hash(hash);
1142 auto* bucket = &at(m_buckets, bucket_idx);
1143 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
1144 if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1145 return {begin() +
static_cast<difference_type
>(bucket->m_value_idx),
false};
1147 }
else if (dist_and_fingerprint > bucket->m_dist_and_fingerprint) {
1148 return do_place_element(dist_and_fingerprint,
1150 std::piecewise_construct,
1151 std::forward_as_tuple(std::forward<K>(key)),
1152 std::forward_as_tuple(std::forward<Args>(args)...));
1154 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1155 bucket_idx = next(bucket_idx);
1159 template <
typename K>
1160 auto do_find(K
const& key) -> iterator {
1161 if (ANKERL_UNORDERED_DENSE_UNLIKELY(empty())) {
1165 auto mh = mixed_hash(key);
1166 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(mh);
1167 auto bucket_idx = bucket_idx_from_hash(mh);
1168 auto* bucket = &at(m_buckets, bucket_idx);
1171 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint && m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1172 return begin() +
static_cast<difference_type
>(bucket->m_value_idx);
1174 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1175 bucket_idx = next(bucket_idx);
1176 bucket = &at(m_buckets, bucket_idx);
1178 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint && m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1179 return begin() +
static_cast<difference_type
>(bucket->m_value_idx);
1181 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1182 bucket_idx = next(bucket_idx);
1183 bucket = &at(m_buckets, bucket_idx);
1186 if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
1187 if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
1188 return begin() +
static_cast<difference_type
>(bucket->m_value_idx);
1190 }
else if (dist_and_fingerprint > bucket->m_dist_and_fingerprint) {
1193 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1194 bucket_idx = next(bucket_idx);
1195 bucket = &at(m_buckets, bucket_idx);
1199 template <
typename K>
1200 auto do_find(K
const& key)
const -> const_iterator {
1201 return const_cast<table*
>(
this)->do_find(key);
1204 template <
typename K,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1205 auto do_at(K
const& key) -> Q& {
1206 if (
auto it = find(key); ANKERL_UNORDERED_DENSE_LIKELY(end() != it)) {
1209 on_error_key_not_found();
1212 template <
typename K,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1213 auto do_at(K
const& key)
const -> Q
const& {
1214 return const_cast<table*
>(
this)->at(key);
1218 explicit table(
size_t bucket_count,
1219 Hash
const& hash = Hash(),
1220 KeyEqual
const& equal = KeyEqual(),
1221 allocator_type
const& alloc_or_container = allocator_type())
1222 : m_values(alloc_or_container)
1223 , m_buckets(alloc_or_container)
1226 if (0 != bucket_count) {
1227 reserve(bucket_count);
1229 allocate_buckets_from_shift();
1237 table(
size_t bucket_count, allocator_type
const& alloc)
1238 : table(bucket_count, Hash(), KeyEqual(), alloc) {}
1240 table(
size_t bucket_count, Hash
const& hash, allocator_type
const& alloc)
1241 : table(bucket_count, hash, KeyEqual(), alloc) {}
1243 explicit table(allocator_type
const& alloc)
1244 : table(0, Hash(), KeyEqual(), alloc) {}
1246 template <
class InputIt>
1247 table(InputIt first,
1249 size_type bucket_count = 0,
1250 Hash
const& hash = Hash(),
1251 KeyEqual
const& equal = KeyEqual(),
1252 allocator_type
const& alloc = allocator_type())
1253 : table(bucket_count, hash, equal, alloc) {
1254 insert(first, last);
1257 template <
class InputIt>
1258 table(InputIt first, InputIt last, size_type bucket_count, allocator_type
const& alloc)
1259 : table(first, last, bucket_count, Hash(), KeyEqual(), alloc) {}
1261 template <
class InputIt>
1262 table(InputIt first, InputIt last, size_type bucket_count, Hash
const& hash, allocator_type
const& alloc)
1263 : table(first, last, bucket_count, hash, KeyEqual(), alloc) {}
1265 table(table
const& other)
1266 : table(other, other.m_values.get_allocator()) {}
1268 table(table
const& other, allocator_type
const& alloc)
1269 : m_values(other.m_values, alloc)
1270 , m_max_load_factor(other.m_max_load_factor)
1271 , m_hash(other.m_hash)
1272 , m_equal(other.m_equal) {
1273 copy_buckets(other);
1276 table(table&& other) noexcept
1277 : table(std::move(other), other.m_values.get_allocator()) {}
1279 table(table&& other, allocator_type
const& alloc) noexcept
1281 *
this = std::move(other);
1284 table(std::initializer_list<value_type> ilist,
1285 size_t bucket_count = 0,
1286 Hash
const& hash = Hash(),
1287 KeyEqual
const& equal = KeyEqual(),
1288 allocator_type
const& alloc = allocator_type())
1289 : table(bucket_count, hash, equal, alloc) {
1293 table(std::initializer_list<value_type> ilist, size_type bucket_count, allocator_type
const& alloc)
1294 : table(ilist, bucket_count, Hash(), KeyEqual(), alloc) {}
1296 table(std::initializer_list<value_type> init, size_type bucket_count, Hash
const& hash, allocator_type
const& alloc)
1297 : table(init, bucket_count, hash, KeyEqual(), alloc) {}
1301 auto operator=(table
const& other) -> table& {
1302 if (&other !=
this) {
1303 deallocate_buckets();
1304 m_values = other.m_values;
1305 m_max_load_factor = other.m_max_load_factor;
1306 m_hash = other.m_hash;
1307 m_equal = other.m_equal;
1308 m_shifts = initial_shifts;
1309 copy_buckets(other);
1314 auto operator=(table&& other)
noexcept(
noexcept(std::is_nothrow_move_assignable_v<value_container_type> &&
1315 std::is_nothrow_move_assignable_v<Hash> &&
1316 std::is_nothrow_move_assignable_v<KeyEqual>)) -> table& {
1317 if (&other !=
this) {
1318 deallocate_buckets();
1319 m_values = std::move(other.m_values);
1320 other.m_values.clear();
1323 if (get_allocator() == other.get_allocator()) {
1324 m_buckets = std::move(other.m_buckets);
1325 other.m_buckets.clear();
1326 m_max_bucket_capacity = std::exchange(other.m_max_bucket_capacity, 0);
1327 m_shifts = std::exchange(other.m_shifts, initial_shifts);
1328 m_max_load_factor = std::exchange(other.m_max_load_factor, default_max_load_factor);
1329 m_hash = std::exchange(other.m_hash, {});
1330 m_equal = std::exchange(other.m_equal, {});
1331 other.allocate_buckets_from_shift();
1332 other.clear_buckets();
1336 m_max_load_factor = other.m_max_load_factor;
1339 copy_buckets(other);
1341 other.clear_buckets();
1342 m_hash = other.m_hash;
1343 m_equal = other.m_equal;
1350 auto operator=(std::initializer_list<value_type> ilist) -> table& {
1356 auto get_allocator() const noexcept -> allocator_type {
1357 return m_values.get_allocator();
1362 auto begin() noexcept -> iterator {
1363 return m_values.begin();
1366 auto begin() const noexcept -> const_iterator {
1367 return m_values.begin();
1370 auto cbegin() const noexcept -> const_iterator {
1371 return m_values.cbegin();
1374 auto end() noexcept -> iterator {
1375 return m_values.end();
1378 auto cend() const noexcept -> const_iterator {
1379 return m_values.cend();
1382 auto end() const noexcept -> const_iterator {
1383 return m_values.end();
1388 [[nodiscard]]
auto empty() const noexcept ->
bool {
1389 return m_values.empty();
1392 [[nodiscard]]
auto size() const noexcept ->
size_t {
1393 return m_values.size();
1396 [[nodiscard]]
static constexpr auto max_size() noexcept ->
size_t {
1397 if constexpr ((std::numeric_limits<value_idx_type>::max)() == (std::numeric_limits<size_t>::max)()) {
1398 return size_t{1} << (
sizeof(value_idx_type) * 8 - 1);
1400 return size_t{1} << (
sizeof(value_idx_type) * 8);
1411 auto insert(value_type
const& value) -> std::pair<iterator, bool> {
1412 return emplace(value);
1415 auto insert(value_type&& value) -> std::pair<iterator, bool> {
1416 return emplace(std::move(value));
1419 template <
class P, std::enable_if_t<std::is_constructible_v<value_type, P&&>,
bool> = true>
1420 auto insert(P&& value) -> std::pair<iterator, bool> {
1421 return emplace(std::forward<P>(value));
1424 auto insert(const_iterator , value_type
const& value) -> iterator {
1425 return insert(value).first;
1428 auto insert(const_iterator , value_type&& value) -> iterator {
1429 return insert(std::move(value)).first;
1432 template <
class P, std::enable_if_t<std::is_constructible_v<value_type, P&&>,
bool> = true>
1433 auto insert(const_iterator , P&& value) -> iterator {
1434 return insert(std::forward<P>(value)).first;
1437 template <
class InputIt>
1438 void insert(InputIt first, InputIt last) {
1439 while (first != last) {
1445 void insert(std::initializer_list<value_type> ilist) {
1446 insert(ilist.begin(), ilist.end());
1451 auto extract() && -> value_container_type {
1452 return std::move(m_values);
1457 auto replace(value_container_type&& container) {
1458 if (ANKERL_UNORDERED_DENSE_UNLIKELY(container.size() > max_size())) {
1459 on_error_too_many_elements();
1461 auto shifts = calc_shifts_for_size(container.size());
1462 if (0 == bucket_count() || shifts < m_shifts || container.get_allocator() != m_values.get_allocator()) {
1464 deallocate_buckets();
1465 allocate_buckets_from_shift();
1469 m_values = std::move(container);
1472 auto value_idx = value_idx_type{};
1475 while (value_idx !=
static_cast<value_idx_type
>(m_values.size())) {
1476 auto const& key = get_key(m_values[value_idx]);
1478 auto hash = mixed_hash(key);
1479 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1480 auto bucket_idx = bucket_idx_from_hash(hash);
1482 bool key_found =
false;
1484 auto const& bucket = at(m_buckets, bucket_idx);
1485 if (dist_and_fingerprint > bucket.m_dist_and_fingerprint) {
1488 if (dist_and_fingerprint == bucket.m_dist_and_fingerprint &&
1489 m_equal(key, get_key(m_values[bucket.m_value_idx]))) {
1493 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1494 bucket_idx = next(bucket_idx);
1498 if (value_idx !=
static_cast<value_idx_type
>(m_values.size() - 1)) {
1499 m_values[value_idx] = std::move(m_values.back());
1501 m_values.pop_back();
1503 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
1509 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1510 auto insert_or_assign(Key
const& key, M&& mapped) -> std::pair<iterator, bool> {
1511 return do_insert_or_assign(key, std::forward<M>(mapped));
1514 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1515 auto insert_or_assign(Key&& key, M&& mapped) -> std::pair<iterator, bool> {
1516 return do_insert_or_assign(std::move(key), std::forward<M>(mapped));
1519 template <
typename K,
1523 typename KE = KeyEqual,
1524 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1525 auto insert_or_assign(K&& key, M&& mapped) -> std::pair<iterator, bool> {
1526 return do_insert_or_assign(std::forward<K>(key), std::forward<M>(mapped));
1529 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1530 auto insert_or_assign(const_iterator , Key
const& key, M&& mapped) -> iterator {
1531 return do_insert_or_assign(key, std::forward<M>(mapped)).first;
1534 template <
class M,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1535 auto insert_or_assign(const_iterator , Key&& key, M&& mapped) -> iterator {
1536 return do_insert_or_assign(std::move(key), std::forward<M>(mapped)).first;
1539 template <
typename K,
1543 typename KE = KeyEqual,
1544 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1545 auto insert_or_assign(const_iterator , K&& key, M&& mapped) -> iterator {
1546 return do_insert_or_assign(std::forward<K>(key), std::forward<M>(mapped)).first;
1553 typename KE = KeyEqual,
1554 std::enable_if_t<!is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1555 auto emplace(K&& key) -> std::pair<iterator, bool> {
1556 auto hash = mixed_hash(key);
1557 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1558 auto bucket_idx = bucket_idx_from_hash(hash);
1560 while (dist_and_fingerprint <= at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
1561 if (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
1562 m_equal(key, m_values[at(m_buckets, bucket_idx).m_value_idx])) {
1564 return {begin() +
static_cast<difference_type
>(at(m_buckets, bucket_idx).m_value_idx),
false};
1566 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1567 bucket_idx = next(bucket_idx);
1571 return do_place_element(dist_and_fingerprint, bucket_idx, std::forward<K>(key));
1574 template <
class... Args>
1575 auto emplace(Args&&... args) -> std::pair<iterator, bool> {
1578 auto& key = get_key(m_values.emplace_back(std::forward<Args>(args)...));
1579 auto hash = mixed_hash(key);
1580 auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
1581 auto bucket_idx = bucket_idx_from_hash(hash);
1583 while (dist_and_fingerprint <= at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
1584 if (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
1585 m_equal(key, get_key(m_values[at(m_buckets, bucket_idx).m_value_idx]))) {
1586 m_values.pop_back();
1587 return {begin() +
static_cast<difference_type
>(at(m_buckets, bucket_idx).m_value_idx),
false};
1589 dist_and_fingerprint = dist_inc(dist_and_fingerprint);
1590 bucket_idx = next(bucket_idx);
1594 auto value_idx =
static_cast<value_idx_type
>(m_values.size() - 1);
1595 if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) {
1600 place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
1602 return {begin() +
static_cast<difference_type
>(value_idx),
true};
1605 template <
class... Args>
1606 auto emplace_hint(const_iterator , Args&&... args) -> iterator {
1607 return emplace(std::forward<Args>(args)...).first;
1610 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1611 auto try_emplace(Key
const& key, Args&&... args) -> std::pair<iterator, bool> {
1612 return do_try_emplace(key, std::forward<Args>(args)...);
1615 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1616 auto try_emplace(Key&& key, Args&&... args) -> std::pair<iterator, bool> {
1617 return do_try_emplace(std::move(key), std::forward<Args>(args)...);
1620 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1621 auto try_emplace(const_iterator , Key
const& key, Args&&... args) -> iterator {
1622 return do_try_emplace(key, std::forward<Args>(args)...).first;
1625 template <
class... Args,
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> =
true>
1626 auto try_emplace(const_iterator , Key&& key, Args&&... args) -> iterator {
1627 return do_try_emplace(std::move(key), std::forward<Args>(args)...).first;
1635 typename KE = KeyEqual,
1636 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE> && is_neither_convertible_v<K&&, iterator, const_iterator>,
1638 auto try_emplace(K&& key, Args&&... args) -> std::pair<iterator, bool> {
1639 return do_try_emplace(std::forward<K>(key), std::forward<Args>(args)...);
1647 typename KE = KeyEqual,
1648 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE> && is_neither_convertible_v<K&&, iterator, const_iterator>,
1650 auto try_emplace(const_iterator , K&& key, Args&&... args) -> iterator {
1651 return do_try_emplace(std::forward<K>(key), std::forward<Args>(args)...).first;
1654 auto erase(iterator it) -> iterator {
1655 auto hash = mixed_hash(get_key(*it));
1656 auto bucket_idx = bucket_idx_from_hash(hash);
1658 auto const value_idx_to_remove =
static_cast<value_idx_type
>(it - cbegin());
1659 while (at(m_buckets, bucket_idx).m_value_idx != value_idx_to_remove) {
1660 bucket_idx = next(bucket_idx);
1663 do_erase(bucket_idx, [](value_type&& ) {
1665 return begin() +
static_cast<difference_type
>(value_idx_to_remove);
1668 auto extract(iterator it) -> value_type {
1669 auto hash = mixed_hash(get_key(*it));
1670 auto bucket_idx = bucket_idx_from_hash(hash);
1672 auto const value_idx_to_remove =
static_cast<value_idx_type
>(it - cbegin());
1673 while (at(m_buckets, bucket_idx).m_value_idx != value_idx_to_remove) {
1674 bucket_idx = next(bucket_idx);
1677 auto tmp = std::optional<value_type>{};
1678 do_erase(bucket_idx, [&tmp](value_type&& val) {
1679 tmp = std::move(val);
1681 return std::move(tmp).value();
1684 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1685 auto erase(const_iterator it) -> iterator {
1686 return erase(begin() + (it - cbegin()));
1689 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1690 auto extract(const_iterator it) -> value_type {
1691 return extract(begin() + (it - cbegin()));
1694 auto erase(const_iterator first, const_iterator last) -> iterator {
1695 auto const idx_first = first - cbegin();
1696 auto const idx_last = last - cbegin();
1697 auto const first_to_last = std::distance(first, last);
1698 auto const last_to_end = std::distance(last, cend());
1701 auto const mid = idx_first + (std::min)(first_to_last, last_to_end);
1702 auto idx = idx_first;
1703 while (idx != mid) {
1704 erase(begin() + idx);
1710 while (idx != mid) {
1712 erase(begin() + idx);
1715 return begin() + idx_first;
1718 auto erase(Key
const& key) ->
size_t {
1719 return do_erase_key(key, [](value_type&& ) {
1723 auto extract(Key
const& key) -> std::optional<value_type> {
1724 auto tmp = std::optional<value_type>{};
1725 do_erase_key(key, [&tmp](value_type&& val) {
1726 tmp = std::move(val);
1731 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1732 auto erase(K&& key) ->
size_t {
1733 return do_erase_key(std::forward<K>(key), [](value_type&& ) {
1737 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1738 auto extract(K&& key) -> std::optional<value_type> {
1739 auto tmp = std::optional<value_type>{};
1740 do_erase_key(std::forward<K>(key), [&tmp](value_type&& val) {
1741 tmp = std::move(val);
1746 void swap(table& other)
noexcept(
noexcept(std::is_nothrow_swappable_v<value_container_type> &&
1747 std::is_nothrow_swappable_v<Hash> && std::is_nothrow_swappable_v<KeyEqual>)) {
1754 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1755 auto at(key_type
const& key) -> Q& {
1759 template <
typename K,
1762 typename KE = KeyEqual,
1763 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1764 auto at(K
const& key) -> Q& {
1768 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1769 auto at(key_type
const& key)
const -> Q
const& {
1773 template <
typename K,
1776 typename KE = KeyEqual,
1777 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1778 auto at(K
const& key)
const -> Q
const& {
1782 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1783 auto operator[](Key
const& key) -> Q& {
1784 return try_emplace(key).first->second;
1787 template <
typename Q = T, std::enable_if_t<is_map_v<Q>,
bool> = true>
1788 auto operator[](Key&& key) -> Q& {
1789 return try_emplace(std::move(key)).first->second;
1792 template <
typename K,
1795 typename KE = KeyEqual,
1796 std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>,
bool> =
true>
1797 auto operator[](K&& key) -> Q& {
1798 return try_emplace(std::forward<K>(key)).first->second;
1801 auto count(Key
const& key)
const ->
size_t {
1802 return find(key) == end() ? 0 : 1;
1805 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1806 auto count(K
const& key)
const ->
size_t {
1807 return find(key) == end() ? 0 : 1;
1810 auto find(Key
const& key) -> iterator {
1811 return do_find(key);
1814 auto find(Key
const& key)
const -> const_iterator {
1815 return do_find(key);
1818 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1819 auto find(K
const& key) -> iterator {
1820 return do_find(key);
1823 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1824 auto find(K
const& key)
const -> const_iterator {
1825 return do_find(key);
1828 auto contains(Key
const& key)
const ->
bool {
1829 return find(key) != end();
1832 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1833 auto contains(K
const& key)
const ->
bool {
1834 return find(key) != end();
1837 auto equal_range(Key
const& key) -> std::pair<iterator, iterator> {
1838 auto it = do_find(key);
1839 return {it, it == end() ? end() : it + 1};
1842 auto equal_range(
const Key& key)
const -> std::pair<const_iterator, const_iterator> {
1843 auto it = do_find(key);
1844 return {it, it == end() ? end() : it + 1};
1847 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1848 auto equal_range(K
const& key) -> std::pair<iterator, iterator> {
1849 auto it = do_find(key);
1850 return {it, it == end() ? end() : it + 1};
1853 template <
class K,
class H = Hash,
class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>,
bool> = true>
1854 auto equal_range(K
const& key)
const -> std::pair<const_iterator, const_iterator> {
1855 auto it = do_find(key);
1856 return {it, it == end() ? end() : it + 1};
1861 auto bucket_count() const noexcept ->
size_t {
1862 return m_buckets.size();
1865 static constexpr auto max_bucket_count() noexcept ->
size_t {
1871 [[nodiscard]]
auto load_factor() const ->
float {
1872 return bucket_count() ?
static_cast<float>(
size()) /
static_cast<float>(bucket_count()) : 0.0F;
1875 [[nodiscard]]
auto max_load_factor() const ->
float {
1876 return m_max_load_factor;
1879 void max_load_factor(
float ml) {
1880 m_max_load_factor = ml;
1881 if (bucket_count() != max_bucket_count()) {
1882 m_max_bucket_capacity =
static_cast<value_idx_type
>(
static_cast<float>(bucket_count()) * max_load_factor());
1886 void rehash(
size_t count) {
1887 count = (std::min)(count, max_size());
1888 auto shifts = calc_shifts_for_size((std::max)(count,
size()));
1889 if (shifts != m_shifts) {
1891 deallocate_buckets();
1892 m_values.shrink_to_fit();
1893 allocate_buckets_from_shift();
1894 clear_and_fill_buckets_from_values();
1898 void reserve(
size_t capa) {
1899 capa = (std::min)(capa, max_size());
1900 if constexpr (has_reserve<value_container_type>) {
1902 m_values.reserve(capa);
1904 auto shifts = calc_shifts_for_size((std::max)(capa,
size()));
1905 if (0 == bucket_count() || shifts < m_shifts) {
1907 deallocate_buckets();
1908 allocate_buckets_from_shift();
1909 clear_and_fill_buckets_from_values();
1915 auto hash_function() const -> hasher {
1919 auto key_eq() const -> key_equal {
1924 [[nodiscard]]
auto values() const noexcept -> value_container_type const& {
1930 friend auto operator==(table
const& a, table
const& b) ->
bool {
1934 if (a.size() != b.size()) {
1937 for (
auto const& b_entry : b) {
1938 auto it = a.find(get_key(b_entry));
1939 if constexpr (is_map_v<T>) {
1941 if (a.end() == it || !(b_entry.second == it->second)) {
1946 if (a.end() == it) {
1954 friend auto operator!=(table
const& a, table
const& b) ->
bool {
1961ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1963 class Hash = hash<Key>,
1964 class KeyEqual = std::equal_to<Key>,
1965 class AllocatorOrContainer = std::allocator<std::pair<Key, T>>,
1966 class Bucket = bucket_type::standard,
1967 class BucketContainer = detail::default_container_t>
1968using map = detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, false>;
1970ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1972 class Hash = hash<Key>,
1973 class KeyEqual = std::equal_to<Key>,
1974 class AllocatorOrContainer = std::allocator<std::pair<Key, T>>,
1975 class Bucket = bucket_type::standard,
1976 class BucketContainer = detail::default_container_t>
1977using segmented_map = detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, true>;
1979ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1980 class Hash = hash<Key>,
1981 class KeyEqual = std::equal_to<Key>,
1982 class AllocatorOrContainer = std::allocator<Key>,
1983 class Bucket = bucket_type::standard,
1984 class BucketContainer = detail::default_container_t>
1985using set = detail::table<Key, void, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, false>;
1987ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
1988 class Hash = hash<Key>,
1989 class KeyEqual = std::equal_to<Key>,
1990 class AllocatorOrContainer = std::allocator<Key>,
1991 class Bucket = bucket_type::standard,
1992 class BucketContainer = detail::default_container_t>
1993using segmented_set = detail::table<Key, void, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, true>;
1995# if defined(ANKERL_UNORDERED_DENSE_PMR)
1999ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2001 class Hash = hash<Key>,
2002 class KeyEqual = std::equal_to<Key>,
2003 class Bucket = bucket_type::standard>
2004using map = detail::table<Key,
2008 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<std::pair<Key, T>>,
2010 detail::default_container_t,
2013ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2015 class Hash = hash<Key>,
2016 class KeyEqual = std::equal_to<Key>,
2017 class Bucket = bucket_type::standard>
2018using segmented_map = detail::table<Key,
2022 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<std::pair<Key, T>>,
2024 detail::default_container_t,
2027ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2028 class Hash = hash<Key>,
2029 class KeyEqual = std::equal_to<Key>,
2030 class Bucket = bucket_type::standard>
2031using set = detail::table<Key,
2035 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<Key>,
2037 detail::default_container_t,
2040ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2041 class Hash = hash<Key>,
2042 class KeyEqual = std::equal_to<Key>,
2043 class Bucket = bucket_type::standard>
2044using segmented_set = detail::table<Key,
2048 ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<Key>,
2050 detail::default_container_t,
2069ANKERL_UNORDERED_DENSE_EXPORT
template <
class Key,
2073 class AllocatorOrContainer,
2076 class BucketContainer,
2080 ankerl::unordered_dense::detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, IsSegmented>&
2082 Pred pred) ->
size_t {
2083 using map_t = ankerl::unordered_dense::detail::
2084 table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BucketContainer, IsSegmented>;
2087 auto const old_size = map.size();
2088 auto idx = old_size;
2091 auto it = map.begin() +
static_cast<typename map_t::difference_type
>(idx);
2097 return old_size - map.size();
bool operator!=(const Version &lhs, const Version &rhs)
Definition Version.hpp:74
bool operator==(const Version &lhs, const Version &rhs)
Definition Version.hpp:67
constexpr Rect operator-(const Rect &lhs, const Rect &rhs)
Rectangle subtraction operator.
Definition Rect.hpp:371
constexpr t_Vector2< T > operator*(const t_Vector2< T > &lhs, T rhs)
Scalar multiplication.
Definition Vector2.hpp:646
constexpr Rect operator+(const Rect &lhs, const Rect &rhs)
Rectangle addition operator.
Definition Rect.hpp:398
Vector3 INFINITY_API_PUBLIC mix(const Vector3 &a, const Vector3 &b, float t)
Linear interpolation between two 3D vectors (alias for lerp).
constexpr auto size(const C &c) -> decltype(c.size())
Definition Span.hpp:185
void void_t
Definition Span.hpp:225