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

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

Issue 23494027: Make LinkedHashMap also have a factory constructor and be customizable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add isValidKey. Created 7 years, 3 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) 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 /** Default function for equality comparison in customized HashMaps */ 7 /** Default function for equality comparison in customized HashMaps */
8 bool _defaultEquals(a, b) => a == b; 8 bool _defaultEquals(a, b) => a == b;
9 /** Default function for hash-code computation in customized HashMaps */ 9 /** Default function for hash-code computation in customized HashMaps */
10 int _defaultHashCode(a) => a.hashCode; 10 int _defaultHashCode(a) => a.hashCode;
(...skipping 23 matching lines...) Expand all
34 * same as long as the map isn't changed. 34 * same as long as the map isn't changed.
35 * 35 *
36 * If [equals] is provided, it is used to compare the keys in the table with 36 * If [equals] is provided, it is used to compare the keys in the table with
37 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used 37 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used
38 * instead. 38 * instead.
39 * 39 *
40 * Similar, if [hashCode] is provided, it is used to produce a hash value 40 * Similar, if [hashCode] is provided, it is used to produce a hash value
41 * for keys in order to place them in the hash table. If it is omitted, the 41 * for keys in order to place them in the hash table. If it is omitted, the
42 * key's own [Object.hashCode] is used. 42 * key's own [Object.hashCode] is used.
43 * 43 *
44 * If using methods like [operator[]], [remove] and [containsKey] together
45 * with a custom equality and hashcode, an extra `isValidKey` function
46 * can be supplied. This function is called before calling [equals] or
47 * [hashCode] with an argument that may not be a [K] instance, and if the
48 * call returns false, the key is assumed to not be in the set.
49 * The [isValidKey] function defaults to just testing if the object is a
50 * [K] instance.
51 *
52 * The [isValidKey] method is ignored if `equals` and `hashCode`
floitsch 2013/09/06 12:33:20 Let's not do that. If supplied it should be called
Lasse Reichstein Nielsen 2013/09/06 12:52:11 Done.
53 * are both omitted, or if `equals` is the [identical] function and
54 * `hashcode` is omitted, because in those cases, the `equals` and `hashCode`
55 * functions are known to apply to all arguments.
56 *
44 * The used `equals` and `hashCode` method should always be consistent, 57 * The used `equals` and `hashCode` method should always be consistent,
45 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash 58 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash
46 * of an object, or what it compares equal to, should not change while the 59 * of an object, or what it compares equal to, should not change while the
47 * object is in the table. If it does change, the result is unpredictable. 60 * object is in the table. If it does change, the result is unpredictable.
48 * 61 *
49 * It is generally the case that if you supply one of [equals] and [hashCode], 62 * It is generally the case that if you supply one of [equals] and [hashCode],
50 * you also want to supply the other. The only common exception is to pass 63 * you also want to supply the other. The only common exception is to pass
51 * [identical] as the equality and use the default hash code. 64 * [identical] as the equality and use the default hash code.
52 */ 65 */
53 external factory HashMap({bool equals(K key1, K key2), int hashCode(K key)}); 66 external factory HashMap({bool equals(K key1, K key2),
67 int hashCode(K key),
68 bool isValidKey(potentialKey)});
54 69
55 /** 70 /**
56 * Creates a [HashMap] that contains all key value pairs of [other]. 71 * Creates a [HashMap] that contains all key value pairs of [other].
57 */ 72 */
58 factory HashMap.from(Map<K, V> other) { 73 factory HashMap.from(Map<K, V> other) {
59 return new HashMap<K, V>()..addAll(other); 74 return new HashMap<K, V>()..addAll(other);
60 } 75 }
61 76
62 /** 77 /**
63 * Creates a [HashMap] where the keys and values are computed from the 78 * Creates a [HashMap] where the keys and values are computed from the
(...skipping 25 matching lines...) Expand all
89 * overwrites the previous value. 104 * overwrites the previous value.
90 * 105 *
91 * It is an error if the two [Iterable]s don't have the same length. 106 * It is an error if the two [Iterable]s don't have the same length.
92 */ 107 */
93 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) { 108 factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
94 HashMap<K, V> map = new HashMap<K, V>(); 109 HashMap<K, V> map = new HashMap<K, V>();
95 Maps._fillMapWithIterables(map, keys, values); 110 Maps._fillMapWithIterables(map, keys, values);
96 return map; 111 return map;
97 } 112 }
98 } 113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698