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

Side by Side Diff: sdk/lib/collection/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: Addressed review comments. 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/linked_hash_map.dart » ('j') | sdk/lib/collection/maps.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 * The keys of a `HashMap` must have consistent [Object.operator==] 10 * The keys of a `HashMap` must have consistent [Object.operator==]
11 * and [Object.hashCode] implementations. This means that the `==` operator 11 * and [Object.hashCode] implementations. This means that the `==` operator
12 * must define a stable equivalence relation on the keys (reflexive, 12 * must define a stable equivalence relation on the keys (reflexive,
13 * anti-symmetric, transitive, and consistent over time), and that `hashCode` 13 * anti-symmetric, transitive, and consistent over time), and that `hashCode`
14 * must be the same for objects that are considered equal by `==`. 14 * must be the same for objects that are considered equal by `==`.
15 * 15 *
16 * The map allows `null` as a key. 16 * The map allows `null` as a key.
17 */ 17 */
18 class HashMap<K, V> implements Map<K, V> { 18 class HashMap<K, V> implements Map<K, V> {
19 external HashMap(); 19 external HashMap();
20 20
21 static _id(x) => x;
22
23 factory HashMap.from(Map<K, V> other) { 21 factory HashMap.from(Map<K, V> other) {
24 return new HashMap<K, V>()..addAll(other); 22 return new HashMap<K, V>()..addAll(other);
25 } 23 }
26 24
27 factory HashMap.fromIterable(Iterable iterable, 25 factory HashMap.fromIterable(Iterable iterable,
floitsch 2013/06/27 16:04:32 Actually I think we need to copy the dart-doc from
zarah 2013/06/27 17:00:07 Done.
28 {K key(element), V value(element)}) { 26 {K key(element), V value(element)}) {
29 HashMap<K, V> map = new HashMap<K, V>(); 27 HashMap<K, V> map = new HashMap<K, V>();
30 28 Maps._fillMapWithMappedIterable(map, iterable, key: key, value: value);
31 if (key == null) key = _id;
32 if (value == null) value = _id;
33
34 for (var element in iterable) {
35 map[key(element)] = value(element);
36 }
37
38 return map; 29 return map;
39 } 30 }
40 31
41 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { 32 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
42 HashMap<K, V> map = new HashMap<K, V>(); 33 HashMap<K, V> map = new HashMap<K, V>();
43 34 Maps._fillMapWithIterables(map, keys, values);
44 Iterator<K> keyIterator = keys.iterator;
45 Iterator<V> valueIterator = values.iterator;
46
47 bool hasNextKey = keyIterator.moveNext();
48 bool hasNextValue = valueIterator.moveNext();
49
50 while (hasNextKey && hasNextValue) {
51 map[keyIterator.current] = valueIterator.current;
52 hasNextKey = keyIterator.moveNext();
53 hasNextValue = valueIterator.moveNext();
54 }
55
56 if (hasNextKey || hasNextValue) {
57 throw new ArgumentError("Iterables do not have same length.");
58 }
59
60 return map; 35 return map;
61 } 36 }
62 37
63 external int get length; 38 external int get length;
64 external bool get isEmpty; 39 external bool get isEmpty;
65 external bool get isNotEmpty; 40 external bool get isNotEmpty;
66 41
67 external Iterable<K> get keys; 42 external Iterable<K> get keys;
68 external Iterable<V> get values; 43 external Iterable<V> get values;
69 44
70 external bool containsKey(Object key); 45 external bool containsKey(Object key);
71 external bool containsValue(Object value); 46 external bool containsValue(Object value);
72 47
73 external void addAll(Map<K, V> other); 48 external void addAll(Map<K, V> other);
74 49
75 external V operator [](Object key); 50 external V operator [](Object key);
76 external void operator []=(K key, V value); 51 external void operator []=(K key, V value);
77 52
78 external V putIfAbsent(K key, V ifAbsent()); 53 external V putIfAbsent(K key, V ifAbsent());
79 54
80 external V remove(Object key); 55 external V remove(Object key);
81 external void clear(); 56 external void clear();
82 57
83 external void forEach(void action(K key, V value)); 58 external void forEach(void action(K key, V value));
84 59
85 String toString() => Maps.mapToString(this); 60 String toString() => Maps.mapToString(this);
86 } 61 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/collection/linked_hash_map.dart » ('j') | sdk/lib/collection/maps.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698