Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 5 | // The reason we write our own hash map instead of using unordered_map in STL, |
| 6 | // is that STL containers use a mutex pool on debug build, which will lead to |
| 7 | // deadlock when we are using async signal handler. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 9 | #ifndef V8_BASE_HASHMAP_H_ |
| 10 | #define V8_BASE_HASHMAP_H_ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | #include "src/base/bits.h" |
| 15 | #include "src/base/logging.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 16 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | namespace v8 { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 18 | namespace base { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 20 | class DefaultAllocationPolicy { |
| 21 | public: |
| 22 | V8_INLINE void* New(size_t size) { return malloc(size); } |
| 23 | V8_INLINE static void Delete(void* p) { free(p); } |
| 24 | }; |
| 25 | |
| 26 | template <class AllocationPolicy> |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 27 | class TemplateHashMapImpl { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 28 | public: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 29 | typedef bool (*MatchFun)(void* key1, void* key2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 30 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | // The default capacity. This is used by the call sites which want |
| 32 | // to pass in a non-default AllocationPolicy but want to use the |
| 33 | // default value of capacity specified by the implementation. |
| 34 | static const uint32_t kDefaultHashMapCapacity = 8; |
| 35 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | // initial_capacity is the size of the initial hash map; |
| 37 | // it must be a power of 2 (and thus must not be 0). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | TemplateHashMapImpl(MatchFun match, |
| 39 | uint32_t capacity = kDefaultHashMapCapacity, |
| 40 | AllocationPolicy allocator = AllocationPolicy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 42 | ~TemplateHashMapImpl(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | |
| 44 | // HashMap entries are (key, value, hash) triplets. |
| 45 | // Some clients may not need to use the value slot |
| 46 | // (e.g. implementers of sets, where the key is the value). |
| 47 | struct Entry { |
| 48 | void* key; |
| 49 | void* value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | uint32_t hash; // The full hash value for key |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 51 | int order; // If you never remove entries this is the insertion order. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | // If an entry with matching key is found, returns that entry. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | // Otherwise, NULL is returned. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | Entry* Lookup(void* key, uint32_t hash) const; |
| 57 | |
| 58 | // If an entry with matching key is found, returns that entry. |
| 59 | // If no matching entry is found, a new entry is inserted with |
| 60 | // corresponding key, key hash, and NULL value. |
| 61 | Entry* LookupOrInsert(void* key, uint32_t hash, |
| 62 | AllocationPolicy allocator = AllocationPolicy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | |
| 64 | // Removes the entry with matching key. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | // It returns the value of the deleted entry |
| 66 | // or null if there is no value for such key. |
| 67 | void* Remove(void* key, uint32_t hash); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | |
| 69 | // Empties the hash map (occupancy() == 0). |
| 70 | void Clear(); |
| 71 | |
| 72 | // The number of (non-empty) entries in the table. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 73 | uint32_t occupancy() const { return occupancy_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | |
| 75 | // The capacity of the table. The implementation |
| 76 | // makes sure that occupancy is at most 80% of |
| 77 | // the table capacity. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 78 | uint32_t capacity() const { return capacity_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | |
| 80 | // Iteration |
| 81 | // |
| 82 | // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { |
| 83 | // ... |
| 84 | // } |
| 85 | // |
| 86 | // If entries are inserted during iteration, the effect of |
| 87 | // calling Next() is undefined. |
| 88 | Entry* Start() const; |
| 89 | Entry* Next(Entry* p) const; |
| 90 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 91 | // Some match functions defined for convenience. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 92 | static bool PointersMatch(void* key1, void* key2) { return key1 == key2; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | MatchFun match_; |
| 96 | Entry* map_; |
| 97 | uint32_t capacity_; |
| 98 | uint32_t occupancy_; |
| 99 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 100 | Entry* map_end() const { return map_ + capacity_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 101 | Entry* Probe(void* key, uint32_t hash) const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | void Initialize(uint32_t capacity, AllocationPolicy allocator); |
| 103 | void Resize(AllocationPolicy allocator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 106 | typedef TemplateHashMapImpl<DefaultAllocationPolicy> HashMap; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 107 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 108 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl( |
| 110 | MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 111 | match_ = match; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | Initialize(initial_capacity, allocator); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 115 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() { |
| 117 | AllocationPolicy::Delete(map_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const { |
| 123 | Entry* p = Probe(key, hash); |
| 124 | return p->key != NULL ? p : NULL; |
| 125 | } |
| 126 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 127 | template <class AllocationPolicy> |
| 128 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
| 129 | TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( |
| 130 | void* key, uint32_t hash, AllocationPolicy allocator) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 131 | // Find a matching entry. |
| 132 | Entry* p = Probe(key, hash); |
| 133 | if (p->key != NULL) { |
| 134 | return p; |
| 135 | } |
| 136 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 137 | // No entry found; insert one. |
| 138 | p->key = key; |
| 139 | p->value = NULL; |
| 140 | p->hash = hash; |
| 141 | p->order = occupancy_; |
| 142 | occupancy_++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 143 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 144 | // Grow the map if we reached >= 80% occupancy. |
| 145 | if (occupancy_ + occupancy_ / 4 >= capacity_) { |
| 146 | Resize(allocator); |
| 147 | p = Probe(key, hash); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 150 | return p; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 153 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 155 | // Lookup the entry for the key to remove. |
| 156 | Entry* p = Probe(key, hash); |
| 157 | if (p->key == NULL) { |
| 158 | // Key not found nothing to remove. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | return NULL; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 162 | void* value = p->value; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 163 | // To remove an entry we need to ensure that it does not create an empty |
| 164 | // entry that will cause the search for another entry to stop too soon. If all |
| 165 | // the entries between the entry to remove and the next empty slot have their |
| 166 | // initial position inside this interval, clearing the entry to remove will |
| 167 | // not break the search. If, while searching for the next empty entry, an |
| 168 | // entry is encountered which does not have its initial position between the |
| 169 | // entry to remove and the position looked at, then this entry can be moved to |
| 170 | // the place of the entry to remove without breaking the search for it. The |
| 171 | // entry made vacant by this move is now the entry to remove and the process |
| 172 | // starts over. |
| 173 | // Algorithm from http://en.wikipedia.org/wiki/Open_addressing. |
| 174 | |
| 175 | // This guarantees loop termination as there is at least one empty entry so |
| 176 | // eventually the removed entry will have an empty entry after it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | DCHECK(occupancy_ < capacity_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 178 | |
| 179 | // p is the candidate entry to clear. q is used to scan forwards. |
| 180 | Entry* q = p; // Start at the entry to remove. |
| 181 | while (true) { |
| 182 | // Move q to the next entry. |
| 183 | q = q + 1; |
| 184 | if (q == map_end()) { |
| 185 | q = map_; |
| 186 | } |
| 187 | |
| 188 | // All entries between p and q have their initial position between p and q |
| 189 | // and the entry p can be cleared without breaking the search for these |
| 190 | // entries. |
| 191 | if (q->key == NULL) { |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | // Find the initial position for the entry at position q. |
| 196 | Entry* r = map_ + (q->hash & (capacity_ - 1)); |
| 197 | |
| 198 | // If the entry at position q has its initial position outside the range |
| 199 | // between p and q it can be moved forward to position p and will still be |
| 200 | // found. There is now a new candidate entry for clearing. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 201 | if ((q > p && (r <= p || r > q)) || (q < p && (r <= p && r > q))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 202 | *p = *q; |
| 203 | p = q; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Clear the entry which is allowed to en emptied. |
| 208 | p->key = NULL; |
| 209 | occupancy_--; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | return value; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 213 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | void TemplateHashMapImpl<AllocationPolicy>::Clear() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 215 | // Mark all entries as empty. |
| 216 | const Entry* end = map_end(); |
| 217 | for (Entry* p = map_; p < end; p++) { |
| 218 | p->key = NULL; |
| 219 | } |
| 220 | occupancy_ = 0; |
| 221 | } |
| 222 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 223 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 224 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 225 | TemplateHashMapImpl<AllocationPolicy>::Start() const { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 226 | return Next(map_ - 1); |
| 227 | } |
| 228 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 229 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 231 | TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 232 | const Entry* end = map_end(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | DCHECK(map_ - 1 <= p && p < end); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 234 | for (p++; p < end; p++) { |
| 235 | if (p->key != NULL) { |
| 236 | return p; |
| 237 | } |
| 238 | } |
| 239 | return NULL; |
| 240 | } |
| 241 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 244 | TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 245 | DCHECK(key != NULL); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 246 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 247 | DCHECK(base::bits::IsPowerOfTwo32(capacity_)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 248 | Entry* p = map_ + (hash & (capacity_ - 1)); |
| 249 | const Entry* end = map_end(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 250 | DCHECK(map_ <= p && p < end); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 251 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 252 | DCHECK(occupancy_ < capacity_); // Guarantees loop termination. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 253 | while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { |
| 254 | p++; |
| 255 | if (p >= end) { |
| 256 | p = map_; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return p; |
| 261 | } |
| 262 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 263 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | void TemplateHashMapImpl<AllocationPolicy>::Initialize( |
| 265 | uint32_t capacity, AllocationPolicy allocator) { |
| 266 | DCHECK(base::bits::IsPowerOfTwo32(capacity)); |
| 267 | map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 268 | if (map_ == NULL) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 269 | FATAL("Out of memory: HashMap::Initialize"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | capacity_ = capacity; |
| 273 | Clear(); |
| 274 | } |
| 275 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 276 | template <class AllocationPolicy> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 277 | void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 278 | Entry* map = map_; |
| 279 | uint32_t n = occupancy_; |
| 280 | |
| 281 | // Allocate larger map. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 282 | Initialize(capacity_ * 2, allocator); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 283 | |
| 284 | // Rehash all current entries. |
| 285 | for (Entry* p = map; n > 0; p++) { |
| 286 | if (p->key != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 287 | Entry* entry = LookupOrInsert(p->key, p->hash, allocator); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 288 | entry->value = p->value; |
| 289 | entry->order = p->order; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 290 | n--; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Delete old map. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 295 | AllocationPolicy::Delete(map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 298 | // A hash map for pointer keys and values with an STL-like interface. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 299 | template <class Key, class Value, class AllocationPolicy> |
| 300 | class TemplateHashMap : private TemplateHashMapImpl<AllocationPolicy> { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 301 | public: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 302 | STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 303 | STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT |
| 304 | struct value_type { |
| 305 | Key* first; |
| 306 | Value* second; |
| 307 | }; |
| 308 | |
| 309 | class Iterator { |
| 310 | public: |
| 311 | Iterator& operator++() { |
| 312 | entry_ = map_->Next(entry_); |
| 313 | return *this; |
| 314 | } |
| 315 | |
| 316 | value_type* operator->() { return reinterpret_cast<value_type*>(entry_); } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 317 | bool operator!=(const Iterator& other) { return entry_ != other.entry_; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 318 | |
| 319 | private: |
| 320 | Iterator(const TemplateHashMapImpl<AllocationPolicy>* map, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 321 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) |
| 322 | : map_(map), entry_(entry) {} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 323 | |
| 324 | const TemplateHashMapImpl<AllocationPolicy>* map_; |
| 325 | typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_; |
| 326 | |
| 327 | friend class TemplateHashMap; |
| 328 | }; |
| 329 | |
| 330 | TemplateHashMap( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match, |
| 332 | AllocationPolicy allocator = AllocationPolicy()) |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 333 | : TemplateHashMapImpl<AllocationPolicy>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 334 | match, |
| 335 | TemplateHashMapImpl<AllocationPolicy>::kDefaultHashMapCapacity, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 336 | allocator) {} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 337 | |
| 338 | Iterator begin() const { return Iterator(this, this->Start()); } |
| 339 | Iterator end() const { return Iterator(this, NULL); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 340 | Iterator find(Key* key, bool insert = false, |
| 341 | AllocationPolicy allocator = AllocationPolicy()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 342 | if (insert) { |
| 343 | return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
| 344 | } |
| 345 | return Iterator(this, this->Lookup(key, key->Hash())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 346 | } |
| 347 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 349 | } // namespace base |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 350 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 351 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 352 | #endif // V8_BASE_HASHMAP_H_ |