OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 file for dart:collection classes. | 5 // Patch file for dart:collection classes. |
6 import 'dart:_foreign_helper' show JS; | 6 import 'dart:_foreign_helper' show JS; |
7 import 'dart:_js_helper' show | 7 import 'dart:_js_helper' show |
8 fillLiteralMap, InternalMap, NoInline, NoThrows, patch, JsLinkedHashMap, | 8 fillLiteralMap, InternalMap, NoInline, NoThrows, patch, JsLinkedHashMap, |
9 LinkedHashMapCell, LinkedHashMapKeyIterable, LinkedHashMapKeyIterator; | 9 LinkedHashMapCell, LinkedHashMapKeyIterable, LinkedHashMapKeyIterator; |
10 | 10 |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 if (identical(JS('var', '#[#]', bucket, i), key)) return i; | 379 if (identical(JS('var', '#[#]', bucket, i), key)) return i; |
380 } | 380 } |
381 return -1; | 381 return -1; |
382 } | 382 } |
383 } | 383 } |
384 | 384 |
385 class _CustomHashMap<K, V> extends _HashMap<K, V> { | 385 class _CustomHashMap<K, V> extends _HashMap<K, V> { |
386 final _Equality<K> _equals; | 386 final _Equality<K> _equals; |
387 final _Hasher<K> _hashCode; | 387 final _Hasher<K> _hashCode; |
388 final _Predicate _validKey; | 388 final _Predicate _validKey; |
| 389 |
389 _CustomHashMap(this._equals, this._hashCode, bool validKey(potentialKey)) | 390 _CustomHashMap(this._equals, this._hashCode, bool validKey(potentialKey)) |
390 : _validKey = (validKey != null) ? validKey : ((v) => v is K); | 391 : _validKey = (validKey != null) ? validKey : ((v) => v is K); |
391 | 392 |
392 V operator[](Object key) { | 393 V operator[](Object key) { |
393 if (!_validKey(key)) return null; | 394 if (!_validKey(key)) return null; |
394 return super._get(key); | 395 return super._get(key); |
395 } | 396 } |
396 | 397 |
397 void operator[]=(K key, V value) { | 398 void operator[]=(K key, V value) { |
398 super._set(key, value); | 399 super._set(key, value); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 | 486 |
486 @patch | 487 @patch |
487 class LinkedHashMap<K, V> { | 488 class LinkedHashMap<K, V> { |
488 @patch | 489 @patch |
489 factory LinkedHashMap({ bool equals(K key1, K key2), | 490 factory LinkedHashMap({ bool equals(K key1, K key2), |
490 int hashCode(K key), | 491 int hashCode(K key), |
491 bool isValidKey(potentialKey) }) { | 492 bool isValidKey(potentialKey) }) { |
492 if (isValidKey == null) { | 493 if (isValidKey == null) { |
493 if (hashCode == null) { | 494 if (hashCode == null) { |
494 if (equals == null) { | 495 if (equals == null) { |
495 return new JsLinkedHashMap<K, V>(); | 496 return new JsLinkedHashMap<K, V>.es6(); |
496 } | 497 } |
497 hashCode = _defaultHashCode; | 498 hashCode = _defaultHashCode; |
498 } else { | 499 } else { |
499 if (identical(identityHashCode, hashCode) && | 500 if (identical(identityHashCode, hashCode) && |
500 identical(identical, equals)) { | 501 identical(identical, equals)) { |
501 return new _LinkedIdentityHashMap<K, V>(); | 502 return new _LinkedIdentityHashMap<K, V>(); |
502 } | 503 } |
503 if (equals == null) { | 504 if (equals == null) { |
504 equals = _defaultEquals; | 505 equals = _defaultEquals; |
505 } | 506 } |
506 } | 507 } |
507 } else { | 508 } else { |
508 if (hashCode == null) { | 509 if (hashCode == null) { |
509 hashCode = _defaultHashCode; | 510 hashCode = _defaultHashCode; |
510 } | 511 } |
511 if (equals == null) { | 512 if (equals == null) { |
512 equals = _defaultEquals; | 513 equals = _defaultEquals; |
513 } | 514 } |
514 } | 515 } |
515 return new _LinkedCustomHashMap<K, V>(equals, hashCode, isValidKey); | 516 return new _LinkedCustomHashMap<K, V>(equals, hashCode, isValidKey); |
516 } | 517 } |
517 | 518 |
518 @patch | 519 @patch |
519 factory LinkedHashMap.identity() = _LinkedIdentityHashMap<K, V>; | 520 factory LinkedHashMap.identity() = _LinkedIdentityHashMap<K, V>; |
520 | 521 |
521 // Private factory constructor called by generated code for map literals. | 522 // Private factory constructor called by generated code for map literals. |
522 @NoInline() | 523 @NoInline() |
523 factory LinkedHashMap._literal(List keyValuePairs) { | 524 factory LinkedHashMap._literal(List keyValuePairs) { |
524 return fillLiteralMap(keyValuePairs, new JsLinkedHashMap<K, V>()); | 525 return fillLiteralMap(keyValuePairs, new JsLinkedHashMap<K, V>.es6()); |
525 } | 526 } |
526 | 527 |
527 // Private factory constructor called by generated code for map literals. | 528 // Private factory constructor called by generated code for map literals. |
528 @NoThrows() @NoInline() | 529 @NoThrows() @NoInline() |
529 factory LinkedHashMap._empty() { | 530 factory LinkedHashMap._empty() { |
530 return new JsLinkedHashMap<K, V>(); | 531 return new JsLinkedHashMap<K, V>.es6(); |
531 } | 532 } |
532 } | 533 } |
533 | 534 |
| 535 // TODO(floitsch): use ES6 Maps when available. |
534 class _LinkedIdentityHashMap<K, V> extends JsLinkedHashMap<K, V> { | 536 class _LinkedIdentityHashMap<K, V> extends JsLinkedHashMap<K, V> { |
535 int internalComputeHashCode(var key) { | 537 int internalComputeHashCode(var key) { |
536 // We force the hash codes to be unsigned 30-bit integers to avoid | 538 // We force the hash codes to be unsigned 30-bit integers to avoid |
537 // issues with problematic keys like '__proto__'. Another option | 539 // issues with problematic keys like '__proto__'. Another option |
538 // would be to throw an exception if the hash code isn't a number. | 540 // would be to throw an exception if the hash code isn't a number. |
539 return JS('int', '# & 0x3ffffff', identityHashCode(key)); | 541 return JS('int', '# & 0x3ffffff', identityHashCode(key)); |
540 } | 542 } |
541 | 543 |
542 int internalFindBucketIndex(var bucket, var key) { | 544 int internalFindBucketIndex(var bucket, var key) { |
543 if (bucket == null) return -1; | 545 if (bucket == null) return -1; |
544 int length = JS('int', '#.length', bucket); | 546 int length = JS('int', '#.length', bucket); |
545 for (int i = 0; i < length; i++) { | 547 for (int i = 0; i < length; i++) { |
546 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); | 548 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); |
547 if (identical(cell.hashMapCellKey, key)) return i; | 549 if (identical(cell.hashMapCellKey, key)) return i; |
548 } | 550 } |
549 return -1; | 551 return -1; |
550 } | 552 } |
551 } | 553 } |
552 | 554 |
| 555 // TODO(floitsch): use ES6 maps when available. |
553 class _LinkedCustomHashMap<K, V> extends JsLinkedHashMap<K, V> { | 556 class _LinkedCustomHashMap<K, V> extends JsLinkedHashMap<K, V> { |
554 final _Equality<K> _equals; | 557 final _Equality<K> _equals; |
555 final _Hasher<K> _hashCode; | 558 final _Hasher<K> _hashCode; |
556 final _Predicate _validKey; | 559 final _Predicate _validKey; |
| 560 |
557 _LinkedCustomHashMap(this._equals, this._hashCode, | 561 _LinkedCustomHashMap(this._equals, this._hashCode, |
558 bool validKey(potentialKey)) | 562 bool validKey(potentialKey)) |
559 : _validKey = (validKey != null) ? validKey : ((v) => v is K); | 563 : _validKey = (validKey != null) ? validKey : ((v) => v is K); |
560 | 564 |
561 V operator[](Object key) { | 565 V operator[](Object key) { |
562 if (!_validKey(key)) return null; | 566 if (!_validKey(key)) return null; |
563 return super.internalGet(key); | 567 return super.internalGet(key); |
564 } | 568 } |
565 | 569 |
566 void operator[]=(K key, V value) { | 570 void operator[]=(K key, V value) { |
(...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1458 } else if (_cell == null) { | 1462 } else if (_cell == null) { |
1459 _current = null; | 1463 _current = null; |
1460 return false; | 1464 return false; |
1461 } else { | 1465 } else { |
1462 _current = _cell._element; | 1466 _current = _cell._element; |
1463 _cell = _cell._next; | 1467 _cell = _cell._next; |
1464 return true; | 1468 return true; |
1465 } | 1469 } |
1466 } | 1470 } |
1467 } | 1471 } |
OLD | NEW |