blob: ab3c11214b5a6349d185cc205f48f9d2cb5f4bad [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.
jsbell41359ee2015-11-16 19:43:3451 sequence_checker_.DetachFromSequence();
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.
58 sequence_checker_.DetachFromSequence();
[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) {
fdoraye2b19a12016-07-29 02:30:1675 DCHECK(sequence_checker_.CalledOnValidSequence());
[email protected]9de09f82009-08-17 20:13:5376 typename HashTable::iterator i = data_.find(id);
initial.commitd7cae122008-07-26 21:49:3877 if (i == data_.end()) {
78 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) {
fdoraye2b19a12016-07-29 02:30:1692 DCHECK(sequence_checker_.CalledOnValidSequence());
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());
michaelnbfea6ec2014-12-09 23:16:1396
aeliasaed214d02016-09-24 01:26:4297 std::swap(i->second, new_data);
98 return new_data;
michaelnbfea6ec2014-12-09 23:16:1399 }
100
[email protected]fea0b9612012-10-29 21:36:22101 void Clear() {
fdoraye2b19a12016-07-29 02:30:16102 DCHECK(sequence_checker_.CalledOnValidSequence());
[email protected]fea0b9612012-10-29 21:36:22103 if (iteration_depth_ == 0) {
aeliasaed214d02016-09-24 01:26:42104 data_.clear();
[email protected]fea0b9612012-10-29 21:36:22105 } else {
106 for (typename HashTable::iterator i = data_.begin();
107 i != data_.end(); ++i)
108 removed_ids_.insert(i->first);
109 }
110 }
111
initial.commitd7cae122008-07-26 21:49:38112 bool IsEmpty() const {
fdoraye2b19a12016-07-29 02:30:16113 DCHECK(sequence_checker_.CalledOnValidSequence());
[email protected]2de069e2010-02-16 09:15:38114 return size() == 0u;
initial.commitd7cae122008-07-26 21:49:38115 }
116
[email protected]9e7e0e02010-01-25 23:25:16117 T* Lookup(KeyType id) const {
fdoraye2b19a12016-07-29 02:30:16118 DCHECK(sequence_checker_.CalledOnValidSequence());
[email protected]9de09f82009-08-17 20:13:53119 typename HashTable::const_iterator i = data_.find(id);
initial.commitd7cae122008-07-26 21:49:38120 if (i == data_.end())
aeliasaed214d02016-09-24 01:26:42121 return nullptr;
122 return &*i->second;
initial.commitd7cae122008-07-26 21:49:38123 }
124
125 size_t size() const {
fdoraye2b19a12016-07-29 02:30:16126 DCHECK(sequence_checker_.CalledOnValidSequence());
[email protected]2de069e2010-02-16 09:15:38127 return data_.size() - removed_ids_.size();
initial.commitd7cae122008-07-26 21:49:38128 }
129
[email protected]0c8b8942012-10-27 01:03:01130#if defined(UNIT_TEST)
131 int iteration_depth() const {
132 return iteration_depth_;
133 }
134#endif // defined(UNIT_TEST)
135
[email protected]9de09f82009-08-17 20:13:53136 // It is safe to remove elements from the map during iteration. All iterators
137 // will remain valid.
138 template<class ReturnType>
139 class Iterator {
140 public:
rlandayde24c6d2016-12-01 03:05:40141 Iterator(IDMap<V, K>* map) : map_(map), iter_(map_->data_.begin()) {
[email protected]0c8b8942012-10-27 01:03:01142 Init();
143 }
144
145 Iterator(const Iterator& iter)
146 : map_(iter.map_),
147 iter_(iter.iter_) {
148 Init();
149 }
150
151 const Iterator& operator=(const Iterator& iter) {
152 map_ = iter.map;
153 iter_ = iter.iter;
154 Init();
155 return *this;
[email protected]9de09f82009-08-17 20:13:53156 }
157
158 ~Iterator() {
fdoraye2b19a12016-07-29 02:30:16159 DCHECK(map_->sequence_checker_.CalledOnValidSequence());
[email protected]0c8b8942012-10-27 01:03:01160
161 // We're going to decrement iteration depth. Make sure it's greater than
162 // zero so that it doesn't become negative.
163 DCHECK_LT(0, map_->iteration_depth_);
164
[email protected]9de09f82009-08-17 20:13:53165 if (--map_->iteration_depth_ == 0)
166 map_->Compact();
167 }
168
169 bool IsAtEnd() const {
fdoraye2b19a12016-07-29 02:30:16170 DCHECK(map_->sequence_checker_.CalledOnValidSequence());
[email protected]9de09f82009-08-17 20:13:53171 return iter_ == map_->data_.end();
172 }
173
[email protected]9e7e0e02010-01-25 23:25:16174 KeyType GetCurrentKey() const {
fdoraye2b19a12016-07-29 02:30:16175 DCHECK(map_->sequence_checker_.CalledOnValidSequence());
[email protected]9de09f82009-08-17 20:13:53176 return iter_->first;
177 }
178
179 ReturnType* GetCurrentValue() const {
fdoraye2b19a12016-07-29 02:30:16180 DCHECK(map_->sequence_checker_.CalledOnValidSequence());
aeliasaed214d02016-09-24 01:26:42181 return &*iter_->second;
[email protected]9de09f82009-08-17 20:13:53182 }
183
184 void Advance() {
fdoraye2b19a12016-07-29 02:30:16185 DCHECK(map_->sequence_checker_.CalledOnValidSequence());
[email protected]9de09f82009-08-17 20:13:53186 ++iter_;
187 SkipRemovedEntries();
188 }
189
190 private:
[email protected]0c8b8942012-10-27 01:03:01191 void Init() {
fdoraye2b19a12016-07-29 02:30:16192 DCHECK(map_->sequence_checker_.CalledOnValidSequence());
[email protected]0c8b8942012-10-27 01:03:01193 ++map_->iteration_depth_;
194 SkipRemovedEntries();
195 }
196
[email protected]9de09f82009-08-17 20:13:53197 void SkipRemovedEntries() {
198 while (iter_ != map_->data_.end() &&
199 map_->removed_ids_.find(iter_->first) !=
200 map_->removed_ids_.end()) {
201 ++iter_;
202 }
203 }
204
rlandayde24c6d2016-12-01 03:05:40205 IDMap<V, K>* map_;
[email protected]9de09f82009-08-17 20:13:53206 typename HashTable::const_iterator iter_;
207 };
208
209 typedef Iterator<T> iterator;
210 typedef Iterator<const T> const_iterator;
211
212 private:
aeliasaed214d02016-09-24 01:26:42213 KeyType AddInternal(V data) {
214 DCHECK(sequence_checker_.CalledOnValidSequence());
215 DCHECK(!check_on_null_data_ || data);
216 KeyType this_id = next_id_;
217 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
218 data_[this_id] = std::move(data);
219 next_id_++;
220 return this_id;
221 }
[email protected]9e7e0e02010-01-25 23:25:16222
aeliasaed214d02016-09-24 01:26:42223 void AddWithIDInternal(V data, KeyType id) {
224 DCHECK(sequence_checker_.CalledOnValidSequence());
225 DCHECK(!check_on_null_data_ || data);
226 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
227 data_[id] = std::move(data);
228 }
[email protected]9e7e0e02010-01-25 23:25:16229
[email protected]9de09f82009-08-17 20:13:53230 void Compact() {
231 DCHECK_EQ(0, iteration_depth_);
jkarlin7bc034c2015-09-25 20:45:47232 for (const auto& i : removed_ids_)
233 Remove(i);
[email protected]9de09f82009-08-17 20:13:53234 removed_ids_.clear();
235 }
236
237 // Keep track of how many iterators are currently iterating on us to safely
238 // handle removing items during iteration.
239 int iteration_depth_;
240
241 // Keep set of IDs that should be removed after the outermost iteration has
242 // finished. This way we manage to not invalidate the iterator when an element
243 // is removed.
brettw1ce49f62017-04-27 19:42:32244 base::flat_set<KeyType> removed_ids_;
[email protected]9de09f82009-08-17 20:13:53245
initial.commitd7cae122008-07-26 21:49:38246 // The next ID that we will return from Add()
[email protected]9e7e0e02010-01-25 23:25:16247 KeyType next_id_;
initial.commitd7cae122008-07-26 21:49:38248
249 HashTable data_;
[email protected]a6ed9432009-07-01 22:35:26250
[email protected]a6ed9432009-07-01 22:35:26251 // See description above setter.
252 bool check_on_null_data_;
[email protected]9de09f82009-08-17 20:13:53253
jsbell41359ee2015-11-16 19:43:34254 base::SequenceChecker sequence_checker_;
255
[email protected]9de09f82009-08-17 20:13:53256 DISALLOW_COPY_AND_ASSIGN(IDMap);
initial.commitd7cae122008-07-26 21:49:38257};
258
Brett Wilsonf976d3f2017-08-18 17:23:39259} // namespace base
260
261#endif // BASE_CONTAINERS_ID_MAP_H_