summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2022-10-19 15:55:10 -0500
committerGitHub <noreply@github.com>2022-10-19 13:55:10 -0700
commit8f0c8ce9b4bcfe446d8908e3104d7992b9896a30 (patch)
treea122bee58b16d4798e25a08df5e1aac08d8a5a75 /src
parent829fa11c56d05335f441aab85953b4ef7ab56ba1 (diff)
downloadbinaryen-8f0c8ce9b4bcfe446d8908e3104d7992b9896a30.tar.gz
binaryen-8f0c8ce9b4bcfe446d8908e3104d7992b9896a30.tar.bz2
binaryen-8f0c8ce9b4bcfe446d8908e3104d7992b9896a30.zip
[NFC] Add a generic hash implementation for tuples (#5162)
We already provided a specialization of `std::hash` for arbitrary pairs, so add one for `std::tuple` as well. Use the new specialization where we were previously using nested pairs just to be able to use the pair specialization.
Diffstat (limited to 'src')
-rw-r--r--src/ir/possible-contents.h9
-rw-r--r--src/support/hash.h18
2 files changed, 22 insertions, 5 deletions
diff --git a/src/ir/possible-contents.h b/src/ir/possible-contents.h
index 8a8d9b635..cf92a578a 100644
--- a/src/ir/possible-contents.h
+++ b/src/ir/possible-contents.h
@@ -531,8 +531,8 @@ template<> struct hash<wasm::ResultLocation> {
template<> struct hash<wasm::BreakTargetLocation> {
size_t operator()(const wasm::BreakTargetLocation& loc) const {
- return std::hash<std::pair<size_t, std::pair<wasm::Name, wasm::Index>>>{}(
- {size_t(loc.func), {loc.target, loc.tupleIndex}});
+ return std::hash<std::tuple<size_t, wasm::Name, wasm::Index>>{}(
+ {size_t(loc.func), loc.target, loc.tupleIndex});
}
};
@@ -578,9 +578,8 @@ template<> struct hash<wasm::NullLocation> {
template<> struct hash<wasm::ConeReadLocation> {
size_t operator()(const wasm::ConeReadLocation& loc) const {
- return std::hash<
- std::pair<wasm::HeapType, std::pair<wasm::Index, wasm::Index>>>{}(
- {loc.type, {loc.depth, loc.index}});
+ return std::hash<std::tuple<wasm::HeapType, wasm::Index, wasm::Index>>{}(
+ {loc.type, loc.depth, loc.index});
}
};
diff --git a/src/support/hash.h b/src/support/hash.h
index fb0e710a5..fc8b358f2 100644
--- a/src/support/hash.h
+++ b/src/support/hash.h
@@ -61,6 +61,24 @@ template<typename T1, typename T2> struct hash<pair<T1, T2>> {
}
};
+// And hashing tuples is useful, too.
+template<typename T, typename... Ts> struct hash<tuple<T, Ts...>> {
+private:
+ template<size_t... I>
+ static void rehash(const tuple<T, Ts...>& tup,
+ size_t& digest,
+ std::index_sequence<I...>) {
+ (wasm::rehash(digest, std::get<1 + I>(tup)), ...);
+ }
+
+public:
+ size_t operator()(const tuple<T, Ts...>& tup) const {
+ auto digest = wasm::hash(std::get<0>(tup));
+ rehash(tup, digest, std::index_sequence_for<Ts...>{});
+ return digest;
+ }
+};
+
} // namespace std
#endif // wasm_support_hash_h