blob: f94def7c3c7a04cb4c0e534074c4f94b092a0f81 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_HASHMAP_H_
6#define V8_HASHMAP_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/allocation.h"
9#include "src/base/bits.h"
10#include "src/base/logging.h"
11#include "src/utils.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000012
Steve Blocka7e24c12009-10-30 11:49:00 +000013namespace v8 {
14namespace internal {
15
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016template<class AllocationPolicy>
17class TemplateHashMapImpl {
Steve Blocka7e24c12009-10-30 11:49:00 +000018 public:
Steve Blocka7e24c12009-10-30 11:49:00 +000019 typedef bool (*MatchFun) (void* key1, void* key2);
20
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021 // The default capacity. This is used by the call sites which want
22 // to pass in a non-default AllocationPolicy but want to use the
23 // default value of capacity specified by the implementation.
24 static const uint32_t kDefaultHashMapCapacity = 8;
25
Steve Blocka7e24c12009-10-30 11:49:00 +000026 // initial_capacity is the size of the initial hash map;
27 // it must be a power of 2 (and thus must not be 0).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 TemplateHashMapImpl(MatchFun match,
29 uint32_t capacity = kDefaultHashMapCapacity,
30 AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +000031
Ben Murdoch3ef787d2012-04-12 10:51:47 +010032 ~TemplateHashMapImpl();
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34 // HashMap entries are (key, value, hash) triplets.
35 // Some clients may not need to use the value slot
36 // (e.g. implementers of sets, where the key is the value).
37 struct Entry {
38 void* key;
39 void* value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040 uint32_t hash; // The full hash value for key
41 int order; // If you never remove entries this is the insertion order.
Steve Blocka7e24c12009-10-30 11:49:00 +000042 };
43
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 // If an entry with matching key is found, returns that entry.
Steve Blocka7e24c12009-10-30 11:49:00 +000045 // Otherwise, NULL is returned.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 Entry* Lookup(void* key, uint32_t hash) const;
47
48 // If an entry with matching key is found, returns that entry.
49 // If no matching entry is found, a new entry is inserted with
50 // corresponding key, key hash, and NULL value.
51 Entry* LookupOrInsert(void* key, uint32_t hash,
52 AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54 // Removes the entry with matching key.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 // It returns the value of the deleted entry
56 // or null if there is no value for such key.
57 void* Remove(void* key, uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +000058
59 // Empties the hash map (occupancy() == 0).
60 void Clear();
61
62 // The number of (non-empty) entries in the table.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010063 uint32_t occupancy() const { return occupancy_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65 // The capacity of the table. The implementation
66 // makes sure that occupancy is at most 80% of
67 // the table capacity.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010068 uint32_t capacity() const { return capacity_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000069
70 // Iteration
71 //
72 // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) {
73 // ...
74 // }
75 //
76 // If entries are inserted during iteration, the effect of
77 // calling Next() is undefined.
78 Entry* Start() const;
79 Entry* Next(Entry* p) const;
80
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 // Some match functions defined for convenience.
82 static bool PointersMatch(void* key1, void* key2) {
83 return key1 == key2;
84 }
85
Steve Blocka7e24c12009-10-30 11:49:00 +000086 private:
Steve Blocka7e24c12009-10-30 11:49:00 +000087 MatchFun match_;
88 Entry* map_;
89 uint32_t capacity_;
90 uint32_t occupancy_;
91
Kristian Monsen0d5e1162010-09-30 15:31:59 +010092 Entry* map_end() const { return map_ + capacity_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 Entry* Probe(void* key, uint32_t hash) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 void Initialize(uint32_t capacity, AllocationPolicy allocator);
95 void Resize(AllocationPolicy allocator);
Steve Blocka7e24c12009-10-30 11:49:00 +000096};
97
Ben Murdoch3ef787d2012-04-12 10:51:47 +010098typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap;
99
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100template<class AllocationPolicy>
101TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
102 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100103 match_ = match;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 Initialize(initial_capacity, allocator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100105}
106
107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108template<class AllocationPolicy>
109TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
110 AllocationPolicy::Delete(map_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100111}
112
113
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115typename TemplateHashMapImpl<AllocationPolicy>::Entry*
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const {
117 Entry* p = Probe(key, hash);
118 return p->key != NULL ? p : NULL;
119}
120
121
122template <class AllocationPolicy>
123typename TemplateHashMapImpl<AllocationPolicy>::Entry*
124TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert(
125 void* key, uint32_t hash, AllocationPolicy allocator) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100126 // Find a matching entry.
127 Entry* p = Probe(key, hash);
128 if (p->key != NULL) {
129 return p;
130 }
131
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 // No entry found; insert one.
133 p->key = key;
134 p->value = NULL;
135 p->hash = hash;
136 p->order = occupancy_;
137 occupancy_++;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 // Grow the map if we reached >= 80% occupancy.
140 if (occupancy_ + occupancy_ / 4 >= capacity_) {
141 Resize(allocator);
142 p = Probe(key, hash);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143 }
144
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 return p;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146}
147
148
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149template<class AllocationPolicy>
150void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100151 // Lookup the entry for the key to remove.
152 Entry* p = Probe(key, hash);
153 if (p->key == NULL) {
154 // Key not found nothing to remove.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 return NULL;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100156 }
157
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 void* value = p->value;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100159 // To remove an entry we need to ensure that it does not create an empty
160 // entry that will cause the search for another entry to stop too soon. If all
161 // the entries between the entry to remove and the next empty slot have their
162 // initial position inside this interval, clearing the entry to remove will
163 // not break the search. If, while searching for the next empty entry, an
164 // entry is encountered which does not have its initial position between the
165 // entry to remove and the position looked at, then this entry can be moved to
166 // the place of the entry to remove without breaking the search for it. The
167 // entry made vacant by this move is now the entry to remove and the process
168 // starts over.
169 // Algorithm from http://en.wikipedia.org/wiki/Open_addressing.
170
171 // This guarantees loop termination as there is at least one empty entry so
172 // eventually the removed entry will have an empty entry after it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 DCHECK(occupancy_ < capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100174
175 // p is the candidate entry to clear. q is used to scan forwards.
176 Entry* q = p; // Start at the entry to remove.
177 while (true) {
178 // Move q to the next entry.
179 q = q + 1;
180 if (q == map_end()) {
181 q = map_;
182 }
183
184 // All entries between p and q have their initial position between p and q
185 // and the entry p can be cleared without breaking the search for these
186 // entries.
187 if (q->key == NULL) {
188 break;
189 }
190
191 // Find the initial position for the entry at position q.
192 Entry* r = map_ + (q->hash & (capacity_ - 1));
193
194 // If the entry at position q has its initial position outside the range
195 // between p and q it can be moved forward to position p and will still be
196 // found. There is now a new candidate entry for clearing.
197 if ((q > p && (r <= p || r > q)) ||
198 (q < p && (r <= p && r > q))) {
199 *p = *q;
200 p = q;
201 }
202 }
203
204 // Clear the entry which is allowed to en emptied.
205 p->key = NULL;
206 occupancy_--;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 return value;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208}
209
210
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211template<class AllocationPolicy>
212void TemplateHashMapImpl<AllocationPolicy>::Clear() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100213 // Mark all entries as empty.
214 const Entry* end = map_end();
215 for (Entry* p = map_; p < end; p++) {
216 p->key = NULL;
217 }
218 occupancy_ = 0;
219}
220
221
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222template<class AllocationPolicy>
223typename TemplateHashMapImpl<AllocationPolicy>::Entry*
224 TemplateHashMapImpl<AllocationPolicy>::Start() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100225 return Next(map_ - 1);
226}
227
228
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229template<class AllocationPolicy>
230typename TemplateHashMapImpl<AllocationPolicy>::Entry*
231 TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100232 const Entry* end = map_end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 DCHECK(map_ - 1 <= p && p < end);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100234 for (p++; p < end; p++) {
235 if (p->key != NULL) {
236 return p;
237 }
238 }
239 return NULL;
240}
241
242
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000243template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244typename TemplateHashMapImpl<AllocationPolicy>::Entry*
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000245TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 DCHECK(key != NULL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100247
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 DCHECK(base::bits::IsPowerOfTwo32(capacity_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100249 Entry* p = map_ + (hash & (capacity_ - 1));
250 const Entry* end = map_end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 DCHECK(map_ <= p && p < end);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 DCHECK(occupancy_ < capacity_); // Guarantees loop termination.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100254 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) {
255 p++;
256 if (p >= end) {
257 p = map_;
258 }
259 }
260
261 return p;
262}
263
264
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265template<class AllocationPolicy>
266void TemplateHashMapImpl<AllocationPolicy>::Initialize(
267 uint32_t capacity, AllocationPolicy allocator) {
268 DCHECK(base::bits::IsPowerOfTwo32(capacity));
269 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100270 if (map_ == NULL) {
271 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize");
272 return;
273 }
274 capacity_ = capacity;
275 Clear();
276}
277
278
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279template<class AllocationPolicy>
280void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100281 Entry* map = map_;
282 uint32_t n = occupancy_;
283
284 // Allocate larger map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 Initialize(capacity_ * 2, allocator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100286
287 // Rehash all current entries.
288 for (Entry* p = map; n > 0; p++) {
289 if (p->key != NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000290 Entry* entry = LookupOrInsert(p->key, p->hash, allocator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 entry->value = p->value;
292 entry->order = p->order;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100293 n--;
294 }
295 }
296
297 // Delete old map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 AllocationPolicy::Delete(map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100299}
300
301
302// A hash map for pointer keys and values with an STL-like interface.
303template<class Key, class Value, class AllocationPolicy>
304class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> {
305 public:
306 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
307 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
308 struct value_type {
309 Key* first;
310 Value* second;
311 };
312
313 class Iterator {
314 public:
315 Iterator& operator++() {
316 entry_ = map_->Next(entry_);
317 return *this;
318 }
319
320 value_type* operator->() { return reinterpret_cast<value_type*>(entry_); }
321 bool operator!=(const Iterator& other) { return entry_ != other.entry_; }
322
323 private:
324 Iterator(const TemplateHashMapImpl<AllocationPolicy>* map,
325 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) :
326 map_(map), entry_(entry) { }
327
328 const TemplateHashMapImpl<AllocationPolicy>* map_;
329 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_;
330
331 friend class TemplateHashMap;
332 };
333
334 TemplateHashMap(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match,
336 AllocationPolicy allocator = AllocationPolicy())
337 : TemplateHashMapImpl<AllocationPolicy>(
338 match,
339 TemplateHashMapImpl<AllocationPolicy>::kDefaultHashMapCapacity,
340 allocator) { }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100341
342 Iterator begin() const { return Iterator(this, this->Start()); }
343 Iterator end() const { return Iterator(this, NULL); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 Iterator find(Key* key, bool insert = false,
345 AllocationPolicy allocator = AllocationPolicy()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000346 if (insert) {
347 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator));
348 }
349 return Iterator(this, this->Lookup(key, key->Hash()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100350 }
351};
Steve Blocka7e24c12009-10-30 11:49:00 +0000352
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000353} // namespace internal
354} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000355
356#endif // V8_HASHMAP_H_