Replace get_eq_slot_fn with a SlotEq member type in hash policies. Callers only need the stateless slot-equality functor type rather than a constexpr factory function. Expose SlotEq as a member type alias on container policies and hash_policy_traits to support upcoming probe loop deduplication. PiperOrigin-RevId: 1021184303
diff --git a/absl/container/flat_hash_map.h b/absl/container/flat_hash_map.h index 5e18999..a63bb85 100644 --- a/absl/container/flat_hash_map.h +++ b/absl/container/flat_hash_map.h
@@ -686,6 +686,7 @@ using DefaultHash = DefaultHashContainerHash<K>; using DefaultEq = DefaultHashContainerEq<K>; using DefaultAlloc = std::allocator<std::pair<const K, V>>; + using SlotEq = SlotEqFn<K, /*kDerefSlot=*/false>; template <class Allocator, class... Args> static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { @@ -711,10 +712,6 @@ return GetTypeErasedApplyToSlotFn<Hash, K, kIsDefault, kSeedShift>(); } - static constexpr auto get_eq_slot_fn() { - return SlotEqFn<K, /*kDerefSlot=*/false>{}; - } - static std::pair<const K, V>& element(slot_type* slot) { return slot->value; } };
diff --git a/absl/container/flat_hash_set.h b/absl/container/flat_hash_set.h index b4245ec..42e186c 100644 --- a/absl/container/flat_hash_set.h +++ b/absl/container/flat_hash_set.h
@@ -563,6 +563,7 @@ using DefaultHash = DefaultHashContainerHash<T>; using DefaultEq = DefaultHashContainerEq<T>; using DefaultAlloc = std::allocator<T>; + using SlotEq = SlotEqFn<T, /*kDerefSlot=*/false>; template <class Allocator, class... Args> static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { @@ -593,10 +594,6 @@ static constexpr HashSlotFn get_hash_slot_fn() { return GetTypeErasedApplyToSlotFn<Hash, T, kIsAbsl, kSeedShift>(); } - - static constexpr auto get_eq_slot_fn() { - return SlotEqFn<T, /*kDerefSlot=*/false>{}; - } }; } // namespace container_internal
diff --git a/absl/container/internal/hash_policy_traits.h b/absl/container/internal/hash_policy_traits.h index 9ce5d0f..d0c211b 100644 --- a/absl/container/internal/hash_policy_traits.h +++ b/absl/container/internal/hash_policy_traits.h
@@ -67,6 +67,30 @@ struct ConstantIteratorsImpl<P, std::void_t<typename P::constant_iterators>> : P::constant_iterators {}; + // Fallback slot equality functor using `Policy::apply` for custom layouts. + struct EqKeyNonTypeErased { + using SlotKeyType = const key_type; + + template <class Eq, class SearchKey> + bool operator()(const Eq& eq, const void* slot, + const SearchKey& key) const { + return Policy::apply( + EqualElement<SearchKey, Eq>{key, eq}, + hash_policy_traits::element( + static_cast<slot_type*>(const_cast<void*>(slot)))); + } + }; + + template <class P = Policy, class = void> + struct SlotEqImpl { + using type = EqKeyNonTypeErased; + }; + + template <class P> + struct SlotEqImpl<P, std::void_t<typename P::SlotEq>> { + using type = typename P::SlotEq; + }; + public: // The actual object stored in the hash table. using slot_type = typename Policy::slot_type; @@ -86,6 +110,11 @@ // Defaults to false if not provided by the policy. using constant_iterators = ConstantIteratorsImpl<>; + // Functor type for comparing keys with a slot. + // TODO(b/575748507): Require `SlotEq` for all policies and remove + // `SlotEqImpl` and `EqKeyNonTypeErased`. + using SlotEq = typename SlotEqImpl<>::type; + // Returns the amount of memory owned by `slot`, exclusive of `sizeof(*slot)`. // // If `slot` is nullptr, returns the constant amount of memory owned by any @@ -166,40 +195,7 @@ #endif } - // Returns a slot-equality functor for comparing keys with a slot. - // TODO(b/575748507): Require `get_eq_slot_fn` for all policies and remove - // `PolicyEqSlotFn` and `EqKeyNonTypeErased`. - static constexpr auto get_eq_slot_fn() { - auto fn = PolicyEqSlotFn(0); - if constexpr (std::is_null_pointer_v<decltype(fn)>) { - return EqKeyNonTypeErased{}; - } else { - return fn; - } - } - private: - // Returns `Policy::get_eq_slot_fn`, or nullptr if `Policy` doesn't define it. - template <class P = Policy> - static constexpr auto PolicyEqSlotFn(int) -> decltype(P::get_eq_slot_fn()) { - return P::get_eq_slot_fn(); - } - static constexpr std::nullptr_t PolicyEqSlotFn(char) { return nullptr; } - - // Fallback slot equality functor using `Policy::apply` for custom layouts. - struct EqKeyNonTypeErased { - using SlotKeyType = const key_type; - - template <class Eq, class SearchKey> - bool operator()(const Eq& eq, const void* slot, - const SearchKey& key) const { - return Policy::apply( - EqualElement<SearchKey, Eq>{key, eq}, - hash_policy_traits::element( - static_cast<slot_type*>(const_cast<void*>(slot)))); - } - }; - template <class Hash, bool kIsAbsl, size_t kSeedShift> static size_t hash_slot_fn_non_type_erased(const void* hash_fn, void* slot, size_t seed) {
diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h index 92f3d2e..3512a1c 100644 --- a/absl/container/node_hash_map.h +++ b/absl/container/node_hash_map.h
@@ -646,6 +646,7 @@ using DefaultHash = DefaultHashContainerHash<Key>; using DefaultEq = DefaultHashContainerEq<Key>; using DefaultAlloc = std::allocator<std::pair<const Key, Value>>; + using SlotEq = SlotEqFn<Key, /*kDerefSlot=*/true>; template <class Allocator, class... Args> static value_type* new_element(Allocator* alloc, Args&&... args) { @@ -687,10 +688,6 @@ static_assert(memory_internal::OffsetOf<value_type>::kFirst == 0); return GetTypeErasedDerefAndApplyToSlotFn<Hash, Key, kIsAbsl, kSeedShift>(); } - - static constexpr auto get_eq_slot_fn() { - return SlotEqFn<Key, /*kDerefSlot=*/true>{}; - } }; } // namespace container_internal
diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h index 862d9ab..e211e33 100644 --- a/absl/container/node_hash_set.h +++ b/absl/container/node_hash_set.h
@@ -552,6 +552,7 @@ using DefaultHash = DefaultHashContainerHash<T>; using DefaultEq = DefaultHashContainerEq<T>; using DefaultAlloc = std::allocator<T>; + using SlotEq = SlotEqFn<T, /*kDerefSlot=*/true>; template <class Allocator, class... Args> static T* new_element(Allocator* alloc, Args&&... args) { @@ -587,10 +588,6 @@ static constexpr HashSlotFn get_hash_slot_fn() { return GetTypeErasedDerefAndApplyToSlotFn<Hash, T, kIsAbsl, kSeedShift>(); } - - static constexpr auto get_eq_slot_fn() { - return SlotEqFn<T, /*kDerefSlot=*/true>{}; - } }; } // namespace container_internal