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

Unified Diff: lib/src/group_set.dart

Issue 1873373002: Add GroupSet and SetGroup classes. (Closed) Base URL: git@github.com:dart-lang/collection@master
Patch Set: a few more docs Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
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..e3500af7966ca86daa8d653a9e9f6ce974e19524
--- /dev/null
+++ b/lib/src/group_set.dart
@@ -0,0 +1,81 @@
+// 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.
Lasse Reichstein Nielsen 2016/04/12 13:55:22 Worth mentioning that the individual sets are assu
nweiz 2016/04/12 20:29:20 Done. We could have the weaker requirement that th
+class GroupSet<E> extends SetMixin<E> with UnmodifiableSetMixin<E> {
+ /// The set of sets that this provides a view of.
+ final Set<Set<E>> _sets;
+
+ /// 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
+ /// 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
+ /// disjoint—that is, that they contain no elements in common. This makes
+ /// many operations including [length] more efficient.
Lasse Reichstein Nielsen 2016/04/12 13:55:22 .. and if the sets turn out to not be disjoint aft
nweiz 2016/04/12 20:29:20 Done.
+ 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.
+ /// However, unlike [new GroupSet], this creates a copy of its parameter, so
+ /// changes in [sets] will not be reflected in this [GroupSet].
+ ///
+ /// If [disjoint] is `true`, this assumes that all component sets are
+ /// disjoint—that is, that they contain no elements in common. This makes
+ /// many operations including [length] more efficient.
+ 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;
+
+ Iterable<E> get _iterable {
+ if (_disjoint) return _sets.expand((set) => set);
+
+ // If the sets aren't guaranteed to be disjoint, keep track of the elements
+ // we've already emitted so that we can de-duplicate elements.
+ 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;
+ }
+}
« no previous file with comments | « lib/collection.dart ('k') | lib/src/set_group.dart » ('j') | lib/src/set_group.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698