Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:11

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
0016 #define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
0017 
0018 #include <cstddef>
0019 #include <memory>
0020 #include <new>
0021 #include <type_traits>
0022 #include <utility>
0023 
0024 #include "absl/container/internal/common_policy_traits.h"
0025 #include "absl/meta/type_traits.h"
0026 
0027 namespace absl {
0028 ABSL_NAMESPACE_BEGIN
0029 namespace container_internal {
0030 
0031 // Defines how slots are initialized/destroyed/moved.
0032 template <class Policy, class = void>
0033 struct hash_policy_traits : common_policy_traits<Policy> {
0034   // The type of the keys stored in the hashtable.
0035   using key_type = typename Policy::key_type;
0036 
0037  private:
0038   struct ReturnKey {
0039     // When C++17 is available, we can use std::launder to provide mutable
0040     // access to the key for use in node handle.
0041 #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
0042     template <class Key,
0043               absl::enable_if_t<std::is_lvalue_reference<Key>::value, int> = 0>
0044     static key_type& Impl(Key&& k, int) {
0045       return *std::launder(
0046           const_cast<key_type*>(std::addressof(std::forward<Key>(k))));
0047     }
0048 #endif
0049 
0050     template <class Key>
0051     static Key Impl(Key&& k, char) {
0052       return std::forward<Key>(k);
0053     }
0054 
0055     // When Key=T&, we forward the lvalue reference.
0056     // When Key=T, we return by value to avoid a dangling reference.
0057     // eg, for string_hash_map.
0058     template <class Key, class... Args>
0059     auto operator()(Key&& k, const Args&...) const
0060         -> decltype(Impl(std::forward<Key>(k), 0)) {
0061       return Impl(std::forward<Key>(k), 0);
0062     }
0063   };
0064 
0065   template <class P = Policy, class = void>
0066   struct ConstantIteratorsImpl : std::false_type {};
0067 
0068   template <class P>
0069   struct ConstantIteratorsImpl<P, absl::void_t<typename P::constant_iterators>>
0070       : P::constant_iterators {};
0071 
0072  public:
0073   // The actual object stored in the hash table.
0074   using slot_type = typename Policy::slot_type;
0075 
0076   // The argument type for insertions into the hashtable. This is different
0077   // from value_type for increased performance. See initializer_list constructor
0078   // and insert() member functions for more details.
0079   using init_type = typename Policy::init_type;
0080 
0081   using reference = decltype(Policy::element(std::declval<slot_type*>()));
0082   using pointer = typename std::remove_reference<reference>::type*;
0083   using value_type = typename std::remove_reference<reference>::type;
0084 
0085   // Policies can set this variable to tell raw_hash_set that all iterators
0086   // should be constant, even `iterator`. This is useful for set-like
0087   // containers.
0088   // Defaults to false if not provided by the policy.
0089   using constant_iterators = ConstantIteratorsImpl<>;
0090 
0091   // Returns the amount of memory owned by `slot`, exclusive of `sizeof(*slot)`.
0092   //
0093   // If `slot` is nullptr, returns the constant amount of memory owned by any
0094   // full slot or -1 if slots own variable amounts of memory.
0095   //
0096   // PRECONDITION: `slot` is INITIALIZED or nullptr
0097   template <class P = Policy>
0098   static size_t space_used(const slot_type* slot) {
0099     return P::space_used(slot);
0100   }
0101 
0102   // Provides generalized access to the key for elements, both for elements in
0103   // the table and for elements that have not yet been inserted (or even
0104   // constructed).  We would like an API that allows us to say: `key(args...)`
0105   // but we cannot do that for all cases, so we use this more general API that
0106   // can be used for many things, including the following:
0107   //
0108   //   - Given an element in a table, get its key.
0109   //   - Given an element initializer, get its key.
0110   //   - Given `emplace()` arguments, get the element key.
0111   //
0112   // Implementations of this must adhere to a very strict technical
0113   // specification around aliasing and consuming arguments:
0114   //
0115   // Let `value_type` be the result type of `element()` without ref- and
0116   // cv-qualifiers. The first argument is a functor, the rest are constructor
0117   // arguments for `value_type`. Returns `std::forward<F>(f)(k, xs...)`, where
0118   // `k` is the element key, and `xs...` are the new constructor arguments for
0119   // `value_type`. It's allowed for `k` to alias `xs...`, and for both to alias
0120   // `ts...`. The key won't be touched once `xs...` are used to construct an
0121   // element; `ts...` won't be touched at all, which allows `apply()` to consume
0122   // any rvalues among them.
0123   //
0124   // If `value_type` is constructible from `Ts&&...`, `Policy::apply()` must not
0125   // trigger a hard compile error unless it originates from `f`. In other words,
0126   // `Policy::apply()` must be SFINAE-friendly. If `value_type` is not
0127   // constructible from `Ts&&...`, either SFINAE or a hard compile error is OK.
0128   //
0129   // If `Ts...` is `[cv] value_type[&]` or `[cv] init_type[&]`,
0130   // `Policy::apply()` must work. A compile error is not allowed, SFINAE or not.
0131   template <class F, class... Ts, class P = Policy>
0132   static auto apply(F&& f, Ts&&... ts)
0133       -> decltype(P::apply(std::forward<F>(f), std::forward<Ts>(ts)...)) {
0134     return P::apply(std::forward<F>(f), std::forward<Ts>(ts)...);
0135   }
0136 
0137   // Returns the "key" portion of the slot.
0138   // Used for node handle manipulation.
0139   template <class P = Policy>
0140   static auto mutable_key(slot_type* slot)
0141       -> decltype(P::apply(ReturnKey(), hash_policy_traits::element(slot))) {
0142     return P::apply(ReturnKey(), hash_policy_traits::element(slot));
0143   }
0144 
0145   // Returns the "value" (as opposed to the "key") portion of the element. Used
0146   // by maps to implement `operator[]`, `at()` and `insert_or_assign()`.
0147   template <class T, class P = Policy>
0148   static auto value(T* elem) -> decltype(P::value(elem)) {
0149     return P::value(elem);
0150   }
0151 
0152   using HashSlotFn = size_t (*)(const void* hash_fn, void* slot);
0153 
0154   template <class Hash>
0155   static constexpr HashSlotFn get_hash_slot_fn() {
0156 // get_hash_slot_fn may return nullptr to signal that non type erased function
0157 // should be used. GCC warns against comparing function address with nullptr.
0158 #if defined(__GNUC__) && !defined(__clang__)
0159 #pragma GCC diagnostic push
0160 // silent error: the address of * will never be NULL [-Werror=address]
0161 #pragma GCC diagnostic ignored "-Waddress"
0162 #endif
0163     return Policy::template get_hash_slot_fn<Hash>() == nullptr
0164                ? &hash_slot_fn_non_type_erased<Hash>
0165                : Policy::template get_hash_slot_fn<Hash>();
0166 #if defined(__GNUC__) && !defined(__clang__)
0167 #pragma GCC diagnostic pop
0168 #endif
0169   }
0170 
0171   // Whether small object optimization is enabled. True by default.
0172   static constexpr bool soo_enabled() { return soo_enabled_impl(Rank1{}); }
0173 
0174  private:
0175   template <class Hash>
0176   struct HashElement {
0177     template <class K, class... Args>
0178     size_t operator()(const K& key, Args&&...) const {
0179       return h(key);
0180     }
0181     const Hash& h;
0182   };
0183 
0184   template <class Hash>
0185   static size_t hash_slot_fn_non_type_erased(const void* hash_fn, void* slot) {
0186     return Policy::apply(HashElement<Hash>{*static_cast<const Hash*>(hash_fn)},
0187                          Policy::element(static_cast<slot_type*>(slot)));
0188   }
0189 
0190   // Use go/ranked-overloads for dispatching. Rank1 is preferred.
0191   struct Rank0 {};
0192   struct Rank1 : Rank0 {};
0193 
0194   // Use auto -> decltype as an enabler.
0195   template <class P = Policy>
0196   static constexpr auto soo_enabled_impl(Rank1) -> decltype(P::soo_enabled()) {
0197     return P::soo_enabled();
0198   }
0199 
0200   static constexpr bool soo_enabled_impl(Rank0) { return true; }
0201 };
0202 
0203 }  // namespace container_internal
0204 ABSL_NAMESPACE_END
0205 }  // namespace absl
0206 
0207 #endif  // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_