Chromium Code Reviews| 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 Data> | |
| 19 class ScopedPtrHashMap { | |
| 20 typedef base::hash_map<Key, Data*> 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, Data*>& other) { | |
| 32 data_.swap(other.data_); | |
| 33 } | |
| 34 | |
| 35 std::pair<iterator, bool> insert( | |
| 36 std::pair<Key, const scoped_ptr<Data> > pair) { | |
| 37 return data_.insert( | |
| 38 std::pair<Key, Data*>(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<Data> data) { | |
| 43 iterator it = find(key); | |
| 44 if (it != end()) | |
| 45 erase(it); | |
| 46 Data* rawPtr = data.release(); | |
| 47 return data_.insert(std::pair<Key, Data*>(key, rawPtr)); | |
| 48 } | |
| 49 | |
| 50 // Does nothing if key is already present | |
| 51 std::pair<iterator, bool> add(Key key, scoped_ptr<Data> data) { | |
| 52 Data* rawPtr = data.release(); | |
| 53 return data_.insert(std::pair<Key, Data*>(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 size_t count = 0; | |
| 64 for (iterator it = data_.find(k); it != data_.end(); it = data_.find(k)) { | |
|
jamesr
2012/09/25 03:53:50
do we need to handle multiple entries with the sam
danakj
2012/09/25 16:13:51
oh thanks. I was learning stl again.. and we don't
| |
| 65 erase(it); | |
| 66 ++count; | |
| 67 } | |
| 68 return count; | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<Data> take(iterator it) { | |
| 72 ASSERT(it != data_.end()); | |
| 73 scoped_ptr<Data> ret(it->second); | |
| 74 data_.erase(it); | |
|
jamesr
2012/09/25 03:53:50
scoped_ptr_vector::take() leaves a null entry in p
danakj
2012/09/25 16:13:51
Hm, iteration.. We can do that but then things lik
| |
| 75 return ret.Pass(); | |
| 76 } | |
| 77 | |
| 78 scoped_ptr<Data> take(const Key& k) { | |
| 79 iterator it = find(k); | |
| 80 if (it == data_.end()) | |
| 81 return scoped_ptr<Data>(NULL); | |
| 82 return take(it); | |
| 83 } | |
| 84 | |
| 85 // Returns the first element in the hash_map that matches the given key. | |
| 86 // If no such element exists it returns NULL. | |
| 87 Data* get(const Key& k) const { | |
| 88 const_iterator it = find(k); | |
| 89 if (it == end()) | |
| 90 return 0; | |
| 91 return it->second; | |
| 92 } | |
| 93 | |
| 94 inline bool contains(const Key& k) const { return data_.count(k); } | |
| 95 | |
| 96 void clear() { STLDeleteValues(&data_); } | |
| 97 | |
| 98 inline const_iterator find(const Key& k) const { return data_.find(k); } | |
| 99 inline iterator find(const Key& k) { return data_.find(k); } | |
| 100 | |
| 101 inline size_t count(const Key& k) const { return data_.count(k); } | |
| 102 inline pair<const_iterator, const_iterator> equal_range(const Key& k) const { | |
| 103 return data_.equal_range(k); | |
| 104 } | |
| 105 inline pair<iterator, iterator> equal_range(const Key& k) { | |
| 106 return data_.equal_range(k); | |
| 107 } | |
| 108 | |
| 109 inline size_t size() const { return data_.size(); } | |
| 110 inline size_t max_size() const { return data_.max_size(); } | |
| 111 | |
| 112 inline bool empty() const { return data_.empty(); } | |
| 113 | |
| 114 inline size_t bucket_count() const { return data_.bucket_count(); } | |
| 115 inline void resize(size_t size) const { return data_.resize(size); } | |
| 116 | |
| 117 inline hasher hash_funct() const { return data_.hash_funct(); } | |
| 118 inline key_equal key_eq() const { return data_.key_eq(); } | |
| 119 | |
| 120 inline iterator begin() { return data_.begin(); } | |
| 121 inline const_iterator begin() const { return data_.begin(); } | |
| 122 inline iterator end() { return data_.end(); } | |
| 123 inline const_iterator end() const { return data_.end(); } | |
| 124 | |
| 125 private: | |
| 126 Container data_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); | |
| 129 }; | |
| 130 | |
| 131 } // namespace cc | |
| 132 | |
| 133 #endif // CC_SCOPED_PTR_HASH_MAP_H_ | |
| OLD | NEW |