blob: 8823c8bb4d97b17078b8838ec26ac62b0d80635b [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001/*
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
17#ifndef ART_LIBARTBASE_BASE_HASH_MAP_H_
18#define ART_LIBARTBASE_BASE_HASH_MAP_H_
19
20#include <utility>
21
22#include "hash_set.h"
23
24namespace art {
25
Paul Duffina82c23a2021-01-25 12:54:21 +000026template <typename Key, typename Value, typename HashFn>
27class HashMapHashWrapper {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000028 public:
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000029 size_t operator()(const Key& key) const {
Paul Duffina82c23a2021-01-25 12:54:21 +000030 return hash_fn_(key);
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000031 }
Paul Duffina82c23a2021-01-25 12:54:21 +000032
33 size_t operator()(const std::pair<Key, Value>& pair) const {
34 return hash_fn_(pair.first);
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000035 }
36
37 private:
Paul Duffina82c23a2021-01-25 12:54:21 +000038 HashFn hash_fn_;
39};
40
41template <typename Key, typename Value, typename PredFn>
42class HashMapPredWrapper {
43 public:
44 bool operator()(const std::pair<Key, Value>& a, const std::pair<Key, Value>& b) const {
45 return pred_fn_(a.first, b.first);
46 }
47
48 template <typename Element>
49 bool operator()(const std::pair<Key, Value>& a, const Element& element) const {
50 return pred_fn_(a.first, element);
51 }
52
53 private:
54 PredFn pred_fn_;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000055};
56
57template <typename Key, typename Value>
58class DefaultMapEmptyFn {
59 public:
60 void MakeEmpty(std::pair<Key, Value>& item) const {
61 item = std::pair<Key, Value>();
62 }
63 bool IsEmpty(const std::pair<Key, Value>& item) const {
64 return item.first == Key();
65 }
66};
67
68template <class Key,
69 class Value,
70 class EmptyFn = DefaultMapEmptyFn<Key, Value>,
71 class HashFn = DefaultHashFn<Key>,
72 class Pred = DefaultPred<Key>,
73 class Alloc = std::allocator<std::pair<Key, Value>>>
74class HashMap : public HashSet<std::pair<Key, Value>,
75 EmptyFn,
Paul Duffina82c23a2021-01-25 12:54:21 +000076 HashMapHashWrapper<Key, Value, HashFn>,
77 HashMapPredWrapper<Key, Value, Pred>,
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000078 Alloc> {
79 private:
80 using Base = HashSet<std::pair<Key, Value>,
81 EmptyFn,
Paul Duffina82c23a2021-01-25 12:54:21 +000082 HashMapHashWrapper<Key, Value, HashFn>,
83 HashMapPredWrapper<Key, Value, Pred>,
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000084 Alloc>;
85
86 public:
Martin Stjernholm361e5f02021-03-19 08:45:36 +000087 // Inherit constructors.
88 using Base::Base;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000089
90 // Used to insert a new mapping.
91 typename Base::iterator Overwrite(const Key& k, const Value& v) {
92 auto res = Base::insert({ k, v }).first;
93 *res = { k, v };
94 return res;
95 }
96};
97
98} // namespace art
99
100#endif // ART_LIBARTBASE_BASE_HASH_MAP_H_