blob: b04bcdd184b686e7dd545976a137c3fa8ce4e122 [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"
jsbell41359ee2015-11-16 19:43:3413#include "base/sequence_checker.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>
jsbell41359ee2015-11-16 19:43:3436class IDMap {
[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) {
jsbell41359ee2015-11-16 19:43:3445 // A number of consumers of IDMap create it on one thread but always
46 // access it from a different, but consistent, thread (or sequence)
47 // post-construction. The first call to CalledOnValidSequencedThread()
48 // will re-bind it.
49 sequence_checker_.DetachFromSequence();
initial.commitd7cae122008-07-26 21:49:3850 }
51
[email protected]9e7e0e02010-01-25 23:25:1652 ~IDMap() {
jsbell41359ee2015-11-16 19:43:3453 // Many IDMap's are static, and hence will be destroyed on the main
54 // thread. However, all the accesses may take place on another thread (or
55 // sequence), such as the IO thread. Detaching again to clean this up.
56 sequence_checker_.DetachFromSequence();
[email protected]9e7e0e02010-01-25 23:25:1657 Releaser<OS, 0>::release_all(&data_);
58 }
59
michaelnbfea6ec2014-12-09 23:16:1360 // Sets whether Add and Replace should DCHECK if passed in NULL data.
61 // Default is false.
[email protected]a6ed9432009-07-01 22:35:2662 void set_check_on_null_data(bool value) { check_on_null_data_ = value; }
63
initial.commitd7cae122008-07-26 21:49:3864 // Adds a view with an automatically generated unique ID. See AddWithID.
[email protected]9e7e0e02010-01-25 23:25:1665 KeyType Add(T* data) {
jsbell41359ee2015-11-16 19:43:3466 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
michaelnbfea6ec2014-12-09 23:16:1367 DCHECK(!check_on_null_data_ || data);
[email protected]9e7e0e02010-01-25 23:25:1668 KeyType this_id = next_id_;
initial.commitd7cae122008-07-26 21:49:3869 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
70 data_[this_id] = data;
71 next_id_++;
72 return this_id;
73 }
74
75 // Adds a new data member with the specified ID. The ID must not be in
76 // the list. The caller either must generate all unique IDs itself and use
77 // this function, or allow this object to generate IDs and call Add. These
78 // two methods may not be mixed, or duplicate IDs may be generated
[email protected]9e7e0e02010-01-25 23:25:1679 void AddWithID(T* data, KeyType id) {
jsbell41359ee2015-11-16 19:43:3480 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
michaelnbfea6ec2014-12-09 23:16:1381 DCHECK(!check_on_null_data_ || data);
initial.commitd7cae122008-07-26 21:49:3882 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
83 data_[id] = data;
84 }
85
[email protected]9e7e0e02010-01-25 23:25:1686 void Remove(KeyType id) {
jsbell41359ee2015-11-16 19:43:3487 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
[email protected]9de09f82009-08-17 20:13:5388 typename HashTable::iterator i = data_.find(id);
initial.commitd7cae122008-07-26 21:49:3889 if (i == data_.end()) {
90 NOTREACHED() << "Attempting to remove an item not in the list";
91 return;
92 }
[email protected]9de09f82009-08-17 20:13:5393
[email protected]9e7e0e02010-01-25 23:25:1694 if (iteration_depth_ == 0) {
95 Releaser<OS, 0>::release(i->second);
[email protected]9de09f82009-08-17 20:13:5396 data_.erase(i);
[email protected]9e7e0e02010-01-25 23:25:1697 } else {
[email protected]9de09f82009-08-17 20:13:5398 removed_ids_.insert(id);
[email protected]9e7e0e02010-01-25 23:25:1699 }
initial.commitd7cae122008-07-26 21:49:38100 }
101
michaelnbfea6ec2014-12-09 23:16:13102 // Replaces the value for |id| with |new_data| and returns a pointer to the
103 // existing value. If there is no entry for |id|, the map is not altered and
104 // nullptr is returned. The OwnershipSemantics of the map have no effect on
105 // how the existing value is treated, the IDMap does not delete the existing
106 // value being replaced.
107 T* Replace(KeyType id, T* new_data) {
jsbell41359ee2015-11-16 19:43:34108 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
michaelnbfea6ec2014-12-09 23:16:13109 DCHECK(!check_on_null_data_ || new_data);
110 typename HashTable::iterator i = data_.find(id);
111 if (i == data_.end()) {
112 NOTREACHED() << "Attempting to replace an item not in the list";
113 return nullptr;
114 }
115
116 T* temp = i->second;
117 i->second = new_data;
118 return temp;
119 }
120
[email protected]fea0b9612012-10-29 21:36:22121 void Clear() {
jsbell41359ee2015-11-16 19:43:34122 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
[email protected]fea0b9612012-10-29 21:36:22123 if (iteration_depth_ == 0) {
124 Releaser<OS, 0>::release_all(&data_);
125 } else {
126 for (typename HashTable::iterator i = data_.begin();
127 i != data_.end(); ++i)
128 removed_ids_.insert(i->first);
129 }
130 }
131
initial.commitd7cae122008-07-26 21:49:38132 bool IsEmpty() const {
jsbell41359ee2015-11-16 19:43:34133 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
[email protected]2de069e2010-02-16 09:15:38134 return size() == 0u;
initial.commitd7cae122008-07-26 21:49:38135 }
136
[email protected]9e7e0e02010-01-25 23:25:16137 T* Lookup(KeyType id) const {
jsbell41359ee2015-11-16 19:43:34138 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
[email protected]9de09f82009-08-17 20:13:53139 typename HashTable::const_iterator i = data_.find(id);
initial.commitd7cae122008-07-26 21:49:38140 if (i == data_.end())
141 return NULL;
142 return i->second;
143 }
144
145 size_t size() const {
jsbell41359ee2015-11-16 19:43:34146 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
[email protected]2de069e2010-02-16 09:15:38147 return data_.size() - removed_ids_.size();
initial.commitd7cae122008-07-26 21:49:38148 }
149
[email protected]0c8b8942012-10-27 01:03:01150#if defined(UNIT_TEST)
151 int iteration_depth() const {
152 return iteration_depth_;
153 }
154#endif // defined(UNIT_TEST)
155
[email protected]9de09f82009-08-17 20:13:53156 // It is safe to remove elements from the map during iteration. All iterators
157 // will remain valid.
158 template<class ReturnType>
159 class Iterator {
160 public:
[email protected]00c39612010-03-06 02:53:28161 Iterator(IDMap<T, OS>* map)
[email protected]9de09f82009-08-17 20:13:53162 : map_(map),
163 iter_(map_->data_.begin()) {
[email protected]0c8b8942012-10-27 01:03:01164 Init();
165 }
166
167 Iterator(const Iterator& iter)
168 : map_(iter.map_),
169 iter_(iter.iter_) {
170 Init();
171 }
172
173 const Iterator& operator=(const Iterator& iter) {
174 map_ = iter.map;
175 iter_ = iter.iter;
176 Init();
177 return *this;
[email protected]9de09f82009-08-17 20:13:53178 }
179
180 ~Iterator() {
jsbell41359ee2015-11-16 19:43:34181 DCHECK(map_->sequence_checker_.CalledOnValidSequencedThread());
[email protected]0c8b8942012-10-27 01:03:01182
183 // We're going to decrement iteration depth. Make sure it's greater than
184 // zero so that it doesn't become negative.
185 DCHECK_LT(0, map_->iteration_depth_);
186
[email protected]9de09f82009-08-17 20:13:53187 if (--map_->iteration_depth_ == 0)
188 map_->Compact();
189 }
190
191 bool IsAtEnd() const {
jsbell41359ee2015-11-16 19:43:34192 DCHECK(map_->sequence_checker_.CalledOnValidSequencedThread());
[email protected]9de09f82009-08-17 20:13:53193 return iter_ == map_->data_.end();
194 }
195
[email protected]9e7e0e02010-01-25 23:25:16196 KeyType GetCurrentKey() const {
jsbell41359ee2015-11-16 19:43:34197 DCHECK(map_->sequence_checker_.CalledOnValidSequencedThread());
[email protected]9de09f82009-08-17 20:13:53198 return iter_->first;
199 }
200
201 ReturnType* GetCurrentValue() const {
jsbell41359ee2015-11-16 19:43:34202 DCHECK(map_->sequence_checker_.CalledOnValidSequencedThread());
[email protected]9de09f82009-08-17 20:13:53203 return iter_->second;
204 }
205
206 void Advance() {
jsbell41359ee2015-11-16 19:43:34207 DCHECK(map_->sequence_checker_.CalledOnValidSequencedThread());
[email protected]9de09f82009-08-17 20:13:53208 ++iter_;
209 SkipRemovedEntries();
210 }
211
212 private:
[email protected]0c8b8942012-10-27 01:03:01213 void Init() {
jsbell41359ee2015-11-16 19:43:34214 DCHECK(map_->sequence_checker_.CalledOnValidSequencedThread());
[email protected]0c8b8942012-10-27 01:03:01215 ++map_->iteration_depth_;
216 SkipRemovedEntries();
217 }
218
[email protected]9de09f82009-08-17 20:13:53219 void SkipRemovedEntries() {
220 while (iter_ != map_->data_.end() &&
221 map_->removed_ids_.find(iter_->first) !=
222 map_->removed_ids_.end()) {
223 ++iter_;
224 }
225 }
226
[email protected]00c39612010-03-06 02:53:28227 IDMap<T, OS>* map_;
[email protected]9de09f82009-08-17 20:13:53228 typename HashTable::const_iterator iter_;
229 };
230
231 typedef Iterator<T> iterator;
232 typedef Iterator<const T> const_iterator;
233
234 private:
[email protected]9e7e0e02010-01-25 23:25:16235
236 // The dummy parameter is there because C++ standard does not allow
237 // explicitly specialized templates inside classes
238 template<IDMapOwnershipSemantics OI, int dummy> struct Releaser {
239 static inline void release(T* ptr) {}
240 static inline void release_all(HashTable* table) {}
241 };
242
243 template<int dummy> struct Releaser<IDMapOwnPointer, dummy> {
244 static inline void release(T* ptr) { delete ptr;}
245 static inline void release_all(HashTable* table) {
246 for (typename HashTable::iterator i = table->begin();
247 i != table->end(); ++i) {
248 delete i->second;
249 }
250 table->clear();
251 }
252 };
253
[email protected]9de09f82009-08-17 20:13:53254 void Compact() {
255 DCHECK_EQ(0, iteration_depth_);
jkarlin7bc034c2015-09-25 20:45:47256 for (const auto& i : removed_ids_)
257 Remove(i);
[email protected]9de09f82009-08-17 20:13:53258 removed_ids_.clear();
259 }
260
261 // Keep track of how many iterators are currently iterating on us to safely
262 // handle removing items during iteration.
263 int iteration_depth_;
264
265 // Keep set of IDs that should be removed after the outermost iteration has
266 // finished. This way we manage to not invalidate the iterator when an element
267 // is removed.
[email protected]9e7e0e02010-01-25 23:25:16268 std::set<KeyType> removed_ids_;
[email protected]9de09f82009-08-17 20:13:53269
initial.commitd7cae122008-07-26 21:49:38270 // The next ID that we will return from Add()
[email protected]9e7e0e02010-01-25 23:25:16271 KeyType next_id_;
initial.commitd7cae122008-07-26 21:49:38272
273 HashTable data_;
[email protected]a6ed9432009-07-01 22:35:26274
[email protected]a6ed9432009-07-01 22:35:26275 // See description above setter.
276 bool check_on_null_data_;
[email protected]9de09f82009-08-17 20:13:53277
jsbell41359ee2015-11-16 19:43:34278 base::SequenceChecker sequence_checker_;
279
[email protected]9de09f82009-08-17 20:13:53280 DISALLOW_COPY_AND_ASSIGN(IDMap);
initial.commitd7cae122008-07-26 21:49:38281};
282
[email protected]925d5d602009-08-19 14:56:38283#endif // BASE_ID_MAP_H_