Line data Source code
1 : // Copyright (c) 2016, 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 : import 'union_set.dart';
6 :
7 : /// A controller that exposes a view of the union of a collection of sets.
8 : ///
9 : /// This is a convenience class for creating a [UnionSet] whose contents change
10 : /// over the lifetime of a class. For example:
11 : ///
12 : /// ```dart
13 : /// class Engine {
14 : /// Set<Test> get activeTests => _activeTestsGroup.set;
15 : /// final _activeTestsGroup = new UnionSetController<Test>();
16 : ///
17 : /// void addSuite(Suite suite) {
18 : /// _activeTestsGroup.add(suite.tests);
19 : /// _runSuite(suite);
20 : /// _activeTestsGroup.remove(suite.tests);
21 : /// }
22 : /// }
23 : /// ```
24 : class UnionSetController<E> {
25 : /// The [UnionSet] that provides a view of the union of sets in [this].
26 5 : UnionSet<E> get set => _set;
27 : UnionSet<E> _set;
28 :
29 : /// The sets whose union is exposed through [set].
30 : final _sets = new Set<Set<E>>();
31 :
32 : /// Creates a set of sets that provides a view of the union of those sets.
33 : ///
34 : /// If [disjoint] is `true`, this assumes that all component sets are
35 : /// disjoint—that is, that they contain no elements in common. This makes
36 : /// many operations including [length] more efficient.
37 5 : UnionSetController({bool disjoint: false}) {
38 15 : _set = new UnionSet<E>(_sets, disjoint: disjoint);
39 : }
40 :
41 : /// Adds the contents of [component] to [set].
42 : ///
43 : /// If the contents of [component] change over time, [set] will change
44 : /// accordingly.
45 : void add(Set<E> component) {
46 10 : _sets.add(component);
47 : }
48 :
49 : /// Removes the contents of [component] to [set].
50 : ///
51 : /// If another set in [this] has overlapping elements with [component], those
52 : /// elements will remain in [set].
53 0 : bool remove(Set<E> component) => _sets.remove(component);
54 : }
|