Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_SAFE_MAP_H_ |
| 18 | #define ART_LIBARTBASE_BASE_SAFE_MAP_H_ |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <map> |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 21 | #include <memory> |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 22 | #include <type_traits> |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 26 | namespace 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 Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 30 | template <typename K, typename V, typename Comparator = std::less<K>, |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 31 | typename Allocator = std::allocator<std::pair<const K, V>>> |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 32 | class SafeMap { |
| 33 | private: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 34 | typedef SafeMap<K, V, Comparator, Allocator> Self; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 35 | |
| 36 | public: |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 37 | typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare; |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 38 | typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 39 | 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 Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 43 | typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 44 | typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type; |
| 45 | |
| 46 | SafeMap() = default; |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 47 | SafeMap(const SafeMap&) = default; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 48 | SafeMap(SafeMap&&) = default; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 49 | explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type()) |
| 50 | : map_(cmp, allocator) { |
| 51 | } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 53 | Self& operator=(const Self& rhs) { |
| 54 | map_ = rhs.map_; |
| 55 | return *this; |
| 56 | } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 57 | |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 58 | allocator_type get_allocator() const { return map_.get_allocator(); } |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 59 | key_compare key_comp() const { return map_.key_comp(); } |
| 60 | value_compare value_comp() const { return map_.value_comp(); } |
| 61 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 62 | 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 Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 70 | void swap(Self& other) { map_.swap(other.map_); } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 71 | void clear() { map_.clear(); } |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 72 | iterator erase(iterator it) { return map_.erase(it); } |
Alex Light | 1adb04f | 2019-06-13 16:22:31 -0700 | [diff] [blame] | 73 | template<typename Kv> size_type erase(const Kv& k) { return map_.erase(k); } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 74 | |
Alex Light | 1adb04f | 2019-06-13 16:22:31 -0700 | [diff] [blame] | 75 | template<typename Kv> iterator find(const Kv& k) { return map_.find(k); } |
| 76 | template<typename Kv> const_iterator find(const Kv& k) const { return map_.find(k); } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 77 | |
Alex Light | 1adb04f | 2019-06-13 16:22:31 -0700 | [diff] [blame] | 78 | template<typename Kv> iterator lower_bound(const Kv& k) { return map_.lower_bound(k); } |
| 79 | template<typename Kv> const_iterator lower_bound(const Kv& k) const { |
| 80 | return map_.lower_bound(k); |
| 81 | } |
Vladimir Marko | 2ac01fc | 2014-05-22 12:09:08 +0100 | [diff] [blame] | 82 | |
Alex Light | 1adb04f | 2019-06-13 16:22:31 -0700 | [diff] [blame] | 83 | template<typename Kv> iterator upper_bound(const Kv& k) { return map_.upper_bound(k); } |
| 84 | template<typename Kv> const_iterator upper_bound(const Kv& k) const { |
| 85 | return map_.upper_bound(k); |
| 86 | } |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 87 | |
Alex Light | 1adb04f | 2019-06-13 16:22:31 -0700 | [diff] [blame] | 88 | template<typename Kv> size_type count(const Kv& k) const { return map_.count(k); } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 89 | |
| 90 | // Note that unlike std::map's operator[], this doesn't return a reference to the value. |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 91 | V Get(const K& k) const { |
| 92 | const_iterator it = map_.find(k); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 93 | DCHECK(it != map_.end()); |
| 94 | return it->second; |
| 95 | } |
| 96 | |
| 97 | // Used to insert a new mapping. |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 98 | iterator Put(const K& k, const V& v) { |
| 99 | std::pair<iterator, bool> result = map_.emplace(k, v); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 100 | DCHECK(result.second); // Check we didn't accidentally overwrite an existing value. |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 101 | return result.first; |
| 102 | } |
Vladimir Marko | 13c7449 | 2015-11-24 17:06:32 +0000 | [diff] [blame] | 103 | iterator Put(const K& k, V&& v) { |
Vladimir Marko | 7856835 | 2015-09-21 12:00:16 +0100 | [diff] [blame] | 104 | std::pair<iterator, bool> result = map_.emplace(k, std::move(v)); |
| 105 | DCHECK(result.second); // Check we didn't accidentally overwrite an existing value. |
| 106 | return result.first; |
| 107 | } |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 108 | |
| 109 | // Used to insert a new mapping at a known position for better performance. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 110 | iterator PutBefore(const_iterator pos, const K& k, const V& v) { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 111 | // Check that we're using the correct position and the key is not in the map. |
| 112 | DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first)); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 113 | DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k)); |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 114 | return map_.emplace_hint(pos, k, v); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 115 | } |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 116 | iterator PutBefore(const_iterator pos, const K& k, V&& v) { |
Vladimir Marko | 7856835 | 2015-09-21 12:00:16 +0100 | [diff] [blame] | 117 | // Check that we're using the correct position and the key is not in the map. |
| 118 | DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first)); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 119 | DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k)); |
Vladimir Marko | 7856835 | 2015-09-21 12:00:16 +0100 | [diff] [blame] | 120 | return map_.emplace_hint(pos, k, std::move(v)); |
| 121 | } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 122 | |
| 123 | // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type |
| 124 | // of this container is a pointer, any overwritten pointer will be lost and if this container |
Vladimir Marko | 7856835 | 2015-09-21 12:00:16 +0100 | [diff] [blame] | 125 | // was the owner, you have a leak. Returns iterator pointing to the new or overwritten entry. |
Jeff Hao | 4686c52 | 2015-09-18 14:44:32 -0700 | [diff] [blame] | 126 | iterator Overwrite(const K& k, const V& v) { |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 127 | std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v)); |
| 128 | if (!result.second) { |
| 129 | // Already there - update the value for the existing key |
| 130 | result.first->second = v; |
| 131 | } |
Jeff Hao | 4686c52 | 2015-09-18 14:44:32 -0700 | [diff] [blame] | 132 | return result.first; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 135 | template <typename CreateFn> |
Alex Light | 184f075 | 2018-07-13 11:18:22 -0700 | [diff] [blame] | 136 | V& GetOrCreate(const K& k, CreateFn create) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 137 | static_assert(std::is_same<V, typename std::result_of<CreateFn()>::type>::value, |
| 138 | "Argument `create` should return a value of type V."); |
| 139 | auto lb = lower_bound(k); |
| 140 | if (lb != end() && !key_comp()(k, lb->first)) { |
| 141 | return lb->second; |
| 142 | } |
| 143 | auto it = PutBefore(lb, k, create()); |
| 144 | return it->second; |
| 145 | } |
| 146 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 147 | iterator FindOrAdd(const K& k, const V& v) { |
| 148 | iterator it = find(k); |
| 149 | return it == end() ? Put(k, v) : it; |
| 150 | } |
| 151 | |
| 152 | iterator FindOrAdd(const K& k) { |
| 153 | iterator it = find(k); |
| 154 | return it == end() ? Put(k, V()) : it; |
| 155 | } |
| 156 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 157 | bool Equals(const Self& rhs) const { |
| 158 | return map_ == rhs.map_; |
| 159 | } |
| 160 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 161 | template <class... Args> |
| 162 | std::pair<iterator, bool> emplace(Args&&... args) { |
| 163 | return map_.emplace(std::forward<Args>(args)...); |
| 164 | } |
| 165 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 166 | private: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 167 | ::std::map<K, V, Comparator, Allocator> map_; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 170 | template <typename K, typename V, typename Comparator, typename Allocator> |
| 171 | bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs, |
| 172 | const SafeMap<K, V, Comparator, Allocator>& rhs) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 173 | return lhs.Equals(rhs); |
| 174 | } |
| 175 | |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 176 | template <typename K, typename V, typename Comparator, typename Allocator> |
| 177 | bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs, |
| 178 | const SafeMap<K, V, Comparator, Allocator>& rhs) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 179 | return !(lhs == rhs); |
| 180 | } |
| 181 | |
| 182 | } // namespace art |
| 183 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 184 | #endif // ART_LIBARTBASE_BASE_SAFE_MAP_H_ |