Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of dart._collection.dev; | 5 part of dart._collection.dev; |
| 6 | 6 |
| 7 | 7 |
| 8 // This is a hack to make @deprecated work in dart:io. Don't remove or use this, | 8 // This is a hack to make @deprecated work in dart:io. Don't remove or use this, |
| 9 // unless coordinated with either me or the core library team. Thanks! | 9 // unless coordinated with either me or the core library team. Thanks! |
| 10 // TODO(ajohnsen): Remove at the 11th of Auguest 2013. | 10 // TODO(ajohnsen): Remove at the 11th of Auguest 2013. |
| (...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 abstract class BidirectionalIterator<T> implements Iterator<T> { | 695 abstract class BidirectionalIterator<T> implements Iterator<T> { |
| 696 bool movePrevious(); | 696 bool movePrevious(); |
| 697 } | 697 } |
| 698 | 698 |
| 699 /** | 699 /** |
| 700 * This class provides default implementations for Iterables (including Lists). | 700 * This class provides default implementations for Iterables (including Lists). |
| 701 * | 701 * |
| 702 * The uses of this class will be replaced by mixins. | 702 * The uses of this class will be replaced by mixins. |
| 703 */ | 703 */ |
| 704 class IterableMixinWorkaround { | 704 class IterableMixinWorkaround { |
| 705 // A list to identify cyclic collections during toString() calls. | |
| 706 static List _toStringList = new List(); | |
| 707 | |
| 705 static bool contains(Iterable iterable, var element) { | 708 static bool contains(Iterable iterable, var element) { |
| 706 for (final e in iterable) { | 709 for (final e in iterable) { |
| 707 if (e == element) return true; | 710 if (e == element) return true; |
| 708 } | 711 } |
| 709 return false; | 712 return false; |
| 710 } | 713 } |
| 711 | 714 |
| 712 static void forEach(Iterable iterable, void f(o)) { | 715 static void forEach(Iterable iterable, void f(o)) { |
| 713 for (final e in iterable) { | 716 for (final e in iterable) { |
| 714 f(e); | 717 f(e); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 888 } else { | 891 } else { |
| 889 buffer.write(list[0]); | 892 buffer.write(list[0]); |
| 890 for (int i = 1; i < list.length; i++) { | 893 for (int i = 1; i < list.length; i++) { |
| 891 buffer.write(separator); | 894 buffer.write(separator); |
| 892 buffer.write(list[i]); | 895 buffer.write(list[i]); |
| 893 } | 896 } |
| 894 } | 897 } |
| 895 return buffer.toString(); | 898 return buffer.toString(); |
| 896 } | 899 } |
| 897 | 900 |
| 901 static String toStringIterable(Iterable iterable) { | |
| 902 for (int i = 0; i < _toStringList.length; i++) { | |
| 903 if (identical(_toStringList[i], iterable)) { return '{...}'; } | |
| 904 } | |
| 905 | |
| 906 var result = new StringBuffer(); | |
|
floitsch
2013/07/08 16:12:34
Type variable.
zarah
2013/07/08 16:57:46
Done.
| |
| 907 try { | |
| 908 _toStringList.add(iterable); | |
| 909 result.write('{'); | |
| 910 bool first = true; | |
| 911 for (var e in iterable) { | |
|
floitsch
2013/07/08 16:12:34
You could see, if iterable.join(", ") doesn't cost
zarah
2013/07/08 16:57:46
Use writeAll instead since it is cheaper.
| |
| 912 if (!first) { | |
| 913 result.write(', '); | |
| 914 } | |
| 915 first = false; | |
| 916 result.write(e); | |
| 917 } | |
| 918 result.write('}'); | |
| 919 } finally { | |
|
floitsch
2013/07/08 16:12:34
assert that you are the last.
zarah
2013/07/08 16:57:46
Done.
| |
| 920 _toStringList.removeLast(); | |
| 921 } | |
| 922 return result.toString(); | |
| 923 } | |
| 924 | |
| 925 static String toStringList(List list) { | |
| 926 for (int i = 0; i < _toStringList.length; i++) { | |
| 927 if (identical(_toStringList[i], list)) { return '[...]'; } | |
| 928 } | |
| 929 | |
| 930 var result = new StringBuffer(); | |
| 931 try { | |
| 932 _toStringList.add(list); | |
| 933 result.write('['); | |
| 934 for (int i = 0; i < list.length; i++) { | |
|
floitsch
2013/07/08 16:12:34
ditto. If list.join(", ") isn't too expensive it w
zarah
2013/07/08 16:57:46
Use writeAll instead since it is cheaper.
| |
| 935 if (i > 0) { | |
| 936 result.write(', '); | |
| 937 } | |
| 938 result.write(list[i]); | |
| 939 } | |
| 940 result.write(']'); | |
| 941 } finally { | |
| 942 _toStringList.removeLast(); | |
| 943 } | |
| 944 | |
| 945 return result.toString(); | |
| 946 } | |
| 947 | |
| 898 static Iterable where(Iterable iterable, bool f(var element)) { | 948 static Iterable where(Iterable iterable, bool f(var element)) { |
| 899 return new WhereIterable(iterable, f); | 949 return new WhereIterable(iterable, f); |
| 900 } | 950 } |
| 901 | 951 |
| 902 static Iterable map(Iterable iterable, f(var element)) { | 952 static Iterable map(Iterable iterable, f(var element)) { |
| 903 return new MappedIterable(iterable, f); | 953 return new MappedIterable(iterable, f); |
| 904 } | 954 } |
| 905 | 955 |
| 906 static Iterable mapList(List list, f(var element)) { | 956 static Iterable mapList(List list, f(var element)) { |
| 907 return new MappedListIterable(list, f); | 957 return new MappedListIterable(list, f); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1064 | 1114 |
| 1065 static Set setDifference(Set set, Set other, Set result) { | 1115 static Set setDifference(Set set, Set other, Set result) { |
| 1066 for (var element in set) { | 1116 for (var element in set) { |
| 1067 if (!other.contains(element)) { | 1117 if (!other.contains(element)) { |
| 1068 result.add(element); | 1118 result.add(element); |
| 1069 } | 1119 } |
| 1070 } | 1120 } |
| 1071 return result; | 1121 return result; |
| 1072 } | 1122 } |
| 1073 } | 1123 } |
| OLD | NEW |