Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
David Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBDEXFILE_DEX_DEX_FILE_REFERENCE_H_ |
| 18 | #define ART_LIBDEXFILE_DEX_DEX_FILE_REFERENCE_H_ |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 19 | |
Alex Light | a2f1319 | 2021-02-03 18:19:03 -0800 | [diff] [blame] | 20 | #include <cstddef> |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 21 | #include <cstdint> |
Alex Light | a2f1319 | 2021-02-03 18:19:03 -0800 | [diff] [blame] | 22 | #include <utility> |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class DexFile; |
| 27 | |
| 28 | class DexFileReference { |
| 29 | public: |
| 30 | DexFileReference(const DexFile* file, uint32_t idx) : dex_file(file), index(idx) {} |
| 31 | const DexFile* dex_file; |
| 32 | uint32_t index; |
| 33 | |
| 34 | struct Comparator { |
| 35 | bool operator()(const DexFileReference& a, const DexFileReference& b) const { |
| 36 | if (a.dex_file != b.dex_file) { |
| 37 | return a.dex_file < b.dex_file; |
| 38 | } |
| 39 | return a.index < b.index; |
| 40 | } |
| 41 | }; |
| 42 | }; |
| 43 | |
Alex Light | 1e52a07 | 2019-06-25 09:12:04 -0700 | [diff] [blame] | 44 | // Default comparators, compares the indices, not the backing data. |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 45 | inline bool operator<(const DexFileReference& a, const DexFileReference& b) { |
| 46 | return DexFileReference::Comparator()(a, b); |
| 47 | } |
| 48 | inline bool operator==(const DexFileReference& a, const DexFileReference& b) { |
| 49 | return a.dex_file == b.dex_file && a.index == b.index; |
| 50 | } |
Alex Light | a2f1319 | 2021-02-03 18:19:03 -0800 | [diff] [blame] | 51 | inline bool operator!=(const DexFileReference& a, const DexFileReference& b) { |
| 52 | return !(a == b); |
| 53 | } |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 54 | |
| 55 | } // namespace art |
| 56 | |
Alex Light | a2f1319 | 2021-02-03 18:19:03 -0800 | [diff] [blame] | 57 | namespace std { |
| 58 | // Hash implementation used for std::set/std::map. Simply xor the hash of the dex-file and index |
| 59 | template <> |
| 60 | struct hash<art::DexFileReference> { |
| 61 | size_t operator()(const art::DexFileReference& ref) const { |
| 62 | return hash<decltype(ref.dex_file)>()(ref.dex_file) ^ |
| 63 | hash<decltype(ref.index)>()(ref.index); |
| 64 | } |
| 65 | }; |
| 66 | } // namespace std |
| 67 | |
David Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 68 | #endif // ART_LIBDEXFILE_DEX_DEX_FILE_REFERENCE_H_ |