blob: 941fd0e2dec3f98866a1a678e416de6dabc81057 [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>
Elliott Hughesa0e18062012-04-13 15:59:59 -070022
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070024
25namespace art {
26
27// Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
28// the implicit insertion of a default-constructed value on failed lookups).
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070029template <typename K, typename V, typename Comparator = std::less<K>,
Ian Rogers700a4022014-05-19 16:49:03 -070030 typename Allocator = std::allocator<std::pair<const K, V>>>
Elliott Hughesa0e18062012-04-13 15:59:59 -070031class SafeMap {
32 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070033 typedef SafeMap<K, V, Comparator, Allocator> Self;
Elliott Hughesa0e18062012-04-13 15:59:59 -070034
35 public:
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000036 typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare;
Vladimir Markobd72fc12014-07-09 16:06:40 +010037 typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000038 typedef typename ::std::map<K, V, Comparator, Allocator>::allocator_type allocator_type;
39 typedef typename ::std::map<K, V, Comparator, Allocator>::iterator iterator;
40 typedef typename ::std::map<K, V, Comparator, Allocator>::const_iterator const_iterator;
41 typedef typename ::std::map<K, V, Comparator, Allocator>::size_type size_type;
Vladimir Markobd72fc12014-07-09 16:06:40 +010042 typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000043 typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type;
44
45 SafeMap() = default;
46 explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
47 : map_(cmp, allocator) {
48 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070049
Elliott Hughes74847412012-06-20 18:10:21 -070050 Self& operator=(const Self& rhs) {
51 map_ = rhs.map_;
52 return *this;
53 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070054
Vladimir Markobd72fc12014-07-09 16:06:40 +010055 key_compare key_comp() const { return map_.key_comp(); }
56 value_compare value_comp() const { return map_.value_comp(); }
57
Elliott Hughesa0e18062012-04-13 15:59:59 -070058 iterator begin() { return map_.begin(); }
59 const_iterator begin() const { return map_.begin(); }
60 iterator end() { return map_.end(); }
61 const_iterator end() const { return map_.end(); }
62
63 bool empty() const { return map_.empty(); }
64 size_type size() const { return map_.size(); }
65
Vladimir Markobd72fc12014-07-09 16:06:40 +010066 void swap(Self& other) { map_.swap(other.map_); }
Ian Rogers08f753d2012-08-24 14:35:25 -070067 void clear() { map_.clear(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010068 iterator erase(iterator it) { return map_.erase(it); }
Elliott Hughesa0e18062012-04-13 15:59:59 -070069 size_type erase(const K& k) { return map_.erase(k); }
70
71 iterator find(const K& k) { return map_.find(k); }
72 const_iterator find(const K& k) const { return map_.find(k); }
73
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010074 iterator lower_bound(const K& k) { return map_.lower_bound(k); }
75 const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
76
Elliott Hughesa0e18062012-04-13 15:59:59 -070077 size_type count(const K& k) const { return map_.count(k); }
78
79 // Note that unlike std::map's operator[], this doesn't return a reference to the value.
TDYa127b2eb5c12012-05-24 15:52:10 -070080 V Get(const K& k) const {
81 const_iterator it = map_.find(k);
Elliott Hughesa0e18062012-04-13 15:59:59 -070082 DCHECK(it != map_.end());
83 return it->second;
84 }
85
86 // Used to insert a new mapping.
Vladimir Markobd72fc12014-07-09 16:06:40 +010087 iterator Put(const K& k, const V& v) {
88 std::pair<iterator, bool> result = map_.emplace(k, v);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070089 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
Vladimir Markobd72fc12014-07-09 16:06:40 +010090 return result.first;
91 }
92
93 // Used to insert a new mapping at a known position for better performance.
94 iterator PutBefore(iterator pos, const K& k, const V& v) {
95 // Check that we're using the correct position and the key is not in the map.
96 DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
97 DCHECK(pos == map_.begin() || map_.key_comp()((--iterator(pos))->first, k));
98 return map_.emplace_hint(pos, k, v);
Elliott Hughesa0e18062012-04-13 15:59:59 -070099 }
100
101 // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
102 // of this container is a pointer, any overwritten pointer will be lost and if this container
103 // was the owner, you have a leak.
104 void Overwrite(const K& k, const V& v) {
buzbeed1643e42012-09-05 14:06:51 -0700105 std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
106 if (!result.second) {
107 // Already there - update the value for the existing key
108 result.first->second = v;
109 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700110 }
111
112 bool Equals(const Self& rhs) const {
113 return map_ == rhs.map_;
114 }
115
116 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700117 ::std::map<K, V, Comparator, Allocator> map_;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700118};
119
Vladimir Markobd72fc12014-07-09 16:06:40 +0100120template <typename K, typename V, typename Comparator, typename Allocator>
121bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
122 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700123 return lhs.Equals(rhs);
124}
125
Vladimir Markobd72fc12014-07-09 16:06:40 +0100126template <typename K, typename V, typename Comparator, typename Allocator>
127bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
128 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700129 return !(lhs == rhs);
130}
131
132} // namespace art
133
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700134#endif // ART_RUNTIME_SAFE_MAP_H_