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

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: 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
28 Map<int, E> asMap() {
29 return new ListMapView(this);
30 }
27 } 31 }
28 32
29 /** 33 /**
30 * Abstract class implementing the non-length changing operations of [List]. 34 * Abstract class implementing the non-length changing operations of [List].
31 * 35 *
32 * All modifications are performed using [[]=]. 36 * All modifications are performed using [[]=].
33 */ 37 */
34 abstract class FixedLengthListBase<E> extends ListBase<E> { 38 abstract class FixedLengthListBase<E> extends ListBase<E> {
35 void operator[]=(int index, E value); 39 void operator[]=(int index, E value);
36 40
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 */ 241 */
238 class CodeUnits extends UnmodifiableListBase<int> { 242 class CodeUnits extends UnmodifiableListBase<int> {
239 /** The string that this is the code units of. */ 243 /** The string that this is the code units of. */
240 String _string; 244 String _string;
241 245
242 CodeUnits(this._string); 246 CodeUnits(this._string);
243 247
244 int get length => _string.length; 248 int get length => _string.length;
245 int operator[](int i) => _string.codeUnitAt(i); 249 int operator[](int i) => _string.codeUnitAt(i);
246 } 250 }
251
252 class _ListIndexIterable extends ListIterable<int> {
253 List _backedList;
254
255 _ListIndexIterable(this._backedList);
256
257 int get length => _backedList.length;
258 int elementAt(int index) {
259 if (index < 0 || index >= length) throw new RangeError(index);
260 return index;
261 }
262 }
263
264 class ListMapView<E> implements Map<int, E> {
265 List<E> _values;
266
267 ListMapView(this._values);
268
269 E operator[] (int key) => containsKey(key) ? _values[key] : null;
270 int get length => _values.length;
271
272 Iterable<E> get values => new SubListIterable<E>(_values, 0, null);
273 Iterable<int> get keys => new _ListIndexIterable(_values);
Lasse Reichstein Nielsen 2013/03/04 08:28:37 _ListIndicesIterable?
floitsch 2013/03/04 16:50:20 Done.
274
275 bool get isEmpty => _values.isEmpty;
276 bool containsValue(E value) => _values.contains(value);
277 bool containsKey(int key) => key is int && key >= 0 && key < length;
Lasse Reichstein Nielsen 2013/03/04 08:28:37 Drop the "key is int".
floitsch 2013/03/04 16:50:20 The key is int is crucial. Otherwise it will retur
278
279 void forEach(void f(int key, E value)) {
280 int length = _values.length;
281 for (int i = 0; i < length; i++) {
282 f(i, _values[i]);
283 if (length != _values.length) {
284 throw new ConcurrentModificationError(_values);
285 }
286 }
287 }
288
289 void operator[]= (int key, E value) {
290 throw new UnsupportedError("Cannot modify an unmodifiable map");
291 }
292
293 E putIfAbsent(int key, E ifAbsent()) {
294 throw new UnsupportedError("Cannot modify an unmodifiable map");
295 }
296
297 E remove(int key) {
298 throw new UnsupportedError("Cannot modify an unmodifiable map");
299 }
300
301 void clear() {
302 throw new UnsupportedError("Cannot modify an unmodifiable map");
303 }
304 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698