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