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

Side by Side Diff: runtime/vm/weak_table.cc

Issue 17992002: - Add a WeakTable to the VM. This is used to remember the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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 | Annotate | Revision Log
« runtime/vm/weak_table.h ('K') | « runtime/vm/weak_table.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 (empty_idx >= 0) {
31 // We will be reusing a slot below.
32 set_used(used() - 1);
33 idx = empty_idx;
34 }
35
36 if (used_ < limit()) {
37 ASSERT(!IsValidEntryAt(idx));
38 // Set the key and value.
39 SetObjectAt(idx, key);
40 SetValueAt(idx, val);
41 // Update the counts.
42 set_used(used() + 1);
43 set_count(count() + 1);
44 return this;
45 }
46
47 // Grow the table and add the hash value.
48 return Rehash()->SetValue(key, val);
siva 2013/06/27 22:11:33 instead of first finding a slot to add and then re
Ivan Posva 2013/06/27 23:59:12 Done.
49 }
50
51
52 WeakTable* WeakTable::Rehash() {
53 intptr_t sz = size();
54 WeakTable* result = NewFrom(this);
55
56 for (intptr_t i = 0; i < sz; i++) {
57 if (IsValidEntryAt(i)) {
58 WeakTable* temp = result->SetValue(ObjectAt(i), ValueAt(i));
59 ASSERT(temp == result);
60 }
61 }
62 return result;
63 }
64
65 } // namespace dart
OLDNEW
« runtime/vm/weak_table.h ('K') | « runtime/vm/weak_table.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698