| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 #include "vm/weak_table.h" | |
| 6 | |
| 7 #include "platform/assert.h" | |
| 8 #include "vm/raw_object.h" | |
| 9 | |
| 10 namespace dart { | |
| 11 | |
| 12 WeakTable* WeakTable::SetValue(RawObject* key, intptr_t val) { | |
| 13 intptr_t sz = size(); | |
| 14 intptr_t idx = Hash(key) % sz; | |
| 15 intptr_t empty_idx = -1; | |
| 16 RawObject* obj = ObjectAt(idx); | |
| 17 | |
| 18 while (obj != NULL) { | |
| 19 if (obj == key) { | |
| 20 SetValueAt(idx, val); | |
| 21 return this; | |
| 22 } else if ((empty_idx < 0) && | |
| 23 (reinterpret_cast<intptr_t>(obj) == kDeletedEntry)) { | |
| 24 empty_idx = idx; // Insert at this location if not found. | |
| 25 } | |
| 26 idx = (idx + 1) % sz; | |
| 27 obj = ObjectAt(idx); | |
| 28 } | |
| 29 | |
| 30 if (val == 0) { | |
| 31 // Do not enter an invalid value. Associating 0 with a key deletes it from | |
| 32 // this weak table above in SetValueAt. If the key was not present in the | |
| 33 // weak table we are done. | |
| 34 return this; | |
| 35 } | |
| 36 | |
| 37 if (empty_idx >= 0) { | |
| 38 // We will be reusing a slot below. | |
| 39 set_used(used() - 1); | |
| 40 idx = empty_idx; | |
| 41 } | |
| 42 | |
| 43 ASSERT(!IsValidEntryAt(idx)); | |
| 44 // Set the key and value. | |
| 45 SetObjectAt(idx, key); | |
| 46 SetValueAt(idx, val); | |
| 47 // Update the counts. | |
| 48 set_used(used() + 1); | |
| 49 set_count(count() + 1); | |
| 50 | |
| 51 // Rehash if needed to ensure that there are empty slots available. | |
| 52 if (used_ >= limit()) { | |
| 53 return Rehash(); | |
| 54 } | |
| 55 return this; | |
| 56 } | |
| 57 | |
| 58 | |
| 59 WeakTable* WeakTable::Rehash() { | |
| 60 intptr_t sz = size(); | |
| 61 WeakTable* result = NewFrom(this); | |
| 62 | |
| 63 for (intptr_t i = 0; i < sz; i++) { | |
| 64 if (IsValidEntryAt(i)) { | |
| 65 WeakTable* temp = result->SetValue(ObjectAt(i), ValueAt(i)); | |
| 66 ASSERT(temp == result); | |
| 67 } | |
| 68 } | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 } // namespace dart | |
| OLD | NEW |