[email protected] | 4bc4f12 | 2011-03-08 02:07:29 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 5 | #ifndef BASE_CONTAINERS_ID_MAP_H_ |
| 6 | #define BASE_CONTAINERS_ID_MAP_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
jkarlin | 7bc034c | 2015-09-25 20:45:47 | [diff] [blame] | 9 | #include <stdint.h> |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 10 | |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 12 | #include <set> |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 13 | #include <type_traits> |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 14 | #include <unordered_map> |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 15 | #include <utility> |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 16 | |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 17 | #include "base/containers/flat_set.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 19 | #include "base/macros.h" |
jsbell | 41359ee | 2015-11-16 19:43:34 | [diff] [blame] | 20 | #include "base/sequence_checker.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 22 | namespace base { |
| 23 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | // 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. |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 32 | |
| 33 | // The map's value type (the V param) can be any dereferenceable type, such as a |
| 34 | // raw pointer or smart pointer |
| 35 | template <typename V, typename K = int32_t> |
| 36 | class IDMap final { |
[email protected] | b8b67081 | 2014-05-27 18:10:06 | [diff] [blame] | 37 | public: |
jkarlin | 7bc034c | 2015-09-25 20:45:47 | [diff] [blame] | 38 | using KeyType = K; |
[email protected] | b8b67081 | 2014-05-27 18:10:06 | [diff] [blame] | 39 | |
| 40 | private: |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 41 | using T = typename std::remove_reference<decltype(*V())>::type; |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 42 | |
| 43 | using HashTable = std::unordered_map<KeyType, V>; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | |
| 45 | public: |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 46 | IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
jsbell | 41359ee | 2015-11-16 19:43:34 | [diff] [blame] | 47 | // A number of consumers of IDMap create it on one thread but always |
| 48 | // access it from a different, but consistent, thread (or sequence) |
fdoray | e2b19a1 | 2016-07-29 02:30:16 | [diff] [blame] | 49 | // post-construction. The first call to CalledOnValidSequence() will re-bind |
| 50 | // it. |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 51 | DETACH_FROM_SEQUENCE(sequence_checker_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 54 | ~IDMap() { |
jsbell | 41359ee | 2015-11-16 19:43:34 | [diff] [blame] | 55 | // 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. |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 58 | DETACH_FROM_SEQUENCE(sequence_checker_); |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 59 | } |
| 60 | |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 61 | // Sets whether Add and Replace should DCHECK if passed in NULL data. |
| 62 | // Default is false. |
[email protected] | a6ed943 | 2009-07-01 22:35:26 | [diff] [blame] | 63 | void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
| 64 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 65 | // Adds a view with an automatically generated unique ID. See AddWithID. |
rlanday | 6eada032 | 2016-11-30 18:59:30 | [diff] [blame] | 66 | KeyType Add(V data) { return AddInternal(std::move(data)); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | |
| 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 |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 71 | // two methods may not be mixed, or duplicate IDs may be generated. |
rlanday | 6eada032 | 2016-11-30 18:59:30 | [diff] [blame] | 72 | void AddWithID(V data, KeyType id) { AddWithIDInternal(std::move(data), id); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 73 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 74 | void Remove(KeyType id) { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 75 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 76 | typename HashTable::iterator i = data_.find(id); |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 77 | if (i == data_.end() || IsRemoved(id)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | NOTREACHED() << "Attempting to remove an item not in the list"; |
| 79 | return; |
| 80 | } |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 81 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 82 | if (iteration_depth_ == 0) { |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 83 | data_.erase(i); |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 84 | } else { |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 85 | removed_ids_.insert(id); |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 86 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | } |
| 88 | |
sungmann.cho | 82e601f9 | 2017-02-17 04:43:13 | [diff] [blame] | 89 | // Replaces the value for |id| with |new_data| and returns the existing value. |
| 90 | // Should only be called with an already added id. |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 91 | V Replace(KeyType id, V new_data) { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 92 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 93 | DCHECK(!check_on_null_data_ || new_data); |
| 94 | typename HashTable::iterator i = data_.find(id); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 95 | DCHECK(i != data_.end()); |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 96 | DCHECK(!IsRemoved(id)); |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 97 | |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 98 | using std::swap; |
| 99 | swap(i->second, new_data); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 100 | return new_data; |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | fea0b961 | 2012-10-29 21:36:22 | [diff] [blame] | 103 | void Clear() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 104 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | fea0b961 | 2012-10-29 21:36:22 | [diff] [blame] | 105 | if (iteration_depth_ == 0) { |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 106 | data_.clear(); |
[email protected] | fea0b961 | 2012-10-29 21:36:22 | [diff] [blame] | 107 | } else { |
| 108 | for (typename HashTable::iterator i = data_.begin(); |
| 109 | i != data_.end(); ++i) |
| 110 | removed_ids_.insert(i->first); |
| 111 | } |
| 112 | } |
| 113 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 114 | bool IsEmpty() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 115 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 2de069e | 2010-02-16 09:15:38 | [diff] [blame] | 116 | return size() == 0u; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 119 | T* Lookup(KeyType id) const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 120 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 121 | typename HashTable::const_iterator i = data_.find(id); |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 122 | if (i == data_.end() || !i->second || IsRemoved(id)) |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 123 | return nullptr; |
| 124 | return &*i->second; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | size_t size() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 128 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 2de069e | 2010-02-16 09:15:38 | [diff] [blame] | 129 | return data_.size() - removed_ids_.size(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 132 | #if defined(UNIT_TEST) |
| 133 | int iteration_depth() const { |
| 134 | return iteration_depth_; |
| 135 | } |
| 136 | #endif // defined(UNIT_TEST) |
| 137 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 138 | // 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: |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 143 | Iterator(IDMap<V, K>* map) : map_(map), iter_(map_->data_.begin()) { |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 144 | 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] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | ~Iterator() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 161 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 162 | |
| 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] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 167 | if (--map_->iteration_depth_ == 0) |
| 168 | map_->Compact(); |
| 169 | } |
| 170 | |
| 171 | bool IsAtEnd() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 172 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 173 | return iter_ == map_->data_.end(); |
| 174 | } |
| 175 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 176 | KeyType GetCurrentKey() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 177 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 178 | return iter_->first; |
| 179 | } |
| 180 | |
| 181 | ReturnType* GetCurrentValue() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 182 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 183 | if (!iter_->second || map_->IsRemoved(iter_->first)) |
| 184 | return nullptr; |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 185 | return &*iter_->second; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void Advance() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 189 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 190 | ++iter_; |
| 191 | SkipRemovedEntries(); |
| 192 | } |
| 193 | |
| 194 | private: |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 195 | void Init() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 196 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 197 | ++map_->iteration_depth_; |
| 198 | SkipRemovedEntries(); |
| 199 | } |
| 200 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 201 | void SkipRemovedEntries() { |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 202 | while (iter_ != map_->data_.end() && map_->IsRemoved(iter_->first)) |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 203 | ++iter_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 204 | } |
| 205 | |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 206 | IDMap<V, K>* map_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 207 | typename HashTable::const_iterator iter_; |
| 208 | }; |
| 209 | |
| 210 | typedef Iterator<T> iterator; |
| 211 | typedef Iterator<const T> const_iterator; |
| 212 | |
| 213 | private: |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 214 | KeyType AddInternal(V data) { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 215 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 216 | 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] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 223 | |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 224 | void AddWithIDInternal(V data, KeyType id) { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 225 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 226 | DCHECK(!check_on_null_data_ || data); |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 227 | if (IsRemoved(id)) { |
| 228 | removed_ids_.erase(id); |
| 229 | } else { |
| 230 | DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| 231 | } |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 232 | data_[id] = std::move(data); |
| 233 | } |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 234 | |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 235 | bool IsRemoved(KeyType key) const { |
| 236 | return removed_ids_.find(key) != removed_ids_.end(); |
| 237 | } |
| 238 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 239 | void Compact() { |
| 240 | DCHECK_EQ(0, iteration_depth_); |
jkarlin | 7bc034c | 2015-09-25 20:45:47 | [diff] [blame] | 241 | for (const auto& i : removed_ids_) |
tzik | d164667 | 2018-03-28 20:41:13 | [diff] [blame^] | 242 | data_.erase(i); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 243 | 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. |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 253 | base::flat_set<KeyType> removed_ids_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 254 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | // The next ID that we will return from Add() |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 256 | KeyType next_id_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 257 | |
| 258 | HashTable data_; |
[email protected] | a6ed943 | 2009-07-01 22:35:26 | [diff] [blame] | 259 | |
[email protected] | a6ed943 | 2009-07-01 22:35:26 | [diff] [blame] | 260 | // See description above setter. |
| 261 | bool check_on_null_data_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 262 | |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame] | 263 | SEQUENCE_CHECKER(sequence_checker_); |
jsbell | 41359ee | 2015-11-16 19:43:34 | [diff] [blame] | 264 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 265 | DISALLOW_COPY_AND_ASSIGN(IDMap); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 266 | }; |
| 267 | |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 268 | } // namespace base |
| 269 | |
| 270 | #endif // BASE_CONTAINERS_ID_MAP_H_ |