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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 /** | 53 /** |
54 * Create a hash set using the provided [equals] as equality. | 54 * Create a hash set using the provided [equals] as equality. |
55 * | 55 * |
56 * The provided [equals] must define a stable equivalence relation, and | 56 * The provided [equals] must define a stable equivalence relation, and |
57 * [hashCode] must be consistent with [equals]. If the [equals] or [hashCode] | 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 | 58 * methods won't work on all objects, but only on some instances of E, the |
59 * [isValidKey] predicate can be used to restrict the keys that they are | 59 * [isValidKey] predicate can be used to restrict the keys that the functions |
60 * applied to. Any key for which [isValidKey] returns false is automatically | 60 * are applied to. |
61 * assumed to not be in the set. | 61 * Any key for which [isValidKey] returns false is automatically assumed |
| 62 * to not be in the set when asking `contains`. |
62 * | 63 * |
63 * If [equals] or [hashCode] are omitted, the set uses | 64 * If [equals] or [hashCode] are omitted, the set uses |
64 * the objects' intrinsic [Object.operator==] and [Object.hashCode]. | 65 * the elements' 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 * | 66 * |
69 * If you supply one of [equals] and [hashCode], | 67 * If you supply one of [equals] and [hashCode], |
70 * you should generally also to supply the other. | 68 * you should generally also to supply the other. |
71 * An example would be using [identical] and [identityHashCode], | 69 * |
72 * which is equivalent to using the shorthand [LinkedSet.identity]). | 70 * If the supplied `equals` or `hashCode` functions won't work on all [E] |
| 71 * objects, and the map will be used in a setting where a non-`E` object |
| 72 * is passed to, e.g., `contains`, then the [isValidKey] function should |
| 73 * also be supplied. |
| 74 * |
| 75 * If [isValidKey] is omitted, it defaults to testing if the object is an |
| 76 * [E] instance. That means that: |
| 77 * |
| 78 * new HashSet<int>(equals: (int e1, int e2) => (e1 - e2) % 5 == 0, |
| 79 * hashCode: (int e) => e % 5) |
| 80 * |
| 81 * does not need an `isValidKey` argument, because it defaults to only |
| 82 * accepting `int` values which are accepted by both `equals` and `hashCode`. |
| 83 * |
| 84 * If neither `equals`, `hashCode`, nor `isValidKey` is provided, |
| 85 * the default `isValidKey` instead accepts all values. |
| 86 * The default equality and hashcode operations are assumed to work on all |
| 87 * objects. |
| 88 * |
| 89 * Likewise, if `equals` is [identical], `hashCode` is [identityHashCode] |
| 90 * and `isValidKey` is omitted, the resulting set is identity based, |
| 91 * and the `isValidKey` defaults to accepting all keys. |
| 92 * Such a map can be created directly using [HashSet.identity]. |
73 */ | 93 */ |
74 external factory HashSet({bool equals(E e1, E e2), | 94 external factory HashSet({bool equals(E e1, E e2), |
75 int hashCode(E e), | 95 int hashCode(E e), |
76 bool isValidKey(potentialKey)}); | 96 bool isValidKey(potentialKey)}); |
77 | 97 |
78 /** | 98 /** |
79 * Creates an unordered identity-based set. | 99 * Creates an unordered identity-based set. |
80 * | 100 * |
81 * Effectively a shorthand for: | 101 * Effectively a shorthand for: |
82 * | 102 * |
83 * new HashSet(equals: identical, hashCode: identityHashCodeOf) | 103 * new HashSet<E>(equals: identical, |
| 104 * hashCode: identityHashCodeOf) |
84 */ | 105 */ |
85 external factory HashSet.identity(); | 106 external factory HashSet.identity(); |
86 | 107 |
87 /** | 108 /** |
88 * Create a hash set containing all [elements]. | 109 * Create a hash set containing all [elements]. |
89 * | 110 * |
90 * Creates a hash set as by `new HashSet<E>()` and adds each element of | 111 * Creates a hash set as by `new HashSet<E>()` and adds each element of |
91 * `elements` to this set in the order they are iterated. | 112 * `elements` to this set in the order they are iterated. |
92 * | 113 * |
93 * All the [elements] should be assignable to [E]. | 114 * All the [elements] should be assignable to [E]. |
(...skipping 11 matching lines...) Expand all Loading... |
105 } | 126 } |
106 | 127 |
107 /** | 128 /** |
108 * Provides an iterator that iterates over the elements of this set. | 129 * Provides an iterator that iterates over the elements of this set. |
109 * | 130 * |
110 * The order of iteration is unspecified, | 131 * The order of iteration is unspecified, |
111 * but consistent between changes to the set. | 132 * but consistent between changes to the set. |
112 */ | 133 */ |
113 Iterator<E> get iterator; | 134 Iterator<E> get iterator; |
114 } | 135 } |
OLD | NEW |