blob: 64f546c763fd35eb9e3e185112e6fc0cc68477c0 [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
Brett Wilsonf976d3f2017-08-18 17:23:395#ifndef BASE_CONTAINERS_ID_MAP_H_
6#define BASE_CONTAINERS_ID_MAP_H_
initial.commitd7cae122008-07-26 21:49:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
jkarlin7bc034c2015-09-25 20:45:479#include <stdint.h>
brettw1ce49f62017-04-27 19:42:3210
aeliasaed214d02016-09-24 01:26:4211#include <memory>
[email protected]9de09f82009-08-17 20:13:5312#include <set>
aeliasaed214d02016-09-24 01:26:4213#include <type_traits>
brettw1ce49f62017-04-27 19:42:3214#include <unordered_map>
aeliasaed214d02016-09-24 01:26:4215#include <utility>
[email protected]9de09f82009-08-17 20:13:5316
brettw1ce49f62017-04-27 19:42:3217#include "base/containers/flat_set.h"
initial.commitd7cae122008-07-26 21:49:3818#include "base/logging.h"
avi9b6f42932015-12-26 22:15:1419#include "base/macros.h"
jsbell41359ee2015-11-16 19:43:3420#include "base/sequence_checker.h"
initial.commitd7cae122008-07-26 21:49:3821
Brett Wilsonf976d3f2017-08-18 17:23:3922namespace base {
23
initial.commitd7cae122008-07-26 21:49:3824// This object maintains a list of IDs that can be quickly converted to
25// pointers to objects. It is implemented as a hash table, optimized for
26// relatively small data sets (in the common case, there will be exactly one
27// item in the list).
28//
29// Items can be inserted into the container with arbitrary ID, but the caller
30// must ensure they are unique. Inserting IDs and relying on automatically
31// generated ones is not allowed because they can collide.
rlandayde24c6d2016-12-01 03:05:4032
33// The map's value type (the V param) can be any dereferenceable type, such as a
34// raw pointer or smart pointer
35template <typename V, typename K = int32_t>
36class IDMap final {
[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:
rlandayde24c6d2016-12-01 03:05:4041 using T = typename std::remove_reference<decltype(*V())>::type;
brettw1ce49f62017-04-27 19:42:3242
43 using HashTable = std::unordered_map<KeyType, V>;
initial.commitd7cae122008-07-26 21:49:3844
45 public:
[email protected]9de09f82009-08-17 20:13:5346 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) {
jsbell41359ee2015-11-16 19:43:3447 // A number of consumers of IDMap create it on one thread but always
48 // access it from a different, but consistent, thread (or sequence)
fdoraye2b19a12016-07-29 02:30:1649 // post-construction. The first call to CalledOnValidSequence() will re-bind
50 // it.
tzikfd754ee2018-03-28 15:49:2851 DETACH_FROM_SEQUENCE(sequence_checker_);
initial.commitd7cae122008-07-26 21:49:3852 }
53
[email protected]9e7e0e02010-01-25 23:25:1654 ~IDMap() {
jsbell41359ee2015-11-16 19:43:3455 // Many IDMap's are static, and hence will be destroyed on the main
56 // thread. However, all the accesses may take place on another thread (or
57 // sequence), such as the IO thread. Detaching again to clean this up.
tzikfd754ee2018-03-28 15:49:2858 DETACH_FROM_SEQUENCE(sequence_checker_);
[email protected]9e7e0e02010-01-25 23:25:1659 }
60
michaelnbfea6ec2014-12-09 23:16:1361 // Sets whether Add and Replace should DCHECK if passed in NULL data.
62 // Default is false.
[email protected]a6ed9432009-07-01 22:35:2663 void set_check_on_null_data(bool value) { check_on_null_data_ = value; }
64
initial.commitd7cae122008-07-26 21:49:3865 // Adds a view with an automatically generated unique ID. See AddWithID.
rlanday6eada0322016-11-30 18:59:3066 KeyType Add(V data) { return AddInternal(std::move(data)); }
initial.commitd7cae122008-07-26 21:49:3867
68 // Adds a new data member with the specified ID. The ID must not be in
69 // the list. The caller either must generate all unique IDs itself and use
70 // this function, or allow this object to generate IDs and call Add. These
aeliasaed214d02016-09-24 01:26:4271 // two methods may not be mixed, or duplicate IDs may be generated.
rlanday6eada0322016-11-30 18:59:3072 void AddWithID(V data, KeyType id) { AddWithIDInternal(std::move(data), id); }
initial.commitd7cae122008-07-26 21:49:3873
[email protected]9e7e0e02010-01-25 23:25:1674 void Remove(KeyType id) {
tzikfd754ee2018-03-28 15:49:2875 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]9de09f82009-08-17 20:13:5376 typename HashTable::iterator i = data_.find(id);
tzikd1646672018-03-28 20:41:1377 if (i == data_.end() || IsRemoved(id)) {
initial.commitd7cae122008-07-26 21:49:3878 NOTREACHED() << "Attempting to remove an item not in the list";
79 return;
80 }
[email protected]9de09f82009-08-17 20:13:5381
[email protected]9e7e0e02010-01-25 23:25:1682 if (iteration_depth_ == 0) {
[email protected]9de09f82009-08-17 20:13:5383 data_.erase(i);
[email protected]9e7e0e02010-01-25 23:25:1684 } else {
[email protected]9de09f82009-08-17 20:13:5385 removed_ids_.insert(id);
[email protected]9e7e0e02010-01-25 23:25:1686 }
initial.commitd7cae122008-07-26 21:49:3887 }
88
sungmann.cho82e601f92017-02-17 04:43:1389 // Replaces the value for |id| with |new_data| and returns the existing value.
90 // Should only be called with an already added id.
aeliasaed214d02016-09-24 01:26:4291 V Replace(KeyType id, V new_data) {
tzikfd754ee2018-03-28 15:49:2892 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
michaelnbfea6ec2014-12-09 23:16:1393 DCHECK(!check_on_null_data_ || new_data);
94 typename HashTable::iterator i = data_.find(id);
aeliasaed214d02016-09-24 01:26:4295 DCHECK(i != data_.end());
tzikd1646672018-03-28 20:41:1396 DCHECK(!IsRemoved(id));
michaelnbfea6ec2014-12-09 23:16:1397
tzikd1646672018-03-28 20:41:1398 using std::swap;
99 swap(i->second, new_data);
aeliasaed214d02016-09-24 01:26:42100 return new_data;
michaelnbfea6ec2014-12-09 23:16:13101 }
102
[email protected]fea0b9612012-10-29 21:36:22103 void Clear() {
tzikfd754ee2018-03-28 15:49:28104 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]fea0b9612012-10-29 21:36:22105 if (iteration_depth_ == 0) {
aeliasaed214d02016-09-24 01:26:42106 data_.clear();
[email protected]fea0b9612012-10-29 21:36:22107 } else {
108 for (typename HashTable::iterator i = data_.begin();
109 i != data_.end(); ++i)
110 removed_ids_.insert(i->first);
111 }
112 }
113
initial.commitd7cae122008-07-26 21:49:38114 bool IsEmpty() const {
tzikfd754ee2018-03-28 15:49:28115 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]2de069e2010-02-16 09:15:38116 return size() == 0u;
initial.commitd7cae122008-07-26 21:49:38117 }
118
[email protected]9e7e0e02010-01-25 23:25:16119 T* Lookup(KeyType id) const {
tzikfd754ee2018-03-28 15:49:28120 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]9de09f82009-08-17 20:13:53121 typename HashTable::const_iterator i = data_.find(id);
tzikd1646672018-03-28 20:41:13122 if (i == data_.end() || !i->second || IsRemoved(id))
aeliasaed214d02016-09-24 01:26:42123 return nullptr;
124 return &*i->second;
initial.commitd7cae122008-07-26 21:49:38125 }
126
127 size_t size() const {
tzikfd754ee2018-03-28 15:49:28128 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]2de069e2010-02-16 09:15:38129 return data_.size() - removed_ids_.size();
initial.commitd7cae122008-07-26 21:49:38130 }
131
[email protected]0c8b8942012-10-27 01:03:01132#if defined(UNIT_TEST)
133 int iteration_depth() const {
134 return iteration_depth_;
135 }
136#endif // defined(UNIT_TEST)
137
[email protected]9de09f82009-08-17 20:13:53138 // It is safe to remove elements from the map during iteration. All iterators
139 // will remain valid.
140 template<class ReturnType>
141 class Iterator {
142 public:
rlandayde24c6d2016-12-01 03:05:40143 Iterator(IDMap<V, K>* map) : map_(map), iter_(map_->data_.begin()) {
[email protected]0c8b8942012-10-27 01:03:01144 Init();
145 }
146
147 Iterator(const Iterator& iter)
148 : map_(iter.map_),
149 iter_(iter.iter_) {
150 Init();
151 }
152
153 const Iterator& operator=(const Iterator& iter) {
154 map_ = iter.map;
155 iter_ = iter.iter;
156 Init();
157 return *this;
[email protected]9de09f82009-08-17 20:13:53158 }
159
160 ~Iterator() {
tzikfd754ee2018-03-28 15:49:28161 DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_);
[email protected]0c8b8942012-10-27 01:03:01162
163 // We're going to decrement iteration depth. Make sure it's greater than
164 // zero so that it doesn't become negative.
165 DCHECK_LT(0, map_->iteration_depth_);
166
[email protected]9de09f82009-08-17 20:13:53167 if (--map_->iteration_depth_ == 0)
168 map_->Compact();
169 }
170
171 bool IsAtEnd() const {
tzikfd754ee2018-03-28 15:49:28172 DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_);
[email protected]9de09f82009-08-17 20:13:53173 return iter_ == map_->data_.end();
174 }
175
[email protected]9e7e0e02010-01-25 23:25:16176 KeyType GetCurrentKey() const {
tzikfd754ee2018-03-28 15:49:28177 DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_);
[email protected]9de09f82009-08-17 20:13:53178 return iter_->first;
179 }
180
181 ReturnType* GetCurrentValue() const {
tzikfd754ee2018-03-28 15:49:28182 DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_);
tzikd1646672018-03-28 20:41:13183 if (!iter_->second || map_->IsRemoved(iter_->first))
184 return nullptr;
aeliasaed214d02016-09-24 01:26:42185 return &*iter_->second;
[email protected]9de09f82009-08-17 20:13:53186 }
187
188 void Advance() {
tzikfd754ee2018-03-28 15:49:28189 DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_);
[email protected]9de09f82009-08-17 20:13:53190 ++iter_;
191 SkipRemovedEntries();
192 }
193
194 private:
[email protected]0c8b8942012-10-27 01:03:01195 void Init() {
tzikfd754ee2018-03-28 15:49:28196 DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_);
[email protected]0c8b8942012-10-27 01:03:01197 ++map_->iteration_depth_;
198 SkipRemovedEntries();
199 }
200
[email protected]9de09f82009-08-17 20:13:53201 void SkipRemovedEntries() {
tzikd1646672018-03-28 20:41:13202 while (iter_ != map_->data_.end() && map_->IsRemoved(iter_->first))
[email protected]9de09f82009-08-17 20:13:53203 ++iter_;
[email protected]9de09f82009-08-17 20:13:53204 }
205
rlandayde24c6d2016-12-01 03:05:40206 IDMap<V, K>* map_;
[email protected]9de09f82009-08-17 20:13:53207 typename HashTable::const_iterator iter_;
208 };
209
210 typedef Iterator<T> iterator;
211 typedef Iterator<const T> const_iterator;
212
213 private:
aeliasaed214d02016-09-24 01:26:42214 KeyType AddInternal(V data) {
tzikfd754ee2018-03-28 15:49:28215 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
aeliasaed214d02016-09-24 01:26:42216 DCHECK(!check_on_null_data_ || data);
217 KeyType this_id = next_id_;
218 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
219 data_[this_id] = std::move(data);
220 next_id_++;
221 return this_id;
222 }
[email protected]9e7e0e02010-01-25 23:25:16223
aeliasaed214d02016-09-24 01:26:42224 void AddWithIDInternal(V data, KeyType id) {
tzikfd754ee2018-03-28 15:49:28225 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
aeliasaed214d02016-09-24 01:26:42226 DCHECK(!check_on_null_data_ || data);
tzikd1646672018-03-28 20:41:13227 if (IsRemoved(id)) {
228 removed_ids_.erase(id);
229 } else {
230 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
231 }
aeliasaed214d02016-09-24 01:26:42232 data_[id] = std::move(data);
233 }
[email protected]9e7e0e02010-01-25 23:25:16234
tzikd1646672018-03-28 20:41:13235 bool IsRemoved(KeyType key) const {
236 return removed_ids_.find(key) != removed_ids_.end();
237 }
238
[email protected]9de09f82009-08-17 20:13:53239 void Compact() {
240 DCHECK_EQ(0, iteration_depth_);
jkarlin7bc034c2015-09-25 20:45:47241 for (const auto& i : removed_ids_)
tzikd1646672018-03-28 20:41:13242 data_.erase(i);
[email protected]9de09f82009-08-17 20:13:53243 removed_ids_.clear();
244 }
245
246 // Keep track of how many iterators are currently iterating on us to safely
247 // handle removing items during iteration.
248 int iteration_depth_;
249
250 // Keep set of IDs that should be removed after the outermost iteration has
251 // finished. This way we manage to not invalidate the iterator when an element
252 // is removed.
brettw1ce49f62017-04-27 19:42:32253 base::flat_set<KeyType> removed_ids_;
[email protected]9de09f82009-08-17 20:13:53254
initial.commitd7cae122008-07-26 21:49:38255 // The next ID that we will return from Add()
[email protected]9e7e0e02010-01-25 23:25:16256 KeyType next_id_;
initial.commitd7cae122008-07-26 21:49:38257
258 HashTable data_;
[email protected]a6ed9432009-07-01 22:35:26259
[email protected]a6ed9432009-07-01 22:35:26260 // See description above setter.
261 bool check_on_null_data_;
[email protected]9de09f82009-08-17 20:13:53262
tzikfd754ee2018-03-28 15:49:28263 SEQUENCE_CHECKER(sequence_checker_);
jsbell41359ee2015-11-16 19:43:34264
[email protected]9de09f82009-08-17 20:13:53265 DISALLOW_COPY_AND_ASSIGN(IDMap);
initial.commitd7cae122008-07-26 21:49:38266};
267
Brett Wilsonf976d3f2017-08-18 17:23:39268} // namespace base
269
270#endif // BASE_CONTAINERS_ID_MAP_H_