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

Side by Side Diff: sdk/lib/collection/splay_tree.dart

Issue 18072004: Add fromIterable(s) constructors in SplayTreeMap and LinkedHashMap. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 * Keys of the map are compared using the `compare` function passed in 238 * Keys of the map are compared using the `compare` function passed in
239 * the constructor. If that is omitted, the objects are assumed to be 239 * the constructor. If that is omitted, the objects are assumed to be
240 * [Comparable], and are compared using their [Comparable.compareTo] 240 * [Comparable], and are compared using their [Comparable.compareTo]
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 static _id(x) => x;
249
248 SplayTreeMap([int compare(K key1, K key2)]) 250 SplayTreeMap([int compare(K key1, K key2)])
249 : _comparator = (compare == null) ? Comparable.compare : compare; 251 : _comparator = (compare == null) ? Comparable.compare : compare;
250 252
251 factory SplayTreeMap.from(Map<K, V> other, [int compare(K key1, K key2)]) => 253 factory SplayTreeMap.from(Map<K, V> other, [int compare(K key1, K key2)]) =>
252 new SplayTreeMap(compare)..addAll(other); 254 new SplayTreeMap(compare)..addAll(other);
253 255
256 factory SplayTreeMap.fromIterable(Iterable iterable,
257 {int compare(K key1, K key2), K key(element), V value(element)}) {
floitsch 2013/06/27 15:21:38 move compare last. It doesn't make any semantic di
zarah 2013/06/27 15:58:52 Done.
258 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare);
259
260 if (key == null) key = _id;
261 if (value == null) value = _id;
262
263 for (var element in iterable) {
264 map[key(element)] = value(element);
265 }
266
267 return map;
268 }
269
270 factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values,
271 [int compare(K key1, K key2)]) {
272 SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare);
273
274 Iterator<K> keyIterator = keys.iterator;
275 Iterator<V> valueIterator = values.iterator;
276
277 bool hasNextKey = keyIterator.moveNext();
278 bool hasNextValue = valueIterator.moveNext();
279
280 while (hasNextKey && hasNextValue) {
281 map[keyIterator.current] = valueIterator.current;
282 hasNextKey = keyIterator.moveNext();
283 hasNextValue = valueIterator.moveNext();
284 }
285
286 if (hasNextKey || hasNextValue) {
287 throw new ArgumentError("Iterables do not have same length.");
288 }
289
290 return map;
291 }
292
254 int _compare(K key1, K key2) => _comparator(key1, key2); 293 int _compare(K key1, K key2) => _comparator(key1, key2);
255 294
256 SplayTreeMap._internal(); 295 SplayTreeMap._internal();
257 296
258 V operator [](Object key) { 297 V operator [](Object key) {
259 if (key == null) throw new ArgumentError(key); 298 if (key == null) throw new ArgumentError(key);
260 if (key is! K) return null; 299 if (key is! K) return null;
261 if (_root != null) { 300 if (_root != null) {
262 int comp = _splay(key); 301 int comp = _splay(key);
263 if (comp == 0) { 302 if (comp == 0) {
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<V> { 576 class _SplayTreeValueIterator<K, V> extends _SplayTreeIterator<V> {
538 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map); 577 _SplayTreeValueIterator(SplayTreeMap<K, V> map): super(map);
539 V _getValue(_SplayTreeMapNode node) => node.value; 578 V _getValue(_SplayTreeMapNode node) => node.value;
540 } 579 }
541 580
542 class _SplayTreeNodeIterator<K> 581 class _SplayTreeNodeIterator<K>
543 extends _SplayTreeIterator<_SplayTreeNode<K>> { 582 extends _SplayTreeIterator<_SplayTreeNode<K>> {
544 _SplayTreeNodeIterator(_SplayTree<K> map): super(map); 583 _SplayTreeNodeIterator(_SplayTree<K> map): super(map);
545 _SplayTreeNode<K> _getValue(_SplayTreeNode node) => node; 584 _SplayTreeNode<K> _getValue(_SplayTreeNode node) => node;
546 } 585 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698