OLD | NEW |
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/bits.h" |
10 #include "src/base/logging.h" | 10 #include "src/base/logging.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 // (e.g. implementers of sets, where the key is the value). | 36 // (e.g. implementers of sets, where the key is the value). |
37 struct Entry { | 37 struct Entry { |
38 void* key; | 38 void* key; |
39 void* value; | 39 void* value; |
40 uint32_t hash; // The full hash value for key | 40 uint32_t hash; // The full hash value for key |
41 int order; // If you never remove entries this is the insertion order. | 41 int order; // If you never remove entries this is the insertion order. |
42 }; | 42 }; |
43 | 43 |
44 // If an entry with matching key is found, returns that entry. | 44 // If an entry with matching key is found, returns that entry. |
45 // Otherwise, NULL is returned. | 45 // Otherwise, NULL is returned. |
46 Entry* Lookup(void* key, uint32_t hash); | 46 Entry* Lookup(void* key, uint32_t hash) const; |
47 | 47 |
48 // If an entry with matching key is found, returns that entry. | 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 | 49 // If no matching entry is found, a new entry is inserted with |
50 // corresponding key, key hash, and NULL value. | 50 // corresponding key, key hash, and NULL value. |
51 Entry* LookupOrInsert(void* key, uint32_t hash, | 51 Entry* LookupOrInsert(void* key, uint32_t hash, |
52 AllocationPolicy allocator = AllocationPolicy()); | 52 AllocationPolicy allocator = AllocationPolicy()); |
53 | 53 |
54 // Removes the entry with matching key. | 54 // Removes the entry with matching key. |
55 // It returns the value of the deleted entry | 55 // It returns the value of the deleted entry |
56 // or null if there is no value for such key. | 56 // or null if there is no value for such key. |
(...skipping 26 matching lines...) Expand all Loading... |
83 return key1 == key2; | 83 return key1 == key2; |
84 } | 84 } |
85 | 85 |
86 private: | 86 private: |
87 MatchFun match_; | 87 MatchFun match_; |
88 Entry* map_; | 88 Entry* map_; |
89 uint32_t capacity_; | 89 uint32_t capacity_; |
90 uint32_t occupancy_; | 90 uint32_t occupancy_; |
91 | 91 |
92 Entry* map_end() const { return map_ + capacity_; } | 92 Entry* map_end() const { return map_ + capacity_; } |
93 Entry* Probe(void* key, uint32_t hash); | 93 Entry* Probe(void* key, uint32_t hash) const; |
94 void Initialize(uint32_t capacity, AllocationPolicy allocator); | 94 void Initialize(uint32_t capacity, AllocationPolicy allocator); |
95 void Resize(AllocationPolicy allocator); | 95 void Resize(AllocationPolicy allocator); |
96 }; | 96 }; |
97 | 97 |
98 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; | 98 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; |
99 | 99 |
100 template<class AllocationPolicy> | 100 template<class AllocationPolicy> |
101 TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl( | 101 TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl( |
102 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { | 102 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { |
103 match_ = match; | 103 match_ = match; |
104 Initialize(initial_capacity, allocator); | 104 Initialize(initial_capacity, allocator); |
105 } | 105 } |
106 | 106 |
107 | 107 |
108 template<class AllocationPolicy> | 108 template<class AllocationPolicy> |
109 TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() { | 109 TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() { |
110 AllocationPolicy::Delete(map_); | 110 AllocationPolicy::Delete(map_); |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 template <class AllocationPolicy> | 114 template <class AllocationPolicy> |
115 typename TemplateHashMapImpl<AllocationPolicy>::Entry* | 115 typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
116 TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) { | 116 TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const { |
117 Entry* p = Probe(key, hash); | 117 Entry* p = Probe(key, hash); |
118 return p->key != NULL ? p : NULL; | 118 return p->key != NULL ? p : NULL; |
119 } | 119 } |
120 | 120 |
121 | 121 |
122 template <class AllocationPolicy> | 122 template <class AllocationPolicy> |
123 typename TemplateHashMapImpl<AllocationPolicy>::Entry* | 123 typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
124 TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( | 124 TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( |
125 void* key, uint32_t hash, AllocationPolicy allocator) { | 125 void* key, uint32_t hash, AllocationPolicy allocator) { |
126 // Find a matching entry. | 126 // Find a matching entry. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 if (p->key != NULL) { | 235 if (p->key != NULL) { |
236 return p; | 236 return p; |
237 } | 237 } |
238 } | 238 } |
239 return NULL; | 239 return NULL; |
240 } | 240 } |
241 | 241 |
242 | 242 |
243 template <class AllocationPolicy> | 243 template <class AllocationPolicy> |
244 typename TemplateHashMapImpl<AllocationPolicy>::Entry* | 244 typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
245 TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) { | 245 TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const { |
246 DCHECK(key != NULL); | 246 DCHECK(key != NULL); |
247 | 247 |
248 DCHECK(base::bits::IsPowerOfTwo32(capacity_)); | 248 DCHECK(base::bits::IsPowerOfTwo32(capacity_)); |
249 Entry* p = map_ + (hash & (capacity_ - 1)); | 249 Entry* p = map_ + (hash & (capacity_ - 1)); |
250 const Entry* end = map_end(); | 250 const Entry* end = map_end(); |
251 DCHECK(map_ <= p && p < end); | 251 DCHECK(map_ <= p && p < end); |
252 | 252 |
253 DCHECK(occupancy_ < capacity_); // Guarantees loop termination. | 253 DCHECK(occupancy_ < capacity_); // Guarantees loop termination. |
254 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { | 254 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { |
255 p++; | 255 p++; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 if (insert) { | 346 if (insert) { |
347 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); | 347 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
348 } | 348 } |
349 return Iterator(this, this->Lookup(key, key->Hash())); | 349 return Iterator(this, this->Lookup(key, key->Hash())); |
350 } | 350 } |
351 }; | 351 }; |
352 | 352 |
353 } } // namespace v8::internal | 353 } } // namespace v8::internal |
354 | 354 |
355 #endif // V8_HASHMAP_H_ | 355 #endif // V8_HASHMAP_H_ |
OLD | NEW |