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 27 matching lines...) Expand all Loading... |
38 * | 38 * |
39 * The elements of a `HashSet` must have consistent equality | 39 * The elements of a `HashSet` must have consistent equality |
40 * and hashCode implementations. This means that the equals operation | 40 * and hashCode implementations. This means that the equals operation |
41 * must define a stable equivalence relation on the elements (reflexive, | 41 * must define a stable equivalence relation on the elements (reflexive, |
42 * symmetric, transitive, and consistent over time), and that the hashCode | 42 * symmetric, transitive, and consistent over time), and that the hashCode |
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 amortized) |
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 * |
| 52 * The iteration order of the set is not specified and depends on |
| 53 * the hashcodes of the provided elements. However, the order is stable: |
| 54 * multiple iterations over the same set produce the same order, as long as |
| 55 * the set is not modified. |
51 */ | 56 */ |
52 abstract class HashSet<E> implements Set<E> { | 57 abstract class HashSet<E> implements Set<E> { |
53 /** | 58 /** |
54 * Create a hash set using the provided [equals] as equality. | 59 * Create a hash set using the provided [equals] as equality. |
55 * | 60 * |
56 * The provided [equals] must define a stable equivalence relation, and | 61 * The provided [equals] must define a stable equivalence relation, and |
57 * [hashCode] must be consistent with [equals]. If the [equals] or [hashCode] | 62 * [hashCode] must be consistent with [equals]. If the [equals] or [hashCode] |
58 * methods won't work on all objects, but only on some instances of E, the | 63 * 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 the functions | 64 * [isValidKey] predicate can be used to restrict the keys that the functions |
60 * are applied to. | 65 * are applied to. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 * Effectively a shorthand for: | 106 * Effectively a shorthand for: |
102 * | 107 * |
103 * new HashSet<E>(equals: identical, | 108 * new HashSet<E>(equals: identical, |
104 * hashCode: identityHashCode) | 109 * hashCode: identityHashCode) |
105 */ | 110 */ |
106 external factory HashSet.identity(); | 111 external factory HashSet.identity(); |
107 | 112 |
108 /** | 113 /** |
109 * Create a hash set containing all [elements]. | 114 * Create a hash set containing all [elements]. |
110 * | 115 * |
111 * Creates a hash set as by `new HashSet<E>()` and adds each element of | 116 * Creates a hash set as by `new HashSet<E>()` and adds all given [elements] |
112 * `elements` to this set in the order they are iterated. | 117 * to the set. The elements are added in order. If [elements] contains |
| 118 * two entries that are equal, but not identical, then the first one is |
| 119 * the one in the resulting set. |
113 * | 120 * |
114 * All the [elements] should be assignable to [E]. | 121 * All the [elements] should be assignable to [E]. |
115 * The `elements` iterable itself may have any element type, so this | 122 * The `elements` iterable itself may have any element type, so this |
116 * constructor can be used to down-cast a `Set`, for example as: | 123 * constructor can be used to down-cast a `Set`, for example as: |
117 * | 124 * |
118 * Set<SuperType> superSet = ...; | 125 * Set<SuperType> superSet = ...; |
119 * Set<SubType> subSet = | 126 * Set<SubType> subSet = |
120 * new HashSet<SubType>.from(superSet.where((e) => e is SubType)); | 127 * new HashSet<SubType>.from(superSet.where((e) => e is SubType)); |
121 */ | 128 */ |
122 factory HashSet.from(Iterable elements) { | 129 factory HashSet.from(Iterable elements) { |
123 HashSet<E> result = new HashSet<E>(); | 130 HashSet<E> result = new HashSet<E>(); |
124 for (final e in elements) { | 131 for (final e in elements) { |
125 E element = e as Object/*=E*/; | 132 E element = e as Object/*=E*/; |
126 result.add(element); | 133 result.add(element); |
127 } | 134 } |
128 return result; | 135 return result; |
129 } | 136 } |
130 | 137 |
131 /** | 138 /** |
132 * Provides an iterator that iterates over the elements of this set. | 139 * Provides an iterator that iterates over the elements of this set. |
133 * | 140 * |
134 * The order of iteration is unspecified, | 141 * The order of iteration is unspecified, |
135 * but consistent between changes to the set. | 142 * but consistent between changes to the set. |
136 */ | 143 */ |
137 Iterator<E> get iterator; | 144 Iterator<E> get iterator; |
138 } | 145 } |
OLD | NEW |