OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RUNTIME_VM_HASH_MAP_H_ | 5 #ifndef RUNTIME_VM_HASH_MAP_H_ |
6 #define RUNTIME_VM_HASH_MAP_H_ | 6 #define RUNTIME_VM_HASH_MAP_H_ |
7 | 7 |
8 #include "vm/growable_array.h" // For Malloc, EmptyBase | 8 #include "vm/growable_array.h" // For Malloc, EmptyBase |
9 #include "vm/zone.h" | 9 #include "vm/zone.h" |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 } | 26 } |
27 | 27 |
28 BaseDirectChainedHashMap(const BaseDirectChainedHashMap& other); | 28 BaseDirectChainedHashMap(const BaseDirectChainedHashMap& other); |
29 | 29 |
30 ~BaseDirectChainedHashMap() { | 30 ~BaseDirectChainedHashMap() { |
31 allocator_->template Free<HashMapListElement>(array_, array_size_); | 31 allocator_->template Free<HashMapListElement>(array_, array_size_); |
32 allocator_->template Free<HashMapListElement>(lists_, lists_size_); | 32 allocator_->template Free<HashMapListElement>(lists_, lists_size_); |
33 } | 33 } |
34 | 34 |
35 void Insert(typename KeyValueTrait::Pair kv); | 35 void Insert(typename KeyValueTrait::Pair kv); |
| 36 bool Remove(typename KeyValueTrait::Key key); |
36 | 37 |
37 typename KeyValueTrait::Value LookupValue( | 38 typename KeyValueTrait::Value LookupValue( |
38 typename KeyValueTrait::Key key) const; | 39 typename KeyValueTrait::Key key) const; |
39 | 40 |
40 typename KeyValueTrait::Pair* Lookup(typename KeyValueTrait::Key key) const; | 41 typename KeyValueTrait::Pair* Lookup(typename KeyValueTrait::Key key) const; |
41 | 42 |
42 bool IsEmpty() const { return count_ == 0; } | 43 bool IsEmpty() const { return count_ == 0; } |
43 | 44 |
44 void Clear() { | 45 void Clear() { |
45 if (!IsEmpty()) { | 46 if (!IsEmpty()) { |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 free_list_head_ = lists_[free_list_head_].next; | 302 free_list_head_ = lists_[free_list_head_].next; |
302 lists_[new_element_pos].kv = kv; | 303 lists_[new_element_pos].kv = kv; |
303 lists_[new_element_pos].next = array_[pos].next; | 304 lists_[new_element_pos].next = array_[pos].next; |
304 ASSERT(array_[pos].next == kNil || | 305 ASSERT(array_[pos].next == kNil || |
305 KeyValueTrait::ValueOf(lists_[array_[pos].next].kv) != kNoValue); | 306 KeyValueTrait::ValueOf(lists_[array_[pos].next].kv) != kNoValue); |
306 array_[pos].next = new_element_pos; | 307 array_[pos].next = new_element_pos; |
307 } | 308 } |
308 } | 309 } |
309 | 310 |
310 | 311 |
| 312 template <typename KeyValueTrait, typename B, typename Allocator> |
| 313 bool BaseDirectChainedHashMap<KeyValueTrait, B, Allocator>::Remove( |
| 314 typename KeyValueTrait::Key key) { |
| 315 uword pos = Bound(static_cast<uword>(KeyValueTrait::Hashcode(key))); |
| 316 |
| 317 // Check to see if the first element in the bucket is the one we want to |
| 318 // remove. |
| 319 if (KeyValueTrait::KeyOf(array_[pos].kv) == key) { |
| 320 if (array_[pos].next == kNil) { |
| 321 array_[pos] = HashMapListElement(); |
| 322 } else { |
| 323 intptr_t next = array_[pos].next; |
| 324 array_[pos] = lists_[next]; |
| 325 lists_[next] = HashMapListElement(); |
| 326 lists_[next].next = free_list_head_; |
| 327 free_list_head_ = next; |
| 328 } |
| 329 count_--; |
| 330 return true; |
| 331 } |
| 332 |
| 333 intptr_t current = array_[pos].next; |
| 334 |
| 335 // If there's only the single element in the bucket and it does not match the |
| 336 // key to be removed, just return. |
| 337 if (current == kNil) { |
| 338 return false; |
| 339 } |
| 340 |
| 341 // Check the case where the second element in the bucket is the one to be |
| 342 // removed. |
| 343 if (KeyValueTrait::KeyOf(lists_[current].kv) == key) { |
| 344 array_[pos].next = lists_[current].next; |
| 345 lists_[current] = HashMapListElement(); |
| 346 lists_[current].next = free_list_head_; |
| 347 free_list_head_ = current; |
| 348 count_--; |
| 349 return true; |
| 350 } |
| 351 |
| 352 // Finally, iterate through the rest of the bucket to see if we can find the |
| 353 // entry that matches our key. |
| 354 intptr_t previous; |
| 355 while (KeyValueTrait::KeyOf(lists_[current].kv) != key) { |
| 356 previous = current; |
| 357 current = lists_[current].next; |
| 358 |
| 359 if (current == kNil) { |
| 360 // Could not find entry with provided key to remove. |
| 361 return false; |
| 362 } |
| 363 } |
| 364 |
| 365 lists_[previous].next = lists_[current].next; |
| 366 lists_[current] = HashMapListElement(); |
| 367 lists_[current].next = free_list_head_; |
| 368 free_list_head_ = current; |
| 369 count_--; |
| 370 return true; |
| 371 } |
| 372 |
| 373 |
311 template <typename KeyValueTrait> | 374 template <typename KeyValueTrait> |
312 class DirectChainedHashMap | 375 class DirectChainedHashMap |
313 : public BaseDirectChainedHashMap<KeyValueTrait, ValueObject> { | 376 : public BaseDirectChainedHashMap<KeyValueTrait, ValueObject> { |
314 public: | 377 public: |
315 DirectChainedHashMap() | 378 DirectChainedHashMap() |
316 : BaseDirectChainedHashMap<KeyValueTrait, ValueObject>( | 379 : BaseDirectChainedHashMap<KeyValueTrait, ValueObject>( |
317 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 380 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
318 }; | 381 }; |
319 | 382 |
320 | 383 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 | 437 |
375 static Key KeyOf(Pair kv) { return kv.key; } | 438 static Key KeyOf(Pair kv) { return kv.key; } |
376 static Value ValueOf(Pair kv) { return kv.value; } | 439 static Value ValueOf(Pair kv) { return kv.value; } |
377 static intptr_t Hashcode(Key key) { return reinterpret_cast<intptr_t>(key); } | 440 static intptr_t Hashcode(Key key) { return reinterpret_cast<intptr_t>(key); } |
378 static bool IsKeyEqual(Pair kv, Key key) { return kv.key == key; } | 441 static bool IsKeyEqual(Pair kv, Key key) { return kv.key == key; } |
379 }; | 442 }; |
380 | 443 |
381 } // namespace dart | 444 } // namespace dart |
382 | 445 |
383 #endif // RUNTIME_VM_HASH_MAP_H_ | 446 #endif // RUNTIME_VM_HASH_MAP_H_ |
OLD | NEW |