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 @patch class Expando<T> { | 5 @patch |
6 @patch Expando([String name]) | 6 class Expando<T> { |
| 7 @patch |
| 8 Expando([String name]) |
7 : name = name, | 9 : name = name, |
8 _data = new List(_minSize), | 10 _data = new List(_minSize), |
9 _used = 0; | 11 _used = 0; |
10 | 12 |
11 static const _minSize = 8; | 13 static const _minSize = 8; |
12 static final _deletedEntry = new _WeakProperty(null, null); | 14 static final _deletedEntry = new _WeakProperty(null, null); |
13 | 15 |
14 @patch T operator[](Object object) { | 16 @patch |
| 17 T operator [](Object object) { |
15 _checkType(object); | 18 _checkType(object); |
16 | 19 |
17 var mask = _size - 1; | 20 var mask = _size - 1; |
18 var idx = object._identityHashCode & mask; | 21 var idx = object._identityHashCode & mask; |
19 var wp = _data[idx]; | 22 var wp = _data[idx]; |
20 | 23 |
21 while (wp != null) { | 24 while (wp != null) { |
22 if (identical(wp.key, object)) { | 25 if (identical(wp.key, object)) { |
23 return wp.value; | 26 return wp.value; |
24 } else if (wp.key == null) { | 27 } else if (wp.key == null) { |
25 // This entry has been cleared by the GC. | 28 // This entry has been cleared by the GC. |
26 _data[idx] = _deletedEntry; | 29 _data[idx] = _deletedEntry; |
27 } | 30 } |
28 idx = (idx + 1) & mask; | 31 idx = (idx + 1) & mask; |
29 wp = _data[idx]; | 32 wp = _data[idx]; |
30 } | 33 } |
31 | 34 |
32 return null; | 35 return null; |
33 } | 36 } |
34 | 37 |
35 @patch void operator[]=(Object object, T value) { | 38 @patch |
| 39 void operator []=(Object object, T value) { |
36 _checkType(object); | 40 _checkType(object); |
37 | 41 |
38 var mask = _size - 1; | 42 var mask = _size - 1; |
39 var idx = object._identityHashCode & mask; | 43 var idx = object._identityHashCode & mask; |
40 var empty_idx = -1; | 44 var empty_idx = -1; |
41 var wp = _data[idx]; | 45 var wp = _data[idx]; |
42 | 46 |
43 while (wp != null) { | 47 while (wp != null) { |
44 if (identical(wp.key, object)) { | 48 if (identical(wp.key, object)) { |
45 if (value != null) { | 49 if (value != null) { |
46 // Update the associated value. | 50 // Update the associated value. |
47 wp.value = value; | 51 wp.value = value; |
48 } else { | 52 } else { |
49 // Mark the entry as deleted. | 53 // Mark the entry as deleted. |
50 _data[idx] = _deletedEntry; | 54 _data[idx] = _deletedEntry; |
51 } | 55 } |
52 return; | 56 return; |
53 } else if ((empty_idx < 0) && identical(wp, _deletedEntry)) { | 57 } else if ((empty_idx < 0) && identical(wp, _deletedEntry)) { |
54 empty_idx = idx; // Insert at this location if not found. | 58 empty_idx = idx; // Insert at this location if not found. |
55 } else if (wp.key == null) { | 59 } else if (wp.key == null) { |
56 // This entry has been cleared by the GC. | 60 // This entry has been cleared by the GC. |
57 _data[idx] = _deletedEntry; | 61 _data[idx] = _deletedEntry; |
58 if (empty_idx < 0) { | 62 if (empty_idx < 0) { |
59 empty_idx = idx; // Insert at this location if not found. | 63 empty_idx = idx; // Insert at this location if not found. |
60 } | 64 } |
61 } | 65 } |
62 idx = (idx + 1) & mask; | 66 idx = (idx + 1) & mask; |
63 wp = _data[idx]; | 67 wp = _data[idx]; |
64 } | 68 } |
65 | 69 |
66 if (value == null) { | 70 if (value == null) { |
67 // Not entering a null value. We just needed to make sure to clear an | 71 // Not entering a null value. We just needed to make sure to clear an |
68 // existing value if it existed. | 72 // existing value if it existed. |
69 return; | 73 return; |
70 } | 74 } |
71 | 75 |
72 if (empty_idx >= 0) { | 76 if (empty_idx >= 0) { |
73 // We will be reusing the empty slot below. | 77 // We will be reusing the empty slot below. |
74 _used--; | 78 _used--; |
75 idx = empty_idx; | 79 idx = empty_idx; |
76 } | 80 } |
77 | 81 |
78 if (_used < _limit) { | 82 if (_used < _limit) { |
79 _data[idx] = new _WeakProperty(object, value); | 83 _data[idx] = new _WeakProperty(object, value); |
80 _used++; | 84 _used++; |
81 return; | 85 return; |
82 } | 86 } |
83 | 87 |
84 // Grow/reallocate if too many slots have been used. | 88 // Grow/reallocate if too many slots have been used. |
85 _rehash(); | 89 _rehash(); |
86 this[object] = value; // Recursively add the value. | 90 this[object] = value; // Recursively add the value. |
87 } | 91 } |
88 | 92 |
89 _rehash() { | 93 _rehash() { |
90 // Determine the population count of the map to allocate an appropriately | 94 // Determine the population count of the map to allocate an appropriately |
91 // sized map below. | 95 // sized map below. |
92 var count = 0; | 96 var count = 0; |
93 var old_data = _data; | 97 var old_data = _data; |
94 var len = old_data.length; | 98 var len = old_data.length; |
95 for (var i = 0; i < len; i++) { | 99 for (var i = 0; i < len; i++) { |
96 var entry = old_data[i]; | 100 var entry = old_data[i]; |
(...skipping 25 matching lines...) Expand all Loading... |
122 var key = entry.key; | 126 var key = entry.key; |
123 if (key != null) { | 127 if (key != null) { |
124 this[key] = val; | 128 this[key] = val; |
125 } | 129 } |
126 } | 130 } |
127 } | 131 } |
128 } | 132 } |
129 | 133 |
130 static _checkType(object) { | 134 static _checkType(object) { |
131 if ((object == null) || | 135 if ((object == null) || |
132 (object is bool) || | 136 (object is bool) || |
133 (object is num) || | 137 (object is num) || |
134 (object is String)) { | 138 (object is String)) { |
135 throw new ArgumentError.value(object, | 139 throw new ArgumentError.value(object, |
136 "Expandos are not allowed on strings, numbers, booleans or null"); | 140 "Expandos are not allowed on strings, numbers, booleans or null"); |
137 } | 141 } |
138 } | 142 } |
139 | 143 |
140 get _size => _data.length; | 144 get _size => _data.length; |
141 get _limit => (3 * (_size ~/ 4)); | 145 get _limit => (3 * (_size ~/ 4)); |
142 | 146 |
143 List _data; | 147 List _data; |
144 int _used; // Number of used (active and deleted) slots. | 148 int _used; // Number of used (active and deleted) slots. |
145 } | 149 } |
OLD | NEW |