[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); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | if (i == data_.end()) { |
| 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()); |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 96 | |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 97 | std::swap(i->second, new_data); |
| 98 | return new_data; |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | fea0b961 | 2012-10-29 21:36:22 | [diff] [blame] | 101 | void Clear() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 102 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | fea0b961 | 2012-10-29 21:36:22 | [diff] [blame] | 103 | if (iteration_depth_ == 0) { |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 104 | data_.clear(); |
[email protected] | fea0b961 | 2012-10-29 21:36:22 | [diff] [blame] | 105 | } else { |
| 106 | for (typename HashTable::iterator i = data_.begin(); |
| 107 | i != data_.end(); ++i) |
| 108 | removed_ids_.insert(i->first); |
| 109 | } |
| 110 | } |
| 111 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 112 | bool IsEmpty() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 113 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 2de069e | 2010-02-16 09:15:38 | [diff] [blame] | 114 | return size() == 0u; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 117 | T* Lookup(KeyType id) const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 118 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 119 | typename HashTable::const_iterator i = data_.find(id); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 120 | if (i == data_.end()) |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 121 | return nullptr; |
| 122 | return &*i->second; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | size_t size() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 126 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 2de069e | 2010-02-16 09:15:38 | [diff] [blame] | 127 | return data_.size() - removed_ids_.size(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 130 | #if defined(UNIT_TEST) |
| 131 | int iteration_depth() const { |
| 132 | return iteration_depth_; |
| 133 | } |
| 134 | #endif // defined(UNIT_TEST) |
| 135 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 136 | // 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: |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 141 | Iterator(IDMap<V, K>* map) : map_(map), iter_(map_->data_.begin()) { |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 142 | 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] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | ~Iterator() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 159 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 160 | |
| 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] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 165 | if (--map_->iteration_depth_ == 0) |
| 166 | map_->Compact(); |
| 167 | } |
| 168 | |
| 169 | bool IsAtEnd() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 170 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 171 | return iter_ == map_->data_.end(); |
| 172 | } |
| 173 | |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 174 | KeyType GetCurrentKey() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 175 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 176 | return iter_->first; |
| 177 | } |
| 178 | |
| 179 | ReturnType* GetCurrentValue() const { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 180 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 181 | return &*iter_->second; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void Advance() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 185 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 186 | ++iter_; |
| 187 | SkipRemovedEntries(); |
| 188 | } |
| 189 | |
| 190 | private: |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 191 | void Init() { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 192 | DCHECK_CALLED_ON_VALID_SEQUENCE(map_->sequence_checker_); |
[email protected] | 0c8b894 | 2012-10-27 01:03:01 | [diff] [blame] | 193 | ++map_->iteration_depth_; |
| 194 | SkipRemovedEntries(); |
| 195 | } |
| 196 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 197 | 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 | |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 205 | IDMap<V, K>* map_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 206 | typename HashTable::const_iterator iter_; |
| 207 | }; |
| 208 | |
| 209 | typedef Iterator<T> iterator; |
| 210 | typedef Iterator<const T> const_iterator; |
| 211 | |
| 212 | private: |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 213 | KeyType AddInternal(V data) { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 214 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 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] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 222 | |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 223 | void AddWithIDInternal(V data, KeyType id) { |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 224 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
aelias | aed214d0 | 2016-09-24 01:26:42 | [diff] [blame] | 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] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 229 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 230 | void Compact() { |
| 231 | DCHECK_EQ(0, iteration_depth_); |
jkarlin | 7bc034c | 2015-09-25 20:45:47 | [diff] [blame] | 232 | for (const auto& i : removed_ids_) |
| 233 | Remove(i); |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 234 | 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. |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 244 | base::flat_set<KeyType> removed_ids_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 245 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 246 | // The next ID that we will return from Add() |
[email protected] | 9e7e0e0 | 2010-01-25 23:25:16 | [diff] [blame] | 247 | KeyType next_id_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | |
| 249 | HashTable data_; |
[email protected] | a6ed943 | 2009-07-01 22:35:26 | [diff] [blame] | 250 | |
[email protected] | a6ed943 | 2009-07-01 22:35:26 | [diff] [blame] | 251 | // See description above setter. |
| 252 | bool check_on_null_data_; |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 253 | |
tzik | fd754ee | 2018-03-28 15:49:28 | [diff] [blame^] | 254 | SEQUENCE_CHECKER(sequence_checker_); |
jsbell | 41359ee | 2015-11-16 19:43:34 | [diff] [blame] | 255 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 256 | DISALLOW_COPY_AND_ASSIGN(IDMap); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 257 | }; |
| 258 | |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 259 | } // namespace base |
| 260 | |
| 261 | #endif // BASE_CONTAINERS_ID_MAP_H_ |