hash_policy_traits.h
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
00016 #define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
00017 
00018 #include <cstddef>
00019 #include <memory>
00020 #include <type_traits>
00021 #include <utility>
00022 
00023 #include "absl/meta/type_traits.h"
00024 
00025 namespace absl {
00026 namespace container_internal {
00027 
00028 // Defines how slots are initialized/destroyed/moved.
00029 template <class Policy, class = void>
00030 struct hash_policy_traits {
00031  private:
00032   struct ReturnKey {
00033     // We return `Key` here.
00034     // When Key=T&, we forward the lvalue reference.
00035     // When Key=T, we return by value to avoid a dangling reference.
00036     // eg, for string_hash_map.
00037     template <class Key, class... Args>
00038     Key operator()(Key&& k, const Args&...) const {
00039       return std::forward<Key>(k);
00040     }
00041   };
00042 
00043   template <class P = Policy, class = void>
00044   struct ConstantIteratorsImpl : std::false_type {};
00045 
00046   template <class P>
00047   struct ConstantIteratorsImpl<P, absl::void_t<typename P::constant_iterators>>
00048       : P::constant_iterators {};
00049 
00050  public:
00051   // The actual object stored in the hash table.
00052   using slot_type = typename Policy::slot_type;
00053 
00054   // The type of the keys stored in the hashtable.
00055   using key_type = typename Policy::key_type;
00056 
00057   // The argument type for insertions into the hashtable. This is different
00058   // from value_type for increased performance. See initializer_list constructor
00059   // and insert() member functions for more details.
00060   using init_type = typename Policy::init_type;
00061 
00062   using reference = decltype(Policy::element(std::declval<slot_type*>()));
00063   using pointer = typename std::remove_reference<reference>::type*;
00064   using value_type = typename std::remove_reference<reference>::type;
00065 
00066   // Policies can set this variable to tell raw_hash_set that all iterators
00067   // should be constant, even `iterator`. This is useful for set-like
00068   // containers.
00069   // Defaults to false if not provided by the policy.
00070   using constant_iterators = ConstantIteratorsImpl<>;
00071 
00072   // PRECONDITION: `slot` is UNINITIALIZED
00073   // POSTCONDITION: `slot` is INITIALIZED
00074   template <class Alloc, class... Args>
00075   static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
00076     Policy::construct(alloc, slot, std::forward<Args>(args)...);
00077   }
00078 
00079   // PRECONDITION: `slot` is INITIALIZED
00080   // POSTCONDITION: `slot` is UNINITIALIZED
00081   template <class Alloc>
00082   static void destroy(Alloc* alloc, slot_type* slot) {
00083     Policy::destroy(alloc, slot);
00084   }
00085 
00086   // Transfers the `old_slot` to `new_slot`. Any memory allocated by the
00087   // allocator inside `old_slot` to `new_slot` can be transferred.
00088   //
00089   // OPTIONAL: defaults to:
00090   //
00091   //     clone(new_slot, std::move(*old_slot));
00092   //     destroy(old_slot);
00093   //
00094   // PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED
00095   // POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is
00096   //                UNINITIALIZED
00097   template <class Alloc>
00098   static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
00099     transfer_impl(alloc, new_slot, old_slot, 0);
00100   }
00101 
00102   // PRECONDITION: `slot` is INITIALIZED
00103   // POSTCONDITION: `slot` is INITIALIZED
00104   template <class P = Policy>
00105   static auto element(slot_type* slot) -> decltype(P::element(slot)) {
00106     return P::element(slot);
00107   }
00108 
00109   // Returns the amount of memory owned by `slot`, exclusive of `sizeof(*slot)`.
00110   //
00111   // If `slot` is nullptr, returns the constant amount of memory owned by any
00112   // full slot or -1 if slots own variable amounts of memory.
00113   //
00114   // PRECONDITION: `slot` is INITIALIZED or nullptr
00115   template <class P = Policy>
00116   static size_t space_used(const slot_type* slot) {
00117     return P::space_used(slot);
00118   }
00119 
00120   // Provides generalized access to the key for elements, both for elements in
00121   // the table and for elements that have not yet been inserted (or even
00122   // constructed).  We would like an API that allows us to say: `key(args...)`
00123   // but we cannot do that for all cases, so we use this more general API that
00124   // can be used for many things, including the following:
00125   //
00126   //   - Given an element in a table, get its key.
00127   //   - Given an element initializer, get its key.
00128   //   - Given `emplace()` arguments, get the element key.
00129   //
00130   // Implementations of this must adhere to a very strict technical
00131   // specification around aliasing and consuming arguments:
00132   //
00133   // Let `value_type` be the result type of `element()` without ref- and
00134   // cv-qualifiers. The first argument is a functor, the rest are constructor
00135   // arguments for `value_type`. Returns `std::forward<F>(f)(k, xs...)`, where
00136   // `k` is the element key, and `xs...` are the new constructor arguments for
00137   // `value_type`. It's allowed for `k` to alias `xs...`, and for both to alias
00138   // `ts...`. The key won't be touched once `xs...` are used to construct an
00139   // element; `ts...` won't be touched at all, which allows `apply()` to consume
00140   // any rvalues among them.
00141   //
00142   // If `value_type` is constructible from `Ts&&...`, `Policy::apply()` must not
00143   // trigger a hard compile error unless it originates from `f`. In other words,
00144   // `Policy::apply()` must be SFINAE-friendly. If `value_type` is not
00145   // constructible from `Ts&&...`, either SFINAE or a hard compile error is OK.
00146   //
00147   // If `Ts...` is `[cv] value_type[&]` or `[cv] init_type[&]`,
00148   // `Policy::apply()` must work. A compile error is not allowed, SFINAE or not.
00149   template <class F, class... Ts, class P = Policy>
00150   static auto apply(F&& f, Ts&&... ts)
00151       -> decltype(P::apply(std::forward<F>(f), std::forward<Ts>(ts)...)) {
00152     return P::apply(std::forward<F>(f), std::forward<Ts>(ts)...);
00153   }
00154 
00155   // Returns the "key" portion of the slot.
00156   // Used for node handle manipulation.
00157   template <class P = Policy>
00158   static auto key(slot_type* slot)
00159       -> decltype(P::apply(ReturnKey(), element(slot))) {
00160     return P::apply(ReturnKey(), element(slot));
00161   }
00162 
00163   // Returns the "value" (as opposed to the "key") portion of the element. Used
00164   // by maps to implement `operator[]`, `at()` and `insert_or_assign()`.
00165   template <class T, class P = Policy>
00166   static auto value(T* elem) -> decltype(P::value(elem)) {
00167     return P::value(elem);
00168   }
00169 
00170  private:
00171   // Use auto -> decltype as an enabler.
00172   template <class Alloc, class P = Policy>
00173   static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
00174                             slot_type* old_slot, int)
00175       -> decltype((void)P::transfer(alloc, new_slot, old_slot)) {
00176     P::transfer(alloc, new_slot, old_slot);
00177   }
00178   template <class Alloc>
00179   static void transfer_impl(Alloc* alloc, slot_type* new_slot,
00180                             slot_type* old_slot, char) {
00181     construct(alloc, new_slot, std::move(element(old_slot)));
00182     destroy(alloc, old_slot);
00183   }
00184 };
00185 
00186 }  // namespace container_internal
00187 }  // namespace absl
00188 
00189 #endif  // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14