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 // Provides the internal API for hashtable_debug.h. 00016 00017 #ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_ 00018 #define ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_ 00019 00020 #include <cstddef> 00021 00022 #include <algorithm> 00023 #include <type_traits> 00024 #include <vector> 00025 00026 namespace absl { 00027 namespace container_internal { 00028 namespace hashtable_debug_internal { 00029 00030 // If it is a map, call get<0>(). 00031 using std::get; 00032 template <typename T, typename = typename T::mapped_type> 00033 auto GetKey(const typename T::value_type& pair, int) -> decltype(get<0>(pair)) { 00034 return get<0>(pair); 00035 } 00036 00037 // If it is not a map, return the value directly. 00038 template <typename T> 00039 const typename T::key_type& GetKey(const typename T::key_type& key, char) { 00040 return key; 00041 } 00042 00043 // Containers should specialize this to provide debug information for that 00044 // container. 00045 template <class Container, typename Enabler = void> 00046 struct HashtableDebugAccess { 00047 // Returns the number of probes required to find `key` in `c`. The "number of 00048 // probes" is a concept that can vary by container. Implementations should 00049 // return 0 when `key` was found in the minimum number of operations and 00050 // should increment the result for each non-trivial operation required to find 00051 // `key`. 00052 // 00053 // The default implementation uses the bucket api from the standard and thus 00054 // works for `std::unordered_*` containers. 00055 static size_t GetNumProbes(const Container& c, 00056 const typename Container::key_type& key) { 00057 if (!c.bucket_count()) return {}; 00058 size_t num_probes = 0; 00059 size_t bucket = c.bucket(key); 00060 for (auto it = c.begin(bucket), e = c.end(bucket);; ++it, ++num_probes) { 00061 if (it == e) return num_probes; 00062 if (c.key_eq()(key, GetKey<Container>(*it, 0))) return num_probes; 00063 } 00064 } 00065 00066 // Returns the number of bytes requested from the allocator by the container 00067 // and not freed. 00068 // 00069 // static size_t AllocatedByteSize(const Container& c); 00070 00071 // Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type 00072 // `Container` and `c.size()` is equal to `num_elements`. 00073 // 00074 // static size_t LowerBoundAllocatedByteSize(size_t num_elements); 00075 }; 00076 00077 } // namespace hashtable_debug_internal 00078 } // namespace container_internal 00079 } // namespace absl 00080 00081 #endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_