Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ | 5 #ifndef BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ |
| 6 #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ | 6 #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of | 19 // This type acts like a hash_map<K, scoped_ptr<V, D> >, based on top of |
| 20 // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data | 20 // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data |
| 21 // structure. | 21 // structure. |
| 22 template <typename Key, typename Value> | 22 template <typename Key, typename ScopedPtr> |
| 23 class ScopedPtrHashMap { | 23 class ScopedPtrHashMap { |
| 24 typedef base::hash_map<Key, Value*> Container; | 24 typedef base::hash_map<Key, typename ScopedPtr::element_type*> Container; |
| 25 | 25 |
| 26 public: | 26 public: |
| 27 typedef typename Container::key_type key_type; | 27 typedef typename Container::key_type key_type; |
| 28 typedef typename Container::mapped_type mapped_type; | 28 typedef typename Container::mapped_type mapped_type; |
| 29 typedef typename Container::value_type value_type; | 29 typedef typename Container::value_type value_type; |
| 30 typedef typename Container::iterator iterator; | 30 typedef typename Container::iterator iterator; |
| 31 typedef typename Container::const_iterator const_iterator; | 31 typedef typename Container::const_iterator const_iterator; |
| 32 typedef typename ScopedPtr::deleter_type deleter_type; | |
| 32 | 33 |
| 33 ScopedPtrHashMap() {} | 34 ScopedPtrHashMap() {} |
| 34 | 35 |
| 35 ~ScopedPtrHashMap() { clear(); } | 36 ~ScopedPtrHashMap() { clear(); } |
| 36 | 37 |
| 37 void swap(ScopedPtrHashMap<Key, Value>& other) { | 38 void swap(ScopedPtrHashMap<Key, ScopedPtr>& other) { |
| 38 data_.swap(other.data_); | 39 data_.swap(other.data_); |
| 39 } | 40 } |
| 40 | 41 |
| 41 // Replaces value but not key if key is already present. | 42 // Replaces value but not key if key is already present. |
| 42 iterator set(const Key& key, scoped_ptr<Value> data) { | 43 iterator set(const Key& key, ScopedPtr data) { |
| 43 iterator it = find(key); | 44 iterator it = find(key); |
| 44 if (it != end()) { | 45 if (it != end()) { |
| 45 delete it->second; | 46 deleter_type()(it->second); |
|
danakj
2015/04/24 17:15:10
Rather than using the deleter manually here, can y
kcwu
2015/04/27 13:28:03
Done.
| |
| 46 it->second = data.release(); | 47 it->second = data.release(); |
| 47 return it; | 48 return it; |
| 48 } | 49 } |
| 49 | 50 |
| 50 return data_.insert(std::make_pair(key, data.release())).first; | 51 return data_.insert(std::make_pair(key, data.release())).first; |
| 51 } | 52 } |
| 52 | 53 |
| 53 // Does nothing if key is already present | 54 // Does nothing if key is already present |
| 54 std::pair<iterator, bool> add(const Key& key, scoped_ptr<Value> data) { | 55 std::pair<iterator, bool> add(const Key& key, ScopedPtr data) { |
| 55 std::pair<iterator, bool> result = | 56 std::pair<iterator, bool> result = |
| 56 data_.insert(std::make_pair(key, data.get())); | 57 data_.insert(std::make_pair(key, data.get())); |
| 57 if (result.second) | 58 if (result.second) |
| 58 ignore_result(data.release()); | 59 ignore_result(data.release()); |
| 59 return result; | 60 return result; |
| 60 } | 61 } |
| 61 | 62 |
| 62 void erase(iterator it) { | 63 void erase(iterator it) { |
| 63 delete it->second; | 64 deleter_type()(it->second); |
|
danakj
2015/04/24 17:15:10
ditto
kcwu
2015/04/27 13:28:03
Done.
| |
| 64 data_.erase(it); | 65 data_.erase(it); |
| 65 } | 66 } |
| 66 | 67 |
| 67 size_t erase(const Key& k) { | 68 size_t erase(const Key& k) { |
| 68 iterator it = data_.find(k); | 69 iterator it = data_.find(k); |
| 69 if (it == data_.end()) | 70 if (it == data_.end()) |
| 70 return 0; | 71 return 0; |
| 71 erase(it); | 72 erase(it); |
| 72 return 1; | 73 return 1; |
| 73 } | 74 } |
| 74 | 75 |
| 75 scoped_ptr<Value> take(iterator it) { | 76 ScopedPtr take(iterator it) { |
| 76 DCHECK(it != data_.end()); | 77 DCHECK(it != data_.end()); |
| 77 if (it == data_.end()) | 78 if (it == data_.end()) |
| 78 return scoped_ptr<Value>(); | 79 return ScopedPtr(); |
| 79 | 80 |
| 80 scoped_ptr<Value> ret(it->second); | 81 ScopedPtr ret(it->second); |
| 81 it->second = NULL; | 82 it->second = NULL; |
| 82 return ret.Pass(); | 83 return ret.Pass(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 scoped_ptr<Value> take(const Key& k) { | 86 ScopedPtr take(const Key& k) { |
| 86 iterator it = find(k); | 87 iterator it = find(k); |
| 87 if (it == data_.end()) | 88 if (it == data_.end()) |
| 88 return scoped_ptr<Value>(); | 89 return ScopedPtr(); |
| 89 | 90 |
| 90 return take(it); | 91 return take(it); |
| 91 } | 92 } |
| 92 | 93 |
| 93 scoped_ptr<Value> take_and_erase(iterator it) { | 94 ScopedPtr take_and_erase(iterator it) { |
| 94 DCHECK(it != data_.end()); | 95 DCHECK(it != data_.end()); |
| 95 if (it == data_.end()) | 96 if (it == data_.end()) |
| 96 return scoped_ptr<Value>(); | 97 return ScopedPtr(); |
| 97 | 98 |
| 98 scoped_ptr<Value> ret(it->second); | 99 ScopedPtr ret(it->second); |
| 99 data_.erase(it); | 100 data_.erase(it); |
| 100 return ret.Pass(); | 101 return ret.Pass(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 scoped_ptr<Value> take_and_erase(const Key& k) { | 104 ScopedPtr take_and_erase(const Key& k) { |
| 104 iterator it = find(k); | 105 iterator it = find(k); |
| 105 if (it == data_.end()) | 106 if (it == data_.end()) |
| 106 return scoped_ptr<Value>(); | 107 return ScopedPtr(); |
| 107 | 108 |
| 108 return take_and_erase(it); | 109 return take_and_erase(it); |
| 109 } | 110 } |
| 110 | 111 |
| 111 // Returns the element in the hash_map that matches the given key. | 112 // Returns the element in the hash_map that matches the given key. |
| 112 // If no such element exists it returns NULL. | 113 // If no such element exists it returns NULL. |
| 113 Value* get(const Key& k) const { | 114 typename ScopedPtr::element_type* get(const Key& k) const { |
| 114 const_iterator it = find(k); | 115 const_iterator it = find(k); |
| 115 if (it == end()) | 116 if (it == end()) |
| 116 return NULL; | 117 return NULL; |
| 117 return it->second; | 118 return it->second; |
| 118 } | 119 } |
| 119 | 120 |
| 120 inline bool contains(const Key& k) const { return data_.count(k) > 0; } | 121 inline bool contains(const Key& k) const { return data_.count(k) > 0; } |
| 121 | 122 |
| 122 inline void clear() { STLDeleteValues(&data_); } | 123 inline void clear() { |
| 124 auto it = data_.begin(); | |
| 125 while (it != data_.end()) { | |
| 126 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. | |
| 127 // Deleting the value does not always invalidate the iterator, but it may | |
| 128 // do so if the key is a pointer into the value object. | |
| 129 auto temp = it; | |
| 130 ++it; | |
| 131 deleter_type()(temp->second); | |
|
danakj
2015/04/24 17:15:10
ditto
kcwu
2015/04/27 13:28:03
Done.
| |
| 132 } | |
| 133 data_.clear(); | |
| 134 } | |
| 123 | 135 |
| 124 inline const_iterator find(const Key& k) const { return data_.find(k); } | 136 inline const_iterator find(const Key& k) const { return data_.find(k); } |
| 125 inline iterator find(const Key& k) { return data_.find(k); } | 137 inline iterator find(const Key& k) { return data_.find(k); } |
| 126 | 138 |
| 127 inline size_t count(const Key& k) const { return data_.count(k); } | 139 inline size_t count(const Key& k) const { return data_.count(k); } |
| 128 inline std::pair<const_iterator, const_iterator> equal_range( | 140 inline std::pair<const_iterator, const_iterator> equal_range( |
| 129 const Key& k) const { | 141 const Key& k) const { |
| 130 return data_.equal_range(k); | 142 return data_.equal_range(k); |
| 131 } | 143 } |
| 132 inline std::pair<iterator, iterator> equal_range(const Key& k) { | 144 inline std::pair<iterator, iterator> equal_range(const Key& k) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 148 | 160 |
| 149 private: | 161 private: |
| 150 Container data_; | 162 Container data_; |
| 151 | 163 |
| 152 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); | 164 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); |
| 153 }; | 165 }; |
| 154 | 166 |
| 155 } // namespace base | 167 } // namespace base |
| 156 | 168 |
| 157 #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ | 169 #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ |
| OLD | NEW |