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

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

Issue 18023003: Add Map constructors taking iterables. (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/core/map.dart » ('j') | sdk/lib/core/map.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
21 factory HashMap.from(Map<K, V> other) { 22 factory HashMap.from(Map<K, V> other) {
22 return new HashMap<K, V>()..addAll(other); 23 return new HashMap<K, V>()..addAll(other);
23 } 24 }
24 25
26 factory HashMap.fromIterable(Iterable iterable,
27 {K key(element), V value(element)}) {
28 HashMap<K, V> map = new HashMap<K, V>();
29
30 var keyFun = (key != null) ? key : (x) => x;
floitsch 2013/06/27 11:45:04 In "dart:" libraries we type local variables too.
zarah 2013/06/27 13:02:43 Done.
31 var valueFun = (value != null) ? value : (x) => x;
32
33 iterable.forEach((e) {
floitsch 2013/06/27 11:45:04 personally I prefer for (var element in iterable)
zarah 2013/06/27 13:02:43 Done.
34 map[keyFun(e)] = valueFun(e);
35 });
36
37 return map;
38 }
39
40 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
41 HashMap<K, V> map = new HashMap<K, V>();
42
43 var keyIterator = keys.iterator;
floitsch 2013/06/27 11:45:04 Use type.
zarah 2013/06/27 13:02:43 Done.
44 var valueIterator = values.iterator;
45
46 bool hasNextKey = keyIterator.moveNext();
47 bool hasNextValue = valueIterator.moveNext();
48
49 while (hasNextKey && hasNextValue) {
50 map[keyIterator.current] = valueIterator.current;
51 hasNextKey = keyIterator.moveNext();
52 hasNextValue = valueIterator.moveNext();
53 }
54
55 if (hasNextKey != hasNextValue) {
floitsch 2013/06/27 11:45:04 if (hasNextKey || hasNextValue) { It has the same
zarah 2013/06/27 13:02:43 Done.
56 throw new ArgumentError("Iterables do not have same length.");
57 }
58
59 return map;
60 }
61
25 external int get length; 62 external int get length;
26 external bool get isEmpty; 63 external bool get isEmpty;
27 external bool get isNotEmpty; 64 external bool get isNotEmpty;
28 65
29 external Iterable<K> get keys; 66 external Iterable<K> get keys;
30 external Iterable<V> get values; 67 external Iterable<V> get values;
31 68
32 external bool containsKey(Object key); 69 external bool containsKey(Object key);
33 external bool containsValue(Object value); 70 external bool containsValue(Object value);
34 71
35 external void addAll(Map<K, V> other); 72 external void addAll(Map<K, V> other);
36 73
37 external V operator [](Object key); 74 external V operator [](Object key);
38 external void operator []=(K key, V value); 75 external void operator []=(K key, V value);
39 76
40 external V putIfAbsent(K key, V ifAbsent()); 77 external V putIfAbsent(K key, V ifAbsent());
41 78
42 external V remove(Object key); 79 external V remove(Object key);
43 external void clear(); 80 external void clear();
44 81
45 external void forEach(void action(K key, V value)); 82 external void forEach(void action(K key, V value));
46 83
47 String toString() => Maps.mapToString(this); 84 String toString() => Maps.mapToString(this);
48 } 85 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/core/map.dart » ('j') | sdk/lib/core/map.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698