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> |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 10 | #include <set> |
| 11 | |
brettw@chromium.org | 30230a8 | 2013-06-12 02:52:44 +0900 | [diff] [blame] | 12 | #include "base/containers/hash_tables.h" |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 13 | #include "base/logging.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 14 | #include "base/macros.h" |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 15 | #include "base/sequence_checker.h" |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 16 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 17 | // Ownership semantics - own pointer means the pointer is deleted in Remove() |
| 18 | // & during destruction |
| 19 | enum IDMapOwnershipSemantics { |
| 20 | IDMapExternalPointer, |
| 21 | IDMapOwnPointer |
| 22 | }; |
| 23 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [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. |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 32 | // |
| 33 | // This class does not have a virtual destructor, do not inherit from it when |
| 34 | // ownership semantics are set to own because pointers will leak. |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 35 | template <typename T, |
| 36 | IDMapOwnershipSemantics OS = IDMapExternalPointer, |
| 37 | typename K = int32_t> |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 38 | class IDMap { |
mnaganov@chromium.org | 0f3c4e6 | 2014-05-28 03:10:06 +0900 | [diff] [blame] | 39 | public: |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 40 | using KeyType = K; |
mnaganov@chromium.org | 0f3c4e6 | 2014-05-28 03:10:06 +0900 | [diff] [blame] | 41 | |
| 42 | private: |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 43 | typedef base::hash_map<KeyType, T*> HashTable; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 44 | |
| 45 | public: |
| 46 | IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [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 | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 49 | // post-construction. The first call to CalledOnValidSequence() will re-bind |
| 50 | // it. |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 51 | sequence_checker_.DetachFromSequence(); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 52 | } |
| 53 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 54 | ~IDMap() { |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [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. |
| 58 | sequence_checker_.DetachFromSequence(); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 59 | Releaser<OS, 0>::release_all(&data_); |
| 60 | } |
| 61 | |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 62 | // Sets whether Add and Replace should DCHECK if passed in NULL data. |
| 63 | // Default is false. |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 64 | void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
| 65 | |
| 66 | // Adds a view with an automatically generated unique ID. See AddWithID. |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 67 | KeyType Add(T* data) { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 68 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 69 | DCHECK(!check_on_null_data_ || data); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 70 | KeyType this_id = next_id_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 71 | DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
| 72 | data_[this_id] = data; |
| 73 | next_id_++; |
| 74 | return this_id; |
| 75 | } |
| 76 | |
| 77 | // Adds a new data member with the specified ID. The ID must not be in |
| 78 | // the list. The caller either must generate all unique IDs itself and use |
| 79 | // this function, or allow this object to generate IDs and call Add. These |
| 80 | // two methods may not be mixed, or duplicate IDs may be generated |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 81 | void AddWithID(T* data, KeyType id) { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 82 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 83 | DCHECK(!check_on_null_data_ || data); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 84 | DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| 85 | data_[id] = data; |
| 86 | } |
| 87 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 88 | void Remove(KeyType id) { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 89 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 90 | typename HashTable::iterator i = data_.find(id); |
| 91 | if (i == data_.end()) { |
| 92 | NOTREACHED() << "Attempting to remove an item not in the list"; |
| 93 | return; |
| 94 | } |
| 95 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 96 | if (iteration_depth_ == 0) { |
| 97 | Releaser<OS, 0>::release(i->second); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 98 | data_.erase(i); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 99 | } else { |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 100 | removed_ids_.insert(id); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 101 | } |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 102 | } |
| 103 | |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 104 | // Replaces the value for |id| with |new_data| and returns a pointer to the |
| 105 | // existing value. If there is no entry for |id|, the map is not altered and |
| 106 | // nullptr is returned. The OwnershipSemantics of the map have no effect on |
| 107 | // how the existing value is treated, the IDMap does not delete the existing |
| 108 | // value being replaced. |
| 109 | T* Replace(KeyType id, T* new_data) { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 110 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 111 | DCHECK(!check_on_null_data_ || new_data); |
| 112 | typename HashTable::iterator i = data_.find(id); |
| 113 | if (i == data_.end()) { |
| 114 | NOTREACHED() << "Attempting to replace an item not in the list"; |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | T* temp = i->second; |
| 119 | i->second = new_data; |
| 120 | return temp; |
| 121 | } |
| 122 | |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 123 | void Clear() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 124 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 125 | if (iteration_depth_ == 0) { |
| 126 | Releaser<OS, 0>::release_all(&data_); |
| 127 | } else { |
| 128 | for (typename HashTable::iterator i = data_.begin(); |
| 129 | i != data_.end(); ++i) |
| 130 | removed_ids_.insert(i->first); |
| 131 | } |
| 132 | } |
| 133 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 134 | bool IsEmpty() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 135 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 136 | return size() == 0u; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 137 | } |
| 138 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 139 | T* Lookup(KeyType id) const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 140 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 141 | typename HashTable::const_iterator i = data_.find(id); |
| 142 | if (i == data_.end()) |
| 143 | return NULL; |
| 144 | return i->second; |
| 145 | } |
| 146 | |
| 147 | size_t size() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 148 | DCHECK(sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 149 | return data_.size() - removed_ids_.size(); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 150 | } |
| 151 | |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 152 | #if defined(UNIT_TEST) |
| 153 | int iteration_depth() const { |
| 154 | return iteration_depth_; |
| 155 | } |
| 156 | #endif // defined(UNIT_TEST) |
| 157 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 158 | // It is safe to remove elements from the map during iteration. All iterators |
| 159 | // will remain valid. |
| 160 | template<class ReturnType> |
| 161 | class Iterator { |
| 162 | public: |
bcwhite | 0ac12b0 | 2016-02-23 07:16:10 +0900 | [diff] [blame] | 163 | Iterator(IDMap<T, OS, K>* map) |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 164 | : map_(map), |
| 165 | iter_(map_->data_.begin()) { |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 166 | Init(); |
| 167 | } |
| 168 | |
| 169 | Iterator(const Iterator& iter) |
| 170 | : map_(iter.map_), |
| 171 | iter_(iter.iter_) { |
| 172 | Init(); |
| 173 | } |
| 174 | |
| 175 | const Iterator& operator=(const Iterator& iter) { |
| 176 | map_ = iter.map; |
| 177 | iter_ = iter.iter; |
| 178 | Init(); |
| 179 | return *this; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | ~Iterator() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 183 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 184 | |
| 185 | // We're going to decrement iteration depth. Make sure it's greater than |
| 186 | // zero so that it doesn't become negative. |
| 187 | DCHECK_LT(0, map_->iteration_depth_); |
| 188 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 189 | if (--map_->iteration_depth_ == 0) |
| 190 | map_->Compact(); |
| 191 | } |
| 192 | |
| 193 | bool IsAtEnd() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 194 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 195 | return iter_ == map_->data_.end(); |
| 196 | } |
| 197 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 198 | KeyType GetCurrentKey() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 199 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 200 | return iter_->first; |
| 201 | } |
| 202 | |
| 203 | ReturnType* GetCurrentValue() const { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 204 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 205 | return iter_->second; |
| 206 | } |
| 207 | |
| 208 | void Advance() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 209 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 210 | ++iter_; |
| 211 | SkipRemovedEntries(); |
| 212 | } |
| 213 | |
| 214 | private: |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 215 | void Init() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame^] | 216 | DCHECK(map_->sequence_checker_.CalledOnValidSequence()); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 217 | ++map_->iteration_depth_; |
| 218 | SkipRemovedEntries(); |
| 219 | } |
| 220 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 221 | void SkipRemovedEntries() { |
| 222 | while (iter_ != map_->data_.end() && |
| 223 | map_->removed_ids_.find(iter_->first) != |
| 224 | map_->removed_ids_.end()) { |
| 225 | ++iter_; |
| 226 | } |
| 227 | } |
| 228 | |
bcwhite | 0ac12b0 | 2016-02-23 07:16:10 +0900 | [diff] [blame] | 229 | IDMap<T, OS, K>* map_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 230 | typename HashTable::const_iterator iter_; |
| 231 | }; |
| 232 | |
| 233 | typedef Iterator<T> iterator; |
| 234 | typedef Iterator<const T> const_iterator; |
| 235 | |
| 236 | private: |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 237 | |
| 238 | // The dummy parameter is there because C++ standard does not allow |
| 239 | // explicitly specialized templates inside classes |
| 240 | template<IDMapOwnershipSemantics OI, int dummy> struct Releaser { |
| 241 | static inline void release(T* ptr) {} |
| 242 | static inline void release_all(HashTable* table) {} |
| 243 | }; |
| 244 | |
| 245 | template<int dummy> struct Releaser<IDMapOwnPointer, dummy> { |
| 246 | static inline void release(T* ptr) { delete ptr;} |
| 247 | static inline void release_all(HashTable* table) { |
| 248 | for (typename HashTable::iterator i = table->begin(); |
| 249 | i != table->end(); ++i) { |
| 250 | delete i->second; |
| 251 | } |
| 252 | table->clear(); |
| 253 | } |
| 254 | }; |
| 255 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 256 | void Compact() { |
| 257 | DCHECK_EQ(0, iteration_depth_); |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 258 | for (const auto& i : removed_ids_) |
| 259 | Remove(i); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 260 | removed_ids_.clear(); |
| 261 | } |
| 262 | |
| 263 | // Keep track of how many iterators are currently iterating on us to safely |
| 264 | // handle removing items during iteration. |
| 265 | int iteration_depth_; |
| 266 | |
| 267 | // Keep set of IDs that should be removed after the outermost iteration has |
| 268 | // finished. This way we manage to not invalidate the iterator when an element |
| 269 | // is removed. |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 270 | std::set<KeyType> removed_ids_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 271 | |
| 272 | // The next ID that we will return from Add() |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 273 | KeyType next_id_; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 274 | |
| 275 | HashTable data_; |
| 276 | |
| 277 | // See description above setter. |
| 278 | bool check_on_null_data_; |
| 279 | |
jsbell | 5c49d68 | 2015-11-17 04:43:34 +0900 | [diff] [blame] | 280 | base::SequenceChecker sequence_checker_; |
| 281 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 282 | DISALLOW_COPY_AND_ASSIGN(IDMap); |
| 283 | }; |
| 284 | |
| 285 | #endif // BASE_ID_MAP_H_ |