Chromium Code Reviews| 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 part of dart.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** A reusable set used to identify cyclic lists during toString() calls. */ | 7 /** A reusable set used to identify cyclic lists during toString() calls. */ |
| 8 Set _toStringVisiting = new HashSet.identity(); | 8 Set _toStringVisiting = new HashSet.identity(); |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 return this[length - 1]; | 73 return this[length - 1]; |
| 74 } | 74 } |
| 75 | 75 |
| 76 E get single { | 76 E get single { |
| 77 if (length == 0) throw new StateError("No elements"); | 77 if (length == 0) throw new StateError("No elements"); |
| 78 if (length > 1) throw new StateError("Too many elements"); | 78 if (length > 1) throw new StateError("Too many elements"); |
| 79 return this[0]; | 79 return this[0]; |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool contains(Object element) { | 82 bool contains(Object element) { |
| 83 int length = this.length; | 83 for (int i = 0; i < this.length; i++) { |
| 84 for (int i = 0; i < length; i++) { | |
| 85 if (this[i] == element) return true; | 84 if (this[i] == element) return true; |
|
Lasse Reichstein Nielsen
2014/02/03 13:27:44
If the '==' operator on the elements modify the li
vicb
2014/02/03 13:38:30
I haven't thought about operator overloading here.
Lasse Reichstein Nielsen
2014/02/04 12:32:00
Pretty unusual, hopefully.
While we could probabl
vicb
2014/02/04 13:00:31
Crashing because of the check or because of any ot
| |
| 86 if (length != this.length) { | |
| 87 throw new ConcurrentModificationError(this); | |
| 88 } | |
| 89 } | 85 } |
| 90 return false; | 86 return false; |
| 91 } | 87 } |
| 92 | 88 |
| 93 bool every(bool test(E element)) { | 89 bool every(bool test(E element)) { |
| 94 int length = this.length; | 90 int length = this.length; |
| 95 for (int i = 0; i < length; i++) { | 91 for (int i = 0; i < length; i++) { |
| 96 if (!test(this[i])) return false; | 92 if (!test(this[i])) return false; |
| 97 if (length != this.length) { | 93 if (length != this.length) { |
| 98 throw new ConcurrentModificationError(this); | 94 throw new ConcurrentModificationError(this); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 } | 149 } |
| 154 if (length != this.length) { | 150 if (length != this.length) { |
| 155 throw new ConcurrentModificationError(this); | 151 throw new ConcurrentModificationError(this); |
| 156 } | 152 } |
| 157 } | 153 } |
| 158 if (matchFound) return match; | 154 if (matchFound) return match; |
| 159 throw new StateError("No matching element"); | 155 throw new StateError("No matching element"); |
| 160 } | 156 } |
| 161 | 157 |
| 162 String join([String separator = ""]) { | 158 String join([String separator = ""]) { |
| 163 int length = this.length; | 159 if (this.length == 0) return ""; |
| 164 if (!separator.isEmpty) { | 160 StringBuffer buffer = new StringBuffer(); |
| 165 if (length == 0) return ""; | 161 if (separator.isEmpty) { |
| 166 String first = "${this[0]}"; | 162 buffer.writeAll(this); |
| 167 if (length != this.length) { | |
| 168 throw new ConcurrentModificationError(this); | |
| 169 } | |
| 170 StringBuffer buffer = new StringBuffer(first); | |
| 171 for (int i = 1; i < length; i++) { | |
| 172 buffer.write(separator); | |
| 173 buffer.write(this[i]); | |
| 174 if (length != this.length) { | |
| 175 throw new ConcurrentModificationError(this); | |
| 176 } | |
| 177 } | |
| 178 return buffer.toString(); | |
| 179 } else { | 163 } else { |
| 180 StringBuffer buffer = new StringBuffer(); | 164 buffer.writeAll(this, separator); |
| 181 for (int i = 0; i < length; i++) { | |
| 182 buffer.write(this[i]); | |
| 183 if (length != this.length) { | |
| 184 throw new ConcurrentModificationError(this); | |
| 185 } | |
| 186 } | |
| 187 return buffer.toString(); | |
| 188 } | 165 } |
| 166 return buffer.toString(); | |
|
Lasse Reichstein Nielsen
2014/02/03 13:27:44
This change looks ok, if performance is as good (o
vicb
2014/02/03 13:38:30
I haven't seen much of a change in perf - it shoul
Lasse Reichstein Nielsen
2014/02/04 12:32:00
I see marginally slower (very, very marginally), p
| |
| 189 } | 167 } |
| 190 | 168 |
| 191 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); | 169 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); |
| 192 | 170 |
| 193 Iterable map(f(E element)) => new MappedListIterable(this, f); | 171 Iterable map(f(E element)) => new MappedListIterable(this, f); |
| 194 | 172 |
| 195 Iterable expand(Iterable f(E element)) => | 173 Iterable expand(Iterable f(E element)) => |
| 196 new ExpandIterable<E, dynamic>(this, f); | 174 new ExpandIterable<E, dynamic>(this, f); |
| 197 | 175 |
| 198 E reduce(E combine(E previousValue, E element)) { | 176 E reduce(E combine(E previousValue, E element)) { |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 result.write('['); | 513 result.write('['); |
| 536 result.writeAll(this, ', '); | 514 result.writeAll(this, ', '); |
| 537 result.write(']'); | 515 result.write(']'); |
| 538 } finally { | 516 } finally { |
| 539 _toStringVisiting.remove(this); | 517 _toStringVisiting.remove(this); |
| 540 } | 518 } |
| 541 | 519 |
| 542 return result.toString(); | 520 return result.toString(); |
| 543 } | 521 } |
| 544 } | 522 } |
| OLD | NEW |