blob: f29869172ebccbd3b9889ad91a3356b0c30bf713 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_SAFE_MAP_H_
18#define ART_RUNTIME_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
Mathieu Chartierbad02672014-08-25 13:08:22 -070024#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070026
27namespace art {
28
29// Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
30// the implicit insertion of a default-constructed value on failed lookups).
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070031template <typename K, typename V, typename Comparator = std::less<K>,
Mathieu Chartierbad02672014-08-25 13:08:22 -070032 typename Allocator = TrackingAllocator<std::pair<const K, V>, kAllocatorTagSafeMap>>
Elliott Hughesa0e18062012-04-13 15:59:59 -070033class SafeMap {
34 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070035 typedef SafeMap<K, V, Comparator, Allocator> Self;
Elliott Hughesa0e18062012-04-13 15:59:59 -070036
37 public:
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000038 typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare;
Vladimir Markobd72fc12014-07-09 16:06:40 +010039 typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000040 typedef typename ::std::map<K, V, Comparator, Allocator>::allocator_type allocator_type;
41 typedef typename ::std::map<K, V, Comparator, Allocator>::iterator iterator;
42 typedef typename ::std::map<K, V, Comparator, Allocator>::const_iterator const_iterator;
43 typedef typename ::std::map<K, V, Comparator, Allocator>::size_type size_type;
Vladimir Markobd72fc12014-07-09 16:06:40 +010044 typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000045 typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type;
46
47 SafeMap() = default;
Andreas Gampe758a8012015-04-03 21:28:42 -070048 SafeMap(const SafeMap&) = default;
Mathieu Chartierea650f32017-05-24 12:04:13 -070049 SafeMap(SafeMap&&) = default;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000050 explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
51 : map_(cmp, allocator) {
52 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070053
Elliott Hughes74847412012-06-20 18:10:21 -070054 Self& operator=(const Self& rhs) {
55 map_ = rhs.map_;
56 return *this;
57 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070058
Vladimir Markob19955d2014-07-29 12:04:10 +010059 allocator_type get_allocator() const { return map_.get_allocator(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010060 key_compare key_comp() const { return map_.key_comp(); }
61 value_compare value_comp() const { return map_.value_comp(); }
62
Elliott Hughesa0e18062012-04-13 15:59:59 -070063 iterator begin() { return map_.begin(); }
64 const_iterator begin() const { return map_.begin(); }
65 iterator end() { return map_.end(); }
66 const_iterator end() const { return map_.end(); }
67
68 bool empty() const { return map_.empty(); }
69 size_type size() const { return map_.size(); }
70
Vladimir Markobd72fc12014-07-09 16:06:40 +010071 void swap(Self& other) { map_.swap(other.map_); }
Ian Rogers08f753d2012-08-24 14:35:25 -070072 void clear() { map_.clear(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010073 iterator erase(iterator it) { return map_.erase(it); }
Elliott Hughesa0e18062012-04-13 15:59:59 -070074 size_type erase(const K& k) { return map_.erase(k); }
75
76 iterator find(const K& k) { return map_.find(k); }
77 const_iterator find(const K& k) const { return map_.find(k); }
78
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010079 iterator lower_bound(const K& k) { return map_.lower_bound(k); }
80 const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
81
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070082 iterator upper_bound(const K& k) { return map_.upper_bound(k); }
83 const_iterator upper_bound(const K& k) const { return map_.upper_bound(k); }
84
Elliott Hughesa0e18062012-04-13 15:59:59 -070085 size_type count(const K& k) const { return map_.count(k); }
86
87 // Note that unlike std::map's operator[], this doesn't return a reference to the value.
TDYa127b2eb5c12012-05-24 15:52:10 -070088 V Get(const K& k) const {
89 const_iterator it = map_.find(k);
Elliott Hughesa0e18062012-04-13 15:59:59 -070090 DCHECK(it != map_.end());
91 return it->second;
92 }
93
94 // Used to insert a new mapping.
Vladimir Markobd72fc12014-07-09 16:06:40 +010095 iterator Put(const K& k, const V& v) {
96 std::pair<iterator, bool> result = map_.emplace(k, v);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070097 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
Vladimir Markobd72fc12014-07-09 16:06:40 +010098 return result.first;
99 }
Vladimir Marko13c74492015-11-24 17:06:32 +0000100 iterator Put(const K& k, V&& v) {
Vladimir Marko78568352015-09-21 12:00:16 +0100101 std::pair<iterator, bool> result = map_.emplace(k, std::move(v));
102 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
103 return result.first;
104 }
Vladimir Markobd72fc12014-07-09 16:06:40 +0100105
106 // Used to insert a new mapping at a known position for better performance.
Vladimir Marko944da602016-02-19 12:27:55 +0000107 iterator PutBefore(const_iterator pos, const K& k, const V& v) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100108 // Check that we're using the correct position and the key is not in the map.
109 DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
Vladimir Marko944da602016-02-19 12:27:55 +0000110 DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
Vladimir Markobd72fc12014-07-09 16:06:40 +0100111 return map_.emplace_hint(pos, k, v);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700112 }
Vladimir Marko944da602016-02-19 12:27:55 +0000113 iterator PutBefore(const_iterator pos, const K& k, V&& v) {
Vladimir Marko78568352015-09-21 12:00:16 +0100114 // Check that we're using the correct position and the key is not in the map.
115 DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
Vladimir Marko944da602016-02-19 12:27:55 +0000116 DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
Vladimir Marko78568352015-09-21 12:00:16 +0100117 return map_.emplace_hint(pos, k, std::move(v));
118 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700119
120 // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
121 // of this container is a pointer, any overwritten pointer will be lost and if this container
Vladimir Marko78568352015-09-21 12:00:16 +0100122 // was the owner, you have a leak. Returns iterator pointing to the new or overwritten entry.
Jeff Hao4686c522015-09-18 14:44:32 -0700123 iterator Overwrite(const K& k, const V& v) {
buzbeed1643e42012-09-05 14:06:51 -0700124 std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
125 if (!result.second) {
126 // Already there - update the value for the existing key
127 result.first->second = v;
128 }
Jeff Hao4686c522015-09-18 14:44:32 -0700129 return result.first;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700130 }
131
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000132 template <typename CreateFn>
133 V GetOrCreate(const K& k, CreateFn create) {
134 static_assert(std::is_same<V, typename std::result_of<CreateFn()>::type>::value,
135 "Argument `create` should return a value of type V.");
136 auto lb = lower_bound(k);
137 if (lb != end() && !key_comp()(k, lb->first)) {
138 return lb->second;
139 }
140 auto it = PutBefore(lb, k, create());
141 return it->second;
142 }
143
Calin Juravle940eb0c2017-01-30 19:30:44 -0800144 iterator FindOrAdd(const K& k, const V& v) {
145 iterator it = find(k);
146 return it == end() ? Put(k, v) : it;
147 }
148
149 iterator FindOrAdd(const K& k) {
150 iterator it = find(k);
151 return it == end() ? Put(k, V()) : it;
152 }
153
Elliott Hughesa0e18062012-04-13 15:59:59 -0700154 bool Equals(const Self& rhs) const {
155 return map_ == rhs.map_;
156 }
157
Mathieu Chartierea650f32017-05-24 12:04:13 -0700158 template <class... Args>
159 std::pair<iterator, bool> emplace(Args&&... args) {
160 return map_.emplace(std::forward<Args>(args)...);
161 }
162
Elliott Hughesa0e18062012-04-13 15:59:59 -0700163 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700164 ::std::map<K, V, Comparator, Allocator> map_;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700165};
166
Vladimir Markobd72fc12014-07-09 16:06:40 +0100167template <typename K, typename V, typename Comparator, typename Allocator>
168bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
169 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700170 return lhs.Equals(rhs);
171}
172
Vladimir Markobd72fc12014-07-09 16:06:40 +0100173template <typename K, typename V, typename Comparator, typename Allocator>
174bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
175 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700176 return !(lhs == rhs);
177}
178
Mathieu Chartierbad02672014-08-25 13:08:22 -0700179template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
180class AllocationTrackingSafeMap : public SafeMap<
Dan Albertafeb3092016-01-12 14:23:14 -0800181 Key, T, Compare, TrackingAllocator<std::pair<const Key, T>, kTag>> {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700182};
183
Elliott Hughesa0e18062012-04-13 15:59:59 -0700184} // namespace art
185
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700186#endif // ART_RUNTIME_SAFE_MAP_H_