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

Side by Side Diff: sdk/lib/collection/linked_hash_map.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
« no previous file with comments | « no previous file | sdk/lib/collection/splay_tree.dart » ('j') | sdk/lib/collection/splay_tree.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 hash-table based implementation of [Map]. 8 * A hash-table based implementation of [Map].
9 * 9 *
10 * Keys insertion order is remembered, and keys are iterated in insertion order. 10 * Keys insertion order is remembered, and keys are iterated in insertion order.
11 * Values are iterated in their corresponding key's order. 11 * Values are iterated in their corresponding key's order.
12 * 12 *
13 * The keys of a `HashMap` must have consistent [Object.operator==] 13 * The keys of a `HashMap` must have consistent [Object.operator==]
14 * and [Object.hashCode] implementations. This means that the `==` operator 14 * and [Object.hashCode] implementations. This means that the `==` operator
15 * must define a stable equivalence relation on the keys (reflexive, 15 * must define a stable equivalence relation on the keys (reflexive,
16 * anti-symmetric, transitive, and consistent over time), and that `hashCode` 16 * anti-symmetric, transitive, and consistent over time), and that `hashCode`
17 * must be the same for objects that are considered equal by `==`. 17 * must be the same for objects that are considered equal by `==`.
18 * 18 *
19 * The map allows `null` as a key. 19 * The map allows `null` as a key.
20 */ 20 */
21 class LinkedHashMap<K, V> implements Map<K, V> { 21 class LinkedHashMap<K, V> implements Map<K, V> {
22 external LinkedHashMap(); 22 external LinkedHashMap();
23 23
24 static _id(x) => x;
25
24 factory LinkedHashMap.from(Map<K, V> other) { 26 factory LinkedHashMap.from(Map<K, V> other) {
25 return new LinkedHashMap<K, V>()..addAll(other); 27 return new LinkedHashMap<K, V>()..addAll(other);
26 } 28 }
27 29
30 factory LinkedHashMap.fromIterable(Iterable iterable,
31 {K key(element), V value(element)}) {
32
33 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>();
floitsch 2013/06/27 15:21:38 We start to have too much code-duplication. Let's
zarah 2013/06/27 15:58:52 Agree! Done.
34
35 if (key == null) key = _id;
36 if (value == null) value = _id;
37
38 for (var element in iterable) {
39 map[key(element)] = value(element);
40 }
41
42 return map;
43 }
44
45 factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
46 LinkedHashMap<K, V> map = new LinkedHashMap<K, V>();
47
48 Iterator<K> keyIterator = keys.iterator;
49 Iterator<V> valueIterator = values.iterator;
50
51 bool hasNextKey = keyIterator.moveNext();
52 bool hasNextValue = valueIterator.moveNext();
53
54 while (hasNextKey && hasNextValue) {
55 map[keyIterator.current] = valueIterator.current;
56 hasNextKey = keyIterator.moveNext();
57 hasNextValue = valueIterator.moveNext();
58 }
59
60 if (hasNextKey || hasNextValue) {
61 throw new ArgumentError("Iterables do not have same length.");
62 }
63
64 return map;
65 }
66
28 external bool containsKey(Object key); 67 external bool containsKey(Object key);
29 68
30 external bool containsValue(Object value); 69 external bool containsValue(Object value);
31 70
32 external void addAll(Map<K, V> other); 71 external void addAll(Map<K, V> other);
33 72
34 external V operator [](Object key); 73 external V operator [](Object key);
35 74
36 external void operator []=(K key, V value); 75 external void operator []=(K key, V value);
37 76
(...skipping 11 matching lines...) Expand all
49 external Iterable<V> get values; 88 external Iterable<V> get values;
50 89
51 external int get length; 90 external int get length;
52 91
53 external bool get isEmpty; 92 external bool get isEmpty;
54 93
55 external bool get isNotEmpty; 94 external bool get isNotEmpty;
56 95
57 String toString() => Maps.mapToString(this); 96 String toString() => Maps.mapToString(this);
58 } 97 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/collection/splay_tree.dart » ('j') | sdk/lib/collection/splay_tree.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698