| 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"); |
| 11 |
| 10 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap { | 12 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap { |
| 11 int _length = 0; | 13 int _length = 0; |
| 12 | 14 |
| 13 // The hash map contents are divided into three parts: one part for | 15 // The hash map contents are divided into three parts: one part for |
| 14 // string keys, one for numeric keys, and one for the rest. String | 16 // string keys, one for numeric keys, and one for the rest. String |
| 15 // and numeric keys map directly to their linked cells, but the rest | 17 // and numeric keys map directly to their linked cells, but the rest |
| 16 // of the entries are stored in bucket lists of the form: | 18 // of the entries are stored in bucket lists of the form: |
| 17 // | 19 // |
| 18 // [cell-0, cell-1, ...] | 20 // [cell-0, cell-1, ...] |
| 19 // | 21 // |
| 20 // where all keys in the same bucket share the same hash code. | 22 // where all keys in the same bucket share the same hash code. |
| 21 var _strings; | 23 var _strings; |
| 22 var _nums; | 24 var _nums; |
| 23 var _rest; | 25 var _rest; |
| 24 | 26 |
| 25 // The keys and values are stored in cells that are linked together | 27 // The keys and values are stored in cells that are linked together |
| 26 // to form a double linked list. | 28 // to form a double linked list. |
| 27 LinkedHashMapCell _first; | 29 LinkedHashMapCell _first; |
| 28 LinkedHashMapCell _last; | 30 LinkedHashMapCell _last; |
| 29 | 31 |
| 30 // We track the number of modifications done to the key set of the | 32 // We track the number of modifications done to the key set of the |
| 31 // hash map to be able to throw when the map is modified while being | 33 // hash map to be able to throw when the map is modified while being |
| 32 // iterated over. | 34 // iterated over. |
| 33 int _modifications = 0; | 35 int _modifications = 0; |
| 34 | 36 |
| 37 static bool get _supportsEs6Maps { |
| 38 return JS('returns:bool;depends:none;effects:none;throws:never;gvn:true', |
| 39 'typeof Map != "undefined"'); |
| 40 } |
| 41 |
| 35 JsLinkedHashMap(); | 42 JsLinkedHashMap(); |
| 36 | 43 |
| 44 /// If ES6 Maps are available returns a linked hash-map backed by an ES6 Map. |
| 45 @ForceInline() |
| 46 factory JsLinkedHashMap.es6() { |
| 47 return (_USE_ES6_MAPS && JsLinkedHashMap._supportsEs6Maps) |
| 48 ? new Es6LinkedHashMap<K, V>() |
| 49 : new JsLinkedHashMap<K, V>(); |
| 50 } |
| 37 | 51 |
| 38 int get length => _length; | 52 int get length => _length; |
| 39 bool get isEmpty => _length == 0; | 53 bool get isEmpty => _length == 0; |
| 40 bool get isNotEmpty => !isEmpty; | 54 bool get isNotEmpty => !isEmpty; |
| 41 | 55 |
| 42 Iterable<K> get keys { | 56 Iterable<K> get keys { |
| 43 return new LinkedHashMapKeyIterable<K>(this); | 57 return new LinkedHashMapKeyIterable<K>(this); |
| 44 } | 58 } |
| 45 | 59 |
| 46 Iterable<V> get values { | 60 Iterable<V> get values { |
| 47 return new MappedIterable<K, V>(keys, (each) => this[each]); | 61 return new MappedIterable<K, V>(keys, (each) => this[each]); |
| 48 } | 62 } |
| 49 | 63 |
| 50 bool containsKey(Object key) { | 64 bool containsKey(Object key) { |
| 51 if (_isStringKey(key)) { | 65 if (_isStringKey(key)) { |
| 52 var strings = _strings; | 66 var strings = _strings; |
| 53 if (strings == null) return false; | 67 if (strings == null) return false; |
| 54 LinkedHashMapCell cell = _getTableEntry(strings, key); | 68 return _containsTableEntry(strings, key); |
| 55 return cell != null; | |
| 56 } else if (_isNumericKey(key)) { | 69 } else if (_isNumericKey(key)) { |
| 57 var nums = _nums; | 70 var nums = _nums; |
| 58 if (nums == null) return false; | 71 if (nums == null) return false; |
| 59 LinkedHashMapCell cell = _getTableEntry(nums, key); | 72 return _containsTableEntry(nums, key); |
| 60 return cell != null; | |
| 61 } else { | 73 } else { |
| 62 return internalContainsKey(key); | 74 return internalContainsKey(key); |
| 63 } | 75 } |
| 64 } | 76 } |
| 65 | 77 |
| 66 bool internalContainsKey(Object key) { | 78 bool internalContainsKey(Object key) { |
| 67 var rest = _rest; | 79 var rest = _rest; |
| 68 if (rest == null) return false; | 80 if (rest == null) return false; |
| 69 var bucket = _getBucket(rest, key); | 81 var bucket = _getBucket(rest, key); |
| 70 return internalFindBucketIndex(bucket, key) >= 0; | 82 return internalFindBucketIndex(bucket, key) >= 0; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 _addHashTableEntry(nums, key, value); | 129 _addHashTableEntry(nums, key, value); |
| 118 } else { | 130 } else { |
| 119 internalSet(key, value); | 131 internalSet(key, value); |
| 120 } | 132 } |
| 121 } | 133 } |
| 122 | 134 |
| 123 void internalSet(K key, V value) { | 135 void internalSet(K key, V value) { |
| 124 var rest = _rest; | 136 var rest = _rest; |
| 125 if (rest == null) _rest = rest = _newHashTable(); | 137 if (rest == null) _rest = rest = _newHashTable(); |
| 126 var hash = internalComputeHashCode(key); | 138 var hash = internalComputeHashCode(key); |
| 127 var bucket = JS('var', '#[#]', rest, hash); | 139 var bucket = _getTableEntry(rest, hash); |
| 128 if (bucket == null) { | 140 if (bucket == null) { |
| 129 LinkedHashMapCell cell = _newLinkedCell(key, value); | 141 LinkedHashMapCell cell = _newLinkedCell(key, value); |
| 130 _setTableEntry(rest, hash, JS('var', '[#]', cell)); | 142 _setTableEntry(rest, hash, JS('var', '[#]', cell)); |
| 131 } else { | 143 } else { |
| 132 int index = internalFindBucketIndex(bucket, key); | 144 int index = internalFindBucketIndex(bucket, key); |
| 133 if (index >= 0) { | 145 if (index >= 0) { |
| 134 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); | 146 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); |
| 135 cell.hashMapCellValue = value; | 147 cell.hashMapCellValue = value; |
| 136 } else { | 148 } else { |
| 137 LinkedHashMapCell cell = _newLinkedCell(key, value); | 149 LinkedHashMapCell cell = _newLinkedCell(key, value); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 assert(cell == _last); | 258 assert(cell == _last); |
| 247 _last = previous; | 259 _last = previous; |
| 248 } else { | 260 } else { |
| 249 next._previous = previous; | 261 next._previous = previous; |
| 250 } | 262 } |
| 251 _length--; | 263 _length--; |
| 252 _modified(); | 264 _modified(); |
| 253 } | 265 } |
| 254 | 266 |
| 255 static bool _isStringKey(var key) { | 267 static bool _isStringKey(var key) { |
| 256 return key is String && key != '__proto__'; | 268 return key is String; |
| 257 } | 269 } |
| 258 | 270 |
| 259 static bool _isNumericKey(var key) { | 271 static bool _isNumericKey(var key) { |
| 260 // Only treat unsigned 30-bit integers as numeric keys. This way, | 272 // Only treat unsigned 30-bit integers as numeric keys. This way, |
| 261 // we avoid converting them to strings when we use them as keys in | 273 // we avoid converting them to strings when we use them as keys in |
| 262 // the JavaScript hash table object. | 274 // the JavaScript hash table object. |
| 263 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key); | 275 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key); |
| 264 } | 276 } |
| 265 | 277 |
| 266 int internalComputeHashCode(var key) { | 278 int internalComputeHashCode(var key) { |
| 267 // We force the hash codes to be unsigned 30-bit integers to avoid | 279 // We force the hash codes to be unsigned 30-bit integers to avoid |
| 268 // issues with problematic keys like '__proto__'. Another option | 280 // issues with problematic keys like '__proto__'. Another option |
| 269 // would be to throw an exception if the hash code isn't a number. | 281 // would be to throw an exception if the hash code isn't a number. |
| 270 return JS('int', '# & 0x3ffffff', key.hashCode); | 282 return JS('int', '# & 0x3ffffff', key.hashCode); |
| 271 } | 283 } |
| 272 | 284 |
| 273 static _getTableEntry(var table, var key) { | |
| 274 return JS('var', '#[#]', table, key); | |
| 275 } | |
| 276 | |
| 277 static void _setTableEntry(var table, var key, var value) { | |
| 278 assert(value != null); | |
| 279 JS('void', '#[#] = #', table, key, value); | |
| 280 } | |
| 281 | |
| 282 static void _deleteTableEntry(var table, var key) { | |
| 283 JS('void', 'delete #[#]', table, key); | |
| 284 } | |
| 285 | |
| 286 List _getBucket(var table, var key) { | 285 List _getBucket(var table, var key) { |
| 287 var hash = internalComputeHashCode(key); | 286 var hash = internalComputeHashCode(key); |
| 288 return JS('var', '#[#]', table, hash); | 287 return _getTableEntry(table, hash); |
| 289 } | 288 } |
| 290 | 289 |
| 291 int internalFindBucketIndex(var bucket, var key) { | 290 int internalFindBucketIndex(var bucket, var key) { |
| 292 if (bucket == null) return -1; | 291 if (bucket == null) return -1; |
| 293 int length = JS('int', '#.length', bucket); | 292 int length = JS('int', '#.length', bucket); |
| 294 for (int i = 0; i < length; i++) { | 293 for (int i = 0; i < length; i++) { |
| 295 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); | 294 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); |
| 296 if (cell.hashMapCellKey == key) return i; | 295 if (cell.hashMapCellKey == key) return i; |
| 297 } | 296 } |
| 298 return -1; | 297 return -1; |
| 299 } | 298 } |
| 300 | 299 |
| 301 static _newHashTable() { | 300 String toString() => Maps.mapToString(this); |
| 301 |
| 302 _getTableEntry(var table, var key) { |
| 303 return JS('var', '#[#]', table, key); |
| 304 } |
| 305 |
| 306 void _setTableEntry(var table, var key, var value) { |
| 307 assert(value != null); |
| 308 JS('void', '#[#] = #', table, key, value); |
| 309 } |
| 310 |
| 311 void _deleteTableEntry(var table, var key) { |
| 312 JS('void', 'delete #[#]', table, key); |
| 313 } |
| 314 |
| 315 bool _containsTableEntry(var table, var key) { |
| 316 LinkedHashMapCell cell = _getTableEntry(table, key); |
| 317 return cell != null; |
| 318 } |
| 319 |
| 320 _newHashTable() { |
| 302 // Create a new JavaScript object to be used as a hash table. Use | 321 // Create a new JavaScript object to be used as a hash table. Use |
| 303 // Object.create to avoid the properties on Object.prototype | 322 // Object.create to avoid the properties on Object.prototype |
| 304 // showing up as entries. | 323 // showing up as entries. |
| 305 var table = JS('var', 'Object.create(null)'); | 324 var table = JS('var', 'Object.create(null)'); |
| 306 // Attempt to force the hash table into 'dictionary' mode by | 325 // Attempt to force the hash table into 'dictionary' mode by |
| 307 // adding a property to it and deleting it again. | 326 // adding a property to it and deleting it again. |
| 308 var temporaryKey = '<non-identifier-key>'; | 327 var temporaryKey = '<non-identifier-key>'; |
| 309 _setTableEntry(table, temporaryKey, table); | 328 _setTableEntry(table, temporaryKey, table); |
| 310 _deleteTableEntry(table, temporaryKey); | 329 _deleteTableEntry(table, temporaryKey); |
| 311 return table; | 330 return table; |
| 312 } | 331 } |
| 332 } |
| 313 | 333 |
| 314 String toString() => Maps.mapToString(this); | 334 class Es6LinkedHashMap<K, V> extends JsLinkedHashMap<K, V> { |
| 335 |
| 336 @override |
| 337 _getTableEntry(var table, var key) { |
| 338 return JS('var', '#.get(#)', table, key); |
| 339 } |
| 340 |
| 341 @override |
| 342 void _setTableEntry(var table, var key, var value) { |
| 343 JS('void', '#.set(#, #)', table, key, value); |
| 344 } |
| 345 |
| 346 @override |
| 347 void _deleteTableEntry(var table, var key) { |
| 348 JS('void', '#.delete(#)', table, key); |
| 349 } |
| 350 |
| 351 @override |
| 352 bool _containsTableEntry(var table, var key) { |
| 353 return JS('bool', '#.has(#)', table, key); |
| 354 } |
| 355 |
| 356 @override |
| 357 _newHashTable() { |
| 358 return JS('var', 'new Map()'); |
| 359 } |
| 315 } | 360 } |
| 316 | 361 |
| 317 class LinkedHashMapCell { | 362 class LinkedHashMapCell { |
| 318 final hashMapCellKey; | 363 final hashMapCellKey; |
| 319 var hashMapCellValue; | 364 var hashMapCellValue; |
| 320 | 365 |
| 321 LinkedHashMapCell _next; | 366 LinkedHashMapCell _next; |
| 322 LinkedHashMapCell _previous; | 367 LinkedHashMapCell _previous; |
| 323 | 368 |
| 324 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue); | 369 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 } else if (_cell == null) { | 416 } else if (_cell == null) { |
| 372 _current = null; | 417 _current = null; |
| 373 return false; | 418 return false; |
| 374 } else { | 419 } else { |
| 375 _current = _cell.hashMapCellKey; | 420 _current = _cell.hashMapCellKey; |
| 376 _cell = _cell._next; | 421 _cell = _cell._next; |
| 377 return true; | 422 return true; |
| 378 } | 423 } |
| 379 } | 424 } |
| 380 } | 425 } |
| OLD | NEW |