1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_VERIFIER_UTIL_MISC_HPP_
17 #define PANDA_VERIFIER_UTIL_MISC_HPP_
18
19 #include "libpandabase/utils/hash.h"
20
21 #include <functional>
22 #include <cstddef>
23 #include <tuple>
24
25 namespace panda::verifier {
26
27 template <typename T>
StdHash(const T & x)28 size_t StdHash(const T &x)
29 {
30 return std::hash<T> {}(x);
31 }
32
33 } // namespace panda::verifier
34
35 namespace std {
36
37 template <typename T1, typename T2>
38 struct hash<std::pair<T1, T2>> {
39 size_t operator()(const std::pair<T1, T2> &pair) const
40 {
41 return panda::merge_hashes(panda::verifier::StdHash(pair.first), panda::verifier::StdHash(pair.second));
42 }
43 };
44
45 template <typename... T>
46 struct hash<std::tuple<T...>> {
47 template <size_t N>
48 size_t Helper(size_t tmp_hash, const std::tuple<T...> &tuple) const
49 {
50 if constexpr (N < sizeof...(T)) {
51 size_t tmp_hash1 = panda::merge_hashes(tmp_hash, panda::verifier::StdHash(std::get<N>(tuple)));
52 return Helper<N + 1>(tmp_hash1, tuple);
53 }
54
55 return tmp_hash;
56 }
57
58 size_t operator()(const std::tuple<T...> &tuple) const
59 {
60 return Helper<1>(panda::verifier::StdHash(std::get<0>(tuple)), tuple);
61 }
62 };
63
64 } // namespace std
65
66 #endif // !PANDA_VERIFIER_UTIL_MISC_HPP_
67