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 * A node in a splay tree. It holds the sorting key and the left | 8 * A node in a splay tree. It holds the sorting key and the left |
9 * and right children in the tree. | 9 * and right children in the tree. |
10 */ | 10 */ |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 * method. This also means that `null` is *not* allowed as a key. | 241 * method. This also means that `null` is *not* allowed as a key. |
242 */ | 242 */ |
243 class SplayTreeMap<K, V> extends _SplayTree<K> implements Map<K, V> { | 243 class SplayTreeMap<K, V> extends _SplayTree<K> implements Map<K, V> { |
244 // TODO(ngeoffray): Restore type when feature is implemented in dart2js | 244 // TODO(ngeoffray): Restore type when feature is implemented in dart2js |
245 // checked mode. http://dartbug.com/7733 | 245 // checked mode. http://dartbug.com/7733 |
246 Function /* Comparator<K> */_comparator; | 246 Function /* Comparator<K> */_comparator; |
247 | 247 |
248 SplayTreeMap([int compare(K key1, K key2)]) | 248 SplayTreeMap([int compare(K key1, K key2)]) |
249 : _comparator = (compare == null) ? Comparable.compare : compare; | 249 : _comparator = (compare == null) ? Comparable.compare : compare; |
250 | 250 |
| 251 /** |
| 252 * Creates a [SplayTreeMap] that contains all key value pairs of [other]. |
| 253 */ |
251 factory SplayTreeMap.from(Map<K, V> other, [int compare(K key1, K key2)]) => | 254 factory SplayTreeMap.from(Map<K, V> other, [int compare(K key1, K key2)]) => |
252 new SplayTreeMap(compare)..addAll(other); | 255 new SplayTreeMap(compare)..addAll(other); |
253 | 256 |
| 257 /** |
| 258 * Creates a [SplayTreeMap] where the keys and values are computed from the |
| 259 * [iterable]. |
| 260 * |
| 261 * For each element of the [iterable] this constructor computes a key/value |
| 262 * pair, by applying [key] and [value] respectively. |
| 263 * |
| 264 * The keys of the key/value pairs do not need to be unique. The last |
| 265 * occurrence of a key will simply overwrite any previous value. |
| 266 * |
| 267 * If no values are specified for [key] and [value] the default is the |
| 268 * identity function. |
| 269 */ |
| 270 factory SplayTreeMap.fromIterable(Iterable<K> iterable, |
| 271 {K key(element), V value(element), int compare(K key1, K key2)}) { |
| 272 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare); |
| 273 Maps._fillMapWithMappedIterable(map, iterable, key, value); |
| 274 return map; |
| 275 } |
| 276 |
| 277 /** |
| 278 * Creates a [SplayTreeMap] associating the given [keys] to [values]. |
| 279 * |
| 280 * This constructor iterates over [keys] and [values] and maps each element of |
| 281 * [keys] to the corresponding element of [values]. |
| 282 * |
| 283 * If [keys] contains the same object multiple times, the last occurrence |
| 284 * overwrites the previous value. |
| 285 * |
| 286 * It is an error if the two [Iterable]s don't have the same length. |
| 287 */ |
| 288 factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values, |
| 289 [int compare(K key1, K key2)]) { |
| 290 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare); |
| 291 Maps._fillMapWithIterables(map, keys, values); |
| 292 return map; |
| 293 } |
| 294 |
254 int _compare(K key1, K key2) => _comparator(key1, key2); | 295 int _compare(K key1, K key2) => _comparator(key1, key2); |
255 | 296 |
256 SplayTreeMap._internal(); | 297 SplayTreeMap._internal(); |
257 | 298 |
258 V operator [](Object key) { | 299 V operator [](Object key) { |
259 if (key == null) throw new ArgumentError(key); | 300 if (key == null) throw new ArgumentError(key); |
260 if (key is! K) return null; | 301 if (key is! K) return null; |
261 if (_root != null) { | 302 if (_root != null) { |
262 int comp = _splay(key); | 303 int comp = _splay(key); |
263 if (comp == 0) { | 304 if (comp == 0) { |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<V> { | 578 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<V> { |
538 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map); | 579 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map); |
539 V _getValue(_SplayTreeMapNode node) => node.value; | 580 V _getValue(_SplayTreeMapNode node) => node.value; |
540 } | 581 } |
541 | 582 |
542 class _SplayTreeNodeIterator<K> | 583 class _SplayTreeNodeIterator<K> |
543 extends _SplayTreeIterator<_SplayTreeNode<K>> { | 584 extends _SplayTreeIterator<_SplayTreeNode<K>> { |
544 _SplayTreeNodeIterator(_SplayTree<K> map): super(map); | 585 _SplayTreeNodeIterator(_SplayTree<K> map): super(map); |
545 _SplayTreeNode<K> _getValue(_SplayTreeNode node) => node; | 586 _SplayTreeNode<K> _getValue(_SplayTreeNode node) => node; |
546 } | 587 } |
OLD | NEW |