Chromium Code Reviews| Index: cc/scoped_ptr_hash_map.h |
| diff --git a/cc/scoped_ptr_hash_map.h b/cc/scoped_ptr_hash_map.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4b54d02405a66573fc3c7231428d02eaa1e541cc |
| --- /dev/null |
| +++ b/cc/scoped_ptr_hash_map.h |
| @@ -0,0 +1,133 @@ |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CC_SCOPED_PTR_HASH_MAP_H_ |
| +#define CC_SCOPED_PTR_HASH_MAP_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "base/hash_tables.h" |
| +#include "base/stl_util.h" |
| +#include "base/memory/scoped_ptr.h" |
| + |
| +namespace cc { |
| + |
| +// This type acts like a hash_map<K, scoped_ptr<V> >, based on top of |
| +// std::hash_map. The ScopedPtrHashMap has ownership of all values in the data |
| +// structure. |
| +template <typename Key, typename Data> |
| +class ScopedPtrHashMap { |
| + typedef base::hash_map<Key, Data*> Container; |
| + public: |
| + typedef typename Container::hasher hasher; |
| + typedef typename Container::key_equal key_equal; |
| + typedef typename Container::iterator iterator; |
| + typedef typename Container::const_iterator const_iterator; |
| + |
| + ScopedPtrHashMap() {} |
| + |
| + ~ScopedPtrHashMap() { clear(); } |
| + |
| + void swap(ScopedPtrHashMap<Key, Data*>& other) { |
| + data_.swap(other.data_); |
| + } |
| + |
| + std::pair<iterator, bool> insert( |
| + std::pair<Key, const scoped_ptr<Data> > pair) { |
| + return data_.insert( |
| + std::pair<Key, Data*>(pair.first, pair.second.release())); |
| + } |
| + |
| + // Replaces value but not key if key is already present. |
| + std::pair<iterator, bool> set(Key key, scoped_ptr<Data> data) { |
| + iterator it = find(key); |
| + if (it != end()) |
| + erase(it); |
| + Data* rawPtr = data.release(); |
| + return data_.insert(std::pair<Key, Data*>(key, rawPtr)); |
| + } |
| + |
| + // Does nothing if key is already present |
| + std::pair<iterator, bool> add(Key key, scoped_ptr<Data> data) { |
| + Data* rawPtr = data.release(); |
| + return data_.insert(std::pair<Key, Data*>(key, rawPtr)); |
| + } |
| + |
| + void erase(iterator it) { |
| + if (it->second) |
| + delete it->second; |
| + data_.erase(it); |
| + } |
| + |
| + size_t erase(const Key& k) { |
| + size_t count = 0; |
| + 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
|
| + erase(it); |
| + ++count; |
| + } |
| + return count; |
| + } |
| + |
| + scoped_ptr<Data> take(iterator it) { |
| + ASSERT(it != data_.end()); |
| + scoped_ptr<Data> ret(it->second); |
| + 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
|
| + return ret.Pass(); |
| + } |
| + |
| + scoped_ptr<Data> take(const Key& k) { |
| + iterator it = find(k); |
| + if (it == data_.end()) |
| + return scoped_ptr<Data>(NULL); |
| + return take(it); |
| + } |
| + |
| + // Returns the first element in the hash_map that matches the given key. |
| + // If no such element exists it returns NULL. |
| + Data* get(const Key& k) const { |
| + const_iterator it = find(k); |
| + if (it == end()) |
| + return 0; |
| + return it->second; |
| + } |
| + |
| + inline bool contains(const Key& k) const { return data_.count(k); } |
| + |
| + void clear() { STLDeleteValues(&data_); } |
| + |
| + inline const_iterator find(const Key& k) const { return data_.find(k); } |
| + inline iterator find(const Key& k) { return data_.find(k); } |
| + |
| + inline size_t count(const Key& k) const { return data_.count(k); } |
| + inline pair<const_iterator, const_iterator> equal_range(const Key& k) const { |
| + return data_.equal_range(k); |
| + } |
| + inline pair<iterator, iterator> equal_range(const Key& k) { |
| + return data_.equal_range(k); |
| + } |
| + |
| + inline size_t size() const { return data_.size(); } |
| + inline size_t max_size() const { return data_.max_size(); } |
| + |
| + inline bool empty() const { return data_.empty(); } |
| + |
| + inline size_t bucket_count() const { return data_.bucket_count(); } |
| + inline void resize(size_t size) const { return data_.resize(size); } |
| + |
| + inline hasher hash_funct() const { return data_.hash_funct(); } |
| + inline key_equal key_eq() const { return data_.key_eq(); } |
| + |
| + inline iterator begin() { return data_.begin(); } |
| + inline const_iterator begin() const { return data_.begin(); } |
| + inline iterator end() { return data_.end(); } |
| + inline const_iterator end() const { return data_.end(); } |
| + |
| + private: |
| + Container data_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); |
| +}; |
| + |
| +} // namespace cc |
| + |
| +#endif // CC_SCOPED_PTR_HASH_MAP_H_ |