diff --git a/src/tint/utils/hash.h b/src/tint/utils/hash.h index 1bc2edb6cb..3deb14bcc4 100644 --- a/src/tint/utils/hash.h +++ b/src/tint/utils/hash.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -45,6 +46,10 @@ struct HashCombineOffset<8> { } // namespace detail +// Forward declaration +template +size_t Hash(const ARGS&... args); + /// HashCombine "hashes" together an existing hash and hashable values. template void HashCombine(size_t* hash, const T& value) { @@ -61,6 +66,13 @@ void HashCombine(size_t* hash, const std::vector& vector) { } } +/// HashCombine "hashes" together an existing hash and hashable values. +template +void HashCombine(size_t* hash, const std::tuple& tuple) { + HashCombine(hash, sizeof...(TYPES)); + HashCombine(hash, std::apply(Hash, tuple)); +} + /// HashCombine "hashes" together an existing hash and hashable values. template void HashCombine(size_t* hash, const T& value, const ARGS&... args) { diff --git a/src/tint/utils/hash_test.cc b/src/tint/utils/hash_test.cc index 068ac454a0..cb74df95c7 100644 --- a/src/tint/utils/hash_test.cc +++ b/src/tint/utils/hash_test.cc @@ -15,6 +15,7 @@ #include "src/tint/utils/hash.h" #include +#include #include #include "gtest/gtest.h" @@ -41,6 +42,13 @@ TEST(HashTests, Vector) { EXPECT_NE(Hash(std::vector({1, 2, 3})), Hash(std::vector({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>;