OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.collection; | |
6 | |
7 /** Common parts of [HashSet] and [LinkedHashSet] implementations. */ | |
8 abstract class _HashSetBase<E> extends SetBase<E> { | |
9 | |
10 // The following two methods override the ones in SetBase. | |
11 // It's possible to be more efficient if we have a way to create an empty | |
12 // set of the correct type. | |
13 | |
14 Set<E> difference(Set<Object> other) { | |
15 Set<E> result = _newSet(); | |
16 for (var element in this) { | |
17 if (!other.contains(element)) result.add(element); | |
18 } | |
19 return result; | |
20 } | |
21 | |
22 Set<E> intersection(Set<Object> other) { | |
23 Set<E> result = _newSet(); | |
24 for (var element in this) { | |
25 if (other.contains(element)) result.add(element); | |
26 } | |
27 return result; | |
28 } | |
29 | |
30 Set<E> _newSet(); | |
31 | |
32 // Subclasses can optimize this further. | |
33 Set<E> toSet() => _newSet()..addAll(this); | |
34 } | |
35 | |
36 /** | |
37 * An unordered hash-table based [Set] implementation. | |
38 * | |
39 * The elements of a `HashSet` must have consistent equality | |
40 * and hashCode implementations. This means that the equals operation | |
41 * must define a stable equivalence relation on the elements (reflexive, | |
42 * symmetric, transitive, and consistent over time), and that the hashCode | |
43 * must consistent with equality, so that the same for objects that are | |
44 * considered equal. | |
45 * | |
46 * The set allows `null` as an element. | |
47 * | |
48 * Most simple operations on `HashSet` are done in (potentially amorteized) | |
49 * constant time: [add], [contains], [remove], and [length], provided the hash | |
50 * codes of objects are well distributed. | |
51 */ | |
52 abstract class HashSet<E> implements Set<E> { | |
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 on some instances of E, the | |
59 * [isValidKey] predicate can be used to restrict the keys that the functions | |
60 * are applied to. | |
61 * Any key for which [isValidKey] returns false is automatically assumed | |
62 * to not be in the set when asking `contains`. | |
63 * | |
64 * If [equals] or [hashCode] are omitted, the set uses | |
65 * the elements' intrinsic [Object.operator==] and [Object.hashCode]. | |
66 * | |
67 * If you supply one of [equals] and [hashCode], | |
68 * you should generally also to supply the other. | |
69 * | |
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]. | |
93 */ | |
94 external factory HashSet({bool equals(E e1, E e2), | |
95 int hashCode(E e), | |
96 bool isValidKey(Object potentialKey)}); | |
97 | |
98 /** | |
99 * Creates an unordered identity-based set. | |
100 * | |
101 * Effectively a shorthand for: | |
102 * | |
103 * new HashSet<E>(equals: identical, | |
104 * hashCode: identityHashCode) | |
105 */ | |
106 external factory HashSet.identity(); | |
107 | |
108 /** | |
109 * Create a hash set containing all [elements]. | |
110 * | |
111 * Creates a hash set as by `new HashSet<E>()` and adds each element of | |
112 * `elements` to this set in the order they are iterated. | |
113 * | |
114 * All the [elements] should be assignable to [E]. | |
115 * The `elements` iterable itself may have any element type, so this | |
116 * constructor can be used to down-cast a `Set`, for example as: | |
117 * | |
118 * Set<SuperType> superSet = ...; | |
119 * Set<SubType> subSet = | |
120 * new HashSet<SubType>.from(superSet.where((e) => e is SubType)); | |
121 */ | |
122 factory HashSet.from(Iterable elements) { | |
123 HashSet<E> result = new HashSet<E>(); | |
124 for (final e in elements) { | |
125 E element = e as Object/*=E*/; | |
126 result.add(element); | |
127 } | |
128 return result; | |
129 } | |
130 | |
131 /** | |
132 * Provides an iterator that iterates over the elements of this set. | |
133 * | |
134 * The order of iteration is unspecified, | |
135 * but consistent between changes to the set. | |
136 */ | |
137 Iterator<E> get iterator; | |
138 } | |
OLD | NEW |