OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 // Efficient JavaScript based implementation of a linked hash map used as a | 5 // Efficient JavaScript based implementation of a linked hash map used as a |
6 // backing map for constant maps and the [LinkedHashMap] patch | 6 // backing map for constant maps and the [LinkedHashMap] patch |
7 | 7 |
8 part of _js_helper; | 8 part of _js_helper; |
9 | 9 |
10 const _USE_ES6_MAPS = const bool.fromEnvironment("dart2js.use.es6.maps"); | 10 const _USE_ES6_MAPS = const bool.fromEnvironment("dart2js.use.es6.maps"); |
(...skipping 26 matching lines...) Expand all Loading... |
37 static bool get _supportsEs6Maps { | 37 static bool get _supportsEs6Maps { |
38 return JS('returns:bool;depends:none;effects:none;throws:never;gvn:true', | 38 return JS('returns:bool;depends:none;effects:none;throws:never;gvn:true', |
39 'typeof Map != "undefined"'); | 39 'typeof Map != "undefined"'); |
40 } | 40 } |
41 | 41 |
42 JsLinkedHashMap(); | 42 JsLinkedHashMap(); |
43 | 43 |
44 /// If ES6 Maps are available returns a linked hash-map backed by an ES6 Map. | 44 /// If ES6 Maps are available returns a linked hash-map backed by an ES6 Map. |
45 @ForceInline() | 45 @ForceInline() |
46 factory JsLinkedHashMap.es6() { | 46 factory JsLinkedHashMap.es6() { |
47 return (_USE_ES6_MAPS && JsLinkedHashMap._supportsEs6Maps) | 47 return (_USE_ES6_MAPS && JsLinkedHashMap._supportsEs6Maps) |
48 ? new Es6LinkedHashMap<K, V>() | 48 ? new Es6LinkedHashMap<K, V>() |
49 : new JsLinkedHashMap<K, V>(); | 49 : new JsLinkedHashMap<K, V>(); |
50 } | 50 } |
51 | 51 |
52 int get length => _length; | 52 int get length => _length; |
53 bool get isEmpty => _length == 0; | 53 bool get isEmpty => _length == 0; |
54 bool get isNotEmpty => !isEmpty; | 54 bool get isNotEmpty => !isEmpty; |
55 | 55 |
56 Iterable<K> get keys { | 56 Iterable<K> get keys { |
57 return new LinkedHashMapKeyIterable<K>(this); | 57 return new LinkedHashMapKeyIterable<K>(this); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 V _removeHashTableEntry(var table, Object key) { | 216 V _removeHashTableEntry(var table, Object key) { |
217 if (table == null) return null; | 217 if (table == null) return null; |
218 LinkedHashMapCell cell = _getTableEntry(table, key); | 218 LinkedHashMapCell cell = _getTableEntry(table, key); |
219 if (cell == null) return null; | 219 if (cell == null) return null; |
220 _unlinkCell(cell); | 220 _unlinkCell(cell); |
221 _deleteTableEntry(table, key); | 221 _deleteTableEntry(table, key); |
222 return cell.hashMapCellValue; | 222 return cell.hashMapCellValue; |
223 } | 223 } |
224 | 224 |
225 void _modified() { | 225 void _modified() { |
226 // Value cycles after 2^30 modifications. If you keep hold of an | 226 // Value cycles after 2^30 modifications so that modification counts are |
227 // iterator for that long, you might miss a modification | 227 // always unboxed (Smi) values. Modification detection will be missed if you |
228 // detection, and iteration can go sour. Don't do that. | 228 // make exactly some multiple of 2^30 modifications between advances of an |
| 229 // iterator. |
229 _modifications = (_modifications + 1) & 0x3ffffff; | 230 _modifications = (_modifications + 1) & 0x3ffffff; |
230 } | 231 } |
231 | 232 |
232 // Create a new cell and link it in as the last one in the list. | 233 // Create a new cell and link it in as the last one in the list. |
233 LinkedHashMapCell _newLinkedCell(K key, V value) { | 234 LinkedHashMapCell _newLinkedCell(K key, V value) { |
234 LinkedHashMapCell cell = new LinkedHashMapCell(key, value); | 235 LinkedHashMapCell cell = new LinkedHashMapCell(key, value); |
235 if (_first == null) { | 236 if (_first == null) { |
236 _first = _last = cell; | 237 _first = _last = cell; |
237 } else { | 238 } else { |
238 LinkedHashMapCell last = _last; | 239 LinkedHashMapCell last = _last; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 } else if (_cell == null) { | 417 } else if (_cell == null) { |
417 _current = null; | 418 _current = null; |
418 return false; | 419 return false; |
419 } else { | 420 } else { |
420 _current = _cell.hashMapCellKey; | 421 _current = _cell.hashMapCellKey; |
421 _cell = _cell._next; | 422 _cell = _cell._next; |
422 return true; | 423 return true; |
423 } | 424 } |
424 } | 425 } |
425 } | 426 } |
OLD | NEW |