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 class HashMap<K, V> { | 5 patch class HashMap<K, V> { |
6 /* patch */ factory HashMap({ bool equals(K key1, K key2), | 6 /* patch */ factory HashMap({ bool equals(K key1, K key2), |
7 int hashCode(K key), | 7 int hashCode(K key), |
8 bool isValidKey(potentialKey) }) { | 8 bool isValidKey(potentialKey) }) { |
9 if (isValidKey == null) { | 9 if (isValidKey == null) { |
10 if (hashCode == null) { | 10 if (hashCode == null) { |
(...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 if (equals == null) { | 533 if (equals == null) { |
534 equals = _defaultEquals; | 534 equals = _defaultEquals; |
535 } | 535 } |
536 } | 536 } |
537 return new _CustomHashSet<E>(equals, hashCode, isValidKey); | 537 return new _CustomHashSet<E>(equals, hashCode, isValidKey); |
538 } | 538 } |
539 | 539 |
540 /* patch */ factory HashSet.identity() = _IdentityHashSet<E>; | 540 /* patch */ factory HashSet.identity() = _IdentityHashSet<E>; |
541 } | 541 } |
542 | 542 |
543 class _HashSet<E> extends _HashSetBase<E> implements HashSet<E> { | 543 class _HashSet<E> extends SetBase<E> implements HashSet<E> { |
544 static const int _INITIAL_CAPACITY = 8; | 544 static const int _INITIAL_CAPACITY = 8; |
545 | 545 |
546 List<_HashSetEntry> _buckets = new List(_INITIAL_CAPACITY); | 546 List<_HashSetEntry> _buckets = new List(_INITIAL_CAPACITY); |
547 int _elementCount = 0; | 547 int _elementCount = 0; |
548 int _modificationCount = 0; | 548 int _modificationCount = 0; |
549 | 549 |
550 bool _equals(e1, e2) => e1 == e2; | 550 bool _equals(e1, e2) => e1 == e2; |
551 int _hashCode(e) => e.hashCode; | 551 int _hashCode(e) => e.hashCode; |
552 | 552 |
553 // Iterable. | 553 // Iterable. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 } | 627 } |
628 | 628 |
629 bool remove(Object object) => _remove(object, _hashCode(object)); | 629 bool remove(Object object) => _remove(object, _hashCode(object)); |
630 | 630 |
631 void removeAll(Iterable<Object> objectsToRemove) { | 631 void removeAll(Iterable<Object> objectsToRemove) { |
632 for (Object object in objectsToRemove) { | 632 for (Object object in objectsToRemove) { |
633 _remove(object, _hashCode(object)); | 633 _remove(object, _hashCode(object)); |
634 } | 634 } |
635 } | 635 } |
636 | 636 |
637 void retainAll(Iterable<Object> objectsToRetain) { | |
638 super._retainAll(objectsToRetain, (o) => o is E); | |
639 } | |
640 | |
641 void _filterWhere(bool test(E element), bool removeMatching) { | 637 void _filterWhere(bool test(E element), bool removeMatching) { |
642 int length = _buckets.length; | 638 int length = _buckets.length; |
643 for (int index = 0; index < length; index++) { | 639 for (int index = 0; index < length; index++) { |
644 _HashSetEntry entry = _buckets[index]; | 640 _HashSetEntry entry = _buckets[index]; |
645 _HashSetEntry previous = null; | 641 _HashSetEntry previous = null; |
646 while (entry != null) { | 642 while (entry != null) { |
647 int modificationCount = _modificationCount; | 643 int modificationCount = _modificationCount; |
648 bool testResult = test(entry.key); | 644 bool testResult = test(entry.key); |
649 if (modificationCount != _modificationCount) { | 645 if (modificationCount != _modificationCount) { |
650 throw new ConcurrentModificationError(this); | 646 throw new ConcurrentModificationError(this); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 _HashSetEntry next = entry.next; | 700 _HashSetEntry next = entry.next; |
705 int newIndex = entry.hashCode & (newLength - 1); | 701 int newIndex = entry.hashCode & (newLength - 1); |
706 entry.next = newBuckets[newIndex]; | 702 entry.next = newBuckets[newIndex]; |
707 newBuckets[newIndex] = entry; | 703 newBuckets[newIndex] = entry; |
708 entry = next; | 704 entry = next; |
709 } | 705 } |
710 } | 706 } |
711 _buckets = newBuckets; | 707 _buckets = newBuckets; |
712 } | 708 } |
713 | 709 |
714 HashSet<E> _newSet() => new _HashSet<E>(); | 710 HashSet<E> cloneEmpty() => new _HashSet<E>(); |
715 } | 711 } |
716 | 712 |
717 class _IdentityHashSet<E> extends _HashSet<E> { | 713 class _IdentityHashSet<E> extends _HashSet<E> { |
718 int _hashCode(e) => identityHashCode(e); | 714 int _hashCode(e) => identityHashCode(e); |
719 bool _equals(e1, e2) => identical(e1, e2); | 715 bool _equals(e1, e2) => identical(e1, e2); |
720 HashSet<E> _newSet() => new _IdentityHashSet<E>(); | 716 HashSet<E> cloneEmpty() => new _IdentityHashSet<E>(); |
721 } | 717 } |
722 | 718 |
723 class _CustomHashSet<E> extends _HashSet<E> { | 719 class _CustomHashSet<E> extends _HashSet<E> { |
724 final _Equality<E> _equality; | 720 final _Equality<E> _equality; |
725 final _Hasher<E> _hasher; | 721 final _Hasher<E> _hasher; |
726 final _Predicate _validKey; | 722 final _Predicate _validKey; |
727 _CustomHashSet(this._equality, this._hasher, bool validKey(Object o)) | 723 _CustomHashSet(this._equality, this._hasher, bool validKey(Object o)) |
728 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; | 724 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; |
729 | 725 |
730 bool remove(Object element) { | 726 bool remove(Object element) { |
(...skipping 19 matching lines...) Expand all Loading... |
750 } | 746 } |
751 | 747 |
752 void removeAll(Iterable<Object> elements) { | 748 void removeAll(Iterable<Object> elements) { |
753 for (Object element in elements) { | 749 for (Object element in elements) { |
754 if (_validKey(element)) { | 750 if (_validKey(element)) { |
755 super._remove(element, _hasher(element)); | 751 super._remove(element, _hasher(element)); |
756 } | 752 } |
757 } | 753 } |
758 } | 754 } |
759 | 755 |
760 void retainAll(Iterable<Object> elements) { | |
761 super._retainAll(elements, _validKey); | |
762 } | |
763 | |
764 bool _equals(e1, e2) => _equality(e1, e2); | 756 bool _equals(e1, e2) => _equality(e1, e2); |
765 int _hashCode(e) => _hasher(e); | 757 int _hashCode(e) => _hasher(e); |
766 | 758 |
767 HashSet<E> _newSet() => new _CustomHashSet<E>(_equality, _hasher, _validKey); | 759 HashSet<E> cloneEmpty() => |
| 760 new _CustomHashSet<E>(_equality, _hasher, _validKey); |
768 } | 761 } |
769 | 762 |
770 class _HashSetEntry { | 763 class _HashSetEntry { |
771 final key; | 764 final key; |
772 final int hashCode; | 765 final int hashCode; |
773 _HashSetEntry next; | 766 _HashSetEntry next; |
774 _HashSetEntry(this.key, this.hashCode, this.next); | 767 _HashSetEntry(this.key, this.hashCode, this.next); |
775 | 768 |
776 _HashSetEntry remove() { | 769 _HashSetEntry remove() { |
777 _HashSetEntry result = next; | 770 _HashSetEntry result = next; |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 // resize the backing store. | 1160 // resize the backing store. |
1168 if ((newElements << 2) > ((length << 1) + length)) _resize(); | 1161 if ((newElements << 2) > ((length << 1) + length)) _resize(); |
1169 _modificationCount = (_modificationCount + 1) & _MODIFICATION_COUNT_MASK; | 1162 _modificationCount = (_modificationCount + 1) & _MODIFICATION_COUNT_MASK; |
1170 } | 1163 } |
1171 | 1164 |
1172 void clear() { | 1165 void clear() { |
1173 _nextEntry = _previousEntry = this; | 1166 _nextEntry = _previousEntry = this; |
1174 super.clear(); | 1167 super.clear(); |
1175 } | 1168 } |
1176 | 1169 |
1177 HashSet<E> _newSet() => new _LinkedHashSet<E>(); | 1170 HashSet<E> cloneEmpty() => new _LinkedHashSet<E>(); |
1178 } | 1171 } |
1179 | 1172 |
1180 class _LinkedIdentityHashSet<E> extends _LinkedHashSet<E> { | 1173 class _LinkedIdentityHashSet<E> extends _LinkedHashSet<E> { |
1181 int _hashCode(e) => identityHashCode(e); | 1174 int _hashCode(e) => identityHashCode(e); |
1182 bool _equals(e1, e2) => identical(e1, e2); | 1175 bool _equals(e1, e2) => identical(e1, e2); |
1183 HashSet<E> _newSet() => new _LinkedIdentityHashSet<E>(); | 1176 HashSet<E> cloneEmpty() => new _LinkedIdentityHashSet<E>(); |
1184 } | 1177 } |
1185 | 1178 |
1186 class _LinkedCustomHashSet<E> extends _LinkedHashSet<E> { | 1179 class _LinkedCustomHashSet<E> extends _LinkedHashSet<E> { |
1187 final _Equality<E> _equality; | 1180 final _Equality<E> _equality; |
1188 final _Hasher<E> _hasher; | 1181 final _Hasher<E> _hasher; |
1189 final _Predicate _validKey; | 1182 final _Predicate _validKey; |
1190 | 1183 |
1191 _LinkedCustomHashSet(this._equality, this._hasher, bool validKey(Object o)) | 1184 _LinkedCustomHashSet(this._equality, this._hasher, bool validKey(Object o)) |
1192 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; | 1185 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; |
1193 | 1186 |
(...skipping 24 matching lines...) Expand all Loading... |
1218 } | 1211 } |
1219 | 1212 |
1220 void removeAll(Iterable<Object> elements) { | 1213 void removeAll(Iterable<Object> elements) { |
1221 for (Object element in elements) { | 1214 for (Object element in elements) { |
1222 if (_validKey(element)) { | 1215 if (_validKey(element)) { |
1223 super._remove(element, _hasher(element)); | 1216 super._remove(element, _hasher(element)); |
1224 } | 1217 } |
1225 } | 1218 } |
1226 } | 1219 } |
1227 | 1220 |
1228 void retainAll(Iterable<Object> elements) { | 1221 HashSet<E> cloneEmpty() => |
1229 super._retainAll(elements, _validKey); | |
1230 } | |
1231 | |
1232 HashSet<E> _newSet() => | |
1233 new _LinkedCustomHashSet<E>(_equality, _hasher, _validKey); | 1222 new _LinkedCustomHashSet<E>(_equality, _hasher, _validKey); |
1234 } | 1223 } |
1235 | 1224 |
1236 class _LinkedHashSetIterator<E> implements Iterator<E> { | 1225 class _LinkedHashSetIterator<E> implements Iterator<E> { |
1237 final _LinkedHashSet _set; | 1226 final _LinkedHashSet _set; |
1238 final int _modificationCount; | 1227 final int _modificationCount; |
1239 var _next; | 1228 var _next; |
1240 E _current; | 1229 E _current; |
1241 | 1230 |
1242 _LinkedHashSetIterator(_LinkedHashSet hashSet) | 1231 _LinkedHashSetIterator(_LinkedHashSet hashSet) |
(...skipping 10 matching lines...) Expand all Loading... |
1253 return false; | 1242 return false; |
1254 } | 1243 } |
1255 _LinkedHashSetEntry entry = _next; | 1244 _LinkedHashSetEntry entry = _next; |
1256 _current = entry.key; | 1245 _current = entry.key; |
1257 _next = entry._nextEntry; | 1246 _next = entry._nextEntry; |
1258 return true; | 1247 return true; |
1259 } | 1248 } |
1260 | 1249 |
1261 E get current => _current; | 1250 E get current => _current; |
1262 } | 1251 } |
OLD | NEW |