intersect method

VersionConstraint intersect (VersionConstraint other)
override

Returns a VersionConstraint that only allows Versions allowed by both this and other.

Implementation

VersionConstraint intersect(VersionConstraint other) {
  var ourRanges = ranges.iterator;
  var theirRanges = _rangesFor(other).iterator;

  // Because both lists of ranges are ordered by minimum version, we can
  // safely move through them linearly here.
  var newRanges = <VersionRange>[];
  ourRanges.moveNext();
  theirRanges.moveNext();
  while (ourRanges.current != null && theirRanges.current != null) {
    var intersection = ourRanges.current.intersect(theirRanges.current);

    if (!intersection.isEmpty) newRanges.add(intersection);

    // Move the constraint with the lower max value forward. This ensures that
    // we keep both lists in sync as much as possible, and that large ranges
    // have a chance to match multiple small ranges that they contain.
    if (allowsHigher(theirRanges.current, ourRanges.current)) {
      ourRanges.moveNext();
    } else {
      theirRanges.moveNext();
    }
  }

  if (newRanges.isEmpty) return VersionConstraint.empty;
  if (newRanges.length == 1) return newRanges.single;

  return new VersionUnion.fromRanges(newRanges);
}