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

Side by Side Diff: sdk/lib/_collection_dev/list.dart

Issue 12391046: Add List.asMap(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 9 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 part of dart._collection.dev; 5 part of dart._collection.dev;
6 6
7 /** 7 /**
8 * Class implementing the read-operations on [List]. 8 * Class implementing the read-operations on [List].
9 * 9 *
10 * Implements all read-only operations, except [:operator[]:] and [:length:], 10 * Implements all read-only operations, except [:operator[]:] and [:length:],
11 * in terms of those two operations. 11 * in terms of those two operations.
12 */ 12 */
13 abstract class ListBase<E> extends ListIterable<E> implements List<E> { 13 abstract class ListBase<E> extends ListIterable<E> implements List<E> {
14 // List interface. 14 // List interface.
15 int get length; 15 int get length;
16 E operator[](int index); 16 E operator[](int index);
17 17
18 // Collection interface. 18 // Collection interface.
19 // Implement in a fully mutable specialized class if necessary. 19 // Implement in a fully mutable specialized class if necessary.
20 // The fixed-length and unmodifiable lists throw on all members 20 // The fixed-length and unmodifiable lists throw on all members
21 // of the collection interface. 21 // of the collection interface.
22 22
23 // Iterable interface. 23 // Iterable interface.
24 E elementAt(int index) { 24 E elementAt(int index) {
25 return this[index]; 25 return this[index];
26 } 26 }
27 27
28 Map<int, E> asMap() {
29 return new ListMapView(this);
30 }
31
28 List<E> getRange(int start, int length) { 32 List<E> getRange(int start, int length) {
29 if (start < 0 || start > this.length) { 33 if (start < 0 || start > this.length) {
30 throw new RangeError.range(start, 0, this.length); 34 throw new RangeError.range(start, 0, this.length);
31 } 35 }
32 if (length < 0 || start + length > this.length) { 36 if (length < 0 || start + length > this.length) {
33 throw new RangeError.range(length, 0, this.length - start); 37 throw new RangeError.range(length, 0, this.length - start);
34 } 38 }
35 List<E> result = new List<E>(length); 39 List<E> result = new List<E>(length);
36 for (int i = 0; i < length; i++) { 40 for (int i = 0; i < length; i++) {
37 result[i] = this[start + i]; 41 result[i] = this[start + i];
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 */ 262 */
259 class CodeUnits extends UnmodifiableListBase<int> { 263 class CodeUnits extends UnmodifiableListBase<int> {
260 /** The string that this is the code units of. */ 264 /** The string that this is the code units of. */
261 String _string; 265 String _string;
262 266
263 CodeUnits(this._string); 267 CodeUnits(this._string);
264 268
265 int get length => _string.length; 269 int get length => _string.length;
266 int operator[](int i) => _string.codeUnitAt(i); 270 int operator[](int i) => _string.codeUnitAt(i);
267 } 271 }
272
273 class _ListIndicesIterable extends ListIterable<int> {
274 List _backedList;
275
276 _ListIndicesIterable(this._backedList);
277
278 int get length => _backedList.length;
279 int elementAt(int index) {
280 if (index < 0 || index >= length) throw new RangeError(index);
281 return index;
282 }
283 }
284
285 class ListMapView<E> implements Map<int, E> {
286 List<E> _values;
287
288 ListMapView(this._values);
289
290 E operator[] (int key) => containsKey(key) ? _values[key] : null;
291 int get length => _values.length;
292
293 Iterable<E> get values => new SubListIterable<E>(_values, 0, null);
294 Iterable<int> get keys => new _ListIndicesIterable(_values);
295
296 bool get isEmpty => _values.isEmpty;
297 bool containsValue(E value) => _values.contains(value);
298 bool containsKey(int key) => key is int && key >= 0 && key < length;
299
300 void forEach(void f(int key, E value)) {
301 int length = _values.length;
302 for (int i = 0; i < length; i++) {
303 f(i, _values[i]);
304 if (length != _values.length) {
305 throw new ConcurrentModificationError(_values);
306 }
307 }
308 }
309
310 void operator[]= (int key, E value) {
311 throw new UnsupportedError("Cannot modify an unmodifiable map");
312 }
313
314 E putIfAbsent(int key, E ifAbsent()) {
315 throw new UnsupportedError("Cannot modify an unmodifiable map");
316 }
317
318 E remove(int key) {
319 throw new UnsupportedError("Cannot modify an unmodifiable map");
320 }
321
322 void clear() {
323 throw new UnsupportedError("Cannot modify an unmodifiable map");
324 }
325 }
OLDNEW
« no previous file with comments | « samples/swarm/swarm_ui_lib/observable/observable.dart ('k') | sdk/lib/_internal/compiler/implementation/lib/js_array.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698