hash: Add support for tuples
There is now builtin support for stdplus::hash<> and
stdplus::hashMulti(...) hashing std::tuple containers.
Change-Id: I0e0983c04d54302f00c811e5de0d949accee03d7
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/meson.build b/include/meson.build
index 358bc32..fc304f3 100644
--- a/include/meson.build
+++ b/include/meson.build
@@ -7,6 +7,7 @@
'stdplus/handle/copyable.hpp',
'stdplus/handle/managed.hpp',
'stdplus/hash.hpp',
+ 'stdplus/hash/tuple.hpp',
'stdplus/pinned.hpp',
'stdplus/raw.hpp',
'stdplus/signal.hpp',
diff --git a/include/stdplus/hash.hpp b/include/stdplus/hash.hpp
index 44cc6a6..f3aa8e6 100644
--- a/include/stdplus/hash.hpp
+++ b/include/stdplus/hash.hpp
@@ -1,5 +1,6 @@
#pragma once
#include <cstddef>
+#include <tuple>
#include <utility>
namespace std
@@ -65,4 +66,14 @@
struct hash : std::hash<Key>
{};
+template <typename... Ts>
+struct hash<std::tuple<Ts...>>
+{
+ constexpr std::size_t operator()(const std::tuple<Ts...>& ts) noexcept(
+ noexcept(hashMulti(std::declval<Ts>()...)))
+ {
+ return std::apply(hashMulti<Ts...>, ts);
+ }
+};
+
} // namespace stdplus
diff --git a/include/stdplus/hash/tuple.hpp b/include/stdplus/hash/tuple.hpp
new file mode 100644
index 0000000..be7a45e
--- /dev/null
+++ b/include/stdplus/hash/tuple.hpp
@@ -0,0 +1,14 @@
+#pragma once
+#include <stdplus/hash.hpp>
+
+#include <tuple>
+
+template <typename... Ts>
+struct std::hash<std::tuple<Ts...>>
+{
+ constexpr auto operator()(const std::tuple<Ts...>& ts) const noexcept(
+ noexcept(stdplus::hashMulti(std::declval<std::tuple<Ts...>>())))
+ {
+ return stdplus::hashMulti(ts);
+ }
+};
diff --git a/src/hash/tuple.cpp b/src/hash/tuple.cpp
new file mode 100644
index 0000000..1cba27e
--- /dev/null
+++ b/src/hash/tuple.cpp
@@ -0,0 +1 @@
+#include <stdplus/hash/tuple.hpp>
diff --git a/src/meson.build b/src/meson.build
index c4662ad..ed994ea 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -48,6 +48,7 @@
'handle/copyable.cpp',
'handle/managed.cpp',
'hash.cpp',
+ 'hash/tuple.cpp',
'pinned.cpp',
'raw.cpp',
'signal.cpp',
diff --git a/test/hash.cpp b/test/hash.cpp
index 23e0e26..d9bcd48 100644
--- a/test/hash.cpp
+++ b/test/hash.cpp
@@ -1,6 +1,7 @@
#include <stdplus/hash.hpp>
#include <string>
+#include <tuple>
#include <gtest/gtest.h>
@@ -11,7 +12,8 @@
{
EXPECT_EQ(0, hashMulti());
EXPECT_EQ(2654435834, hashMulti(1, 2));
- hashMulti(1, std::string("bacon"), nullptr);
+ std::tuple<std::string, int> t = {"s", 8};
+ hashMulti(1, std::string("bacon"), nullptr, t);
}
} // namespace stdplus
diff --git a/test/hash/tuple.cpp b/test/hash/tuple.cpp
new file mode 100644
index 0000000..1e87df6
--- /dev/null
+++ b/test/hash/tuple.cpp
@@ -0,0 +1,16 @@
+#include <stdplus/hash/tuple.hpp>
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+namespace stdplus
+{
+
+TEST(HashTuple, Basic)
+{
+ std::tuple<std::string, int> a{"bacon", 2};
+ std::hash<decltype(a)>{}(a);
+}
+
+} // namespace stdplus
diff --git a/test/meson.build b/test/meson.build
index 98435bf..ab788ff 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -4,6 +4,7 @@
'handle/copyable': [stdplus_dep, gtest_main_dep],
'handle/managed': [stdplus_dep, gtest_main_dep],
'hash': [stdplus_dep, gtest_main_dep],
+ 'hash/tuple': [stdplus_dep, gtest_main_dep],
'pinned': [stdplus_dep, gtest_main_dep],
'raw': [stdplus_dep, gmock_dep, gtest_main_dep],
'signal': [stdplus_dep, gtest_main_dep],