1 /**
2 * Copyright (c) 2021-2024 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 ark::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 ark::verifier
34
35 namespace std {
36
37 template <typename T1, typename T2>
38 struct hash<std::pair<T1, T2>> {
operator ()ark::std::hash39 size_t operator()(const std::pair<T1, T2> &pair) const
40 {
41 return ark::MergeHashes(ark::verifier::StdHash(pair.first),
42 ark::verifier::StdHash(pair.second)); // NOLINT
43 }
44 };
45
46 template <typename... T>
47 struct hash<std::tuple<T...>> {
48 template <size_t N>
Helperark::std::hash49 size_t Helper(size_t tmpHash, const std::tuple<T...> &tuple) const
50 {
51 if constexpr (N < sizeof...(T)) {
52 size_t tmp_hash1 = ark::MergeHashes(tmpHash, ark::verifier::StdHash(std::get<N>(tuple))); // NOLINT
53 return Helper<N + 1>(tmp_hash1, tuple);
54 }
55
56 return tmpHash;
57 }
58
59 size_t operator()(const std::tuple<T...> &tuple) const
60 {
61 return Helper<1>(ark::verifier::StdHash(std::get<0>(tuple)), tuple);
62 }
63 };
64
65 } // namespace std
66
67 #endif // !PANDA_VERIFIER_UTIL_MISC_HPP_
68