blob: b1abdfdcee4207e6fe1ef70bb308bdfcbfc4bbff [file] [log] [blame]
[email protected]4bc4f122011-03-08 02:07:291// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]925d5d602009-08-19 14:56:385#ifndef BASE_ID_MAP_H_
6#define BASE_ID_MAP_H_
initial.commitd7cae122008-07-26 21:49:387
jkarlin7bc034c2015-09-25 20:45:478#include <stdint.h>
[email protected]9de09f82009-08-17 20:13:539#include <set>
10
[email protected]14c1c232013-06-11 17:52:4411#include "base/containers/hash_tables.h"
initial.commitd7cae122008-07-26 21:49:3812#include "base/logging.h"
[email protected]4bc4f122011-03-08 02:07:2913#include "base/threading/non_thread_safe.h"
initial.commitd7cae122008-07-26 21:49:3814
[email protected]9e7e0e02010-01-25 23:25:1615// Ownership semantics - own pointer means the pointer is deleted in Remove()
16// & during destruction
17enum IDMapOwnershipSemantics {
18 IDMapExternalPointer,
19 IDMapOwnPointer
20};
21
initial.commitd7cae122008-07-26 21:49:3822// 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.
[email protected]9e7e0e02010-01-25 23:25:1630//
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.
jkarlin7bc034c2015-09-25 20:45:4733template <typename T,
34 IDMapOwnershipSemantics OS = IDMapExternalPointer,
35 typename K = int32_t>
[email protected]4bc4f122011-03-08 02:07:2936class IDMap : public base::NonThreadSafe {
[email protected]b8b670812014-05-27 18:10:0637 public:
jkarlin7bc034c2015-09-25 20:45:4738 using KeyType = K;
[email protected]b8b670812014-05-27 18:10:0639
40 private:
[email protected]9e7e0e02010-01-25 23:25:1641 typedef base::hash_map<KeyType, T*> HashTable;
initial.commitd7cae122008-07-26 21:49:3842
43 public:
[email protected]9de09f82009-08-17 20:13:5344 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) {
[email protected]4bc4f122011-03-08 02:07:2945 // A number of consumers of IDMap create it on one thread but always access
46 // it from a different, but consitent, thread post-construction.
47 DetachFromThread();
initial.commitd7cae122008-07-26 21:49:3848 }
49
[email protected]9e7e0e02010-01-25 23:25:1650 ~IDMap() {
[email protected]4bc4f122011-03-08 02:07:2951 // Many IDMap's are static, and hence will be destroyed on the main thread.
52 // However, all the accesses may take place on another thread, such as the
53 // IO thread. Detaching again to clean this up.
54 DetachFromThread();
[email protected]9e7e0e02010-01-25 23:25:1655 Releaser<OS, 0>::release_all(&data_);
56 }
57
michaelnbfea6ec2014-12-09 23:16:1358 // Sets whether Add and Replace should DCHECK if passed in NULL data.
59 // Default is false.
[email protected]a6ed9432009-07-01 22:35:2660 void set_check_on_null_data(bool value) { check_on_null_data_ = value; }
61
initial.commitd7cae122008-07-26 21:49:3862 // Adds a view with an automatically generated unique ID. See AddWithID.
[email protected]9e7e0e02010-01-25 23:25:1663 KeyType Add(T* data) {
[email protected]4bc4f122011-03-08 02:07:2964 DCHECK(CalledOnValidThread());
michaelnbfea6ec2014-12-09 23:16:1365 DCHECK(!check_on_null_data_ || data);
[email protected]9e7e0e02010-01-25 23:25:1666 KeyType this_id = next_id_;
initial.commitd7cae122008-07-26 21:49:3867 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
68 data_[this_id] = data;
69 next_id_++;
70 return this_id;
71 }
72
73 // Adds a new data member with the specified ID. The ID must not be in
74 // the list. The caller either must generate all unique IDs itself and use
75 // this function, or allow this object to generate IDs and call Add. These
76 // two methods may not be mixed, or duplicate IDs may be generated
[email protected]9e7e0e02010-01-25 23:25:1677 void AddWithID(T* data, KeyType id) {
[email protected]4bc4f122011-03-08 02:07:2978 DCHECK(CalledOnValidThread());
michaelnbfea6ec2014-12-09 23:16:1379 DCHECK(!check_on_null_data_ || data);
initial.commitd7cae122008-07-26 21:49:3880 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
81 data_[id] = data;
82 }
83
[email protected]9e7e0e02010-01-25 23:25:1684 void Remove(KeyType id) {
[email protected]4bc4f122011-03-08 02:07:2985 DCHECK(CalledOnValidThread());
[email protected]9de09f82009-08-17 20:13:5386 typename HashTable::iterator i = data_.find(id);
initial.commitd7cae122008-07-26 21:49:3887 if (i == data_.end()) {
88 NOTREACHED() << "Attempting to remove an item not in the list";
89 return;
90 }
[email protected]9de09f82009-08-17 20:13:5391
[email protected]9e7e0e02010-01-25 23:25:1692 if (iteration_depth_ == 0) {
93 Releaser<OS, 0>::release(i->second);
[email protected]9de09f82009-08-17 20:13:5394 data_.erase(i);
[email protected]9e7e0e02010-01-25 23:25:1695 } else {
[email protected]9de09f82009-08-17 20:13:5396 removed_ids_.insert(id);
[email protected]9e7e0e02010-01-25 23:25:1697 }
initial.commitd7cae122008-07-26 21:49:3898 }
99
michaelnbfea6ec2014-12-09 23:16:13100 // Replaces the value for |id| with |new_data| and returns a pointer to the
101 // existing value. If there is no entry for |id|, the map is not altered and
102 // nullptr is returned. The OwnershipSemantics of the map have no effect on
103 // how the existing value is treated, the IDMap does not delete the existing
104 // value being replaced.
105 T* Replace(KeyType id, T* new_data) {
106 DCHECK(CalledOnValidThread());
107 DCHECK(!check_on_null_data_ || new_data);
108 typename HashTable::iterator i = data_.find(id);
109 if (i == data_.end()) {
110 NOTREACHED() << "Attempting to replace an item not in the list";
111 return nullptr;
112 }
113
114 T* temp = i->second;
115 i->second = new_data;
116 return temp;
117 }
118
[email protected]fea0b9612012-10-29 21:36:22119 void Clear() {
120 DCHECK(CalledOnValidThread());
121 if (iteration_depth_ == 0) {
122 Releaser<OS, 0>::release_all(&data_);
123 } else {
124 for (typename HashTable::iterator i = data_.begin();
125 i != data_.end(); ++i)
126 removed_ids_.insert(i->first);
127 }
128 }
129
initial.commitd7cae122008-07-26 21:49:38130 bool IsEmpty() const {
[email protected]4bc4f122011-03-08 02:07:29131 DCHECK(CalledOnValidThread());
[email protected]2de069e2010-02-16 09:15:38132 return size() == 0u;
initial.commitd7cae122008-07-26 21:49:38133 }
134
[email protected]9e7e0e02010-01-25 23:25:16135 T* Lookup(KeyType id) const {
[email protected]4bc4f122011-03-08 02:07:29136 DCHECK(CalledOnValidThread());
[email protected]9de09f82009-08-17 20:13:53137 typename HashTable::const_iterator i = data_.find(id);
initial.commitd7cae122008-07-26 21:49:38138 if (i == data_.end())
139 return NULL;
140 return i->second;
141 }
142
143 size_t size() const {
[email protected]4bc4f122011-03-08 02:07:29144 DCHECK(CalledOnValidThread());
[email protected]2de069e2010-02-16 09:15:38145 return data_.size() - removed_ids_.size();
initial.commitd7cae122008-07-26 21:49:38146 }
147
[email protected]0c8b8942012-10-27 01:03:01148#if defined(UNIT_TEST)
149 int iteration_depth() const {
150 return iteration_depth_;
151 }
152#endif // defined(UNIT_TEST)
153
[email protected]9de09f82009-08-17 20:13:53154 // It is safe to remove elements from the map during iteration. All iterators
155 // will remain valid.
156 template<class ReturnType>
157 class Iterator {
158 public:
[email protected]00c39612010-03-06 02:53:28159 Iterator(IDMap<T, OS>* map)
[email protected]9de09f82009-08-17 20:13:53160 : map_(map),
161 iter_(map_->data_.begin()) {
[email protected]0c8b8942012-10-27 01:03:01162 Init();
163 }
164
165 Iterator(const Iterator& iter)
166 : map_(iter.map_),
167 iter_(iter.iter_) {
168 Init();
169 }
170
171 const Iterator& operator=(const Iterator& iter) {
172 map_ = iter.map;
173 iter_ = iter.iter;
174 Init();
175 return *this;
[email protected]9de09f82009-08-17 20:13:53176 }
177
178 ~Iterator() {
[email protected]4bc4f122011-03-08 02:07:29179 DCHECK(map_->CalledOnValidThread());
[email protected]0c8b8942012-10-27 01:03:01180
181 // We're going to decrement iteration depth. Make sure it's greater than
182 // zero so that it doesn't become negative.
183 DCHECK_LT(0, map_->iteration_depth_);
184
[email protected]9de09f82009-08-17 20:13:53185 if (--map_->iteration_depth_ == 0)
186 map_->Compact();
187 }
188
189 bool IsAtEnd() const {
[email protected]4bc4f122011-03-08 02:07:29190 DCHECK(map_->CalledOnValidThread());
[email protected]9de09f82009-08-17 20:13:53191 return iter_ == map_->data_.end();
192 }
193
[email protected]9e7e0e02010-01-25 23:25:16194 KeyType GetCurrentKey() const {
[email protected]4bc4f122011-03-08 02:07:29195 DCHECK(map_->CalledOnValidThread());
[email protected]9de09f82009-08-17 20:13:53196 return iter_->first;
197 }
198
199 ReturnType* GetCurrentValue() const {
[email protected]4bc4f122011-03-08 02:07:29200 DCHECK(map_->CalledOnValidThread());
[email protected]9de09f82009-08-17 20:13:53201 return iter_->second;
202 }
203
204 void Advance() {
[email protected]4bc4f122011-03-08 02:07:29205 DCHECK(map_->CalledOnValidThread());
[email protected]9de09f82009-08-17 20:13:53206 ++iter_;
207 SkipRemovedEntries();
208 }
209
210 private:
[email protected]0c8b8942012-10-27 01:03:01211 void Init() {
212 DCHECK(map_->CalledOnValidThread());
213 ++map_->iteration_depth_;
214 SkipRemovedEntries();
215 }
216
[email protected]9de09f82009-08-17 20:13:53217 void SkipRemovedEntries() {
218 while (iter_ != map_->data_.end() &&
219 map_->removed_ids_.find(iter_->first) !=
220 map_->removed_ids_.end()) {
221 ++iter_;
222 }
223 }
224
[email protected]00c39612010-03-06 02:53:28225 IDMap<T, OS>* map_;
[email protected]9de09f82009-08-17 20:13:53226 typename HashTable::const_iterator iter_;
227 };
228
229 typedef Iterator<T> iterator;
230 typedef Iterator<const T> const_iterator;
231
232 private:
[email protected]9e7e0e02010-01-25 23:25:16233
234 // The dummy parameter is there because C++ standard does not allow
235 // explicitly specialized templates inside classes
236 template<IDMapOwnershipSemantics OI, int dummy> struct Releaser {
237 static inline void release(T* ptr) {}
238 static inline void release_all(HashTable* table) {}
239 };
240
241 template<int dummy> struct Releaser<IDMapOwnPointer, dummy> {
242 static inline void release(T* ptr) { delete ptr;}
243 static inline void release_all(HashTable* table) {
244 for (typename HashTable::iterator i = table->begin();
245 i != table->end(); ++i) {
246 delete i->second;
247 }
248 table->clear();
249 }
250 };
251
[email protected]9de09f82009-08-17 20:13:53252 void Compact() {
253 DCHECK_EQ(0, iteration_depth_);
jkarlin7bc034c2015-09-25 20:45:47254 for (const auto& i : removed_ids_)
255 Remove(i);
[email protected]9de09f82009-08-17 20:13:53256 removed_ids_.clear();
257 }
258
259 // Keep track of how many iterators are currently iterating on us to safely
260 // handle removing items during iteration.
261 int iteration_depth_;
262
263 // Keep set of IDs that should be removed after the outermost iteration has
264 // finished. This way we manage to not invalidate the iterator when an element
265 // is removed.
[email protected]9e7e0e02010-01-25 23:25:16266 std::set<KeyType> removed_ids_;
[email protected]9de09f82009-08-17 20:13:53267
initial.commitd7cae122008-07-26 21:49:38268 // The next ID that we will return from Add()
[email protected]9e7e0e02010-01-25 23:25:16269 KeyType next_id_;
initial.commitd7cae122008-07-26 21:49:38270
271 HashTable data_;
[email protected]a6ed9432009-07-01 22:35:26272
[email protected]a6ed9432009-07-01 22:35:26273 // See description above setter.
274 bool check_on_null_data_;
[email protected]9de09f82009-08-17 20:13:53275
276 DISALLOW_COPY_AND_ASSIGN(IDMap);
initial.commitd7cae122008-07-26 21:49:38277};
278
[email protected]925d5d602009-08-19 14:56:38279#endif // BASE_ID_MAP_H_