| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 if (containsKey(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 Iterable<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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |