| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool containsValue(Object value) { | 63 bool containsValue(Object value) { |
| 64 for (K key in keys) { | 64 for (K key in keys) { |
| 65 if (this[key] == value) return true; | 65 if (this[key] == value) return true; |
| 66 } | 66 } |
| 67 return false; | 67 return false; |
| 68 } | 68 } |
| 69 | 69 |
| 70 V putIfAbsent(K key, V ifAbsent()) { | 70 V putIfAbsent(K key, V ifAbsent()) { |
| 71 if (keys.contains(key)) { | 71 if (containsKey(key)) { |
| 72 return this[key]; | 72 return this[key]; |
| 73 } | 73 } |
| 74 return this[key] = ifAbsent(); | 74 return this[key] = ifAbsent(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool containsKey(Object key) => keys.contains(key); | 77 bool containsKey(Object key) => keys.contains(key); |
| 78 int get length => keys.length; | 78 int get length => keys.length; |
| 79 bool get isEmpty => keys.isEmpty; | 79 bool get isEmpty => keys.isEmpty; |
| 80 bool get isNotEmpty => keys.isNotEmpty; | 80 bool get isNotEmpty => keys.isNotEmpty; |
| 81 Iterable<V> get values => new _MapBaseValueIterable<V>(this); | 81 Iterable<V> get values => new _MapBaseValueIterable<K, V>(this); |
| 82 String toString() => Maps.mapToString(this); | 82 String toString() => Maps.mapToString(this); |
| 83 } | 83 } |
| 84 | 84 |
| 85 /** | 85 /** |
| 86 * Basic implementation of an unmodifiable [Map]. | 86 * Basic implementation of an unmodifiable [Map]. |
| 87 * | 87 * |
| 88 * This class has a basic implementation of all but two of the members of | 88 * This class has a basic implementation of all but two of the members of |
| 89 * an umodifiable [Map]. | 89 * an umodifiable [Map]. |
| 90 * A simple unmodifiable `Map` class can be implemented by extending this | 90 * A simple unmodifiable `Map` class can be implemented by extending this |
| 91 * class and implementing `keys` and `operator[]`. | 91 * class and implementing `keys` and `operator[]`. |
| (...skipping 12 matching lines...) Expand all 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<K, V> extends Iterable<V> |
| 115 implements EfficientLength { | 115 implements EfficientLength { |
| 116 final Map _map; | 116 final Map<K, V> _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]; |
| 125 | 125 |
| 126 Iterator<V> get iterator => new _MapBaseValueIterator<V>(_map); | 126 Iterator<V> get iterator => new _MapBaseValueIterator<K, V>(_map); |
| 127 } | 127 } |
| 128 | 128 |
| 129 /** | 129 /** |
| 130 * Iterator created by [_MapBaseValueIterable]. | 130 * Iterator created by [_MapBaseValueIterable]. |
| 131 * | 131 * |
| 132 * Iterates over the values of a map by iterating its keys and lookup up the | 132 * Iterates over the values of a map by iterating its keys and lookup up the |
| 133 * values. | 133 * values. |
| 134 */ | 134 */ |
| 135 class _MapBaseValueIterator<V> implements Iterator<V> { | 135 class _MapBaseValueIterator<K, V> implements Iterator<V> { |
| 136 final Iterator _keys; | 136 final Iterator<K> _keys; |
| 137 final Map _map; | 137 final Map<K, V> _map; |
| 138 V _current = null; | 138 V _current = null; |
| 139 | 139 |
| 140 _MapBaseValueIterator(Map map) : _map = map, _keys = map.keys.iterator; | 140 _MapBaseValueIterator(Map<K, V> map) |
| 141 : _map = map, |
| 142 _keys = map.keys.iterator; |
| 141 | 143 |
| 142 bool moveNext() { | 144 bool moveNext() { |
| 143 if (_keys.moveNext()) { | 145 if (_keys.moveNext()) { |
| 144 _current = _map[_keys.current]; | 146 _current = _map[_keys.current]; |
| 145 return true; | 147 return true; |
| 146 } | 148 } |
| 147 _current = null; | 149 _current = null; |
| 148 return false; | 150 return false; |
| 149 } | 151 } |
| 150 | 152 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 class UnmodifiableMapView<K, V> = | 213 class UnmodifiableMapView<K, V> = |
| 212 MapView<K, V> with _UnmodifiableMapMixin<K, V>; | 214 MapView<K, V> with _UnmodifiableMapMixin<K, V>; |
| 213 | 215 |
| 214 /** | 216 /** |
| 215 * Helper class which implements complex [Map] operations | 217 * Helper class which implements complex [Map] operations |
| 216 * in term of basic ones ([Map.keys], [Map.operator []], | 218 * in term of basic ones ([Map.keys], [Map.operator []], |
| 217 * [Map.operator []=] and [Map.remove].) Not all methods are | 219 * [Map.operator []=] and [Map.remove].) Not all methods are |
| 218 * necessary to implement each particular operation. | 220 * necessary to implement each particular operation. |
| 219 */ | 221 */ |
| 220 class Maps { | 222 class Maps { |
| 221 static bool containsValue(Map map, value) { | 223 static bool containsValue(Map map, Object value) { |
| 222 for (final v in map.values) { | 224 for (final v in map.values) { |
| 223 if (value == v) { | 225 if (v == value) { |
| 224 return true; | 226 return true; |
| 225 } | 227 } |
| 226 } | 228 } |
| 227 return false; | 229 return false; |
| 228 } | 230 } |
| 229 | 231 |
| 230 static bool containsKey(Map map, key) { | 232 static bool containsKey(Map map, Object key) { |
| 231 for (final k in map.keys) { | 233 for (final k in map.keys) { |
| 232 if (key == k) { | 234 if (k == key) { |
| 233 return true; | 235 return true; |
| 234 } | 236 } |
| 235 } | 237 } |
| 236 return false; | 238 return false; |
| 237 } | 239 } |
| 238 | 240 |
| 239 static putIfAbsent(Map map, key, ifAbsent()) { | 241 static putIfAbsent(Map map, key, ifAbsent()) { |
| 240 if (map.containsKey(key)) { | 242 if (map.containsKey(key)) { |
| 241 return map[key]; | 243 return map[key]; |
| 242 } | 244 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 * or maps, the contained reference is rendered as [:'{...}':]. This | 280 * or maps, the contained reference is rendered as [:'{...}':]. This |
| 279 * prevents the infinite regress that would otherwise occur. So, for example, | 281 * 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' | 282 * calling this method on a map whose sole entry maps the string key 'me' |
| 281 * to a reference to the map would return [:'{me: {...}}':]. | 283 * to a reference to the map would return [:'{me: {...}}':]. |
| 282 * | 284 * |
| 283 * A typical implementation of a map's [toString] method will | 285 * A typical implementation of a map's [toString] method will |
| 284 * simply return the results of this method applied to the collection. | 286 * simply return the results of this method applied to the collection. |
| 285 */ | 287 */ |
| 286 static String mapToString(Map m) { | 288 static String mapToString(Map m) { |
| 287 // Reuse the list in IterableBase for detecting toString cycles. | 289 // Reuse the list in IterableBase for detecting toString cycles. |
| 288 if (IterableBase._isToStringVisiting(m)) { return '{...}'; } | 290 if (_isToStringVisiting(m)) { return '{...}'; } |
| 289 | 291 |
| 290 var result = new StringBuffer(); | 292 var result = new StringBuffer(); |
| 291 try { | 293 try { |
| 292 IterableBase._toStringVisiting.add(m); | 294 _toStringVisiting.add(m); |
| 293 result.write('{'); | 295 result.write('{'); |
| 294 bool first = true; | 296 bool first = true; |
| 295 m.forEach((k, v) { | 297 m.forEach((k, v) { |
| 296 if(!first) { | 298 if(!first) { |
| 297 result.write(', '); | 299 result.write(', '); |
| 298 } | 300 } |
| 299 first = false; | 301 first = false; |
| 300 result.write(k); | 302 result.write(k); |
| 301 result.write(': '); | 303 result.write(': '); |
| 302 result.write(v); | 304 result.write(v); |
| 303 }); | 305 }); |
| 304 result.write('}'); | 306 result.write('}'); |
| 305 } finally { | 307 } finally { |
| 306 assert(identical(IterableBase._toStringVisiting.last, m)); | 308 assert(identical(_toStringVisiting.last, m)); |
| 307 IterableBase._toStringVisiting.removeLast(); | 309 _toStringVisiting.removeLast(); |
| 308 } | 310 } |
| 309 | 311 |
| 310 return result.toString(); | 312 return result.toString(); |
| 311 } | 313 } |
| 312 | 314 |
| 313 static _id(x) => x; | 315 static _id(x) => x; |
| 314 | 316 |
| 315 /** | 317 /** |
| 316 * Fills a map with key/value pairs computed from [iterable]. | 318 * Fills a map with key/value pairs computed from [iterable]. |
| 317 * | 319 * |
| (...skipping 26 matching lines...) Expand all Loading... |
| 344 map[keyIterator.current] = valueIterator.current; | 346 map[keyIterator.current] = valueIterator.current; |
| 345 hasNextKey = keyIterator.moveNext(); | 347 hasNextKey = keyIterator.moveNext(); |
| 346 hasNextValue = valueIterator.moveNext(); | 348 hasNextValue = valueIterator.moveNext(); |
| 347 } | 349 } |
| 348 | 350 |
| 349 if (hasNextKey || hasNextValue) { | 351 if (hasNextKey || hasNextValue) { |
| 350 throw new ArgumentError("Iterables do not have same length."); | 352 throw new ArgumentError("Iterables do not have same length."); |
| 351 } | 353 } |
| 352 } | 354 } |
| 353 } | 355 } |
| OLD | NEW |