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 // The reason we write our own hash map instead of using unordered_map in STL, | 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 | 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. | 7 // deadlock when we are using async signal handler. |
8 | 8 |
9 #ifndef V8_BASE_HASHMAP_H_ | 9 #ifndef V8_BASE_HASHMAP_H_ |
10 #define V8_BASE_HASHMAP_H_ | 10 #define V8_BASE_HASHMAP_H_ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 // If an entry with matching key is found, returns that entry. | 45 // If an entry with matching key is found, returns that entry. |
46 // Otherwise, nullptr is returned. | 46 // Otherwise, nullptr is returned. |
47 Entry* Lookup(const Key& key, uint32_t hash) const; | 47 Entry* Lookup(const Key& key, uint32_t hash) const; |
48 | 48 |
49 // If an entry with matching key is found, returns that entry. | 49 // If an entry with matching key is found, returns that entry. |
50 // If no matching entry is found, a new entry is inserted with | 50 // If no matching entry is found, a new entry is inserted with |
51 // corresponding key, key hash, and default initialized value. | 51 // corresponding key, key hash, and default initialized value. |
52 Entry* LookupOrInsert(const Key& key, uint32_t hash, | 52 Entry* LookupOrInsert(const Key& key, uint32_t hash, |
53 AllocationPolicy allocator = AllocationPolicy()); | 53 AllocationPolicy allocator = AllocationPolicy()); |
54 | 54 |
| 55 // If an entry with matching key is found, returns that entry. |
| 56 // If no matching entry is found, a new entry is inserted with |
| 57 // corresponding key, key hash, and value created by func. |
| 58 template <typename Func> |
| 59 Entry* LookupOrInsert(const Key& key, uint32_t hash, const Func& value_func, |
| 60 AllocationPolicy allocator = AllocationPolicy()); |
| 61 |
55 Entry* InsertNew(const Key& key, uint32_t hash, | 62 Entry* InsertNew(const Key& key, uint32_t hash, |
56 AllocationPolicy allocator = AllocationPolicy()); | 63 AllocationPolicy allocator = AllocationPolicy()); |
57 | 64 |
58 // Removes the entry with matching key. | 65 // Removes the entry with matching key. |
59 // It returns the value of the deleted entry | 66 // It returns the value of the deleted entry |
60 // or null if there is no value for such key. | 67 // or null if there is no value for such key. |
61 Value Remove(const Key& key, uint32_t hash); | 68 Value Remove(const Key& key, uint32_t hash); |
62 | 69 |
63 // Empties the hash map (occupancy() == 0). | 70 // Empties the hash map (occupancy() == 0). |
64 void Clear(); | 71 void Clear(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 const Key& key, uint32_t hash) const { | 126 const Key& key, uint32_t hash) const { |
120 Entry* entry = Probe(key, hash); | 127 Entry* entry = Probe(key, hash); |
121 return entry->exists() ? entry : nullptr; | 128 return entry->exists() ? entry : nullptr; |
122 } | 129 } |
123 | 130 |
124 template <typename Key, typename Value, typename MatchFun, | 131 template <typename Key, typename Value, typename MatchFun, |
125 class AllocationPolicy> | 132 class AllocationPolicy> |
126 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* | 133 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
127 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert( | 134 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert( |
128 const Key& key, uint32_t hash, AllocationPolicy allocator) { | 135 const Key& key, uint32_t hash, AllocationPolicy allocator) { |
| 136 return LookupOrInsert(key, hash, []() { return Value(); }, allocator); |
| 137 } |
| 138 |
| 139 template <typename Key, typename Value, typename MatchFun, |
| 140 class AllocationPolicy> |
| 141 template <typename Func> |
| 142 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
| 143 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert( |
| 144 const Key& key, uint32_t hash, const Func& value_func, |
| 145 AllocationPolicy allocator) { |
129 // Find a matching entry. | 146 // Find a matching entry. |
130 Entry* entry = Probe(key, hash); | 147 Entry* entry = Probe(key, hash); |
131 if (entry->exists()) { | 148 if (entry->exists()) { |
132 return entry; | 149 return entry; |
133 } | 150 } |
134 | 151 |
135 return FillEmptyEntry(entry, key, Value(), hash, allocator); | 152 return FillEmptyEntry(entry, key, value_func(), hash, allocator); |
136 } | 153 } |
137 | 154 |
138 template <typename Key, typename Value, typename MatchFun, | 155 template <typename Key, typename Value, typename MatchFun, |
139 class AllocationPolicy> | 156 class AllocationPolicy> |
140 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* | 157 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
141 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::InsertNew( | 158 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::InsertNew( |
142 const Key& key, uint32_t hash, AllocationPolicy allocator) { | 159 const Key& key, uint32_t hash, AllocationPolicy allocator) { |
143 Entry* entry = Probe(key, hash); | 160 Entry* entry = Probe(key, hash); |
144 return FillEmptyEntry(entry, key, Value(), hash, allocator); | 161 return FillEmptyEntry(entry, key, Value(), hash, allocator); |
145 } | 162 } |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); | 403 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
387 } | 404 } |
388 return Iterator(this, this->Lookup(key, key->Hash())); | 405 return Iterator(this, this->Lookup(key, key->Hash())); |
389 } | 406 } |
390 }; | 407 }; |
391 | 408 |
392 } // namespace base | 409 } // namespace base |
393 } // namespace v8 | 410 } // namespace v8 |
394 | 411 |
395 #endif // V8_BASE_HASHMAP_H_ | 412 #endif // V8_BASE_HASHMAP_H_ |
OLD | NEW |