OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CC_SCOPED_PTR_HASH_MAP_H_ | |
6 #define CC_SCOPED_PTR_HASH_MAP_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/hash_tables.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 | |
13 namespace cc { | |
14 | |
15 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of | |
16 // std::hash_map. The ScopedPtrHashMap has ownership of all values in the data | |
17 // structure. | |
18 template <typename Key, typename Value> | |
19 class ScopedPtrHashMap { | |
20 typedef base::hash_map<Key, Value*> Container; | |
21 public: | |
22 typedef typename Container::hasher hasher; | |
23 typedef typename Container::key_equal key_equal; | |
24 typedef typename Container::iterator iterator; | |
25 typedef typename Container::const_iterator const_iterator; | |
26 | |
27 ScopedPtrHashMap() {} | |
28 | |
29 ~ScopedPtrHashMap() { clear(); } | |
30 | |
31 void swap(ScopedPtrHashMap<Key, Value*>& other) { | |
32 data_.swap(other.data_); | |
33 } | |
34 | |
35 std::pair<iterator, bool> insert( | |
36 std::pair<Key, const scoped_ptr<Value> > pair) { | |
37 return data_.insert( | |
38 std::pair<Key, Value*>(pair.first, pair.second.release())); | |
39 } | |
40 | |
41 // Replaces value but not key if key is already present. | |
42 std::pair<iterator, bool> set(Key key, scoped_ptr<Value> data) { | |
43 iterator it = find(key); | |
44 if (it != end()) | |
45 erase(it); | |
46 Value* rawPtr = data.release(); | |
47 return data_.insert(std::pair<Key, Value*>(key, rawPtr)); | |
48 } | |
49 | |
50 // Does nothing if key is already present | |
51 std::pair<iterator, bool> add(Key key, scoped_ptr<Value> data) { | |
52 Value* rawPtr = data.release(); | |
53 return data_.insert(std::pair<Key, Value*>(key, rawPtr)); | |
54 } | |
55 | |
56 void erase(iterator it) { | |
57 if (it->second) | |
58 delete it->second; | |
59 data_.erase(it); | |
60 } | |
61 | |
62 size_t erase(const Key& k) { | |
63 iterator it = data_.find(k); | |
64 if (it == data_.end()) | |
65 return 0; | |
66 erase(it); | |
67 return 1; | |
68 } | |
69 | |
70 scoped_ptr<Value> take(iterator it) { | |
71 ASSERT(it != data_.end()); | |
72 if (it == data_.end()) | |
73 return scoped_ptr<Value>(NULL); | |
74 | |
75 Key key = it->first; | |
76 scoped_ptr<Value> ret(it->second); | |
77 data_.erase(it); | |
78 data_.insert(std::pair<Key, Value*>(key, NULL)); | |
jamesr
2012/09/25 21:37:42
hmm, can we mutate it->second instead of doing an
danakj
2012/09/25 21:42:59
I don't think so. All I see in the hash_map interf
| |
79 return ret.Pass(); | |
80 } | |
81 | |
82 scoped_ptr<Value> take(const Key& k) { | |
83 iterator it = find(k); | |
84 if (it == data_.end()) | |
85 return scoped_ptr<Value>(NULL); | |
86 | |
87 return take(it); | |
88 } | |
89 | |
90 scoped_ptr<Value> take_and_erase(iterator it) { | |
91 ASSERT(it != data_.end()); | |
92 if (it == data_.end()) | |
93 return scoped_ptr<Value>(NULL); | |
94 | |
95 scoped_ptr<Value> ret(it->second); | |
96 data_.erase(it); | |
97 return ret.Pass(); | |
98 } | |
99 | |
100 scoped_ptr<Value> take_and_erase(const Key& k) { | |
101 iterator it = find(k); | |
102 if (it == data_.end()) | |
103 return scoped_ptr<Value>(NULL); | |
104 | |
105 return take_and_erase(it); | |
106 } | |
107 | |
108 // Returns the first element in the hash_map that matches the given key. | |
109 // If no such element exists it returns NULL. | |
110 Value* get(const Key& k) const { | |
111 const_iterator it = find(k); | |
112 if (it == end()) | |
113 return 0; | |
114 return it->second; | |
115 } | |
116 | |
117 inline bool contains(const Key& k) const { return data_.count(k); } | |
118 | |
119 inline void clear() { STLDeleteValues(&data_); } | |
120 | |
121 inline const_iterator find(const Key& k) const { return data_.find(k); } | |
122 inline iterator find(const Key& k) { return data_.find(k); } | |
123 | |
124 inline size_t count(const Key& k) const { return data_.count(k); } | |
125 inline pair<const_iterator, const_iterator> equal_range(const Key& k) const { | |
126 return data_.equal_range(k); | |
127 } | |
128 inline pair<iterator, iterator> equal_range(const Key& k) { | |
129 return data_.equal_range(k); | |
130 } | |
131 | |
132 inline size_t size() const { return data_.size(); } | |
133 inline size_t max_size() const { return data_.max_size(); } | |
134 | |
135 inline bool empty() const { return data_.empty(); } | |
136 | |
137 inline size_t bucket_count() const { return data_.bucket_count(); } | |
138 inline void resize(size_t size) const { return data_.resize(size); } | |
139 | |
140 inline hasher hash_funct() const { return data_.hash_funct(); } | |
141 inline key_equal key_eq() const { return data_.key_eq(); } | |
142 | |
143 inline iterator begin() { return data_.begin(); } | |
144 inline const_iterator begin() const { return data_.begin(); } | |
145 inline iterator end() { return data_.end(); } | |
146 inline const_iterator end() const { return data_.end(); } | |
147 | |
148 private: | |
149 Container data_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); | |
152 }; | |
153 | |
154 } // namespace cc | |
155 | |
156 #endif // CC_SCOPED_PTR_HASH_MAP_H_ | |
OLD | NEW |