Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: cc/scoped_ptr_hash_map.h

Issue 12472028: Part 1 of cc/ directory shuffles: base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/logging.h"
11 #include "base/stl_util.h"
12 #include "base/memory/scoped_ptr.h"
13
14 namespace cc {
15
16 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of
17 // std::hash_map. The ScopedPtrHashMap has ownership of all values in the data
18 // structure.
19 template <typename Key, typename Value>
20 class ScopedPtrHashMap {
21 typedef base::hash_map<Key, Value*> Container;
22 public:
23 typedef typename Container::iterator iterator;
24 typedef typename Container::const_iterator const_iterator;
25
26 ScopedPtrHashMap() {}
27
28 ~ScopedPtrHashMap() { clear(); }
29
30 void swap(ScopedPtrHashMap<Key, Value*>& other) {
31 data_.swap(other.data_);
32 }
33
34 std::pair<iterator, bool> insert(
35 std::pair<Key, const scoped_ptr<Value> > pair) {
36 return data_.insert(
37 std::pair<Key, Value*>(pair.first, pair.second.release()));
38 }
39
40 // Replaces value but not key if key is already present.
41 std::pair<iterator, bool> set(Key key, scoped_ptr<Value> data) {
42 iterator it = find(key);
43 if (it != end())
44 erase(it);
45 Value* rawPtr = data.release();
46 return data_.insert(std::pair<Key, Value*>(key, rawPtr));
47 }
48
49 // Does nothing if key is already present
50 std::pair<iterator, bool> add(Key key, scoped_ptr<Value> data) {
51 Value* rawPtr = data.release();
52 return data_.insert(std::pair<Key, Value*>(key, rawPtr));
53 }
54
55 void erase(iterator it) {
56 if (it->second)
57 delete it->second;
58 data_.erase(it);
59 }
60
61 size_t erase(const Key& k) {
62 iterator it = data_.find(k);
63 if (it == data_.end())
64 return 0;
65 erase(it);
66 return 1;
67 }
68
69 scoped_ptr<Value> take(iterator it) {
70 DCHECK(it != data_.end());
71 if (it == data_.end())
72 return scoped_ptr<Value>(NULL);
73
74 Key key = it->first;
75 scoped_ptr<Value> ret(it->second);
76 data_.erase(it);
77 data_.insert(std::pair<Key, Value*>(key, static_cast<Value*>(NULL)));
78 return ret.Pass();
79 }
80
81 scoped_ptr<Value> take(const Key& k) {
82 iterator it = find(k);
83 if (it == data_.end())
84 return scoped_ptr<Value>(NULL);
85
86 return take(it);
87 }
88
89 scoped_ptr<Value> take_and_erase(iterator it) {
90 DCHECK(it != data_.end());
91 if (it == data_.end())
92 return scoped_ptr<Value>(NULL);
93
94 scoped_ptr<Value> ret(it->second);
95 data_.erase(it);
96 return ret.Pass();
97 }
98
99 scoped_ptr<Value> take_and_erase(const Key& k) {
100 iterator it = find(k);
101 if (it == data_.end())
102 return scoped_ptr<Value>(NULL);
103
104 return take_and_erase(it);
105 }
106
107 // Returns the first element in the hash_map that matches the given key.
108 // If no such element exists it returns NULL.
109 Value* get(const Key& k) const {
110 const_iterator it = find(k);
111 if (it == end())
112 return 0;
113 return it->second;
114 }
115
116 inline bool contains(const Key& k) const { return data_.count(k); }
117
118 inline void clear() { STLDeleteValues(&data_); }
119
120 inline const_iterator find(const Key& k) const { return data_.find(k); }
121 inline iterator find(const Key& k) { return data_.find(k); }
122
123 inline size_t count(const Key& k) const { return data_.count(k); }
124 inline std::pair<const_iterator, const_iterator> equal_range(
125 const Key& k) const {
126 return data_.equal_range(k);
127 }
128 inline std::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 iterator begin() { return data_.begin(); }
141 inline const_iterator begin() const { return data_.begin(); }
142 inline iterator end() { return data_.end(); }
143 inline const_iterator end() const { return data_.end(); }
144
145 private:
146 Container data_;
147
148 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap);
149 };
150
151 } // namespace cc
152
153 #endif // CC_SCOPED_PTR_HASH_MAP_H_
OLDNEW
« cc/cc.gyp ('K') | « cc/scoped_ptr_deque.h ('k') | cc/scoped_ptr_vector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698