[PATCH] D83887: Add hashing support for std::tuple
Michael Forster via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 15 10:15:03 PDT 2020
MForster created this revision.
MForster added a reviewer: gribozavr2.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.
All tuple values are passed directly to hash_combine. This is inspired by the implementation used for Swift:
https://github.com/llvm/llvm-project-staging/commit/4a1b4edbe1d1969284c1528e2950ac81b25edc8f
https://github.com/llvm/llvm-project-staging/commit/845f3829b91522920a59c351b9011af01c5c7f87
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D83887
Files:
llvm/include/llvm/ADT/Hashing.h
llvm/unittests/ADT/HashingTest.cpp
Index: llvm/unittests/ADT/HashingTest.cpp
===================================================================
--- llvm/unittests/ADT/HashingTest.cpp
+++ llvm/unittests/ADT/HashingTest.cpp
@@ -101,6 +101,12 @@
hash_value(std::make_pair(obj1, std::make_pair(obj2, obj3))));
}
+TEST(HashingTest, HashValueStdTuple) {
+ EXPECT_EQ(hash_combine(), hash_value(std::make_tuple()));
+ EXPECT_EQ(hash_combine(42), hash_value(std::make_tuple(42)));
+ EXPECT_EQ(hash_combine(42, 'c'), hash_value(std::make_tuple(42, 'c')));
+}
+
TEST(HashingTest, HashValueStdString) {
std::string s = "Hello World!";
EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size()), hash_value(s));
Index: llvm/include/llvm/ADT/Hashing.h
===================================================================
--- llvm/include/llvm/ADT/Hashing.h
+++ llvm/include/llvm/ADT/Hashing.h
@@ -52,6 +52,7 @@
#include <cassert>
#include <cstring>
#include <string>
+#include <tuple>
#include <utility>
namespace llvm {
@@ -116,6 +117,8 @@
template <typename T>
hash_code hash_value(const std::basic_string<T> &arg);
+/// Compute a hash_code for a tuple.
+template <typename... Ts> hash_code hash_value(const std::tuple<Ts...> &arg);
/// Override the execution seed with a fixed value.
///
@@ -652,6 +655,45 @@
return hash_combine_range(arg.begin(), arg.end());
}
+// Implementation details for the hash_value overload for std::tuple<...>(...).
+namespace hashing {
+namespace detail {
+
+// The indices of a tuple (0..n-1) are collected as the Indices template
+// parameter of the TupleIndexSet template.
+template <unsigned... Indices> struct TupleIndexSet {};
+
+// Collecting the indices uses recursive template metaprogramming via the
+// MakeTupleIndexSet struct. The I template parameter recursively
+// counts up to N-1. The values are collected in the Indices template parameter
+// and then returned in the second template specialization when I reaches N.
+template <unsigned I, unsigned N, unsigned... Indices>
+struct MakeTupleIndexSet {
+ typedef typename MakeTupleIndexSet<I + 1, N, Indices..., I>::Type Type;
+};
+
+template <unsigned N, unsigned... Indices>
+struct MakeTupleIndexSet<N, N, Indices...> {
+ typedef TupleIndexSet<Indices...> Type;
+};
+
+// The indices are then expanded in the hash_value_tuple_helper function into
+// std::get<...>(...) calls.
+template <typename... Ts, unsigned... Indices>
+hash_code hash_value_tuple_helper(const std::tuple<Ts...> &arg,
+ TupleIndexSet<Indices...> indices) {
+ return hash_combine(std::get<Indices>(arg)...);
+}
+
+} // namespace detail
+} // namespace hashing
+
+template <typename... Ts> hash_code hash_value(const std::tuple<Ts...> &arg) {
+ typename ::llvm::hashing::detail::MakeTupleIndexSet<0, sizeof...(Ts)>::Type
+ indices;
+ return hash_value_tuple_helper(arg, indices);
+}
+
} // namespace llvm
#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83887.278234.patch
Type: text/x-patch
Size: 2932 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200715/6fbda8bc/attachment.bin>
More information about the llvm-commits
mailing list