blob: a4d845996d6f4355244a4191b104b36b7b9a127e [file] [log] [blame]
Elliott Hughes74847412012-06-20 18:10:21 -07001/*
2 * Copyright (C) 2012 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 Sehr67bf42e2018-02-26 16:43:04 -080017#ifndef ART_LIBARTBASE_BASE_SAFE_MAP_H_
18#define ART_LIBARTBASE_BASE_SAFE_MAP_H_
Elliott Hughesa0e18062012-04-13 15:59:59 -070019
20#include <map>
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070021#include <memory>
Vladimir Markocac5a7e2016-02-22 10:39:50 +000022#include <type_traits>
Elliott Hughesa0e18062012-04-13 15:59:59 -070023
Andreas Gampe57943812017-12-06 21:39:13 -080024#include <android-base/logging.h>
25
Elliott Hughesa0e18062012-04-13 15:59:59 -070026namespace art {
27
28// Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
29// the implicit insertion of a default-constructed value on failed lookups).
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070030template <typename K, typename V, typename Comparator = std::less<K>,
David Sehr67bf42e2018-02-26 16:43:04 -080031 typename Allocator = std::allocator<std::pair<const K, V>>>
Elliott Hughesa0e18062012-04-13 15:59:59 -070032class SafeMap {
33 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070034 typedef SafeMap<K, V, Comparator, Allocator> Self;
Elliott Hughesa0e18062012-04-13 15:59:59 -070035
36 public:
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000037 typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare;
Vladimir Markobd72fc12014-07-09 16:06:40 +010038 typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000039 typedef typename ::std::map<K, V, Comparator, Allocator>::allocator_type allocator_type;
40 typedef typename ::std::map<K, V, Comparator, Allocator>::iterator iterator;
41 typedef typename ::std::map<K, V, Comparator, Allocator>::const_iterator const_iterator;
42 typedef typename ::std::map<K, V, Comparator, Allocator>::size_type size_type;
Vladimir Markobd72fc12014-07-09 16:06:40 +010043 typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000044 typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type;
45
46 SafeMap() = default;
Andreas Gampe758a8012015-04-03 21:28:42 -070047 SafeMap(const SafeMap&) = default;
Mathieu Chartierea650f32017-05-24 12:04:13 -070048 SafeMap(SafeMap&&) = default;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000049 explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
50 : map_(cmp, allocator) {
51 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070052
Elliott Hughes74847412012-06-20 18:10:21 -070053 Self& operator=(const Self& rhs) {
54 map_ = rhs.map_;
55 return *this;
56 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070057
Vladimir Markob19955d2014-07-29 12:04:10 +010058 allocator_type get_allocator() const { return map_.get_allocator(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010059 key_compare key_comp() const { return map_.key_comp(); }
60 value_compare value_comp() const { return map_.value_comp(); }
61
Elliott Hughesa0e18062012-04-13 15:59:59 -070062 iterator begin() { return map_.begin(); }
63 const_iterator begin() const { return map_.begin(); }
64 iterator end() { return map_.end(); }
65 const_iterator end() const { return map_.end(); }
66
67 bool empty() const { return map_.empty(); }
68 size_type size() const { return map_.size(); }
69
Vladimir Markobd72fc12014-07-09 16:06:40 +010070 void swap(Self& other) { map_.swap(other.map_); }
Ian Rogers08f753d2012-08-24 14:35:25 -070071 void clear() { map_.clear(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010072 iterator erase(iterator it) { return map_.erase(it); }
Elliott Hughesa0e18062012-04-13 15:59:59 -070073 size_type erase(const K& k) { return map_.erase(k); }
74
75 iterator find(const K& k) { return map_.find(k); }
76 const_iterator find(const K& k) const { return map_.find(k); }
77
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010078 iterator lower_bound(const K& k) { return map_.lower_bound(k); }
79 const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
80
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070081 iterator upper_bound(const K& k) { return map_.upper_bound(k); }
82 const_iterator upper_bound(const K& k) const { return map_.upper_bound(k); }
83
Elliott Hughesa0e18062012-04-13 15:59:59 -070084 size_type count(const K& k) const { return map_.count(k); }
85
86 // Note that unlike std::map's operator[], this doesn't return a reference to the value.
TDYa127b2eb5c12012-05-24 15:52:10 -070087 V Get(const K& k) const {
88 const_iterator it = map_.find(k);
Elliott Hughesa0e18062012-04-13 15:59:59 -070089 DCHECK(it != map_.end());
90 return it->second;
91 }
92
93 // Used to insert a new mapping.
Vladimir Markobd72fc12014-07-09 16:06:40 +010094 iterator Put(const K& k, const V& v) {
95 std::pair<iterator, bool> result = map_.emplace(k, v);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070096 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
Vladimir Markobd72fc12014-07-09 16:06:40 +010097 return result.first;
98 }
Vladimir Marko13c74492015-11-24 17:06:32 +000099 iterator Put(const K& k, V&& v) {
Vladimir Marko78568352015-09-21 12:00:16 +0100100 std::pair<iterator, bool> result = map_.emplace(k, std::move(v));
101 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
102 return result.first;
103 }
Vladimir Markobd72fc12014-07-09 16:06:40 +0100104
105 // Used to insert a new mapping at a known position for better performance.
Vladimir Marko944da602016-02-19 12:27:55 +0000106 iterator PutBefore(const_iterator pos, const K& k, const V& v) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100107 // Check that we're using the correct position and the key is not in the map.
108 DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
Vladimir Marko944da602016-02-19 12:27:55 +0000109 DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
Vladimir Markobd72fc12014-07-09 16:06:40 +0100110 return map_.emplace_hint(pos, k, v);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700111 }
Vladimir Marko944da602016-02-19 12:27:55 +0000112 iterator PutBefore(const_iterator pos, const K& k, V&& v) {
Vladimir Marko78568352015-09-21 12:00:16 +0100113 // Check that we're using the correct position and the key is not in the map.
114 DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
Vladimir Marko944da602016-02-19 12:27:55 +0000115 DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
Vladimir Marko78568352015-09-21 12:00:16 +0100116 return map_.emplace_hint(pos, k, std::move(v));
117 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700118
119 // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
120 // of this container is a pointer, any overwritten pointer will be lost and if this container
Vladimir Marko78568352015-09-21 12:00:16 +0100121 // was the owner, you have a leak. Returns iterator pointing to the new or overwritten entry.
Jeff Hao4686c522015-09-18 14:44:32 -0700122 iterator Overwrite(const K& k, const V& v) {
buzbeed1643e42012-09-05 14:06:51 -0700123 std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
124 if (!result.second) {
125 // Already there - update the value for the existing key
126 result.first->second = v;
127 }
Jeff Hao4686c522015-09-18 14:44:32 -0700128 return result.first;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700129 }
130
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000131 template <typename CreateFn>
Alex Light184f0752018-07-13 11:18:22 -0700132 V& GetOrCreate(const K& k, CreateFn create) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000133 static_assert(std::is_same<V, typename std::result_of<CreateFn()>::type>::value,
134 "Argument `create` should return a value of type V.");
135 auto lb = lower_bound(k);
136 if (lb != end() && !key_comp()(k, lb->first)) {
137 return lb->second;
138 }
139 auto it = PutBefore(lb, k, create());
140 return it->second;
141 }
142
Calin Juravle940eb0c2017-01-30 19:30:44 -0800143 iterator FindOrAdd(const K& k, const V& v) {
144 iterator it = find(k);
145 return it == end() ? Put(k, v) : it;
146 }
147
148 iterator FindOrAdd(const K& k) {
149 iterator it = find(k);
150 return it == end() ? Put(k, V()) : it;
151 }
152
Elliott Hughesa0e18062012-04-13 15:59:59 -0700153 bool Equals(const Self& rhs) const {
154 return map_ == rhs.map_;
155 }
156
Mathieu Chartierea650f32017-05-24 12:04:13 -0700157 template <class... Args>
158 std::pair<iterator, bool> emplace(Args&&... args) {
159 return map_.emplace(std::forward<Args>(args)...);
160 }
161
Elliott Hughesa0e18062012-04-13 15:59:59 -0700162 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700163 ::std::map<K, V, Comparator, Allocator> map_;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700164};
165
Vladimir Markobd72fc12014-07-09 16:06:40 +0100166template <typename K, typename V, typename Comparator, typename Allocator>
167bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
168 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700169 return lhs.Equals(rhs);
170}
171
Vladimir Markobd72fc12014-07-09 16:06:40 +0100172template <typename K, typename V, typename Comparator, typename Allocator>
173bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
174 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700175 return !(lhs == rhs);
176}
177
178} // namespace art
179
David Sehr67bf42e2018-02-26 16:43:04 -0800180#endif // ART_LIBARTBASE_BASE_SAFE_MAP_H_