tint: Support tuples in utils::Hash().

Change-Id: I6c4da42dae52d7e02de8f59756949d476935e6b4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/90527
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2022-05-19 17:59:19 +00:00 committed by Dawn LUCI CQ
parent e61a390b31
commit b0664684cd
2 changed files with 20 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include <stdint.h>
#include <cstdio>
#include <functional>
#include <tuple>
#include <utility>
#include <vector>
@ -45,6 +46,10 @@ struct HashCombineOffset<8> {
} // namespace detail
// Forward declaration
template <typename... ARGS>
size_t Hash(const ARGS&... args);
/// HashCombine "hashes" together an existing hash and hashable values.
template <typename T>
void HashCombine(size_t* hash, const T& value) {
@ -61,6 +66,13 @@ void HashCombine(size_t* hash, const std::vector<T>& vector) {
}
}
/// HashCombine "hashes" together an existing hash and hashable values.
template <typename... TYPES>
void HashCombine(size_t* hash, const std::tuple<TYPES...>& tuple) {
HashCombine(hash, sizeof...(TYPES));
HashCombine(hash, std::apply(Hash<TYPES...>, tuple));
}
/// HashCombine "hashes" together an existing hash and hashable values.
template <typename T, typename... ARGS>
void HashCombine(size_t* hash, const T& value, const ARGS&... args) {

View File

@ -15,6 +15,7 @@
#include "src/tint/utils/hash.h"
#include <string>
#include <tuple>
#include <unordered_map>
#include "gtest/gtest.h"
@ -41,6 +42,13 @@ TEST(HashTests, Vector) {
EXPECT_NE(Hash(std::vector<int>({1, 2, 3})), Hash(std::vector<int>({1, 2, 3, 4})));
}
TEST(HashTests, Tuple) {
EXPECT_EQ(Hash(std::make_tuple(1)), Hash(std::make_tuple(1)));
EXPECT_EQ(Hash(std::make_tuple(1, 2, 3)), Hash(std::make_tuple(1, 2, 3)));
EXPECT_NE(Hash(std::make_tuple(1, 2, 3)), Hash(std::make_tuple(1, 2, 4)));
EXPECT_NE(Hash(std::make_tuple(1, 2, 3)), Hash(std::make_tuple(1, 2, 3, 4)));
}
TEST(HashTests, UnorderedKeyWrapper) {
using W = UnorderedKeyWrapper<std::vector<int>>;