Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 */ | |
| 8 bool _defaultEquals(a, b) => a == b; | |
| 9 /** Default function for hash-code computation in customized HashMaps */ | |
| 10 int _defaultHashCode(a) => a.hashCode; | |
| 11 | |
| 7 /** | 12 /** |
| 8 * A hash-table based implementation of [Map]. | 13 * A hash-table based implementation of [Map]. |
| 9 * | 14 * |
| 10 * The keys of a `HashMap` must have consistent [Object.operator==] | 15 * The keys of a `HashMap` must have consistent [Object.operator==] |
| 11 * and [Object.hashCode] implementations. This means that the `==` operator | 16 * and [Object.hashCode] implementations. This means that the `==` operator |
| 12 * must define a stable equivalence relation on the keys (reflexive, | 17 * must define a stable equivalence relation on the keys (reflexive, |
| 13 * anti-symmetric, transitive, and consistent over time), and that `hashCode` | 18 * anti-symmetric, transitive, and consistent over time), and that `hashCode` |
| 14 * must be the same for objects that are considered equal by `==`. | 19 * must be the same for objects that are considered equal by `==`. |
| 15 * | 20 * |
| 16 * The map allows `null` as a key. | 21 * The map allows `null` as a key. |
| 17 */ | 22 */ |
| 18 class HashMap<K, V> implements Map<K, V> { | 23 class HashMap<K, V> implements Map<K, V> { |
| 19 external HashMap(); | 24 /** |
| 25 * Creates an unordered hash-table based [Map]. | |
|
floitsch
2013/09/03 11:19:49
I'm not sure I like the word "unordered" here.
Don
Lasse Reichstein Nielsen
2013/09/03 11:44:28
Done.
| |
| 26 * | |
| 27 * If [equals] is provided, it is used to compare the keys in the table with | |
| 28 * new keys. If [equals] is omitted, the key's own [Object.operator==] is used | |
| 29 * instead. | |
| 30 * | |
| 31 * Similar, if [hashCode] is provided, it is used to produce a hash value | |
| 32 * for keys in order to place them in the hash table. If it is omitted, the | |
| 33 * key's own [Object.hashCode] is used. | |
| 34 * | |
| 35 * The used `equals` and `hashCode` method should always be consistent, | |
| 36 * so that if `equals(a, b)` then `hashCode(a) == hashCode(b)`. The hash | |
| 37 * of an object, or what it compares equal to, should not change while the | |
| 38 * object is in the table. If it does change, the result is unpredictable. | |
| 39 */ | |
| 40 factory HashMap({bool equals(K key1, K key2), int hashCode(K key)}) { | |
| 41 if (equals != null || hashCode != null) { | |
| 42 if (equals == null) { | |
| 43 equals = _defaultEquals; | |
| 44 } else if (hashCode == null) { | |
| 45 hashCode = _defaultHashCode; | |
| 46 } | |
| 47 // Create new CustomHashMap<K, V>(equals, hashCode). | |
| 48 throw new UnimplementedError("Not implemented yet"); | |
| 49 } | |
| 50 return new HashMap<K, V>._internal(); | |
| 51 } | |
| 52 | |
| 53 /** Creates an empty `HashMap` with the default equals and hashCode. */ | |
| 54 external HashMap._internal(); | |
| 20 | 55 |
| 21 /** | 56 /** |
| 22 * Creates a [HashMap] that contains all key value pairs of [other]. | 57 * Creates a [HashMap] that contains all key value pairs of [other]. |
| 23 */ | 58 */ |
| 24 factory HashMap.from(Map<K, V> other) { | 59 factory HashMap.from(Map<K, V> other) { |
| 25 return new HashMap<K, V>()..addAll(other); | 60 return new HashMap<K, V>()..addAll(other); |
| 26 } | 61 } |
| 27 | 62 |
| 28 /** | 63 /** |
| 29 * Creates a [HashMap] where the keys and values are computed from the | 64 * Creates a [HashMap] where the keys and values are computed from the |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 | 114 |
| 80 external V putIfAbsent(K key, V ifAbsent()); | 115 external V putIfAbsent(K key, V ifAbsent()); |
| 81 | 116 |
| 82 external V remove(Object key); | 117 external V remove(Object key); |
| 83 external void clear(); | 118 external void clear(); |
| 84 | 119 |
| 85 external void forEach(void action(K key, V value)); | 120 external void forEach(void action(K key, V value)); |
| 86 | 121 |
| 87 String toString() => Maps.mapToString(this); | 122 String toString() => Maps.mapToString(this); |
| 88 } | 123 } |
| OLD | NEW |