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

Unified Diff: runtime/lib/immutable_map.dart

Issue 11664006: Make Map.keys/values Iterables. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Add TODO that map.keys should return a Set. Created 7 years, 12 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: runtime/lib/immutable_map.dart
diff --git a/runtime/lib/immutable_map.dart b/runtime/lib/immutable_map.dart
index 6594fda54aff8c4b9648b3d61c0c2aa157861286..82f54580e4490a66e346b3f206475b8a93fdb8c8 100644
--- a/runtime/lib/immutable_map.dart
+++ b/runtime/lib/immutable_map.dart
@@ -35,22 +35,12 @@ class ImmutableMap<K, V> implements Map<K, V> {
}
}
- Collection<K> get keys {
- int numKeys = length;
- List<K> list = new List<K>.fixedLength(numKeys);
- for (int i = 0; i < numKeys; i++) {
- list[i] = kvPairs_[i*2];
- }
- return list;
+ Iterable<K> get keys {
+ return new _ImmutableMapKeyIterable<K>(this);
}
- Collection<V> get values {
- int numValues = length;
- List<V> list = new List<V>.fixedLength(numValues);
- for (int i = 0; i < numValues; i++) {
- list[i] = kvPairs_[i*2 + 1];
- }
- return list;
+ Iterable<V> get values {
+ return new _ImmutableMapValueIterable<V>(this);
}
bool containsKey(K key) {
@@ -92,3 +82,64 @@ class ImmutableMap<K, V> implements Map<K, V> {
}
}
+class _ImmutableMapKeyIterable<E> extends Iterable<E> {
+ final ImmutableMap _map;
+ _ImmutableMapKeyIterable(this._map);
+
+ Iterator<E> get iterator {
+ return new _ImmutableMapKeyIterator<E>(_map);
+ }
+}
+
+class _ImmutableMapValueIterable<E> extends Iterable<E> {
+ final ImmutableMap _map;
+ _ImmutableMapValueIterable(this._map);
+
+ Iterator<E> get iterator {
+ return new _ImmutableMapValueIterator<E>(_map);
+ }
+}
+
+class _ImmutableMapKeyIterator<E> implements Iterator<E> {
+ ImmutableMap _map;
+ int _index = -1;
+ E _current;
+
+ _ImmutableMapKeyIterator(this._map);
+
+ bool moveNext() {
+ int newIndex = _index + 1;
+ if (newIndex < _map.length) {
+ _index = newIndex;
+ _current = _map.kvPairs_[newIndex * 2];
+ return true;
+ }
+ _current = null;
+ _index = _map.length;
+ return false;
+ }
+
+ E get current => _current;
+}
+
+class _ImmutableMapValueIterator<E> implements Iterator<E> {
+ ImmutableMap _map;
+ int _index = -1;
+ E _current;
+
+ _ImmutableMapValueIterator(this._map);
+
+ bool moveNext() {
+ int newIndex = _index + 1;
+ if (newIndex < _map.length) {
+ _index = newIndex;
+ _current = _map.kvPairs_[newIndex * 2 + 1];
+ return true;
+ }
+ _current = null;
+ _index = _map.length;
+ return false;
+ }
+
+ E get current => _current;
+}
« no previous file with comments | « pkg/serialization/test/serialization_test.dart ('k') | runtime/tests/vm/dart/isolate_mirror_local_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698