blob: f9d81dc3cb7451e70b40be9e50a3a24a66f9afc5 [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
Mathieu Chartierbad02672014-08-25 13:08:22 -070023#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025
26namespace 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>,
Mathieu Chartierbad02672014-08-25 13:08:22 -070031 typename Allocator = TrackingAllocator<std::pair<const K, V>, kAllocatorTagSafeMap>>
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;
47 explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
48 : map_(cmp, allocator) {
49 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070050
Elliott Hughes74847412012-06-20 18:10:21 -070051 Self& operator=(const Self& rhs) {
52 map_ = rhs.map_;
53 return *this;
54 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070055
Vladimir Markob19955d2014-07-29 12:04:10 +010056 allocator_type get_allocator() const { return map_.get_allocator(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010057 key_compare key_comp() const { return map_.key_comp(); }
58 value_compare value_comp() const { return map_.value_comp(); }
59
Elliott Hughesa0e18062012-04-13 15:59:59 -070060 iterator begin() { return map_.begin(); }
61 const_iterator begin() const { return map_.begin(); }
62 iterator end() { return map_.end(); }
63 const_iterator end() const { return map_.end(); }
64
65 bool empty() const { return map_.empty(); }
66 size_type size() const { return map_.size(); }
67
Vladimir Markobd72fc12014-07-09 16:06:40 +010068 void swap(Self& other) { map_.swap(other.map_); }
Ian Rogers08f753d2012-08-24 14:35:25 -070069 void clear() { map_.clear(); }
Vladimir Markobd72fc12014-07-09 16:06:40 +010070 iterator erase(iterator it) { return map_.erase(it); }
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 size_type erase(const K& k) { return map_.erase(k); }
72
73 iterator find(const K& k) { return map_.find(k); }
74 const_iterator find(const K& k) const { return map_.find(k); }
75
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010076 iterator lower_bound(const K& k) { return map_.lower_bound(k); }
77 const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
78
Elliott Hughesa0e18062012-04-13 15:59:59 -070079 size_type count(const K& k) const { return map_.count(k); }
80
81 // Note that unlike std::map's operator[], this doesn't return a reference to the value.
TDYa127b2eb5c12012-05-24 15:52:10 -070082 V Get(const K& k) const {
83 const_iterator it = map_.find(k);
Elliott Hughesa0e18062012-04-13 15:59:59 -070084 DCHECK(it != map_.end());
85 return it->second;
86 }
87
88 // Used to insert a new mapping.
Vladimir Markobd72fc12014-07-09 16:06:40 +010089 iterator Put(const K& k, const V& v) {
90 std::pair<iterator, bool> result = map_.emplace(k, v);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070091 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
Vladimir Markobd72fc12014-07-09 16:06:40 +010092 return result.first;
93 }
94
95 // Used to insert a new mapping at a known position for better performance.
96 iterator PutBefore(iterator pos, const K& k, const V& v) {
97 // Check that we're using the correct position and the key is not in the map.
98 DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
99 DCHECK(pos == map_.begin() || map_.key_comp()((--iterator(pos))->first, k));
100 return map_.emplace_hint(pos, k, v);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700101 }
102
103 // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
104 // of this container is a pointer, any overwritten pointer will be lost and if this container
105 // was the owner, you have a leak.
106 void Overwrite(const K& k, const V& v) {
buzbeed1643e42012-09-05 14:06:51 -0700107 std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
108 if (!result.second) {
109 // Already there - update the value for the existing key
110 result.first->second = v;
111 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700112 }
113
114 bool Equals(const Self& rhs) const {
115 return map_ == rhs.map_;
116 }
117
118 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700119 ::std::map<K, V, Comparator, Allocator> map_;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700120};
121
Vladimir Markobd72fc12014-07-09 16:06:40 +0100122template <typename K, typename V, typename Comparator, typename Allocator>
123bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
124 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700125 return lhs.Equals(rhs);
126}
127
Vladimir Markobd72fc12014-07-09 16:06:40 +0100128template <typename K, typename V, typename Comparator, typename Allocator>
129bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
130 const SafeMap<K, V, Comparator, Allocator>& rhs) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700131 return !(lhs == rhs);
132}
133
Mathieu Chartierbad02672014-08-25 13:08:22 -0700134template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
135class AllocationTrackingSafeMap : public SafeMap<
136 Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>> {
137};
138
Elliott Hughesa0e18062012-04-13 15:59:59 -0700139} // namespace art
140
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700141#endif // ART_RUNTIME_SAFE_MAP_H_