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 = 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 : final UnionSet<E> set; 27 : 28 : /// The sets whose union is exposed through [set]. 29 : final Set<Set<E>> _sets; 30 : 31 : /// Creates a set of sets that provides a view of the union of those sets. 32 : /// 33 : /// If [disjoint] is `true`, this assumes that all component sets are 34 : /// disjoint—that is, that they contain no elements in common. This makes 35 : /// many operations including [length] more efficient. 36 0 : UnionSetController({bool disjoint = false}) : this._(<Set<E>>{}, disjoint); 37 : 38 : /// Creates a controller with the provided [_sets]. 39 0 : UnionSetController._(this._sets, bool disjoint) 40 0 : : set = UnionSet<E>(_sets, disjoint: disjoint); 41 : 42 : /// Adds the contents of [component] to [set]. 43 : /// 44 : /// If the contents of [component] change over time, [set] will change 45 : /// accordingly. 46 0 : void add(Set<E> component) { 47 0 : _sets.add(component); 48 : } 49 : 50 : /// Removes the contents of [component] to [set]. 51 : /// 52 : /// If another set in [this] has overlapping elements with [component], those 53 : /// elements will remain in [set]. 54 0 : bool remove(Set<E> component) => _sets.remove(component); 55 : }