mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-07 15:13:29 +00:00
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 <dsinclair@chromium.org> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
6a17e33f3d
commit
0c7c23b9c4
@ -496,4 +496,19 @@ inline f16 operator""_h(unsigned long long int value) { // NOLINT
|
|||||||
|
|
||||||
} // namespace tint::number_suffixes
|
} // namespace tint::number_suffixes
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
|
||||||
|
/// Custom std::hash specialization for tint::Number<T>
|
||||||
|
template <typename T>
|
||||||
|
class hash<tint::Number<T>> {
|
||||||
|
public:
|
||||||
|
/// @param n the Number
|
||||||
|
/// @return the hash value
|
||||||
|
inline std::size_t operator()(const tint::Number<T>& n) const {
|
||||||
|
return std::hash<decltype(n.value)>()(n.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
#endif // SRC_TINT_NUMBER_H_
|
#endif // SRC_TINT_NUMBER_H_
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "src/tint/utils/vector.h"
|
#include "src/tint/utils/vector.h"
|
||||||
@ -117,6 +118,16 @@ struct Hasher<std::tuple<TYPES...>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Hasher specialization for std::tuple
|
||||||
|
template <typename... TYPES>
|
||||||
|
struct Hasher<std::variant<TYPES...>> {
|
||||||
|
/// @param variant the variant to hash
|
||||||
|
/// @returns a hash of the tuple
|
||||||
|
size_t operator()(const std::variant<TYPES...>& variant) const {
|
||||||
|
return std::visit([](auto&& val) { return Hash(val); }, variant);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// @returns a hash of the variadic list of arguments.
|
/// @returns a hash of the variadic list of arguments.
|
||||||
/// The returned hash is dependent on the order of the arguments.
|
/// The returned hash is dependent on the order of the arguments.
|
||||||
template <typename... ARGS>
|
template <typename... ARGS>
|
||||||
|
@ -243,11 +243,11 @@ class Vector {
|
|||||||
|
|
||||||
/// Move constructor from a mutable vector reference
|
/// Move constructor from a mutable vector reference
|
||||||
/// @param other the vector reference to move
|
/// @param other the vector reference to move
|
||||||
explicit Vector(VectorRef<T>&& other) { MoveOrCopy(std::move(other)); }
|
Vector(VectorRef<T>&& other) { MoveOrCopy(std::move(other)); } // NOLINT(runtime/explicit)
|
||||||
|
|
||||||
/// Copy constructor from an immutable vector reference
|
/// Copy constructor from an immutable vector reference
|
||||||
/// @param other the vector reference to copy
|
/// @param other the vector reference to copy
|
||||||
explicit Vector(const VectorRef<T>& other) { Copy(other.slice_); }
|
Vector(const VectorRef<T>& other) { Copy(other.slice_); } // NOLINT(runtime/explicit)
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Vector() { ClearAndFree(); }
|
~Vector() { ClearAndFree(); }
|
||||||
@ -475,6 +475,12 @@ class Vector {
|
|||||||
return true;
|
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:
|
private:
|
||||||
/// Friend class (differing specializations of this class)
|
/// Friend class (differing specializations of this class)
|
||||||
template <typename, size_t>
|
template <typename, size_t>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user