diff options
author | Dennis Kormalev <denk@google.com> | 2024-04-24 14:05:14 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-04-24 14:06:05 -0700 |
commit | 08b21bd037990c18d44fda1691211e73835bf214 (patch) | |
tree | dcc50e661487803c63501710f3cc3fd965651f43 | |
parent | b0160ba46be86c3728b5e72a194c34f166740af1 (diff) | |
download | abseil-08b21bd037990c18d44fda1691211e73835bf214.tar.gz abseil-08b21bd037990c18d44fda1691211e73835bf214.tar.bz2 abseil-08b21bd037990c18d44fda1691211e73835bf214.zip |
Add public aliases for default hash/eq types in hash-based containers
Generic user code sometimes wants to provide more flexibility for its
own users and provide type arguments that are used as Hash/Eq in
underlying containers.
However, there is no sensible publicly available default value for it
yet.
This CL fixes this issue and provides publicly visible aliases that such
user code can use.
PiperOrigin-RevId: 627844757
Change-Id: I4c393007244ad8d998da02883c623eae1715c0df
-rw-r--r-- | CMake/AbseilDll.cmake | 1 | ||||
-rw-r--r-- | absl/container/BUILD.bazel | 19 | ||||
-rw-r--r-- | absl/container/CMakeLists.txt | 21 | ||||
-rw-r--r-- | absl/container/flat_hash_map.h | 10 | ||||
-rw-r--r-- | absl/container/flat_hash_set.h | 6 | ||||
-rw-r--r-- | absl/container/hash_container_defaults.h | 45 | ||||
-rw-r--r-- | absl/container/node_hash_map.h | 10 | ||||
-rw-r--r-- | absl/container/node_hash_set.h | 9 |
8 files changed, 93 insertions, 28 deletions
diff --git a/CMake/AbseilDll.cmake b/CMake/AbseilDll.cmake index 47f3beeb..6353e2a7 100644 --- a/CMake/AbseilDll.cmake +++ b/CMake/AbseilDll.cmake @@ -66,6 +66,7 @@ set(ABSL_INTERNAL_DLL_FILES "cleanup/internal/cleanup.h" "container/btree_map.h" "container/btree_set.h" + "container/hash_container_defaults.h" "container/fixed_array.h" "container/flat_hash_map.h" "container/flat_hash_set.h" diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel index 2eaece69..859163f8 100644 --- a/absl/container/BUILD.bazel +++ b/absl/container/BUILD.bazel @@ -248,7 +248,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":container_memory", - ":hash_function_defaults", + ":hash_container_defaults", ":raw_hash_map", "//absl/algorithm:container", "//absl/base:core_headers", @@ -285,7 +285,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":container_memory", - ":hash_function_defaults", + ":hash_container_defaults", ":raw_hash_set", "//absl/algorithm:container", "//absl/base:core_headers", @@ -324,7 +324,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":container_memory", - ":hash_function_defaults", + ":hash_container_defaults", ":node_slot_policy", ":raw_hash_map", "//absl/algorithm:container", @@ -359,7 +359,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":container_memory", - ":hash_function_defaults", + ":hash_container_defaults", ":node_slot_policy", ":raw_hash_set", "//absl/algorithm:container", @@ -433,6 +433,17 @@ cc_library( ], ) +cc_library( + name = "hash_container_defaults", + hdrs = ["hash_container_defaults.h"], + copts = ABSL_DEFAULT_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + ":hash_function_defaults", + "//absl/base:config", + ], +) + cc_test( name = "hash_function_defaults_test", srcs = ["internal/hash_function_defaults_test.cc"], diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt index 576e83e2..b1f5f9d8 100644 --- a/absl/container/CMakeLists.txt +++ b/absl/container/CMakeLists.txt @@ -289,7 +289,7 @@ absl_cc_library( DEPS absl::container_memory absl::core_headers - absl::hash_function_defaults + absl::hash_container_defaults absl::raw_hash_map absl::algorithm_container absl::memory @@ -326,7 +326,7 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} DEPS absl::container_memory - absl::hash_function_defaults + absl::hash_container_defaults absl::raw_hash_set absl::algorithm_container absl::core_headers @@ -368,7 +368,7 @@ absl_cc_library( DEPS absl::container_memory absl::core_headers - absl::hash_function_defaults + absl::hash_container_defaults absl::node_slot_policy absl::raw_hash_map absl::algorithm_container @@ -404,7 +404,7 @@ absl_cc_library( DEPS absl::container_memory absl::core_headers - absl::hash_function_defaults + absl::hash_container_defaults absl::node_slot_policy absl::raw_hash_set absl::algorithm_container @@ -430,6 +430,19 @@ absl_cc_test( GTest::gmock_main ) +absl_cc_library( + NAME + hash_container_defaults + HDRS + "hash_container_defaults.h" + COPTS + ${ABSL_DEFAULT_COPTS} + DEPS + absl::config + absl::hash_function_defaults + PUBLIC +) + # Internal-only target, do not depend on directly. absl_cc_library( NAME diff --git a/absl/container/flat_hash_map.h b/absl/container/flat_hash_map.h index a33c794f..aa2c5c8c 100644 --- a/absl/container/flat_hash_map.h +++ b/absl/container/flat_hash_map.h @@ -31,16 +31,15 @@ #define ABSL_CONTAINER_FLAT_HASH_MAP_H_ #include <cstddef> -#include <new> +#include <memory> #include <type_traits> #include <utility> #include "absl/algorithm/container.h" #include "absl/base/macros.h" +#include "absl/container/hash_container_defaults.h" #include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export #include "absl/container/internal/raw_hash_map.h" // IWYU pragma: export -#include "absl/memory/memory.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -119,9 +118,8 @@ struct FlatHashMapPolicy; // if (result != ducks.end()) { // std::cout << "Result: " << result->second << std::endl; // } -template <class K, class V, - class Hash = absl::container_internal::hash_default_hash<K>, - class Eq = absl::container_internal::hash_default_eq<K>, +template <class K, class V, class Hash = DefaultHashContainerHash<K>, + class Eq = DefaultHashContainerEq<K>, class Allocator = std::allocator<std::pair<const K, V>>> class flat_hash_map : public absl::container_internal::raw_hash_map< absl::container_internal::FlatHashMapPolicy<K, V>, diff --git a/absl/container/flat_hash_set.h b/absl/container/flat_hash_set.h index 5f72f954..e558b071 100644 --- a/absl/container/flat_hash_set.h +++ b/absl/container/flat_hash_set.h @@ -36,8 +36,8 @@ #include "absl/algorithm/container.h" #include "absl/base/macros.h" +#include "absl/container/hash_container_defaults.h" #include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export #include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export #include "absl/memory/memory.h" @@ -114,8 +114,8 @@ struct FlatHashSetPolicy; // if (ducks.contains("dewey")) { // std::cout << "We found dewey!" << std::endl; // } -template <class T, class Hash = absl::container_internal::hash_default_hash<T>, - class Eq = absl::container_internal::hash_default_eq<T>, +template <class T, class Hash = DefaultHashContainerHash<T>, + class Eq = DefaultHashContainerEq<T>, class Allocator = std::allocator<T>> class flat_hash_set : public absl::container_internal::raw_hash_set< diff --git a/absl/container/hash_container_defaults.h b/absl/container/hash_container_defaults.h new file mode 100644 index 00000000..eb944a7c --- /dev/null +++ b/absl/container/hash_container_defaults.h @@ -0,0 +1,45 @@ +// Copyright 2024 The Abseil Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ABSL_CONTAINER_HASH_CONTAINER_DEFAULTS_H_ +#define ABSL_CONTAINER_HASH_CONTAINER_DEFAULTS_H_ + +#include "absl/base/config.h" +#include "absl/container/internal/hash_function_defaults.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN + +// DefaultHashContainerHash is a convenience alias for the functor that is used +// by default by Abseil hash-based (unordered) containers for hashing when +// `Hash` type argument is not explicitly specified. +// +// This type alias can be used by generic code that wants to provide more +// flexibility for defining underlying containers. +template <typename T> +using DefaultHashContainerHash = absl::container_internal::hash_default_hash<T>; + +// DefaultHashContainerEq is a convenience alias for the functor that is used by +// default by Abseil hash-based (unordered) containers for equality check when +// `Eq` type argument is not explicitly specified. +// +// This type alias can be used by generic code that wants to provide more +// flexibility for defining underlying containers. +template <typename T> +using DefaultHashContainerEq = absl::container_internal::hash_default_eq<T>; + +ABSL_NAMESPACE_END +} // namespace absl + +#endif // ABSL_CONTAINER_HASH_CONTAINER_DEFAULTS_H_ diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h index cb41543c..31beb1da 100644 --- a/absl/container/node_hash_map.h +++ b/absl/container/node_hash_map.h @@ -37,14 +37,13 @@ #define ABSL_CONTAINER_NODE_HASH_MAP_H_ #include <cstddef> -#include <tuple> +#include <memory> #include <type_traits> #include <utility> #include "absl/algorithm/container.h" -#include "absl/base/macros.h" +#include "absl/container/hash_container_defaults.h" #include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export #include "absl/container/internal/node_slot_policy.h" #include "absl/container/internal/raw_hash_map.h" // IWYU pragma: export #include "absl/memory/memory.h" @@ -114,9 +113,8 @@ class NodeHashMapPolicy; // if (result != ducks.end()) { // std::cout << "Result: " << result->second << std::endl; // } -template <class Key, class Value, - class Hash = absl::container_internal::hash_default_hash<Key>, - class Eq = absl::container_internal::hash_default_eq<Key>, +template <class Key, class Value, class Hash = DefaultHashContainerHash<Key>, + class Eq = DefaultHashContainerEq<Key>, class Alloc = std::allocator<std::pair<const Key, Value>>> class node_hash_map : public absl::container_internal::raw_hash_map< diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h index 8cc4b624..deeb49ce 100644 --- a/absl/container/node_hash_set.h +++ b/absl/container/node_hash_set.h @@ -36,12 +36,12 @@ #define ABSL_CONTAINER_NODE_HASH_SET_H_ #include <cstddef> +#include <memory> #include <type_traits> #include "absl/algorithm/container.h" -#include "absl/base/macros.h" +#include "absl/container/hash_container_defaults.h" #include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export #include "absl/container/internal/node_slot_policy.h" #include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export #include "absl/memory/memory.h" @@ -109,9 +109,8 @@ struct NodeHashSetPolicy; // if (ducks.contains("dewey")) { // std::cout << "We found dewey!" << std::endl; // } -template <class T, class Hash = absl::container_internal::hash_default_hash<T>, - class Eq = absl::container_internal::hash_default_eq<T>, - class Alloc = std::allocator<T>> +template <class T, class Hash = DefaultHashContainerHash<T>, + class Eq = DefaultHashContainerEq<T>, class Alloc = std::allocator<T>> class node_hash_set : public absl::container_internal::raw_hash_set< absl::container_internal::NodeHashSetPolicy<T>, Hash, Eq, Alloc> { |