-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: There are hash-related traits in folly/Traits.h that would ideally be colocated with hash-related code. Reviewed By: DenisYaroshevskiy Differential Revision: D62502066 fbshipit-source-id: 0d0d50e8744eeedad1b0adcdcefdb33a0d6cc2cf
- Loading branch information
1 parent
ac6f850
commit a662cd3
Showing
10 changed files
with
207 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
#include <folly/hash/traits.h> | ||
|
||
#include <folly/portability/GTest.h> | ||
|
||
using namespace folly; | ||
|
||
namespace { | ||
|
||
struct HashableStruct1 {}; | ||
struct HashableStruct2 {}; | ||
struct UnhashableStruct {}; | ||
|
||
template <typename X, typename Y> | ||
struct CompositeStruct { | ||
X x; | ||
Y y; | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace std { | ||
|
||
template <> | ||
struct hash<HashableStruct1> { | ||
[[maybe_unused]] size_t operator()(const HashableStruct1&) const noexcept { | ||
return 0; | ||
} | ||
}; | ||
|
||
template <> | ||
struct hash<HashableStruct2> { | ||
[[maybe_unused]] size_t operator()(const HashableStruct2&) const noexcept { | ||
return 0; | ||
} | ||
}; | ||
|
||
template <typename X, typename Y> | ||
struct hash<enable_std_hash_helper<CompositeStruct<X, Y>, X, Y>> { | ||
[[maybe_unused]] size_t operator()( | ||
const CompositeStruct<X, Y>& value) const noexcept { | ||
return std::hash<X>{}(value.x) + std::hash<Y>{}(value.y); | ||
} | ||
}; | ||
|
||
} // namespace std | ||
|
||
static_assert(is_hashable_v<HashableStruct1>); | ||
static_assert(is_hashable_v<HashableStruct2>); | ||
static_assert(!is_hashable_v<UnhashableStruct>); | ||
static_assert(is_hashable_v<CompositeStruct<HashableStruct1, HashableStruct1>>); | ||
static_assert(is_hashable_v<CompositeStruct<HashableStruct1, HashableStruct2>>); | ||
static_assert( | ||
!is_hashable_v<CompositeStruct<HashableStruct1, UnhashableStruct>>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <type_traits> | ||
|
||
#include <folly/Traits.h> | ||
|
||
namespace folly { | ||
|
||
/** | ||
* Checks the requirements that the Hasher class must satisfy | ||
* in order to be used with the standard library containers, | ||
* for example `std::unordered_set<T, Hasher>`. | ||
*/ | ||
template <typename T, typename Hasher> | ||
using is_hasher_usable = std::integral_constant< | ||
bool, | ||
std::is_default_constructible_v<Hasher> && | ||
std::is_copy_constructible_v<Hasher> && | ||
std::is_move_constructible_v<Hasher> && | ||
std::is_invocable_r_v<size_t, Hasher, const T&>>; | ||
|
||
/** | ||
* Checks the requirements that the Hasher class must satisfy | ||
* in order to be used with the standard library containers, | ||
* for example `std::unordered_set<T, Hasher>`. | ||
*/ | ||
template <typename T, typename Hasher> | ||
inline constexpr bool is_hasher_usable_v = is_hasher_usable<T, Hasher>::value; | ||
|
||
/** | ||
* Checks that the given hasher template's specialization for the given type | ||
* is usable with the standard library containters, | ||
* for example `std::unordered_set<T, Hasher<T>>`. | ||
*/ | ||
template <typename T, template <typename U> typename Hasher = std::hash> | ||
using is_hashable = | ||
std::integral_constant<bool, is_hasher_usable_v<T, Hasher<T>>>; | ||
|
||
/** | ||
* Checks that the given hasher template's specialization for the given type | ||
* is usable with the standard library containters, | ||
* for example `std::unordered_set<T, Hasher<T>>`. | ||
*/ | ||
template <typename T, template <typename U> typename Hasher = std::hash> | ||
inline constexpr bool is_hashable_v = is_hashable<T, Hasher>::value; | ||
|
||
namespace detail { | ||
|
||
template <typename T, typename> | ||
using enable_hasher_helper_impl = T; | ||
|
||
} // namespace detail | ||
|
||
/** | ||
* A helper for defining partial specializations of a hasher class that rely | ||
* on other partial specializations of that hasher class being usable. | ||
* | ||
* Example: | ||
* ``` | ||
* template <typename T> | ||
* struct hash< | ||
* folly::enable_std_hash_helper<folly::Optional<T>, remove_const_t<T>>> { | ||
* size_t operator()(folly::Optional<T> const& obj) const { | ||
* return static_cast<bool>(obj) ? hash<remove_const_t<T>>()(*obj) : 0; | ||
* } | ||
* }; | ||
* ``` | ||
*/ | ||
template < | ||
typename T, | ||
template <typename U> | ||
typename Hasher, | ||
typename... Dependencies> | ||
using enable_hasher_helper = detail::enable_hasher_helper_impl< | ||
T, | ||
std::enable_if_t< | ||
StrictConjunction<is_hashable<Dependencies, Hasher>...>::value>>; | ||
|
||
/** | ||
* A helper for defining partial specializations of a hasher class that rely | ||
* on other partial specializations of that hasher class being usable. | ||
* | ||
* Example: | ||
* ``` | ||
* template <typename T> | ||
* struct hash< | ||
* folly::enable_std_hash_helper<folly::Optional<T>, remove_const_t<T>>> { | ||
* size_t operator()(folly::Optional<T> const& obj) const { | ||
* return static_cast<bool>(obj) ? hash<remove_const_t<T>>()(*obj) : 0; | ||
* } | ||
* }; | ||
* ``` | ||
*/ | ||
template <typename T, typename... Dependencies> | ||
using enable_std_hash_helper = | ||
enable_hasher_helper<T, std::hash, Dependencies...>; | ||
|
||
} // namespace folly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.