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

Side by Side Diff: lib/src/version_union.dart

Issue 1127783002: Add more set-like version constraint operations. (Closed) Base URL: git@github.com:dart-lang/pub_semver@master
Patch Set: Code review changes Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, 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 library pub_semver.src.version_union;
6
7 import 'package:collection/collection.dart';
8
9 import 'utils.dart';
10 import 'version.dart';
11 import 'version_constraint.dart';
12 import 'version_range.dart';
13
14 /// A (package-private) version constraint representing a union of multiple
15 /// disjoint version constraints.
16 ///
17 /// An instance of this will only be created if the version can't be represented
18 /// as a non-compound value.
19 class VersionUnion implements VersionConstraint {
20 /// The constraints that compose this union.
21 ///
22 /// This list has two invariants:
23 ///
24 /// * Its contents are sorted from lowest to highest matched versions.
25 /// * Its contents are disjoint and non-adjacent. In other words, for any two
26 /// constraints next to each other in the list, there's some version between
27 /// those constraints that they don't match.
28 final List<VersionRange> constraints;
29
30 bool get isEmpty => false;
31
32 bool get isAny => false;
33
34 /// Returns the union of [constraints].
35 ///
36 /// This ensures that an actual [VersionUnion] is only returned if necessary.
37 /// It also takes care of sorting and merging the constraints to ensure that
38 /// they're disjoint.
39 static VersionConstraint create(Iterable<VersionConstraint> constraints) {
40 var flattened = constraints.expand((constraint) {
41 if (constraint.isEmpty) return [];
42 if (constraint is VersionUnion) return constraint.constraints;
43 return [constraint];
44 }).toList();
45
46 if (flattened.isEmpty) return VersionConstraint.empty;
47
48 if (flattened.any((constraint) => constraint.isAny)) {
49 return VersionConstraint.any;
50 }
51
52 // Only allow Versions and VersionRanges here so we can more easily reason
53 // about everything in [flattened]. _EmptyVersions and VersionUnions are
54 // filtered out above.
55 for (var constraint in flattened) {
56 if (constraint is VersionRange) continue;
57 throw new ArgumentError('Unknown VersionConstraint type $constraint.');
58 }
59
60 (flattened as List).sort(compareMax);
61
62 var merged = [];
63 for (var constraint in flattened) {
64 // Merge this constraint with the previous one, but only if they touch.
65 if (merged.isEmpty ||
66 (!merged.last.allowsAny(constraint) &&
67 !areAdjacent(merged.last, constraint))) {
68 merged.add(constraint);
69 } else {
70 merged[merged.length - 1] = merged.last.union(constraint);
71 }
72 }
73
74 if (merged.length == 1) return merged.single;
75 return new VersionUnion._(merged);
76 }
77
78 VersionUnion._(this.constraints);
79
80 bool allows(Version version) =>
81 constraints.any((constraint) => constraint.allows(version));
82
83 bool allowsAll(VersionConstraint other) {
84 var ourConstraints = constraints.iterator;
85 var theirConstraints = _constraintsFor(other).iterator;
86
87 // Because both lists of constraints are ordered by minimum version, we can
88 // safely move through them linearly here.
89 ourConstraints.moveNext();
90 theirConstraints.moveNext();
91 while (ourConstraints.current != null && theirConstraints.current != null) {
92 if (ourConstraints.current.allowsAll(theirConstraints.current)) {
93 theirConstraints.moveNext();
94 } else {
95 ourConstraints.moveNext();
96 }
97 }
98
99 // If our constraints have allowed all of their constraints, we'll have
100 // consumed all of them.
101 return theirConstraints.current == null;
102 }
103
104 bool allowsAny(VersionConstraint other) {
105 var ourConstraints = constraints.iterator;
106 var theirConstraints = _constraintsFor(other).iterator;
107
108 // Because both lists of constraints are ordered by minimum version, we can
109 // safely move through them linearly here.
110 ourConstraints.moveNext();
111 theirConstraints.moveNext();
112 while (ourConstraints.current != null && theirConstraints.current != null) {
113 if (ourConstraints.current.allowsAny(theirConstraints.current)) {
114 return true;
115 }
116
117 // Move the constraint with the higher max value forward. This ensures
118 // that we keep both lists in sync as much as possible.
119 if (compareMax(ourConstraints.current, theirConstraints.current) < 0) {
120 ourConstraints.moveNext();
121 } else {
122 theirConstraints.moveNext();
123 }
124 }
125
126 return false;
127 }
128
129 VersionConstraint intersect(VersionConstraint other) {
130 var ourConstraints = constraints.iterator;
131 var theirConstraints = _constraintsFor(other).iterator;
132
133 // Because both lists of constraints are ordered by minimum version, we can
134 // safely move through them linearly here.
135 var newConstraints = [];
136 ourConstraints.moveNext();
137 theirConstraints.moveNext();
138 while (ourConstraints.current != null && theirConstraints.current != null) {
139 var intersection = ourConstraints.current
140 .intersect(theirConstraints.current);
141
142 if (!intersection.isEmpty) newConstraints.add(intersection);
143
144 // Move the constraint with the higher max value forward. This ensures
145 // that we keep both lists in sync as much as possible, and that large
146 // constraints have a chance to match multiple small constraints that they
147 // contain.
148 if (compareMax(ourConstraints.current, theirConstraints.current) < 0) {
149 ourConstraints.moveNext();
150 } else {
151 theirConstraints.moveNext();
152 }
153 }
154
155 if (newConstraints.isEmpty) return VersionConstraint.empty;
156 if (newConstraints.length == 1) return newConstraints.single;
157
158 return new VersionUnion._(newConstraints);
159 }
160
161 /// Returns [constraint] as a list of constraints.
162 ///
163 /// This is used to normalize constraints of various types.
164 List<VersionRange> _constraintsFor(VersionConstraint constraint) {
165 if (constraint.isEmpty) return [];
166 if (constraint is VersionUnion) return constraint.constraints;
167 if (constraint is VersionRange) return [constraint];
168 throw new ArgumentError('Unknown VersionConstraint type $constraint.');
169 }
170
171 VersionConstraint union(VersionConstraint other) =>
172 new VersionConstraint.unionOf([this, other]);
173
174 bool operator ==(other) {
175 if (other is! VersionUnion) return false;
176 return const ListEquality().equals(constraints, other.constraints);
177 }
178
179 int get hashCode => const ListEquality().hash(constraints);
180
181 String toString() => constraints.join(" or ");
182 }
OLDNEW
« lib/src/version_range.dart ('K') | « lib/src/version_range.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698