cbentzel@chromium.org | 95cb751 | 2011-03-08 11:07:29 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 2 | // 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 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 9 | #include <stdint.h> |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 10 | #include <memory> |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 11 | #include <set> |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 12 | #include <type_traits> |
| 13 | #include <utility> |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 14 | |
brettw@chromium.org | 30230a8 | 2013-06-12 02:52:44 +0900 | [diff] [blame] | 15 | #include "base/containers/hash_tables.h" |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 16 | #include "base/logging.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 17 | #include "base/macros.h" |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 18 | #include "base/sequence_checker.h" |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 19 | |
| 20 | // This object maintains a list of IDs that can be quickly converted to |
| 21 | // pointers to objects. It is implemented as a hash table, optimized for |
| 22 | // relatively small data sets (in the common case, there will be exactly one |
| 23 | // item in the list). |
| 24 | // |
| 25 | // Items can be inserted into the container with arbitrary ID, but the caller |
| 26 | // must ensure they are unique. Inserting IDs and relying on automatically |
| 27 | // generated ones is not allowed because they can collide. |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 28 | |
| 29 | // The map's value type (the V param) can be any dereferenceable type, such as a |
| 30 | // raw pointer or smart pointer |
| 31 | template <typename V, typename K = int32_t> |
| 32 | class IDMap final { |
mnaganov@chromium.org | 0f3c4e6 | 2014-05-28 03:10:06 +0900 | [diff] [blame] | 33 | public: |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 34 | using KeyType = K; |
mnaganov@chromium.org | 0f3c4e6 | 2014-05-28 03:10:06 +0900 | [diff] [blame] | 35 | |
| 36 | private: |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 37 | using T = typename std::remove_reference<decltype(*V())>::type; |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 38 | using HashTable = base::hash_map<KeyType, V>; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 39 | |
| 40 | public: |
| 41 | IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 42 | // A number of consumers of IDMap create it on one thread but always |
| 43 | // access it from a different, but consistent, thread (or sequence) |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 44 | // post-construction. The first call to CalledOnValidSequence() will re-bind |
| 45 | // it. |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 46 | sequence_checker_.DetachFromSequence(); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 47 | } |
| 48 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 49 | ~IDMap() { |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 50 | // Many IDMap's are static, and hence will be destroyed on the main |
| 51 | // thread. However, all the accesses may take place on another thread (or |
| 52 | // sequence), such as the IO thread. Detaching again to clean this up. |
| 53 | sequence_checker_.DetachFromSequence(); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 54 | } |
| 55 | |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 56 | // Sets whether Add and Replace should DCHECK if passed in NULL data. |
| 57 | // Default is false. |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 58 | void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
| 59 | |
| 60 | // Adds a view with an automatically generated unique ID. See AddWithID. |
rlanday | f5b01dd | 2016-12-01 03:59:30 +0900 | [diff] [blame] | 61 | KeyType Add(V data) { return AddInternal(std::move(data)); } |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 62 | |
| 63 | // Adds a new data member with the specified ID. The ID must not be in |
| 64 | // the list. The caller either must generate all unique IDs itself and use |
| 65 | // this function, or allow this object to generate IDs and call Add. These |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 66 | // two methods may not be mixed, or duplicate IDs may be generated. |
rlanday | f5b01dd | 2016-12-01 03:59:30 +0900 | [diff] [blame] | 67 | void AddWithID(V data, KeyType id) { AddWithIDInternal(std::move(data), id); } |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 68 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 69 | void Remove(KeyType id) { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 70 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 71 | typename HashTable::iterator i = data_.find(id); |
| 72 | if (i == data_.end()) { |
| 73 | NOTREACHED() << "Attempting to remove an item not in the list"; |
| 74 | return; |
| 75 | } |
| 76 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 77 | if (iteration_depth_ == 0) { |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 78 | data_.erase(i); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 79 | } else { |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 80 | removed_ids_.insert(id); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 81 | } |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 82 | } |
| 83 | |
sungmann.cho | 9ae032a | 2017-02-17 13:43:13 +0900 | [diff] [blame] | 84 | // Replaces the value for |id| with |new_data| and returns the existing value. |
| 85 | // Should only be called with an already added id. |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 86 | V Replace(KeyType id, V new_data) { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 87 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 88 | DCHECK(!check_on_null_data_ || new_data); |
| 89 | typename HashTable::iterator i = data_.find(id); |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 90 | DCHECK(i != data_.end()); |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 91 | |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 92 | std::swap(i->second, new_data); |
| 93 | return new_data; |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 94 | } |
| 95 | |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 96 | void Clear() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 97 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 98 | if (iteration_depth_ == 0) { |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 99 | data_.clear(); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 100 | } else { |
| 101 | for (typename HashTable::iterator i = data_.begin(); |
| 102 | i != data_.end(); ++i) |
| 103 | removed_ids_.insert(i->first); |
| 104 | } |
| 105 | } |
| 106 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 107 | bool IsEmpty() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 108 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 109 | return size() == 0u; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 110 | } |
| 111 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 112 | T* Lookup(KeyType id) const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 113 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 114 | typename HashTable::const_iterator i = data_.find(id); |
| 115 | if (i == data_.end()) |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 116 | return nullptr; |
| 117 | return &*i->second; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | size_t size() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 121 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 122 | return data_.size() - removed_ids_.size(); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 123 | } |
| 124 | |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 125 | #if defined(UNIT_TEST) |
| 126 | int iteration_depth() const { |
| 127 | return iteration_depth_; |
| 128 | } |
| 129 | #endif // defined(UNIT_TEST) |
| 130 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 131 | // It is safe to remove elements from the map during iteration. All iterators |
| 132 | // will remain valid. |
| 133 | template<class ReturnType> |
| 134 | class Iterator { |
| 135 | public: |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 136 | Iterator(IDMap<V, K>* map) : map_(map), iter_(map_->data_.begin()) { |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 137 | Init(); |
| 138 | } |
| 139 | |
| 140 | Iterator(const Iterator& iter) |
| 141 | : map_(iter.map_), |
| 142 | iter_(iter.iter_) { |
| 143 | Init(); |
| 144 | } |
| 145 | |
| 146 | const Iterator& operator=(const Iterator& iter) { |
| 147 | map_ = iter.map; |
| 148 | iter_ = iter.iter; |
| 149 | Init(); |
| 150 | return *this; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | ~Iterator() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 154 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 155 | |
| 156 | // We're going to decrement iteration depth. Make sure it's greater than |
| 157 | // zero so that it doesn't become negative. |
| 158 | DCHECK_LT(0, map_->iteration_depth_); |
| 159 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 160 | if (--map_->iteration_depth_ == 0) |
| 161 | map_->Compact(); |
| 162 | } |
| 163 | |
| 164 | bool IsAtEnd() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 165 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 166 | return iter_ == map_->data_.end(); |
| 167 | } |
| 168 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 169 | KeyType GetCurrentKey() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 170 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 171 | return iter_->first; |
| 172 | } |
| 173 | |
| 174 | ReturnType* GetCurrentValue() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 175 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 176 | return &*iter_->second; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void Advance() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 180 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 181 | ++iter_; |
| 182 | SkipRemovedEntries(); |
| 183 | } |
| 184 | |
| 185 | private: |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 186 | void Init() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 187 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 188 | ++map_->iteration_depth_; |
| 189 | SkipRemovedEntries(); |
| 190 | } |
| 191 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 192 | void SkipRemovedEntries() { |
| 193 | while (iter_ != map_->data_.end() && |
| 194 | map_->removed_ids_.find(iter_->first) != |
| 195 | map_->removed_ids_.end()) { |
| 196 | ++iter_; |
| 197 | } |
| 198 | } |
| 199 | |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 200 | IDMap<V, K>* map_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 201 | typename HashTable::const_iterator iter_; |
| 202 | }; |
| 203 | |
| 204 | typedef Iterator<T> iterator; |
| 205 | typedef Iterator<const T> const_iterator; |
| 206 | |
| 207 | private: |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 208 | KeyType AddInternal(V data) { |
| 209 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 210 | DCHECK(!check_on_null_data_ || data); |
| 211 | KeyType this_id = next_id_; |
| 212 | DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
| 213 | data_[this_id] = std::move(data); |
| 214 | next_id_++; |
| 215 | return this_id; |
| 216 | } |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 217 | |
aelias | 9fd2570 | 2016-09-24 10:26:42 +0900 | [diff] [blame] | 218 | void AddWithIDInternal(V data, KeyType id) { |
| 219 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 220 | DCHECK(!check_on_null_data_ || data); |
| 221 | DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| 222 | data_[id] = std::move(data); |
| 223 | } |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 224 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 225 | void Compact() { |
| 226 | DCHECK_EQ(0, iteration_depth_); |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 227 | for (const auto& i : removed_ids_) |
| 228 | Remove(i); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 229 | removed_ids_.clear(); |
| 230 | } |
| 231 | |
| 232 | // Keep track of how many iterators are currently iterating on us to safely |
| 233 | // handle removing items during iteration. |
| 234 | int iteration_depth_; |
| 235 | |
| 236 | // Keep set of IDs that should be removed after the outermost iteration has |
| 237 | // finished. This way we manage to not invalidate the iterator when an element |
| 238 | // is removed. |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 239 | std::set<KeyType> removed_ids_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 240 | |
| 241 | // The next ID that we will return from Add() |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 242 | KeyType next_id_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 243 | |
| 244 | HashTable data_; |
| 245 | |
| 246 | // See description above setter. |
| 247 | bool check_on_null_data_; |
| 248 | |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 249 | base::SequenceChecker sequence_checker_; |
| 250 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 251 | DISALLOW_COPY_AND_ASSIGN(IDMap); |
| 252 | }; |
| 253 | |
| 254 | #endif // BASE_ID_MAP_H_ |