Index: lib/src/group_set.dart |
diff --git a/lib/src/group_set.dart b/lib/src/group_set.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3f9c395ee6382f8a54a7d31f93d0eb284ea31654 |
--- /dev/null |
+++ b/lib/src/group_set.dart |
@@ -0,0 +1,89 @@ |
+// Copyright (c) 2016, 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. |
+ |
+import 'dart:collection'; |
+ |
+import 'unmodifiable_wrappers.dart'; |
+ |
+/// A single set that provides a view of a set of sets. |
+/// |
+/// This behaves like the union of all sets passed in to [new GroupSet]. Since |
+/// it's just a view, it will reflect any changes in the underlying sets. |
+/// |
+/// If an element is in multiple sets and the outer set is ordered, the version |
+/// in the earliest inner set is preferred. Component sets are assumed to use |
+/// `==` and `hashCode` for equality. |
+class GroupSet<E> extends SetMixin<E> with UnmodifiableSetMixin<E> { |
floitsch
2016/04/25 16:11:09
What about "UnionSet" ?
nweiz
2016/04/26 21:23:52
I like it. Done.
|
+ /// The set of sets that this provides a view of. |
+ final Set<Set<E>> _sets; |
floitsch
2016/04/25 16:11:10
I'm not sure I like the fact that this has to be a
nweiz
2016/04/26 21:23:52
I discussed this with lrn earlier. Ideally this wo
|
+ |
+ /// Whether the sets in [_sets] are guaranteed to be disjoint. |
+ final bool _disjoint; |
+ |
+ /// Creates a new set that's a view of the union of all sets in [sets]. |
+ /// |
+ /// If any sets in [sets] change, this [GroupSet] will reflect that change. If |
floitsch
2016/04/25 16:11:10
reflects that change. ... reflects ...
nweiz
2016/04/26 21:23:53
Done.
|
+ /// a new set is added to [sets], this [GroupSet] will reflect that as well. |
+ /// |
+ /// If [disjoint] is `true`, this assumes that all component sets are |
floitsch
2016/04/25 16:11:09
If [disjoint] is `true`, then all component sets m
nweiz
2016/04/26 21:23:52
Done.
floitsch
2016/04/27 10:56:52
Imho, it's the change to "must" that makes the 'in
Lasse Reichstein Nielsen
2016/04/27 12:17:53
Disagree. When we use "must" we generally mean tha
floitsch
2016/04/27 12:31:20
I can live with the comment, so let's just keep it
|
+ /// disjoint—that is, that they contain no elements in common. This makes many |
floitsch
2016/04/25 16:11:09
Avoid non-ascii characters in documentation.
nweiz
2016/04/26 21:23:53
Done, although I don't think this is a good genera
|
+ /// operations including [length] more efficient. If the component sets turn |
+ /// out not to be disjoint, some operations may behave inconsistently. |
+ GroupSet(this._sets, {bool disjoint: false}) : _disjoint = disjoint; |
+ |
+ /// Creates a new set that's a view of the union of all sets in [sets]. |
+ /// |
+ /// If any sets in [sets] change, this [GroupSet] will reflect that change. |
floitsch
2016/04/25 16:11:10
reflects
nweiz
2016/04/26 21:23:53
Done.
|
+ /// However, unlike [new GroupSet], this creates a copy of its parameter, so |
+ /// changes in [sets] will not be reflected in this [GroupSet]. |
floitsch
2016/04/25 16:11:10
are not reflected
nweiz
2016/04/26 21:23:53
Done.
|
+ /// |
+ /// If [disjoint] is `true`, this assumes that all component sets are |
floitsch
2016/04/25 16:11:10
as above.
nweiz
2016/04/26 21:23:52
Done.
|
+ /// disjoint—that is, that they contain no elements in common. This makes many |
+ /// operations including [length] more efficient. If the component sets turn |
+ /// out not to be disjoint, some operations may behave inconsistently. |
+ GroupSet.from(Iterable<Set<E>> sets, {bool disjoint: false}) |
+ : this(sets.toSet(), disjoint: disjoint); |
+ |
+ int get length => _disjoint |
+ ? _sets.fold(0, (length, set) => length + set.length) |
+ : _iterable.length; |
+ |
+ Iterator<E> get iterator => _iterable.iterator; |
+ |
+ /// Returns an iterable over the contents of all the sets in [this]. |
+ Iterable<E> get _iterable => |
+ _disjoint ? _sets.expand((set) => set) : _dedupIterable; |
+ |
+ /// Returns an iterable over the contents of all the sets in [this] that |
+ /// de-duplicates elements. |
+ /// |
+ /// If the sets aren't guaranteed to be disjoint, this keeps track of the |
+ /// elements we've already emitted so that we can de-duplicate them. |
+ Iterable<E> get _dedupIterable { |
+ var seen = new Set<E>(); |
+ return _sets.expand((set) => set).where((element) { |
+ if (seen.contains(element)) return false; |
+ seen.add(element); |
+ return true; |
+ }); |
+ } |
+ |
+ bool contains(Object element) => _sets.any((set) => set.contains(element)); |
+ |
+ E lookup(Object element) { |
+ if (element == null) return null; |
+ |
+ return _sets |
+ .map((set) => set.lookup(element)) |
+ .firstWhere((result) => result != null, orElse: () => null); |
+ } |
+ |
+ Set<E> toSet() { |
+ var result = new Set<E>(); |
+ for (var set in _sets) { |
+ result.addAll(set); |
+ } |
+ return result; |
+ } |
+} |