Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: sdk/lib/collection/hash_set.dart

Issue 24104003: Reapply "Convert HashSet, LinkedHashSet to factory methods and custom implementations." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Punctuation and whitespace. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/lib/collection_patch.dart ('k') | sdk/lib/collection/linked_hash_set.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 IterableBase<E> implements Set<E> { 8 abstract class _HashSetBase<E> extends IterableBase<E> implements Set<E> {
9 9
10 // Set. 10 // Set.
11 bool containsAll(Iterable<Object> other) { 11 bool containsAll(Iterable<Object> other) {
12 for (Object object in other) { 12 for (Object object in other) {
13 if (!this.contains(object)) return false; 13 if (!this.contains(object)) return false;
14 } 14 }
15 return true; 15 return true;
16 } 16 }
17 17
18 /** Create a new Set of the same type as this. */ 18 /** Create a new Set of the same type as this. */
19 Set _newSet(); 19 HashSet<E> _newSet();
20 20
21 Set<E> intersection(Set<Object> other) { 21 Set<E> intersection(Set<Object> other) {
22 Set<E> result = _newSet(); 22 Set<E> result = _newSet();
23 if (other.length < this.length) { 23 if (other.length < this.length) {
24 for (var element in other) { 24 for (var element in other) {
25 if (this.contains(element)) result.add(element); 25 if (this.contains(element)) result.add(element);
26 } 26 }
27 } else { 27 } else {
28 for (E element in this) { 28 for (E element in this) {
29 if (other.contains(element)) result.add(element); 29 if (other.contains(element)) result.add(element);
30 } 30 }
31 } 31 }
32 return result; 32 return result;
33 } 33 }
34 34
35 Set<E> union(Set<E> other) { 35 Set<E> union(Set<E> other) {
36 return _newSet()..addAll(this)..addAll(other); 36 return _newSet()..addAll(this)..addAll(other);
37 } 37 }
38 38
39 Set<E> difference(Set<E> other) { 39 Set<E> difference(Set<E> other) {
40 HashSet<E> result = _newSet(); 40 HashSet<E> result = _newSet();
41 for (E element in this) { 41 for (E element in this) {
42 if (!other.contains(element)) result.add(element); 42 if (!other.contains(element)) result.add(element);
43 } 43 }
44 return result; 44 return result;
45 } 45 }
46 46
47 void retainAll(Iterable objectsToRetain) { 47 void _retainAll(Iterable objectsToRetain, bool isValidKey(Object o)) {
48 Set retainSet; 48 // TODO(lrn): Consider optimizing table based versions by
49 if (objectsToRetain is Set) { 49 // building a new table of the entries to retain.
50 retainSet = objectsToRetain; 50 Set retainSet = _newSet();
51 } else { 51 for (Object o in objectsToRetain) {
52 retainSet = objectsToRetain.toSet(); 52 if (isValidKey(o)) {
53 retainSet.add(o);
54 }
53 } 55 }
54 retainWhere(retainSet.contains); 56 retainWhere(retainSet.contains);
55 } 57 }
56 58
59 List<E> toList({bool growable: true}) {
60 List<E> result = new List<E>()..length = this.length;
61 int i = 0;
62 for (E element in this) result[i++] = element;
63 return result;
64 }
65
66 Set<E> toSet() => _newSet()..addAll(this);
67
57 // TODO(zarah) Remove this, and let it be inherited by IterableBase 68 // TODO(zarah) Remove this, and let it be inherited by IterableBase
58 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); 69 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}');
59 } 70 }
60 71
61 /** 72 /**
62 * A [HashSet] is a hash-table based [Set] implementation. 73 * A [HashSet] is a hash-table based [Set] implementation.
63 * 74 *
64 * The elements of a `HashSet` must have consistent [Object.operator==] 75 * The elements of a `HashSet` must have consistent equality
65 * and [Object.hashCode] implementations. This means that the `==` operator 76 * and hashCode implementations. This means that the equals operation
66 * must define a stable equivalence relation on the elements (reflexive, 77 * must define a stable equivalence relation on the elements (reflexive,
67 * anti-symmetric, transitive, and consistent over time), and that `hashCode` 78 * anti-symmetric, transitive, and consistent over time), and that the hashCode
68 * must be the same for objects that are considered equal by `==`. 79 * must consistent with equality, so that the same for objects that are
80 * considered equal.
69 * 81 *
70 * The set allows `null` as an element. 82 * The set allows `null` as an element.
71 * 83 *
72 * Most simple operations on `HashSet` are done in constant time: [add], 84 * Most simple operations on `HashSet` are done in constant time: [add],
73 * [contains], [remove], and [length]. 85 * [contains], [remove], and [length].
74 */ 86 */
75 class HashSet<E> extends _HashSetBase<E> { 87 class HashSet<E> implements Set<E> {
76 external HashSet(); 88 /**
89 * Create a hash set using the provided [equals] as equality.
90 *
91 * The provided [equals] must define a stable equivalence relation, and
92 * [hashCode] must be consistent with [equals]. If the [equals] or [hashCode]
93 * methods won't work on all objects, but only to instances of E, the
94 * [isValidKey] predicate can be used to restrict the keys that they are
95 * applied to. Any key for which [isValidKey] returns false is automatically
96 * assumed to not be in the set.
97 *
98 * If [equals], [hashCode] and [isValidKey] are omitted, the set uses
99 * the objects' intrinsic [Object.operator==] and [Object.hashCode].
100 *
101 * If [isValidKey] is omitted, it defaults to testing if the object is an
102 * [E] instance.
103 *
104 * If [equals] is [identical], this creates an identity set. Any hashCode
105 * is compatible with [identical], and it applies to all objects, so
106 * [hashCode] and [isValidKey] can safely be omitted.
107 */
108 external factory HashSet({ bool equals(E e1, E e2),
109 int hashCode(E e),
110 bool isValidKey(potentialKey) });
77 111
112 /**
113 * Create a hash set containing the elements of [iterable].
114 *
115 * Creates a hash set as by `new HashSet<E>()` and adds each element of
116 * `iterable` to this set in the order they are iterated.
117 */
78 factory HashSet.from(Iterable<E> iterable) { 118 factory HashSet.from(Iterable<E> iterable) {
79 return new HashSet<E>()..addAll(iterable); 119 return new HashSet<E>()..addAll(iterable);
80 } 120 }
81
82 // Iterable.
83 external Iterator<E> get iterator;
84
85 external int get length;
86
87 external bool get isEmpty;
88
89 external bool get isNotEmpty;
90
91 external bool contains(Object object);
92
93 // Set.
94 external void add(E element);
95
96 external void addAll(Iterable<E> objects);
97
98 external bool remove(Object object);
99
100 external void removeAll(Iterable<Object> objectsToRemove);
101
102 external void removeWhere(bool test(E element));
103
104 external void retainWhere(bool test(E element));
105
106 external void clear();
107
108 Set<E> _newSet() => new HashSet<E>();
109 } 121 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/lib/collection_patch.dart ('k') | sdk/lib/collection/linked_hash_set.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698