Provide default node allocation helpers in node_slot_policy and remove unused forwarders. NodeHashMapPolicy and NodeHashSetPolicy duplicated identical new_element, delete_element, and element_space_used implementations that rebind the allocator to the node element type. Move those defaults into node_slot_policy (still dispatched via Policy::new_element, Policy::delete_element, and Policy::element_space_used so custom node policies can override them) and remove the duplicates from NodeHashMapPolicy and NodeHashSetPolicy. Also remove the unused self-referential `value` and `apply` forwarding methods from node_slot_policy, since policies inherit from node_slot_policy and define their own `value` and `apply` members directly. PiperOrigin-RevId: 1025723419
diff --git a/absl/container/internal/node_slot_policy.h b/absl/container/internal/node_slot_policy.h index 4a7dca2..09c3cde 100644 --- a/absl/container/internal/node_slot_policy.h +++ b/absl/container/internal/node_slot_policy.h
@@ -14,26 +14,12 @@ // // Adapts a policy for nodes. // -// The node policy should model: -// -// struct Policy { -// // Returns a new node allocated and constructed using the allocator, using -// // the specified arguments. -// template <class Alloc, class... Args> -// value_type* new_element(Alloc* alloc, Args&&... args) const; -// -// // Destroys and deallocates node using the allocator. -// template <class Alloc> -// void delete_element(Alloc* alloc, value_type* node) const; -// }; -// -// It may also optionally define `value()` and `apply()`. For documentation on -// these, see hash_policy_traits.h. +// `Policy` may optionally customize allocation/deallocation and accounting via +// `new_element()`, `delete_element()`, and `element_space_used()`. #ifndef ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_ #define ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_ -#include <cassert> #include <cstddef> #include <memory> #include <type_traits> @@ -49,7 +35,33 @@ struct node_slot_policy { static_assert(std::is_lvalue_reference_v<Reference>); - using slot_type = std::remove_cv_t<std::remove_reference_t<Reference>>*; + private: + using Elem = std::remove_cvref_t<Reference>; + + public: + using slot_type = Elem*; + + template <class Alloc, class... Args> + static slot_type new_element(Alloc* alloc, Args&&... args) { + using ElemAlloc = + typename std::allocator_traits<Alloc>::template rebind_alloc<Elem>; + ElemAlloc elem_alloc(*alloc); + slot_type res = std::allocator_traits<ElemAlloc>::allocate(elem_alloc, 1); + std::allocator_traits<ElemAlloc>::construct(elem_alloc, res, + std::forward<Args>(args)...); + return res; + } + + template <class Alloc> + static void delete_element(Alloc* alloc, slot_type elem) { + using ElemAlloc = + typename std::allocator_traits<Alloc>::template rebind_alloc<Elem>; + ElemAlloc elem_alloc(*alloc); + std::allocator_traits<ElemAlloc>::destroy(elem_alloc, elem); + std::allocator_traits<ElemAlloc>::deallocate(elem_alloc, elem, 1); + } + + static size_t element_space_used(const Elem*) { return sizeof(Elem); } template <class Alloc, class... Args> static void construct(Alloc* alloc, slot_type* slot, Args&&... args) { @@ -75,16 +87,6 @@ } static Reference element(slot_type* slot) { return **slot; } - - template <class T, class P = Policy> - static auto value(T* elem) -> decltype(P::value(elem)) { - return P::value(elem); - } - - template <class... Ts, class P = Policy> - static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) { - return P::apply(std::forward<Ts>(ts)...); - } }; } // namespace container_internal
diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h index 3512a1c..4901023 100644 --- a/absl/container/node_hash_map.h +++ b/absl/container/node_hash_map.h
@@ -648,26 +648,6 @@ 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) { - using PairAlloc = typename std::allocator_traits< - Allocator>::template rebind_alloc<value_type>; - PairAlloc pair_alloc(*alloc); - value_type* res = std::allocator_traits<PairAlloc>::allocate(pair_alloc, 1); - std::allocator_traits<PairAlloc>::construct(pair_alloc, res, - std::forward<Args>(args)...); - return res; - } - - template <class Allocator> - static void delete_element(Allocator* alloc, value_type* pair) { - using PairAlloc = typename std::allocator_traits< - Allocator>::template rebind_alloc<value_type>; - PairAlloc pair_alloc(*alloc); - std::allocator_traits<PairAlloc>::destroy(pair_alloc, pair); - std::allocator_traits<PairAlloc>::deallocate(pair_alloc, pair, 1); - } - template <class F, class... Args> static decltype(absl::container_internal::DecomposePair( std::declval<F>(), std::declval<Args>()...)) @@ -676,10 +656,6 @@ std::forward<Args>(args)...); } - static size_t element_space_used(const value_type*) { - return sizeof(value_type); - } - static Value& value(value_type* elem) { return elem->second; } static const Value& value(const value_type* elem) { return elem->second; }
diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h index e211e33..7017a80 100644 --- a/absl/container/node_hash_set.h +++ b/absl/container/node_hash_set.h
@@ -554,26 +554,6 @@ using DefaultAlloc = std::allocator<T>; using SlotEq = SlotEqFn<T, /*kDerefSlot=*/true>; - template <class Allocator, class... Args> - static T* new_element(Allocator* alloc, Args&&... args) { - using ValueAlloc = - typename std::allocator_traits<Allocator>::template rebind_alloc<T>; - ValueAlloc value_alloc(*alloc); - T* res = std::allocator_traits<ValueAlloc>::allocate(value_alloc, 1); - std::allocator_traits<ValueAlloc>::construct(value_alloc, res, - std::forward<Args>(args)...); - return res; - } - - template <class Allocator> - static void delete_element(Allocator* alloc, T* elem) { - using ValueAlloc = - typename std::allocator_traits<Allocator>::template rebind_alloc<T>; - ValueAlloc value_alloc(*alloc); - std::allocator_traits<ValueAlloc>::destroy(value_alloc, elem); - std::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1); - } - template <class F, class... Args> static decltype(absl::container_internal::DecomposeValue( std::declval<F>(), std::declval<Args>()...)) @@ -582,8 +562,6 @@ std::forward<F>(f), std::forward<Args>(args)...); } - static size_t element_space_used(const T*) { return sizeof(T); } - template <class Hash, bool kIsAbsl, size_t kSeedShift> static constexpr HashSlotFn get_hash_slot_fn() { return GetTypeErasedDerefAndApplyToSlotFn<Hash, T, kIsAbsl, kSeedShift>();