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 fillLiteralMap, NoInline; | 7 import 'dart:_js_helper' show fillLiteralMap, NoInline; |
8 | 8 |
9 patch class HashMap<K, V> { | 9 patch class HashMap<K, V> { |
10 patch factory HashMap({ bool equals(K key1, K key2), | 10 patch factory HashMap({ bool equals(K key1, K key2), |
(...skipping 1108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 // TODO(kasperl): Consider getting rid of the bucket list when | 1119 // TODO(kasperl): Consider getting rid of the bucket list when |
1120 // the length reaches zero. | 1120 // the length reaches zero. |
1121 _length--; | 1121 _length--; |
1122 _elements = null; | 1122 _elements = null; |
1123 // TODO(kasperl): It would probably be faster to move the | 1123 // TODO(kasperl): It would probably be faster to move the |
1124 // element to the end and reduce the length of the bucket list. | 1124 // element to the end and reduce the length of the bucket list. |
1125 JS('void', '#.splice(#, 1)', bucket, index); | 1125 JS('void', '#.splice(#, 1)', bucket, index); |
1126 return true; | 1126 return true; |
1127 } | 1127 } |
1128 | 1128 |
1129 void removeAll(Iterable<Object> objectsToRemove) { | |
1130 for (var each in objectsToRemove) { | |
1131 remove(each); | |
1132 } | |
1133 } | |
1134 | |
1135 void retainAll(Iterable<Object> elements) { | |
1136 super._retainAll(elements, (o) => o is E); | |
1137 } | |
1138 | |
1139 void removeWhere(bool test(E element)) { | |
1140 removeAll(_computeElements().where(test)); | |
1141 } | |
1142 | |
1143 void retainWhere(bool test(E element)) { | |
1144 removeAll(_computeElements().where((E element) => !test(element))); | |
1145 } | |
1146 | |
1147 void clear() { | 1129 void clear() { |
1148 if (_length > 0) { | 1130 if (_length > 0) { |
1149 _strings = _nums = _rest = _elements = null; | 1131 _strings = _nums = _rest = _elements = null; |
1150 _length = 0; | 1132 _length = 0; |
1151 } | 1133 } |
1152 } | 1134 } |
1153 | 1135 |
1154 List _computeElements() { | 1136 List _computeElements() { |
1155 if (_elements != null) return _elements; | 1137 if (_elements != null) return _elements; |
1156 List result = new List(_length); | 1138 List result = new List(_length); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1340 | 1322 |
1341 E lookup(Object object) { | 1323 E lookup(Object object) { |
1342 if (!_validKey(object)) return null; | 1324 if (!_validKey(object)) return null; |
1343 return super._lookup(object); | 1325 return super._lookup(object); |
1344 } | 1326 } |
1345 | 1327 |
1346 bool remove(Object object) { | 1328 bool remove(Object object) { |
1347 if (!_validKey(object)) return false; | 1329 if (!_validKey(object)) return false; |
1348 return super._remove(object); | 1330 return super._remove(object); |
1349 } | 1331 } |
1350 | |
1351 bool containsAll(Iterable<Object> elements) { | |
1352 for (Object element in elements) { | |
1353 if (!_validKey(element) || !this.contains(element)) return false; | |
1354 } | |
1355 return true; | |
1356 } | |
1357 | |
1358 void removeAll(Iterable<Object> elements) { | |
1359 for (Object element in elements) { | |
1360 if (_validKey(element)) { | |
1361 super._remove(element); | |
1362 } | |
1363 } | |
1364 } | |
1365 | |
1366 void retainAll(Iterable<Object> elements) { | |
1367 super._retainAll(elements, _validKey); | |
1368 } | |
1369 } | 1332 } |
1370 | 1333 |
1371 // TODO(kasperl): Share this code with HashMapKeyIterator<E>? | 1334 // TODO(kasperl): Share this code with HashMapKeyIterator<E>? |
1372 class HashSetIterator<E> implements Iterator<E> { | 1335 class HashSetIterator<E> implements Iterator<E> { |
1373 final _set; | 1336 final _set; |
1374 final List _elements; | 1337 final List _elements; |
1375 int _offset = 0; | 1338 int _offset = 0; |
1376 E _current; | 1339 E _current; |
1377 | 1340 |
1378 HashSetIterator(this._set, this._elements); | 1341 HashSetIterator(this._set, this._elements); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1561 _setTableEntry(rest, hash, JS('var', '[#]', cell)); | 1524 _setTableEntry(rest, hash, JS('var', '[#]', cell)); |
1562 } else { | 1525 } else { |
1563 int index = _findBucketIndex(bucket, element); | 1526 int index = _findBucketIndex(bucket, element); |
1564 if (index >= 0) return false; | 1527 if (index >= 0) return false; |
1565 LinkedHashSetCell cell = _newLinkedCell(element); | 1528 LinkedHashSetCell cell = _newLinkedCell(element); |
1566 JS('void', '#.push(#)', bucket, cell); | 1529 JS('void', '#.push(#)', bucket, cell); |
1567 } | 1530 } |
1568 return true; | 1531 return true; |
1569 } | 1532 } |
1570 | 1533 |
1571 void addAll(Iterable<E> objects) { | |
1572 for (E object in objects) { | |
1573 add(object); | |
1574 } | |
1575 } | |
1576 | |
1577 bool remove(Object object) { | 1534 bool remove(Object object) { |
1578 if (_isStringElement(object)) { | 1535 if (_isStringElement(object)) { |
1579 return _removeHashTableEntry(_strings, object); | 1536 return _removeHashTableEntry(_strings, object); |
1580 } else if (_isNumericElement(object)) { | 1537 } else if (_isNumericElement(object)) { |
1581 return _removeHashTableEntry(_nums, object); | 1538 return _removeHashTableEntry(_nums, object); |
1582 } else { | 1539 } else { |
1583 return _remove(object); | 1540 return _remove(object); |
1584 } | 1541 } |
1585 } | 1542 } |
1586 | 1543 |
1587 bool _remove(Object object) { | 1544 bool _remove(Object object) { |
1588 var rest = _rest; | 1545 var rest = _rest; |
1589 if (rest == null) return false; | 1546 if (rest == null) return false; |
1590 var bucket = _getBucket(rest, object); | 1547 var bucket = _getBucket(rest, object); |
1591 int index = _findBucketIndex(bucket, object); | 1548 int index = _findBucketIndex(bucket, object); |
1592 if (index < 0) return false; | 1549 if (index < 0) return false; |
1593 // Use splice to remove the [cell] element at the index and | 1550 // Use splice to remove the [cell] element at the index and |
1594 // unlink it. | 1551 // unlink it. |
1595 LinkedHashSetCell cell = JS('var', '#.splice(#, 1)[0]', bucket, index); | 1552 LinkedHashSetCell cell = JS('var', '#.splice(#, 1)[0]', bucket, index); |
1596 _unlinkCell(cell); | 1553 _unlinkCell(cell); |
1597 return true; | 1554 return true; |
1598 } | 1555 } |
1599 | 1556 |
1600 void removeAll(Iterable objectsToRemove) { | |
1601 for (var each in objectsToRemove) { | |
1602 remove(each); | |
1603 } | |
1604 } | |
1605 | |
1606 void retainAll(Iterable<Object> elements) { | |
1607 super._retainAll(elements, (o) => o is E); | |
1608 } | |
1609 | |
1610 void removeWhere(bool test(E element)) { | 1557 void removeWhere(bool test(E element)) { |
1611 _filterWhere(test, true); | 1558 _filterWhere(test, true); |
1612 } | 1559 } |
1613 | 1560 |
1614 void retainWhere(bool test(E element)) { | 1561 void retainWhere(bool test(E element)) { |
1615 _filterWhere(test, false); | 1562 _filterWhere(test, false); |
1616 } | 1563 } |
1617 | 1564 |
1618 void _filterWhere(bool test(E element), bool removeMatching) { | 1565 void _filterWhere(bool test(E element), bool removeMatching) { |
1619 LinkedHashSetCell cell = _first; | 1566 LinkedHashSetCell cell = _first; |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1832 return true; | 1779 return true; |
1833 } | 1780 } |
1834 | 1781 |
1835 void removeAll(Iterable<Object> elements) { | 1782 void removeAll(Iterable<Object> elements) { |
1836 for (Object element in elements) { | 1783 for (Object element in elements) { |
1837 if (_validKey(element)) { | 1784 if (_validKey(element)) { |
1838 super._remove(element); | 1785 super._remove(element); |
1839 } | 1786 } |
1840 } | 1787 } |
1841 } | 1788 } |
1842 | |
1843 void retainAll(Iterable<Object> elements) { | |
1844 super._retainAll(elements, _validKey); | |
1845 } | |
1846 } | 1789 } |
1847 | 1790 |
1848 class LinkedHashSetCell { | 1791 class LinkedHashSetCell { |
1849 final _element; | 1792 final _element; |
1850 | 1793 |
1851 LinkedHashSetCell _next; | 1794 LinkedHashSetCell _next; |
1852 LinkedHashSetCell _previous; | 1795 LinkedHashSetCell _previous; |
1853 | 1796 |
1854 LinkedHashSetCell(this._element); | 1797 LinkedHashSetCell(this._element); |
1855 } | 1798 } |
(...skipping 17 matching lines...) Expand all Loading... |
1873 } else if (_cell == null) { | 1816 } else if (_cell == null) { |
1874 _current = null; | 1817 _current = null; |
1875 return false; | 1818 return false; |
1876 } else { | 1819 } else { |
1877 _current = _cell._element; | 1820 _current = _cell._element; |
1878 _cell = _cell._next; | 1821 _cell = _cell._next; |
1879 return true; | 1822 return true; |
1880 } | 1823 } |
1881 } | 1824 } |
1882 } | 1825 } |
OLD | NEW |