OLD | NEW |
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 | 4 |
5 part of dart.collection; | 5 part of dart.collection; |
6 | 6 |
7 /** | 7 /** |
8 * Base class for implementing a [Map]. | 8 * Base class for implementing a [Map]. |
9 * | 9 * |
10 * This class has a basic implementation of all but five of the members of | 10 * This class has a basic implementation of all but five of the members of |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 abstract class UnmodifiableMapBase<K, V> = | 104 abstract class UnmodifiableMapBase<K, V> = |
105 MapBase<K, V> with _UnmodifiableMapMixin<K, V>; | 105 MapBase<K, V> with _UnmodifiableMapMixin<K, V>; |
106 | 106 |
107 /** | 107 /** |
108 * Implementation of [Map.values] based on the map and its [Map.keys] iterable. | 108 * Implementation of [Map.values] based on the map and its [Map.keys] iterable. |
109 * | 109 * |
110 * Iterable that iterates over the values of a `Map`. | 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 | 111 * It accesses the values by iterating over the keys of the map, and using the |
112 * map's `operator[]` to lookup the keys. | 112 * map's `operator[]` to lookup the keys. |
113 */ | 113 */ |
114 class _MapBaseValueIterable<V> extends IterableBase<V> | 114 class _MapBaseValueIterable<V> extends Iterable<V> |
115 implements EfficientLength { | 115 implements EfficientLength { |
116 final Map _map; | 116 final Map _map; |
117 _MapBaseValueIterable(this._map); | 117 _MapBaseValueIterable(this._map); |
118 | 118 |
119 int get length => _map.length; | 119 int get length => _map.length; |
120 bool get isEmpty => _map.isEmpty; | 120 bool get isEmpty => _map.isEmpty; |
121 bool get isNotEmpty => _map.isNotEmpty; | 121 bool get isNotEmpty => _map.isNotEmpty; |
122 V get first => _map[_map.keys.first]; | 122 V get first => _map[_map.keys.first]; |
123 V get single => _map[_map.keys.single]; | 123 V get single => _map[_map.keys.single]; |
124 V get last => _map[_map.keys.last]; | 124 V get last => _map[_map.keys.last]; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 * or maps, the contained reference is rendered as [:'{...}':]. This | 278 * or maps, the contained reference is rendered as [:'{...}':]. This |
279 * prevents the infinite regress that would otherwise occur. So, for example, | 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' | 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: {...}}':]. | 281 * to a reference to the map would return [:'{me: {...}}':]. |
282 * | 282 * |
283 * A typical implementation of a map's [toString] method will | 283 * A typical implementation of a map's [toString] method will |
284 * simply return the results of this method applied to the collection. | 284 * simply return the results of this method applied to the collection. |
285 */ | 285 */ |
286 static String mapToString(Map m) { | 286 static String mapToString(Map m) { |
287 // Reuse the list in IterableBase for detecting toString cycles. | 287 // Reuse the list in IterableBase for detecting toString cycles. |
288 if (IterableBase._isToStringVisiting(m)) { return '{...}'; } | 288 if (_isToStringVisiting(m)) { return '{...}'; } |
289 | 289 |
290 var result = new StringBuffer(); | 290 var result = new StringBuffer(); |
291 try { | 291 try { |
292 IterableBase._toStringVisiting.add(m); | 292 _toStringVisiting.add(m); |
293 result.write('{'); | 293 result.write('{'); |
294 bool first = true; | 294 bool first = true; |
295 m.forEach((k, v) { | 295 m.forEach((k, v) { |
296 if(!first) { | 296 if(!first) { |
297 result.write(', '); | 297 result.write(', '); |
298 } | 298 } |
299 first = false; | 299 first = false; |
300 result.write(k); | 300 result.write(k); |
301 result.write(': '); | 301 result.write(': '); |
302 result.write(v); | 302 result.write(v); |
303 }); | 303 }); |
304 result.write('}'); | 304 result.write('}'); |
305 } finally { | 305 } finally { |
306 assert(identical(IterableBase._toStringVisiting.last, m)); | 306 assert(identical(_toStringVisiting.last, m)); |
307 IterableBase._toStringVisiting.removeLast(); | 307 _toStringVisiting.removeLast(); |
308 } | 308 } |
309 | 309 |
310 return result.toString(); | 310 return result.toString(); |
311 } | 311 } |
312 | 312 |
313 static _id(x) => x; | 313 static _id(x) => x; |
314 | 314 |
315 /** | 315 /** |
316 * Fills a map with key/value pairs computed from [iterable]. | 316 * Fills a map with key/value pairs computed from [iterable]. |
317 * | 317 * |
(...skipping 26 matching lines...) Expand all Loading... |
344 map[keyIterator.current] = valueIterator.current; | 344 map[keyIterator.current] = valueIterator.current; |
345 hasNextKey = keyIterator.moveNext(); | 345 hasNextKey = keyIterator.moveNext(); |
346 hasNextValue = valueIterator.moveNext(); | 346 hasNextValue = valueIterator.moveNext(); |
347 } | 347 } |
348 | 348 |
349 if (hasNextKey || hasNextValue) { | 349 if (hasNextKey || hasNextValue) { |
350 throw new ArgumentError("Iterables do not have same length."); | 350 throw new ArgumentError("Iterables do not have same length."); |
351 } | 351 } |
352 } | 352 } |
353 } | 353 } |
OLD | NEW |