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

Side by Side Diff: test/generated_sdk/lib/collection/maps.dart

Issue 1162723007: remove generated_sdk from checked in code (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart.collection;
6
7 /**
8 * Base class for implementing a [Map].
9 *
10 * This class has a basic implementation of all but five of the members of
11 * [Map].
12 * A basic `Map` class can be implemented by extending this class and
13 * implementing `keys`, `operator[]`, `operator[]=`, `remove` and `clear`.
14 * The remaining operations are implemented in terms of these five.
15 *
16 * The `keys` iterable should have efficient [length] and [contains]
17 * operations, and it should catch concurrent modifications of the keys
18 * while iterating.
19 *
20 * A more efficient implementation is usually possible by overriding
21 * some of the other members as well.
22 */
23 abstract class MapBase<K, V> = Object with MapMixin<K, V>;
24
25
26 /**
27 * Mixin implementing a [Map].
28 *
29 * This mixin has a basic implementation of all but five of the members of
30 * [Map].
31 * A basic `Map` class can be implemented by mixin in this class and
32 * implementing `keys`, `operator[]`, `operator[]=`, `remove` and `clear`.
33 * The remaining operations are implemented in terms of these five.
34 *
35 * The `keys` iterable should have efficient [length] and [contains]
36 * operations, and it should catch concurrent modifications of the keys
37 * while iterating.
38 *
39 * A more efficient implementation is usually possible by overriding
40 * some of the other members as well.
41 */
42 abstract class MapMixin<K, V> implements Map<K, V> {
43 Iterable<K> get keys;
44 V operator[](Object key);
45 operator []=(K key, V value);
46 V remove(Object key);
47 // The `clear` operation should not be based on `remove`.
48 // It should clear the map even if some keys are not equal to themselves.
49 void clear();
50
51 void forEach(void action(K key, V value)) {
52 for (K key in keys) {
53 action(key, this[key]);
54 }
55 }
56
57 void addAll(Map<K, V> other) {
58 for (K key in other.keys) {
59 this[key] = other[key];
60 }
61 }
62
63 bool containsValue(Object value) {
64 for (K key in keys) {
65 if (this[key] == value) return true;
66 }
67 return false;
68 }
69
70 V putIfAbsent(K key, V ifAbsent()) {
71 if (keys.contains(key)) {
72 return this[key];
73 }
74 return this[key] = ifAbsent();
75 }
76
77 bool containsKey(Object key) => keys.contains(key);
78 int get length => keys.length;
79 bool get isEmpty => keys.isEmpty;
80 bool get isNotEmpty => keys.isNotEmpty;
81 Iterable<V> get values => new _MapBaseValueIterable<V>(this);
82 String toString() => Maps.mapToString(this);
83 }
84
85 /**
86 * Basic implementation of an unmodifiable [Map].
87 *
88 * This class has a basic implementation of all but two of the members of
89 * an umodifiable [Map].
90 * A simple unmodifiable `Map` class can be implemented by extending this
91 * class and implementing `keys` and `operator[]`.
92 *
93 * Modifying operations throw when used.
94 * The remaining non-modifying operations are implemented in terms of `keys`
95 * and `operator[]`.
96 *
97 * The `keys` iterable should have efficient [length] and [contains]
98 * operations, and it should catch concurrent modifications of the keys
99 * while iterating.
100 *
101 * A more efficient implementation is usually possible by overriding
102 * some of the other members as well.
103 */
104 abstract class UnmodifiableMapBase<K, V> =
105 MapBase<K, V> with _UnmodifiableMapMixin<K, V>;
106
107 /**
108 * Implementation of [Map.values] based on the map and its [Map.keys] iterable.
109 *
110 * Iterable that iterates over the values of a `Map`.
111 * It accesses the values by iterating over the keys of the map, and using the
112 * map's `operator[]` to lookup the keys.
113 */
114 class _MapBaseValueIterable<V> extends IterableBase<V>
115 implements EfficientLength {
116 final Map _map;
117 _MapBaseValueIterable(this._map);
118
119 int get length => _map.length;
120 bool get isEmpty => _map.isEmpty;
121 bool get isNotEmpty => _map.isNotEmpty;
122 V get first => _map[_map.keys.first];
123 V get single => _map[_map.keys.single];
124 V get last => _map[_map.keys.last];
125
126 Iterator<V> get iterator => new _MapBaseValueIterator<V>(_map);
127 }
128
129 /**
130 * Iterator created by [_MapBaseValueIterable].
131 *
132 * Iterates over the values of a map by iterating its keys and lookup up the
133 * values.
134 */
135 class _MapBaseValueIterator<V> implements Iterator<V> {
136 final Iterator _keys;
137 final Map _map;
138 V _current = null;
139
140 _MapBaseValueIterator(Map map) : _map = map, _keys = map.keys.iterator;
141
142 bool moveNext() {
143 if (_keys.moveNext()) {
144 _current = _map[_keys.current];
145 return true;
146 }
147 _current = null;
148 return false;
149 }
150
151 V get current => _current;
152 }
153
154 /**
155 * Mixin that overrides mutating map operations with implementations that throw.
156 */
157 abstract class _UnmodifiableMapMixin<K, V> implements Map<K, V> {
158 void operator[]=(K key, V value) {
159 throw new UnsupportedError("Cannot modify unmodifiable map");
160 }
161 void addAll(Map<K, V> other) {
162 throw new UnsupportedError("Cannot modify unmodifiable map");
163 }
164 void clear() {
165 throw new UnsupportedError("Cannot modify unmodifiable map");
166 }
167 V remove(Object key) {
168 throw new UnsupportedError("Cannot modify unmodifiable map");
169 }
170 V putIfAbsent(K key, V ifAbsent()) {
171 throw new UnsupportedError("Cannot modify unmodifiable map");
172 }
173 }
174
175 /**
176 * Wrapper around a class that implements [Map] that only exposes `Map` members.
177 *
178 * A simple wrapper that delegates all `Map` members to the map provided in the
179 * constructor.
180 *
181 * Base for delegating map implementations like [UnmodifiableMapView].
182 */
183 class MapView<K, V> implements Map<K, V> {
184 final Map<K, V> _map;
185 const MapView(Map<K, V> map) : _map = map;
186
187 V operator[](Object key) => _map[key];
188 void operator[]=(K key, V value) { _map[key] = value; }
189 void addAll(Map<K, V> other) { _map.addAll(other); }
190 void clear() { _map.clear(); }
191 V putIfAbsent(K key, V ifAbsent()) => _map.putIfAbsent(key, ifAbsent);
192 bool containsKey(Object key) => _map.containsKey(key);
193 bool containsValue(Object value) => _map.containsValue(value);
194 void forEach(void action(K key, V value)) { _map.forEach(action); }
195 bool get isEmpty => _map.isEmpty;
196 bool get isNotEmpty => _map.isNotEmpty;
197 int get length => _map.length;
198 Iterable<K> get keys => _map.keys;
199 V remove(Object key) => _map.remove(key);
200 String toString() => _map.toString();
201 Iterable<V> get values => _map.values;
202 }
203
204 /**
205 * View of a [Map] that disallow modifying the map.
206 *
207 * A wrapper around a `Map` that forwards all members to the map provided in
208 * the constructor, except for operations that modify the map.
209 * Modifying operations throw instead.
210 */
211 class UnmodifiableMapView<K, V> =
212 MapView<K, V> with _UnmodifiableMapMixin<K, V>;
213
214 /**
215 * Helper class which implements complex [Map] operations
216 * in term of basic ones ([Map.keys], [Map.operator []],
217 * [Map.operator []=] and [Map.remove].) Not all methods are
218 * necessary to implement each particular operation.
219 */
220 class Maps {
221 static bool containsValue(Map map, value) {
222 for (final v in map.values) {
223 if (value == v) {
224 return true;
225 }
226 }
227 return false;
228 }
229
230 static bool containsKey(Map map, key) {
231 for (final k in map.keys) {
232 if (key == k) {
233 return true;
234 }
235 }
236 return false;
237 }
238
239 static putIfAbsent(Map map, key, ifAbsent()) {
240 if (map.containsKey(key)) {
241 return map[key];
242 }
243 final v = ifAbsent();
244 map[key] = v;
245 return v;
246 }
247
248 static clear(Map map) {
249 for (final k in map.keys.toList()) {
250 map.remove(k);
251 }
252 }
253
254 static forEach(Map map, void f(key, value)) {
255 for (final k in map.keys) {
256 f(k, map[k]);
257 }
258 }
259
260 static Iterable getValues(Map map) {
261 return map.keys.map((key) => map[key]);
262 }
263
264 static int length(Map map) => map.keys.length;
265
266 static bool isEmpty(Map map) => map.keys.isEmpty;
267
268 static bool isNotEmpty(Map map) => map.keys.isNotEmpty;
269
270 /**
271 * Returns a string representing the specified map. The returned string
272 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':].
273 * The value returned by its [toString] method is used to represent each
274 * key or value.
275 *
276 * If the map collection contains a reference to itself, either
277 * directly as a key or value, or indirectly through other collections
278 * or maps, the contained reference is rendered as [:'{...}':]. This
279 * prevents the infinite regress that would otherwise occur. So, for example,
280 * calling this method on a map whose sole entry maps the string key 'me'
281 * to a reference to the map would return [:'{me: {...}}':].
282 *
283 * A typical implementation of a map's [toString] method will
284 * simply return the results of this method applied to the collection.
285 */
286 static String mapToString(Map m) {
287 // Reuse the list in IterableBase for detecting toString cycles.
288 if (IterableBase._isToStringVisiting(m)) { return '{...}'; }
289
290 var result = new StringBuffer();
291 try {
292 IterableBase._toStringVisiting.add(m);
293 result.write('{');
294 bool first = true;
295 m.forEach((k, v) {
296 if(!first) {
297 result.write(', ');
298 }
299 first = false;
300 result.write(k);
301 result.write(': ');
302 result.write(v);
303 });
304 result.write('}');
305 } finally {
306 assert(identical(IterableBase._toStringVisiting.last, m));
307 IterableBase._toStringVisiting.removeLast();
308 }
309
310 return result.toString();
311 }
312
313 static _id(x) => x;
314
315 /**
316 * Fills a map with key/value pairs computed from [iterable].
317 *
318 * This method is used by Map classes in the named constructor fromIterable.
319 */
320 static void _fillMapWithMappedIterable(Map map, Iterable iterable,
321 key(element), value(element)) {
322 if (key == null) key = _id;
323 if (value == null) value = _id;
324
325 for (var element in iterable) {
326 map[key(element)] = value(element);
327 }
328 }
329
330 /**
331 * Fills a map by associating the [keys] to [values].
332 *
333 * This method is used by Map classes in the named constructor fromIterables.
334 */
335 static void _fillMapWithIterables(Map map, Iterable keys,
336 Iterable values) {
337 Iterator keyIterator = keys.iterator;
338 Iterator valueIterator = values.iterator;
339
340 bool hasNextKey = keyIterator.moveNext();
341 bool hasNextValue = valueIterator.moveNext();
342
343 while (hasNextKey && hasNextValue) {
344 map[keyIterator.current] = valueIterator.current;
345 hasNextKey = keyIterator.moveNext();
346 hasNextValue = valueIterator.moveNext();
347 }
348
349 if (hasNextKey || hasNextValue) {
350 throw new ArgumentError("Iterables do not have same length.");
351 }
352 }
353 }
OLDNEW
« no previous file with comments | « test/generated_sdk/lib/collection/list.dart ('k') | test/generated_sdk/lib/collection/queue.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698