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

Side by Side Diff: runtime/lib/immutable_map.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | runtime/lib/integers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // Immutable map class for compiler generated map literals. 4 // Immutable map class for compiler generated map literals.
5 5
6 class ImmutableMap<K, V> implements Map<K, V> { 6 class ImmutableMap<K, V> implements Map<K, V> {
7 final _ImmutableArray kvPairs_; 7 final _ImmutableArray kvPairs_;
8 8
9 const ImmutableMap._create(_ImmutableArray keyValuePairs) 9 const ImmutableMap._create(_ImmutableArray keyValuePairs)
10 : kvPairs_ = keyValuePairs; 10 : kvPairs_ = keyValuePairs;
(...skipping 17 matching lines...) Expand all
28 int get length { 28 int get length {
29 return kvPairs_.length ~/ 2; 29 return kvPairs_.length ~/ 2;
30 } 30 }
31 31
32 void forEach(void f(K key, V value)) { 32 void forEach(void f(K key, V value)) {
33 for (int i = 0; i < kvPairs_.length; i += 2) { 33 for (int i = 0; i < kvPairs_.length; i += 2) {
34 f(kvPairs_[i], kvPairs_[i+1]); 34 f(kvPairs_[i], kvPairs_[i+1]);
35 } 35 }
36 } 36 }
37 37
38 Collection<K> get keys { 38 Iterable<K> get keys {
39 int numKeys = length; 39 return new _ImmutableMapKeyIterable<K>(this);
40 List<K> list = new List<K>(numKeys);
41 for (int i = 0; i < numKeys; i++) {
42 list[i] = kvPairs_[i*2];
43 }
44 return list;
45 } 40 }
46 41
47 Collection<V> get values { 42 Iterable<V> get values {
48 int numValues = length; 43 return new _ImmutableMapValueIterable<V>(this);
49 List<V> list = new List<V>(numValues);
50 for (int i = 0; i < numValues; i++) {
51 list[i] = kvPairs_[i*2 + 1];
52 }
53 return list;
54 } 44 }
55 45
56 bool containsKey(K key) { 46 bool containsKey(K key) {
57 for (int i = 0; i < kvPairs_.length; i += 2) { 47 for (int i = 0; i < kvPairs_.length; i += 2) {
58 if (key == kvPairs_[i]) { 48 if (key == kvPairs_[i]) {
59 return true; 49 return true;
60 } 50 }
61 } 51 }
62 return false; 52 return false;
63 } 53 }
(...skipping 21 matching lines...) Expand all
85 75
86 V remove(K key) { 76 V remove(K key) {
87 throw new UnsupportedError("Cannot remove from unmodifiable Map"); 77 throw new UnsupportedError("Cannot remove from unmodifiable Map");
88 } 78 }
89 79
90 String toString() { 80 String toString() {
91 return Maps.mapToString(this); 81 return Maps.mapToString(this);
92 } 82 }
93 } 83 }
94 84
85 class _ImmutableMapKeyIterable<E> extends Iterable<E> {
86 final ImmutableMap _map;
87 _ImmutableMapKeyIterable(this._map);
88
89 Iterator<E> get iterator {
90 return new _ImmutableMapKeyIterator<E>(_map);
91 }
92 }
93
94 class _ImmutableMapValueIterable<E> extends Iterable<E> {
95 final ImmutableMap _map;
96 _ImmutableMapValueIterable(this._map);
97
98 Iterator<E> get iterator {
99 return new _ImmutableMapValueIterator<E>(_map);
100 }
101 }
102
103 class _ImmutableMapKeyIterator<E> implements Iterator<E> {
104 ImmutableMap _map;
105 int _index = -1;
106 E _current;
107
108 _ImmutableMapKeyIterator(this._map);
109
110 bool moveNext() {
111 int newIndex = _index + 1;
112 if (newIndex < _map.length) {
113 _index = newIndex;
114 _current = _map.kvPairs_[newIndex * 2];
115 return true;
116 }
117 _current = null;
118 _index = _map.length;
119 return false;
120 }
121
122 E get current => _current;
123 }
124
125 class _ImmutableMapValueIterator<E> implements Iterator<E> {
126 ImmutableMap _map;
127 int _index = -1;
128 E _current;
129
130 _ImmutableMapValueIterator(this._map);
131
132 bool moveNext() {
133 int newIndex = _index + 1;
134 if (newIndex < _map.length) {
135 _index = newIndex;
136 _current = _map.kvPairs_[newIndex * 2 + 1];
137 return true;
138 }
139 _current = null;
140 _index = _map.length;
141 return false;
142 }
143
144 E get current => _current;
145 }
OLDNEW
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | runtime/lib/integers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698