| 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 /** Common parts of [HashSet] and [LinkedHashSet] implementations. */ | 7 /** Common parts of [HashSet] and [LinkedHashSet] implementations. */ |
| 8 abstract class _HashSetBase<E> extends SetBase<E> { | 8 abstract class _HashSetBase<E> extends SetBase<E> { |
| 9 | 9 |
| 10 // The following two methods override the ones in SetBase. | 10 // The following two methods override the ones in SetBase. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 * must consistent with equality, so that the same for objects that are | 43 * must consistent with equality, so that the same for objects that are |
| 44 * considered equal. | 44 * considered equal. |
| 45 * | 45 * |
| 46 * The set allows `null` as an element. | 46 * The set allows `null` as an element. |
| 47 * | 47 * |
| 48 * Most simple operations on `HashSet` are done in (potentially amorteized) | 48 * Most simple operations on `HashSet` are done in (potentially amorteized) |
| 49 * constant time: [add], [contains], [remove], and [length], provided the hash | 49 * constant time: [add], [contains], [remove], and [length], provided the hash |
| 50 * codes of objects are well distributed. | 50 * codes of objects are well distributed. |
| 51 */ | 51 */ |
| 52 abstract class HashSet<E> implements Set<E> { | 52 abstract class HashSet<E> implements Set<E> { |
| 53 @patch | 53 /** |
| 54 * Create a hash set using the provided [equals] as equality. |
| 55 * |
| 56 * The provided [equals] must define a stable equivalence relation, and |
| 57 * [hashCode] must be consistent with [equals]. If the [equals] or [hashCode] |
| 58 * methods won't work on all objects, but only to instances of E, the |
| 59 * [isValidKey] predicate can be used to restrict the keys that they are |
| 60 * applied to. Any key for which [isValidKey] returns false is automatically |
| 61 * assumed to not be in the set. |
| 62 * |
| 63 * If [equals] or [hashCode] are omitted, the set uses |
| 64 * the objects' intrinsic [Object.operator==] and [Object.hashCode]. |
| 65 * |
| 66 * If [isValidKey] is omitted, it defaults to testing if the object is an |
| 67 * [E] instance. |
| 68 * |
| 69 * If you supply one of [equals] and [hashCode], |
| 70 * you should generally also to supply the other. |
| 71 * An example would be using [identical] and [identityHashCode], |
| 72 * which is equivalent to using the shorthand [LinkedSet.identity]). |
| 73 */ |
| 54 factory HashSet({ bool equals(E e1, E e2), | 74 factory HashSet({ bool equals(E e1, E e2), |
| 55 int hashCode(E e), | 75 int hashCode(E e), |
| 56 bool isValidKey(potentialKey) }) { | 76 bool isValidKey(potentialKey) }) { |
| 57 if (isValidKey == null) { | 77 if (isValidKey == null) { |
| 58 if (hashCode == null) { | 78 if (hashCode == null) { |
| 59 if (equals == null) { | 79 if (equals == null) { |
| 60 return new _HashSet<E>(); | 80 return new _HashSet<E>(); |
| 61 } | 81 } |
| 62 hashCode = _defaultHashCode; | 82 hashCode = _defaultHashCode; |
| 63 } else { | 83 } else { |
| 64 if (identical(identityHashCode, hashCode) && | 84 if (identical(identityHashCode, hashCode) && |
| 65 identical(identical, equals)) { | 85 identical(identical, equals)) { |
| 66 return new _IdentityHashSet<E>(); | 86 return new _IdentityHashSet<E>(); |
| 67 } | 87 } |
| 68 if (equals == null) { | 88 if (equals == null) { |
| 69 equals = _defaultEquals; | 89 equals = _defaultEquals; |
| 70 } | 90 } |
| 71 } | 91 } |
| 72 } else { | 92 } else { |
| 73 if (hashCode == null) { | 93 if (hashCode == null) { |
| 74 hashCode = _defaultHashCode; | 94 hashCode = _defaultHashCode; |
| 75 } | 95 } |
| 76 if (equals == null) { | 96 if (equals == null) { |
| 77 equals = _defaultEquals; | 97 equals = _defaultEquals; |
| 78 } | 98 } |
| 79 } | 99 } |
| 80 return new _CustomHashSet<E>(equals, hashCode, isValidKey); | 100 return new _CustomHashSet<E>(equals, hashCode, isValidKey); |
| 81 } | 101 } |
| 82 | 102 |
| 83 @patch | 103 /** |
| 104 * Creates an unordered identity-based set. |
| 105 * |
| 106 * Effectively a shorthand for: |
| 107 * |
| 108 * new HashSet(equals: identical, hashCode: identityHashCodeOf) |
| 109 */ |
| 84 factory HashSet.identity() = _IdentityHashSet<E>; | 110 factory HashSet.identity() = _IdentityHashSet<E>; |
| 85 | 111 |
| 86 /** | 112 /** |
| 87 * Create a hash set containing all [elements]. | 113 * Create a hash set containing all [elements]. |
| 88 * | 114 * |
| 89 * Creates a hash set as by `new HashSet<E>()` and adds each element of | 115 * Creates a hash set as by `new HashSet<E>()` and adds each element of |
| 90 * `elements` to this set in the order they are iterated. | 116 * `elements` to this set in the order they are iterated. |
| 91 * | 117 * |
| 92 * All the [elements] should be assignable to [E]. | 118 * All the [elements] should be assignable to [E]. |
| 93 * The `elements` iterable itself may have any element type, so this | 119 * The `elements` iterable itself may have any element type, so this |
| (...skipping 10 matching lines...) Expand all Loading... |
| 104 } | 130 } |
| 105 | 131 |
| 106 /** | 132 /** |
| 107 * Provides an iterator that iterates over the elements of this set. | 133 * Provides an iterator that iterates over the elements of this set. |
| 108 * | 134 * |
| 109 * The order of iteration is unspecified, | 135 * The order of iteration is unspecified, |
| 110 * but consistent between changes to the set. | 136 * but consistent between changes to the set. |
| 111 */ | 137 */ |
| 112 Iterator<E> get iterator; | 138 Iterator<E> get iterator; |
| 113 } | 139 } |
| OLD | NEW |