OLD | NEW |
---|---|
(Empty) | |
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 'dart:collection'; | |
6 | |
7 import 'unmodifiable_wrappers.dart'; | |
8 | |
9 /// A single set that provides a view of a set of sets. | |
10 /// | |
11 /// This behaves like the union of all sets passed in to [new GroupSet]. Since | |
12 /// it's just a view, it will reflect any changes in the underlying sets. | |
13 /// | |
14 /// If an element is in multiple sets and the outer set is ordered, the version | |
15 /// in the earliest inner set is preferred. Component sets are assumed to use | |
16 /// `==` and `hashCode` for equality. | |
17 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.
| |
18 /// The set of sets that this provides a view of. | |
19 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
| |
20 | |
21 /// Whether the sets in [_sets] are guaranteed to be disjoint. | |
22 final bool _disjoint; | |
23 | |
24 /// Creates a new set that's a view of the union of all sets in [sets]. | |
25 /// | |
26 /// 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.
| |
27 /// a new set is added to [sets], this [GroupSet] will reflect that as well. | |
28 /// | |
29 /// 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
| |
30 /// 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
| |
31 /// operations including [length] more efficient. If the component sets turn | |
32 /// out not to be disjoint, some operations may behave inconsistently. | |
33 GroupSet(this._sets, {bool disjoint: false}) : _disjoint = disjoint; | |
34 | |
35 /// Creates a new set that's a view of the union of all sets in [sets]. | |
36 /// | |
37 /// 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.
| |
38 /// However, unlike [new GroupSet], this creates a copy of its parameter, so | |
39 /// 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.
| |
40 /// | |
41 /// 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.
| |
42 /// disjoint—that is, that they contain no elements in common. This makes many | |
43 /// operations including [length] more efficient. If the component sets turn | |
44 /// out not to be disjoint, some operations may behave inconsistently. | |
45 GroupSet.from(Iterable<Set<E>> sets, {bool disjoint: false}) | |
46 : this(sets.toSet(), disjoint: disjoint); | |
47 | |
48 int get length => _disjoint | |
49 ? _sets.fold(0, (length, set) => length + set.length) | |
50 : _iterable.length; | |
51 | |
52 Iterator<E> get iterator => _iterable.iterator; | |
53 | |
54 /// Returns an iterable over the contents of all the sets in [this]. | |
55 Iterable<E> get _iterable => | |
56 _disjoint ? _sets.expand((set) => set) : _dedupIterable; | |
57 | |
58 /// Returns an iterable over the contents of all the sets in [this] that | |
59 /// de-duplicates elements. | |
60 /// | |
61 /// If the sets aren't guaranteed to be disjoint, this keeps track of the | |
62 /// elements we've already emitted so that we can de-duplicate them. | |
63 Iterable<E> get _dedupIterable { | |
64 var seen = new Set<E>(); | |
65 return _sets.expand((set) => set).where((element) { | |
66 if (seen.contains(element)) return false; | |
67 seen.add(element); | |
68 return true; | |
69 }); | |
70 } | |
71 | |
72 bool contains(Object element) => _sets.any((set) => set.contains(element)); | |
73 | |
74 E lookup(Object element) { | |
75 if (element == null) return null; | |
76 | |
77 return _sets | |
78 .map((set) => set.lookup(element)) | |
79 .firstWhere((result) => result != null, orElse: () => null); | |
80 } | |
81 | |
82 Set<E> toSet() { | |
83 var result = new Set<E>(); | |
84 for (var set in _sets) { | |
85 result.addAll(set); | |
86 } | |
87 return result; | |
88 } | |
89 } | |
OLD | NEW |