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