hashtable_debug_hooks.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Provides the internal API for hashtable_debug.h.
  16. #ifndef Y_ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
  17. #define Y_ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
  18. #include <cstddef>
  19. #include <algorithm>
  20. #include <type_traits>
  21. #include <vector>
  22. #include "y_absl/base/config.h"
  23. namespace y_absl {
  24. Y_ABSL_NAMESPACE_BEGIN
  25. namespace container_internal {
  26. namespace hashtable_debug_internal {
  27. // If it is a map, call get<0>().
  28. using std::get;
  29. template <typename T, typename = typename T::mapped_type>
  30. auto GetKey(const typename T::value_type& pair, int) -> decltype(get<0>(pair)) {
  31. return get<0>(pair);
  32. }
  33. // If it is not a map, return the value directly.
  34. template <typename T>
  35. const typename T::key_type& GetKey(const typename T::key_type& key, char) {
  36. return key;
  37. }
  38. // Containers should specialize this to provide debug information for that
  39. // container.
  40. template <class Container, typename Enabler = void>
  41. struct HashtableDebugAccess {
  42. // Returns the number of probes required to find `key` in `c`. The "number of
  43. // probes" is a concept that can vary by container. Implementations should
  44. // return 0 when `key` was found in the minimum number of operations and
  45. // should increment the result for each non-trivial operation required to find
  46. // `key`.
  47. //
  48. // The default implementation uses the bucket api from the standard and thus
  49. // works for `std::unordered_*` containers.
  50. static size_t GetNumProbes(const Container& c,
  51. const typename Container::key_type& key) {
  52. if (!c.bucket_count()) return {};
  53. size_t num_probes = 0;
  54. size_t bucket = c.bucket(key);
  55. for (auto it = c.begin(bucket), e = c.end(bucket);; ++it, ++num_probes) {
  56. if (it == e) return num_probes;
  57. if (c.key_eq()(key, GetKey<Container>(*it, 0))) return num_probes;
  58. }
  59. }
  60. // Returns the number of bytes requested from the allocator by the container
  61. // and not freed.
  62. //
  63. // static size_t AllocatedByteSize(const Container& c);
  64. // Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type
  65. // `Container` and `c.size()` is equal to `num_elements`.
  66. //
  67. // static size_t LowerBoundAllocatedByteSize(size_t num_elements);
  68. };
  69. } // namespace hashtable_debug_internal
  70. } // namespace container_internal
  71. Y_ABSL_NAMESPACE_END
  72. } // namespace y_absl
  73. #endif // Y_ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_