From 0c7c23b9c4b84d0c241b4e970116d7f89c3fa4b6 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 31 Aug 2022 23:51:48 +0000 Subject: [PATCH] tint: Misc hash / container contract improvements Add a hash implementation to tint::Number. Add a utils::Hasher specialization for std::variant. Add an operator!= for Vector. Needed for std::variants. Drop the need for explicit on Vector constructors from refs. These all help general usage with STL and util containers. Change-Id: I1e594edf532e78f531062c534dacaee7616cded5 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/100905 Reviewed-by: Dan Sinclair Commit-Queue: Ben Clayton --- src/tint/number.h | 15 +++++++++++++++ src/tint/utils/hash.h | 11 +++++++++++ src/tint/utils/vector.h | 10 ++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/tint/number.h b/src/tint/number.h index a36ccfbab0..4635051177 100644 --- a/src/tint/number.h +++ b/src/tint/number.h @@ -496,4 +496,19 @@ inline f16 operator""_h(unsigned long long int value) { // NOLINT } // namespace tint::number_suffixes +namespace std { + +/// Custom std::hash specialization for tint::Number +template +class hash> { + public: + /// @param n the Number + /// @return the hash value + inline std::size_t operator()(const tint::Number& n) const { + return std::hash()(n.value); + } +}; + +} // namespace std + #endif // SRC_TINT_NUMBER_H_ diff --git a/src/tint/utils/hash.h b/src/tint/utils/hash.h index ad53841f5f..717b35f6fd 100644 --- a/src/tint/utils/hash.h +++ b/src/tint/utils/hash.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "src/tint/utils/vector.h" @@ -117,6 +118,16 @@ struct Hasher> { } }; +/// Hasher specialization for std::tuple +template +struct Hasher> { + /// @param variant the variant to hash + /// @returns a hash of the tuple + size_t operator()(const std::variant& variant) const { + return std::visit([](auto&& val) { return Hash(val); }, variant); + } +}; + /// @returns a hash of the variadic list of arguments. /// The returned hash is dependent on the order of the arguments. template diff --git a/src/tint/utils/vector.h b/src/tint/utils/vector.h index b718840a5c..f0cbf58a4e 100644 --- a/src/tint/utils/vector.h +++ b/src/tint/utils/vector.h @@ -243,11 +243,11 @@ class Vector { /// Move constructor from a mutable vector reference /// @param other the vector reference to move - explicit Vector(VectorRef&& other) { MoveOrCopy(std::move(other)); } + Vector(VectorRef&& other) { MoveOrCopy(std::move(other)); } // NOLINT(runtime/explicit) /// Copy constructor from an immutable vector reference /// @param other the vector reference to copy - explicit Vector(const VectorRef& other) { Copy(other.slice_); } + Vector(const VectorRef& other) { Copy(other.slice_); } // NOLINT(runtime/explicit) /// Destructor ~Vector() { ClearAndFree(); } @@ -475,6 +475,12 @@ class Vector { return true; } + /// Inequality operator + /// @param other the other vector + /// @returns true if this vector is not the same length as `other`, or all elements are not + /// equal. + bool operator!=(const Vector& other) const { return !(*this == other); } + private: /// Friend class (differing specializations of this class) template