blob: 9cbc1f8978fa8836b770f8370ff3b2a3f44e2017 [file] [log] [blame]
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_ID_MAP_H_
6#define BASE_ID_MAP_H_
7
8#include <set>
9
10#include "base/basictypes.h"
brettw@chromium.org30230a82013-06-12 02:52:44 +090011#include "base/containers/hash_tables.h"
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090012#include "base/logging.h"
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090013#include "base/threading/non_thread_safe.h"
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090014
neb@chromium.orged1f8d72010-01-26 08:25:16 +090015// Ownership semantics - own pointer means the pointer is deleted in Remove()
16// & during destruction
17enum IDMapOwnershipSemantics {
18 IDMapExternalPointer,
19 IDMapOwnPointer
20};
21
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090022// This object maintains a list of IDs that can be quickly converted to
23// pointers to objects. It is implemented as a hash table, optimized for
24// relatively small data sets (in the common case, there will be exactly one
25// item in the list).
26//
27// Items can be inserted into the container with arbitrary ID, but the caller
28// must ensure they are unique. Inserting IDs and relying on automatically
29// generated ones is not allowed because they can collide.
neb@chromium.orged1f8d72010-01-26 08:25:16 +090030//
31// This class does not have a virtual destructor, do not inherit from it when
32// ownership semantics are set to own because pointers will leak.
33template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer>
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090034class IDMap : public base::NonThreadSafe {
mnaganov@chromium.org0f3c4e62014-05-28 03:10:06 +090035 public:
neb@chromium.orged1f8d72010-01-26 08:25:16 +090036 typedef int32 KeyType;
mnaganov@chromium.org0f3c4e62014-05-28 03:10:06 +090037
38 private:
neb@chromium.orged1f8d72010-01-26 08:25:16 +090039 typedef base::hash_map<KeyType, T*> HashTable;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090040
41 public:
42 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090043 // A number of consumers of IDMap create it on one thread but always access
44 // it from a different, but consitent, thread post-construction.
45 DetachFromThread();
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090046 }
47
neb@chromium.orged1f8d72010-01-26 08:25:16 +090048 ~IDMap() {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090049 // Many IDMap's are static, and hence will be destroyed on the main thread.
50 // However, all the accesses may take place on another thread, such as the
51 // IO thread. Detaching again to clean this up.
52 DetachFromThread();
neb@chromium.orged1f8d72010-01-26 08:25:16 +090053 Releaser<OS, 0>::release_all(&data_);
54 }
55
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090056 // Sets whether Add should CHECK if passed in NULL data. Default is false.
57 void set_check_on_null_data(bool value) { check_on_null_data_ = value; }
58
59 // Adds a view with an automatically generated unique ID. See AddWithID.
neb@chromium.orged1f8d72010-01-26 08:25:16 +090060 KeyType Add(T* data) {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090061 DCHECK(CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090062 CHECK(!check_on_null_data_ || data);
neb@chromium.orged1f8d72010-01-26 08:25:16 +090063 KeyType this_id = next_id_;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090064 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
65 data_[this_id] = data;
66 next_id_++;
67 return this_id;
68 }
69
70 // Adds a new data member with the specified ID. The ID must not be in
71 // the list. The caller either must generate all unique IDs itself and use
72 // this function, or allow this object to generate IDs and call Add. These
73 // two methods may not be mixed, or duplicate IDs may be generated
neb@chromium.orged1f8d72010-01-26 08:25:16 +090074 void AddWithID(T* data, KeyType id) {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090075 DCHECK(CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090076 CHECK(!check_on_null_data_ || data);
77 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
78 data_[id] = data;
79 }
80
neb@chromium.orged1f8d72010-01-26 08:25:16 +090081 void Remove(KeyType id) {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +090082 DCHECK(CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090083 typename HashTable::iterator i = data_.find(id);
84 if (i == data_.end()) {
85 NOTREACHED() << "Attempting to remove an item not in the list";
86 return;
87 }
88
neb@chromium.orged1f8d72010-01-26 08:25:16 +090089 if (iteration_depth_ == 0) {
90 Releaser<OS, 0>::release(i->second);
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090091 data_.erase(i);
neb@chromium.orged1f8d72010-01-26 08:25:16 +090092 } else {
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090093 removed_ids_.insert(id);
neb@chromium.orged1f8d72010-01-26 08:25:16 +090094 }
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +090095 }
96
jsbell@chromium.org8fe2a8f2012-10-30 06:36:22 +090097 void Clear() {
98 DCHECK(CalledOnValidThread());
99 if (iteration_depth_ == 0) {
100 Releaser<OS, 0>::release_all(&data_);
101 } else {
102 for (typename HashTable::iterator i = data_.begin();
103 i != data_.end(); ++i)
104 removed_ids_.insert(i->first);
105 }
106 }
107
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900108 bool IsEmpty() const {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900109 DCHECK(CalledOnValidThread());
phajdan.jr@chromium.org61544a02010-02-16 18:15:38 +0900110 return size() == 0u;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900111 }
112
neb@chromium.orged1f8d72010-01-26 08:25:16 +0900113 T* Lookup(KeyType id) const {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900114 DCHECK(CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900115 typename HashTable::const_iterator i = data_.find(id);
116 if (i == data_.end())
117 return NULL;
118 return i->second;
119 }
120
121 size_t size() const {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900122 DCHECK(CalledOnValidThread());
phajdan.jr@chromium.org61544a02010-02-16 18:15:38 +0900123 return data_.size() - removed_ids_.size();
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900124 }
125
phajdan.jr@chromium.org7c63ed82012-10-27 10:03:01 +0900126#if defined(UNIT_TEST)
127 int iteration_depth() const {
128 return iteration_depth_;
129 }
130#endif // defined(UNIT_TEST)
131
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900132 // It is safe to remove elements from the map during iteration. All iterators
133 // will remain valid.
134 template<class ReturnType>
135 class Iterator {
136 public:
brettw@chromium.org5808ed32010-03-06 11:53:28 +0900137 Iterator(IDMap<T, OS>* map)
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900138 : map_(map),
139 iter_(map_->data_.begin()) {
phajdan.jr@chromium.org7c63ed82012-10-27 10:03:01 +0900140 Init();
141 }
142
143 Iterator(const Iterator& iter)
144 : map_(iter.map_),
145 iter_(iter.iter_) {
146 Init();
147 }
148
149 const Iterator& operator=(const Iterator& iter) {
150 map_ = iter.map;
151 iter_ = iter.iter;
152 Init();
153 return *this;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900154 }
155
156 ~Iterator() {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900157 DCHECK(map_->CalledOnValidThread());
phajdan.jr@chromium.org7c63ed82012-10-27 10:03:01 +0900158
159 // We're going to decrement iteration depth. Make sure it's greater than
160 // zero so that it doesn't become negative.
161 DCHECK_LT(0, map_->iteration_depth_);
162
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900163 if (--map_->iteration_depth_ == 0)
164 map_->Compact();
165 }
166
167 bool IsAtEnd() const {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900168 DCHECK(map_->CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900169 return iter_ == map_->data_.end();
170 }
171
neb@chromium.orged1f8d72010-01-26 08:25:16 +0900172 KeyType GetCurrentKey() const {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900173 DCHECK(map_->CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900174 return iter_->first;
175 }
176
177 ReturnType* GetCurrentValue() const {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900178 DCHECK(map_->CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900179 return iter_->second;
180 }
181
182 void Advance() {
cbentzel@chromium.org95cb7512011-03-08 11:07:29 +0900183 DCHECK(map_->CalledOnValidThread());
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900184 ++iter_;
185 SkipRemovedEntries();
186 }
187
188 private:
phajdan.jr@chromium.org7c63ed82012-10-27 10:03:01 +0900189 void Init() {
190 DCHECK(map_->CalledOnValidThread());
191 ++map_->iteration_depth_;
192 SkipRemovedEntries();
193 }
194
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900195 void SkipRemovedEntries() {
196 while (iter_ != map_->data_.end() &&
197 map_->removed_ids_.find(iter_->first) !=
198 map_->removed_ids_.end()) {
199 ++iter_;
200 }
201 }
202
brettw@chromium.org5808ed32010-03-06 11:53:28 +0900203 IDMap<T, OS>* map_;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900204 typename HashTable::const_iterator iter_;
205 };
206
207 typedef Iterator<T> iterator;
208 typedef Iterator<const T> const_iterator;
209
210 private:
neb@chromium.orged1f8d72010-01-26 08:25:16 +0900211
212 // The dummy parameter is there because C++ standard does not allow
213 // explicitly specialized templates inside classes
214 template<IDMapOwnershipSemantics OI, int dummy> struct Releaser {
215 static inline void release(T* ptr) {}
216 static inline void release_all(HashTable* table) {}
217 };
218
219 template<int dummy> struct Releaser<IDMapOwnPointer, dummy> {
220 static inline void release(T* ptr) { delete ptr;}
221 static inline void release_all(HashTable* table) {
222 for (typename HashTable::iterator i = table->begin();
223 i != table->end(); ++i) {
224 delete i->second;
225 }
226 table->clear();
227 }
228 };
229
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900230 void Compact() {
231 DCHECK_EQ(0, iteration_depth_);
neb@chromium.orged1f8d72010-01-26 08:25:16 +0900232 for (std::set<KeyType>::const_iterator i = removed_ids_.begin();
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900233 i != removed_ids_.end(); ++i) {
234 Remove(*i);
235 }
236 removed_ids_.clear();
237 }
238
239 // Keep track of how many iterators are currently iterating on us to safely
240 // handle removing items during iteration.
241 int iteration_depth_;
242
243 // Keep set of IDs that should be removed after the outermost iteration has
244 // finished. This way we manage to not invalidate the iterator when an element
245 // is removed.
neb@chromium.orged1f8d72010-01-26 08:25:16 +0900246 std::set<KeyType> removed_ids_;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900247
248 // The next ID that we will return from Add()
neb@chromium.orged1f8d72010-01-26 08:25:16 +0900249 KeyType next_id_;
phajdan.jr@chromium.orga12746c2009-08-19 23:56:38 +0900250
251 HashTable data_;
252
253 // See description above setter.
254 bool check_on_null_data_;
255
256 DISALLOW_COPY_AND_ASSIGN(IDMap);
257};
258
259#endif // BASE_ID_MAP_H_