Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: runtime/lib/collection_patch.dart

Issue 26832002: Add Set.lookup method to get the instance in the set. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 bool contains(Object object) { 562 bool contains(Object object) {
563 int index = _hashCode(object) & (_buckets.length - 1); 563 int index = _hashCode(object) & (_buckets.length - 1);
564 HashSetEntry entry = _buckets[index]; 564 HashSetEntry entry = _buckets[index];
565 while (entry != null) { 565 while (entry != null) {
566 if (_equals(entry.key, object)) return true; 566 if (_equals(entry.key, object)) return true;
567 entry = entry.next; 567 entry = entry.next;
568 } 568 }
569 return false; 569 return false;
570 } 570 }
571 571
572 E lookup(Object object) {
573 int index = _hashCode(object) & (_buckets.length - 1);
574 HashSetEntry entry = _buckets[index];
575 while (entry != null) {
576 var key = entry.key;
577 if (_equals(key, object)) return key;
578 entry = entry.next;
579 }
580 return null;
581 }
582
572 // Set. 583 // Set.
573 584
574 void add(E element) { 585 void add(E element) {
575 int hashCode = _hashCode(element); 586 int hashCode = _hashCode(element);
576 int index = hashCode & (_buckets.length - 1); 587 int index = hashCode & (_buckets.length - 1);
577 HashSetEntry entry = _buckets[index]; 588 HashSetEntry entry = _buckets[index];
578 while (entry != null) { 589 while (entry != null) {
579 if (_equals(entry.key, element)) return; 590 if (_equals(entry.key, element)) return;
580 entry = entry.next; 591 entry = entry.next;
581 } 592 }
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 bool remove(Object element) { 728 bool remove(Object element) {
718 if (!_validKey(element)) return false; 729 if (!_validKey(element)) return false;
719 return super.remove(element); 730 return super.remove(element);
720 } 731 }
721 732
722 bool contains(Object element) { 733 bool contains(Object element) {
723 if (!_validKey(element)) return false; 734 if (!_validKey(element)) return false;
724 return super.contains(element); 735 return super.contains(element);
725 } 736 }
726 737
738 E lookup(Object element) {
739 if (!_validKey(element)) return null;
740 return super.lookup(element);
741 }
742
727 bool containsAll(Iterable<Object> elements) { 743 bool containsAll(Iterable<Object> elements) {
728 for (Object element in elements) { 744 for (Object element in elements) {
729 if (!_validKey(element) || !this.contains(element)) return false; 745 if (!_validKey(element) || !this.contains(element)) return false;
730 } 746 }
731 return true; 747 return true;
732 } 748 }
733 749
734 void removeAll(Iterable<Object> elements) { 750 void removeAll(Iterable<Object> elements) {
735 for (Object element in elements) { 751 for (Object element in elements) {
736 if (_validKey(element)) { 752 if (_validKey(element)) {
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 1189
1174 bool _equals(e1, e2) => _equality(e1, e2); 1190 bool _equals(e1, e2) => _equality(e1, e2);
1175 1191
1176 int _hashCode(e) => _hasher(e); 1192 int _hashCode(e) => _hasher(e);
1177 1193
1178 bool contains(Object o) { 1194 bool contains(Object o) {
1179 if (!_validKey(o)) return false; 1195 if (!_validKey(o)) return false;
1180 return super.contains(o); 1196 return super.contains(o);
1181 } 1197 }
1182 1198
1199 E lookup(Object o) {
1200 if (!_validKey(o)) return null;
1201 return super.lookup(o);
1202 }
1203
1183 bool remove(Object o) { 1204 bool remove(Object o) {
1184 if (!_validKey(o)) return false; 1205 if (!_validKey(o)) return false;
1185 return super.remove(o); 1206 return super.remove(o);
1186 } 1207 }
1187 1208
1188 bool containsAll(Iterable<Object> elements) { 1209 bool containsAll(Iterable<Object> elements) {
1189 for (Object element in elements) { 1210 for (Object element in elements) {
1190 if (!_validKey(element) || !this.contains(element)) return false; 1211 if (!_validKey(element) || !this.contains(element)) return false;
1191 } 1212 }
1192 return true; 1213 return true;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1228 return false; 1249 return false;
1229 } 1250 }
1230 _LinkedHashSetEntry entry = _next; 1251 _LinkedHashSetEntry entry = _next;
1231 _current = entry.key; 1252 _current = entry.key;
1232 _next = entry._nextEntry; 1253 _next = entry._nextEntry;
1233 return true; 1254 return true;
1234 } 1255 }
1235 1256
1236 E get current => _current; 1257 E get current => _current;
1237 } 1258 }
OLDNEW
« no previous file with comments | « pkg/unmodifiable_collection/lib/unmodifiable_collection.dart ('k') | sdk/lib/_internal/lib/collection_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698