Chromium Code Reviews| Index: sdk/lib/collection/set.dart |
| diff --git a/sdk/lib/collection/set.dart b/sdk/lib/collection/set.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..abd6f114b71671e5cb502579e8e12823b8fe47c9 |
| --- /dev/null |
| +++ b/sdk/lib/collection/set.dart |
| @@ -0,0 +1,133 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * Base implementations of [Set]. |
| + */ |
| +part of dart.collection; |
| + |
| +/** |
| + * Mixin implementation of [Set]. |
| + * |
| + * This class provides a base implementation of a `Set` that depends only |
| + * on the abstract members: [add], [contains], [lookup], [remove], |
| + * [iterator] and [cloneEmpty]. |
| + * |
| + * Implementations of `Set` using this mixin should consider also implementing |
| + * `clear` in constant time. The default implementation works by removing every |
| + * element. |
| + * |
| + * This class does not implement most of the methods that Set inherit |
| + * from [Iterable]. |
| + * When using the mixin, it should be applied to an iterable, |
| + * or be combined with [IterableMixin]. |
|
floitsch
2014/05/19 13:13:43
This is not how ListMixin is implemented. I prefer
|
| + */ |
| +abstract class SetMixin<E> implements Set<E> { |
| + |
| + bool add(E element); |
| + |
| + bool contains(Object element); |
| + |
| + E lookup(E element); |
| + |
| + bool remove(Object element); |
| + |
| + Iterable<E> get iterator; |
| + |
| + Set<E> cloneEmpty(); |
| + |
| + void clear() { |
| + removeAll(toList()); |
| + } |
| + |
| + void addAll(Iterable<E> elements) { |
| + for (E element in elements) add(element); |
| + } |
| + |
| + void removeAll(Iterable<Object> elements) { |
| + for (Object element in elements) remove(element); |
| + } |
| + |
| + void retainAll(Iterable<Object> elements) { |
| + // Create a copy of the set, remove all of elements from the copy, |
| + // then remove all remaining elements in copy from this. |
| + Set<E> toRemove = toSet(); |
| + for (Object o in elements) { |
| + toRemove.remove(o); |
| + } |
| + removeAll(toRemove); |
| + } |
| + |
| + void removeWhere(bool test(E element)) { |
| + List toRemove = []; |
| + for (E element in this) { |
| + if (test(element)) toRemove.add(element); |
| + } |
| + removeAll(toRemove); |
| + } |
| + |
| + void retainWhere(bool test(E element)) { |
| + List toRemove = []; |
| + for (E element in this) { |
| + if (!test(element)) toRemove.add(element); |
| + } |
| + removeAll(toRemove); |
| + } |
| + |
| + bool containsAll(Iterable<Object> other) { |
| + for (Object o in other) { |
| + if (!contains(o)) return false; |
| + } |
| + return true; |
| + } |
| + |
| + Set<E> intersection(Set<Object> other) { |
| + Set<E> result = cloneEmpty(); |
| + for (E element in this) { |
| + if (other.contains(element)) result.add(element); |
| + } |
| + return result; |
| + } |
| + |
| + Set<E> union(Set<E> other) { |
| + return toSet()..addAll(other); |
| + } |
| + |
| + Set<E> difference(Set<E> other) { |
| + Set<E> result = cloneEmpty(); |
| + for (E element in this) { |
| + if (!other.contains(element)) result.add(element); |
| + } |
| + return result; |
| + } |
| + |
| + List<E> toList({bool growable: true}) { |
| + List<E> result = growable ? (new List<E>()..length = length) |
| + : new List<E>(length); |
| + int i = 0; |
| + for (E element in this) result[i++] = element; |
| + return result; |
| + } |
| + |
| + Set<E> toSet() { |
| + return cloneEmpty()..addAll(this); |
| + } |
| + |
| + String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); |
| +} |
| + |
| +/** |
| + * Base implementation of [Set]. |
| + * |
| + * This class provides a base implementation of a `Set` that depends only |
| + * on the abstract members: [add], [contains], [lookup], [remove], |
| + * [iterator] and [cloneEmpty]. |
| + * It includes [Iterable] methods from [IterableBase]. |
| + * |
| + * Implementations of `Set` using this base should consider also implementing |
| + * `clear` in constant time. The default implementation works by removing every |
| + * element. |
| + */ |
| +abstract class SetBase<E> = IterableBase<E> with SetMixin<E>; |
| + |