Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_HASH_MAP_H_ |
| 18 | #define ART_LIBARTBASE_BASE_HASH_MAP_H_ |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 19 | |
| 20 | #include <utility> |
| 21 | |
| 22 | #include "hash_set.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | template <typename Fn> |
| 27 | class HashMapWrapper { |
| 28 | public: |
| 29 | // Hash function. |
| 30 | template <class Key, class Value> |
| 31 | size_t operator()(const std::pair<Key, Value>& pair) const { |
| 32 | return fn_(pair.first); |
| 33 | } |
| 34 | template <class Key> |
| 35 | size_t operator()(const Key& key) const { |
| 36 | return fn_(key); |
| 37 | } |
| 38 | template <class Key, class Value> |
| 39 | bool operator()(const std::pair<Key, Value>& a, const std::pair<Key, Value>& b) const { |
| 40 | return fn_(a.first, b.first); |
| 41 | } |
| 42 | template <class Key, class Value, class Element> |
| 43 | bool operator()(const std::pair<Key, Value>& a, const Element& element) const { |
| 44 | return fn_(a.first, element); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | Fn fn_; |
| 49 | }; |
| 50 | |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 51 | template <class Key, |
| 52 | class Value, |
| 53 | class EmptyFn, |
| 54 | class HashFn = DefaultHashFn<Key>, |
| 55 | class Pred = DefaultPred<Key>, |
| 56 | class Alloc = std::allocator<std::pair<Key, Value>>> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 57 | class HashMap : public HashSet<std::pair<Key, Value>, |
| 58 | EmptyFn, |
| 59 | HashMapWrapper<HashFn>, |
| 60 | HashMapWrapper<Pred>, |
| 61 | Alloc> { |
| 62 | private: |
| 63 | using Base = HashSet<std::pair<Key, Value>, |
| 64 | EmptyFn, |
| 65 | HashMapWrapper<HashFn>, |
| 66 | HashMapWrapper<Pred>, |
| 67 | Alloc>; |
| 68 | |
| 69 | public: |
| 70 | HashMap() : Base() { } |
| 71 | explicit HashMap(const Alloc& alloc) |
| 72 | : Base(alloc) { } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace art |
| 76 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 77 | #endif // ART_LIBARTBASE_BASE_HASH_MAP_H_ |