Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/hashmap.h

Issue 2010243003: Move hashmap into base/. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_HASHMAP_H_ 5 #ifndef V8_HASHMAP_H_
6 #define V8_HASHMAP_H_ 6 #define V8_HASHMAP_H_
7 7
8 #include "src/allocation.h" 8 #include "src/allocation.h"
9 #include "src/base/bits.h" 9 #include "src/base/hashmap.h"
10 #include "src/base/logging.h"
11 #include "src/utils.h" 10 #include "src/utils.h"
12 11
13 namespace v8 { 12 namespace v8 {
14 namespace internal { 13 namespace internal {
15 14
16 template<class AllocationPolicy> 15 template <class AllocationPolicy>
17 class TemplateHashMapImpl { 16 class TemplateHashMapImpl : public base::HashMapImpl {
18 public: 17 public:
19 typedef bool (*MatchFun) (void* key1, void* key2);
20
21 // 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
26 // initial_capacity is the size of the initial hash map; 18 // initial_capacity is the size of the initial hash map;
27 // it must be a power of 2 (and thus must not be 0). 19 // it must be a power of 2 (and thus must not be 0).
28 TemplateHashMapImpl(MatchFun match, 20 TemplateHashMapImpl(MatchFun match,
29 uint32_t capacity = kDefaultHashMapCapacity, 21 uint32_t capacity = kDefaultHashMapCapacity,
30 AllocationPolicy allocator = AllocationPolicy()); 22 AllocationPolicy allocator = AllocationPolicy());
31 23
32 ~TemplateHashMapImpl(); 24 ~TemplateHashMapImpl() override;
33
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;
40 uint32_t hash; // The full hash value for key
41 int order; // If you never remove entries this is the insertion order.
42 };
43
44 // If an entry with matching key is found, returns that entry.
45 // Otherwise, NULL is returned.
46 Entry* Lookup(void* key, uint32_t hash) const;
47 25
48 // If an entry with matching key is found, returns that entry. 26 // If an entry with matching key is found, returns that entry.
49 // If no matching entry is found, a new entry is inserted with 27 // If no matching entry is found, a new entry is inserted with
50 // corresponding key, key hash, and NULL value. 28 // corresponding key, key hash, and NULL value.
51 Entry* LookupOrInsert(void* key, uint32_t hash, 29 Entry* LookupOrInsert(void* key, uint32_t hash,
52 AllocationPolicy allocator = AllocationPolicy()); 30 AllocationPolicy allocator = AllocationPolicy());
53 31
54 // Removes the entry with matching key.
55 // 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);
58
59 // Empties the hash map (occupancy() == 0).
60 void Clear();
61
62 // The number of (non-empty) entries in the table.
63 uint32_t occupancy() const { return occupancy_; }
64
65 // The capacity of the table. The implementation
66 // makes sure that occupancy is at most 80% of
67 // the table capacity.
68 uint32_t capacity() const { return capacity_; }
69
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
81 // Some match functions defined for convenience.
82 static bool PointersMatch(void* key1, void* key2) {
83 return key1 == key2;
84 }
85
86 private: 32 private:
87 MatchFun match_;
88 Entry* map_;
89 uint32_t capacity_;
90 uint32_t occupancy_;
91
92 Entry* map_end() const { return map_ + capacity_; }
93 Entry* Probe(void* key, uint32_t hash) const;
94 void Initialize(uint32_t capacity, AllocationPolicy allocator); 33 void Initialize(uint32_t capacity, AllocationPolicy allocator);
34 void Resize() override;
95 void Resize(AllocationPolicy allocator); 35 void Resize(AllocationPolicy allocator);
96 }; 36 };
97 37
98 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; 38 template <class AllocationPolicy>
99
100 template<class AllocationPolicy>
101 TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl( 39 TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
102 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { 40 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator)
41 : base::HashMapImpl() {
103 match_ = match; 42 match_ = match;
104 Initialize(initial_capacity, allocator); 43 Initialize(initial_capacity, allocator);
105 } 44 }
106 45
107 46
108 template<class AllocationPolicy> 47 template<class AllocationPolicy>
109 TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() { 48 TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
110 AllocationPolicy::Delete(map_); 49 AllocationPolicy::Delete(map_);
111 } 50 }
112 51
113 52
114 template <class AllocationPolicy> 53 template <class AllocationPolicy>
115 typename TemplateHashMapImpl<AllocationPolicy>::Entry* 54 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
116 TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const {
117 Entry* p = Probe(key, hash);
118 return p->key != NULL ? p : NULL;
119 }
120
121
122 template <class AllocationPolicy>
123 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
124 TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( 55 TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert(
125 void* key, uint32_t hash, AllocationPolicy allocator) { 56 void* key, uint32_t hash, AllocationPolicy allocator) {
126 // Find a matching entry. 57 // Find a matching entry.
127 Entry* p = Probe(key, hash); 58 Entry* p = Probe(key, hash);
128 if (p->key != NULL) { 59 if (p->key != NULL) {
129 return p; 60 return p;
130 } 61 }
131 62
132 // No entry found; insert one. 63 // No entry found; insert one.
133 p->key = key; 64 p->key = key;
134 p->value = NULL; 65 p->value = NULL;
135 p->hash = hash; 66 p->hash = hash;
136 p->order = occupancy_; 67 p->order = occupancy_;
137 occupancy_++; 68 occupancy_++;
138 69
139 // Grow the map if we reached >= 80% occupancy. 70 // Grow the map if we reached >= 80% occupancy.
140 if (occupancy_ + occupancy_ / 4 >= capacity_) { 71 if (occupancy_ + occupancy_ / 4 >= capacity_) {
141 Resize(allocator); 72 Resize(allocator);
142 p = Probe(key, hash); 73 p = Probe(key, hash);
143 } 74 }
144 75
145 return p; 76 return p;
146 } 77 }
147 78
148 79
149 template<class AllocationPolicy> 80 template<class AllocationPolicy>
150 void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
151 // 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.
155 return NULL;
156 }
157
158 void* value = p->value;
159 // 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.
173 DCHECK(occupancy_ < capacity_);
174
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_--;
207 return value;
208 }
209
210
211 template<class AllocationPolicy>
212 void TemplateHashMapImpl<AllocationPolicy>::Clear() {
213 // 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
222 template<class AllocationPolicy>
223 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
224 TemplateHashMapImpl<AllocationPolicy>::Start() const {
225 return Next(map_ - 1);
226 }
227
228
229 template<class AllocationPolicy>
230 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
231 TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
232 const Entry* end = map_end();
233 DCHECK(map_ - 1 <= p && p < end);
234 for (p++; p < end; p++) {
235 if (p->key != NULL) {
236 return p;
237 }
238 }
239 return NULL;
240 }
241
242
243 template <class AllocationPolicy>
244 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
245 TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const {
246 DCHECK(key != NULL);
247
248 DCHECK(base::bits::IsPowerOfTwo32(capacity_));
249 Entry* p = map_ + (hash & (capacity_ - 1));
250 const Entry* end = map_end();
251 DCHECK(map_ <= p && p < end);
252
253 DCHECK(occupancy_ < capacity_); // Guarantees loop termination.
254 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
265 template<class AllocationPolicy>
266 void TemplateHashMapImpl<AllocationPolicy>::Initialize( 81 void TemplateHashMapImpl<AllocationPolicy>::Initialize(
267 uint32_t capacity, AllocationPolicy allocator) { 82 uint32_t capacity, AllocationPolicy allocator) {
268 DCHECK(base::bits::IsPowerOfTwo32(capacity)); 83 DCHECK(base::bits::IsPowerOfTwo32(capacity));
269 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); 84 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
270 if (map_ == NULL) { 85 if (map_ == NULL) {
271 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize"); 86 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize");
272 return; 87 return;
273 } 88 }
274 capacity_ = capacity; 89 capacity_ = capacity;
275 Clear(); 90 Clear();
276 } 91 }
277 92
93 template <class AllocationPolicy>
94 void TemplateHashMapImpl<AllocationPolicy>::Resize() {
95 UNREACHABLE();
96 }
278 97
279 template<class AllocationPolicy> 98 template <class AllocationPolicy>
280 void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) { 99 void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) {
281 Entry* map = map_; 100 Entry* map = map_;
282 uint32_t n = occupancy_; 101 uint32_t n = occupancy_;
283 102
284 // Allocate larger map. 103 // Allocate larger map.
285 Initialize(capacity_ * 2, allocator); 104 Initialize(capacity_ * 2, allocator);
286 105
287 // Rehash all current entries. 106 // Rehash all current entries.
288 for (Entry* p = map; n > 0; p++) { 107 for (Entry* p = map; n > 0; p++) {
289 if (p->key != NULL) { 108 if (p->key != NULL) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); 166 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator));
348 } 167 }
349 return Iterator(this, this->Lookup(key, key->Hash())); 168 return Iterator(this, this->Lookup(key, key->Hash()));
350 } 169 }
351 }; 170 };
352 171
353 } // namespace internal 172 } // namespace internal
354 } // namespace v8 173 } // namespace v8
355 174
356 #endif // V8_HASHMAP_H_ 175 #endif // V8_HASHMAP_H_
OLDNEW
« src/base/hashmap.cc ('K') | « src/gdb-jit.cc ('k') | src/heap/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698