• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef ART_LIBARTBASE_BASE_SAFE_MAP_H_
18 #define ART_LIBARTBASE_BASE_SAFE_MAP_H_
19 
20 #include <map>
21 #include <memory>
22 #include <type_traits>
23 
24 #include <android-base/logging.h>
25 
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).
30 template <typename K, typename V, typename Comparator = std::less<K>,
31           typename Allocator = std::allocator<std::pair<const K, V>>>
32 class SafeMap {
33  private:
34   typedef SafeMap<K, V, Comparator, Allocator> Self;
35 
36  public:
37   typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare;
38   typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare;
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;
43   typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type;
44   typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type;
45 
46   SafeMap() = default;
47   SafeMap(const SafeMap&) = default;
48   SafeMap(SafeMap&&) = default;
49   explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
map_(cmp,allocator)50     : map_(cmp, allocator) {
51   }
52 
53   Self& operator=(const Self& rhs) {
54     map_ = rhs.map_;
55     return *this;
56   }
57 
get_allocator()58   allocator_type get_allocator() const { return map_.get_allocator(); }
key_comp()59   key_compare key_comp() const { return map_.key_comp(); }
value_comp()60   value_compare value_comp() const { return map_.value_comp(); }
61 
begin()62   iterator begin() { return map_.begin(); }
begin()63   const_iterator begin() const { return map_.begin(); }
end()64   iterator end() { return map_.end(); }
end()65   const_iterator end() const { return map_.end(); }
66 
empty()67   bool empty() const { return map_.empty(); }
size()68   size_type size() const { return map_.size(); }
69 
swap(Self & other)70   void swap(Self& other) { map_.swap(other.map_); }
clear()71   void clear() { map_.clear(); }
erase(iterator it)72   iterator erase(iterator it) { return map_.erase(it); }
erase(const K & k)73   size_type erase(const K& k) { return map_.erase(k); }
74 
find(const K & k)75   iterator find(const K& k) { return map_.find(k); }
find(const K & k)76   const_iterator find(const K& k) const { return map_.find(k); }
77 
lower_bound(const K & k)78   iterator lower_bound(const K& k) { return map_.lower_bound(k); }
lower_bound(const K & k)79   const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
80 
upper_bound(const K & k)81   iterator upper_bound(const K& k) { return map_.upper_bound(k); }
upper_bound(const K & k)82   const_iterator upper_bound(const K& k) const { return map_.upper_bound(k); }
83 
count(const K & k)84   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.
Get(const K & k)87   V Get(const K& k) const {
88     const_iterator it = map_.find(k);
89     DCHECK(it != map_.end());
90     return it->second;
91   }
92 
93   // Used to insert a new mapping.
Put(const K & k,const V & v)94   iterator Put(const K& k, const V& v) {
95     std::pair<iterator, bool> result = map_.emplace(k, v);
96     DCHECK(result.second);  // Check we didn't accidentally overwrite an existing value.
97     return result.first;
98   }
Put(const K & k,V && v)99   iterator Put(const K& k, V&& v) {
100     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   }
104 
105   // Used to insert a new mapping at a known position for better performance.
PutBefore(const_iterator pos,const K & k,const V & v)106   iterator PutBefore(const_iterator pos, const K& k, const V& v) {
107     // 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));
109     DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
110     return map_.emplace_hint(pos, k, v);
111   }
PutBefore(const_iterator pos,const K & k,V && v)112   iterator PutBefore(const_iterator pos, const K& k, V&& v) {
113     // 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));
115     DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
116     return map_.emplace_hint(pos, k, std::move(v));
117   }
118 
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
121   // was the owner, you have a leak. Returns iterator pointing to the new or overwritten entry.
Overwrite(const K & k,const V & v)122   iterator Overwrite(const K& k, const V& v) {
123     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     }
128     return result.first;
129   }
130 
131   template <typename CreateFn>
GetOrCreate(const K & k,CreateFn create)132   V& GetOrCreate(const K& k, CreateFn create) {
133     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 
FindOrAdd(const K & k,const V & v)143   iterator FindOrAdd(const K& k, const V& v) {
144     iterator it = find(k);
145     return it == end() ? Put(k, v) : it;
146   }
147 
FindOrAdd(const K & k)148   iterator FindOrAdd(const K& k) {
149     iterator it = find(k);
150     return it == end() ? Put(k, V()) : it;
151   }
152 
Equals(const Self & rhs)153   bool Equals(const Self& rhs) const {
154     return map_ == rhs.map_;
155   }
156 
157   template <class... Args>
emplace(Args &&...args)158   std::pair<iterator, bool> emplace(Args&&... args) {
159     return map_.emplace(std::forward<Args>(args)...);
160   }
161 
162  private:
163   ::std::map<K, V, Comparator, Allocator> map_;
164 };
165 
166 template <typename K, typename V, typename Comparator, typename Allocator>
167 bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
168                 const SafeMap<K, V, Comparator, Allocator>& rhs) {
169   return lhs.Equals(rhs);
170 }
171 
172 template <typename K, typename V, typename Comparator, typename Allocator>
173 bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
174                 const SafeMap<K, V, Comparator, Allocator>& rhs) {
175   return !(lhs == rhs);
176 }
177 
178 }  // namespace art
179 
180 #endif  // ART_LIBARTBASE_BASE_SAFE_MAP_H_
181