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::iterator iterator; |
| 23 typedef typename Container::const_iterator const_iterator; |
| 24 |
| 25 ScopedPtrHashMap() {} |
| 26 |
| 27 ~ScopedPtrHashMap() { clear(); } |
| 28 |
| 29 void swap(ScopedPtrHashMap<Key, Value*>& other) { |
| 30 data_.swap(other.data_); |
| 31 } |
| 32 |
| 33 std::pair<iterator, bool> insert( |
| 34 std::pair<Key, const scoped_ptr<Value> > pair) { |
| 35 return data_.insert( |
| 36 std::pair<Key, Value*>(pair.first, pair.second.release())); |
| 37 } |
| 38 |
| 39 // Replaces value but not key if key is already present. |
| 40 std::pair<iterator, bool> set(Key key, scoped_ptr<Value> data) { |
| 41 iterator it = find(key); |
| 42 if (it != end()) |
| 43 erase(it); |
| 44 Value* rawPtr = data.release(); |
| 45 return data_.insert(std::pair<Key, Value*>(key, rawPtr)); |
| 46 } |
| 47 |
| 48 // Does nothing if key is already present |
| 49 std::pair<iterator, bool> add(Key key, scoped_ptr<Value> data) { |
| 50 Value* rawPtr = data.release(); |
| 51 return data_.insert(std::pair<Key, Value*>(key, rawPtr)); |
| 52 } |
| 53 |
| 54 void erase(iterator it) { |
| 55 if (it->second) |
| 56 delete it->second; |
| 57 data_.erase(it); |
| 58 } |
| 59 |
| 60 size_t erase(const Key& k) { |
| 61 iterator it = data_.find(k); |
| 62 if (it == data_.end()) |
| 63 return 0; |
| 64 erase(it); |
| 65 return 1; |
| 66 } |
| 67 |
| 68 scoped_ptr<Value> take(iterator it) { |
| 69 ASSERT(it != data_.end()); |
| 70 if (it == data_.end()) |
| 71 return scoped_ptr<Value>(NULL); |
| 72 |
| 73 Key key = it->first; |
| 74 scoped_ptr<Value> ret(it->second); |
| 75 data_.erase(it); |
| 76 data_.insert(std::pair<Key, Value*>(key, static_cast<Value*>(NULL))); |
| 77 return ret.Pass(); |
| 78 } |
| 79 |
| 80 scoped_ptr<Value> take(const Key& k) { |
| 81 iterator it = find(k); |
| 82 if (it == data_.end()) |
| 83 return scoped_ptr<Value>(NULL); |
| 84 |
| 85 return take(it); |
| 86 } |
| 87 |
| 88 scoped_ptr<Value> take_and_erase(iterator it) { |
| 89 ASSERT(it != data_.end()); |
| 90 if (it == data_.end()) |
| 91 return scoped_ptr<Value>(NULL); |
| 92 |
| 93 scoped_ptr<Value> ret(it->second); |
| 94 data_.erase(it); |
| 95 return ret.Pass(); |
| 96 } |
| 97 |
| 98 scoped_ptr<Value> take_and_erase(const Key& k) { |
| 99 iterator it = find(k); |
| 100 if (it == data_.end()) |
| 101 return scoped_ptr<Value>(NULL); |
| 102 |
| 103 return take_and_erase(it); |
| 104 } |
| 105 |
| 106 // Returns the first element in the hash_map that matches the given key. |
| 107 // If no such element exists it returns NULL. |
| 108 Value* get(const Key& k) const { |
| 109 const_iterator it = find(k); |
| 110 if (it == end()) |
| 111 return 0; |
| 112 return it->second; |
| 113 } |
| 114 |
| 115 inline bool contains(const Key& k) const { return data_.count(k); } |
| 116 |
| 117 inline void clear() { STLDeleteValues(&data_); } |
| 118 |
| 119 inline const_iterator find(const Key& k) const { return data_.find(k); } |
| 120 inline iterator find(const Key& k) { return data_.find(k); } |
| 121 |
| 122 inline size_t count(const Key& k) const { return data_.count(k); } |
| 123 inline std::pair<const_iterator, const_iterator> equal_range( |
| 124 const Key& k) const { |
| 125 return data_.equal_range(k); |
| 126 } |
| 127 inline std::pair<iterator, iterator> equal_range(const Key& k) { |
| 128 return data_.equal_range(k); |
| 129 } |
| 130 |
| 131 inline size_t size() const { return data_.size(); } |
| 132 inline size_t max_size() const { return data_.max_size(); } |
| 133 |
| 134 inline bool empty() const { return data_.empty(); } |
| 135 |
| 136 inline size_t bucket_count() const { return data_.bucket_count(); } |
| 137 inline void resize(size_t size) const { return data_.resize(size); } |
| 138 |
| 139 inline iterator begin() { return data_.begin(); } |
| 140 inline const_iterator begin() const { return data_.begin(); } |
| 141 inline iterator end() { return data_.end(); } |
| 142 inline const_iterator end() const { return data_.end(); } |
| 143 |
| 144 private: |
| 145 Container data_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); |
| 148 }; |
| 149 |
| 150 } // namespace cc |
| 151 |
| 152 #endif // CC_SCOPED_PTR_HASH_MAP_H_ |
OLD | NEW |