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

Unified 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, 10 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_collection_dev/list.dart
diff --git a/sdk/lib/_collection_dev/list.dart b/sdk/lib/_collection_dev/list.dart
index 9384627d9b97c62565b3dca4adfc9ab6ec1b8f95..9d845e08293e004a248a3cd4e447b6f3c005b764 100644
--- a/sdk/lib/_collection_dev/list.dart
+++ b/sdk/lib/_collection_dev/list.dart
@@ -24,6 +24,10 @@ abstract class ListBase<E> extends ListIterable<E> implements List<E> {
E elementAt(int index) {
return this[index];
}
+
+ Map<int, E> asMap() {
+ return new ListMapView(this);
+ }
}
/**
@@ -244,3 +248,57 @@ class CodeUnits extends UnmodifiableListBase<int> {
int get length => _string.length;
int operator[](int i) => _string.codeUnitAt(i);
}
+
+class _ListIndexIterable extends ListIterable<int> {
+ List _backedList;
+
+ _ListIndexIterable(this._backedList);
+
+ int get length => _backedList.length;
+ int elementAt(int index) {
+ if (index < 0 || index >= length) throw new RangeError(index);
+ return index;
+ }
+}
+
+class ListMapView<E> implements Map<int, E> {
+ List<E> _values;
+
+ ListMapView(this._values);
+
+ E operator[] (int key) => containsKey(key) ? _values[key] : null;
+ int get length => _values.length;
+
+ Iterable<E> get values => new SubListIterable<E>(_values, 0, null);
+ 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.
+
+ bool get isEmpty => _values.isEmpty;
+ bool containsValue(E value) => _values.contains(value);
+ 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
+
+ void forEach(void f(int key, E value)) {
+ int length = _values.length;
+ for (int i = 0; i < length; i++) {
+ f(i, _values[i]);
+ if (length != _values.length) {
+ throw new ConcurrentModificationError(_values);
+ }
+ }
+ }
+
+ void operator[]= (int key, E value) {
+ throw new UnsupportedError("Cannot modify an unmodifiable map");
+ }
+
+ E putIfAbsent(int key, E ifAbsent()) {
+ throw new UnsupportedError("Cannot modify an unmodifiable map");
+ }
+
+ E remove(int key) {
+ throw new UnsupportedError("Cannot modify an unmodifiable map");
+ }
+
+ void clear() {
+ throw new UnsupportedError("Cannot modify an unmodifiable map");
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698