Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend Interval api with intersect and enclose operations #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,67 @@ class Interval<T extends Comparable<T>> {
upperClosed: upperClosed);
}

/// The maximal interval which is enclosed in each interval in [intervals].
///
/// If [intervals] is empty, the returned interval contains all values.
///
/// If [intervals] do not overlap, `null` is returned.
factory Interval.intersectAll(Iterable<Interval<T>> intervals) {

var iterator = intervals.iterator;
if (!iterator.moveNext()) return new Interval.all();
var interval = iterator.current;
var lower = interval.lower;
var upper = interval.upper;
var lowerClosed = interval.lowerClosed;
var upperClosed = interval.upperClosed;
while (iterator.moveNext()) {
interval = iterator.current;
if (lower == null) {
lower = interval.lower;;
lowerClosed = interval.lowerClosed;
} else {
if (interval.lower != null) {
var cmp = Comparable.compare(lower, interval.lower);
if (cmp<=0) {
lower = interval.lower;
lowerClosed = (cmp!=0||lowerClosed) && interval.lowerClosed;
}
}
}
if (upper == null) {
upper = interval.upper;
upperClosed = interval.upperClosed;
} else {
if (interval.upper != null) {
var cmp = Comparable.compare(upper, interval.upper);
if (cmp>=0) {
upper = interval.upper;
upperClosed = (cmp!=0||upperClosed) && interval.upperClosed;
}
}
}
}
var cmp = Comparable.compare(lower, upper);
if (cmp>0) return null;
if (cmp==0&&(!upperClosed||!lowerClosed)) return null;
return new Interval<T>(
lower: lower,
upper: upper,
lowerClosed: lowerClosed,
upperClosed: upperClosed);
}

/// Returns the intersection of `this` interval with [other].
///
/// If the intervals do not intersect, `null` is returned.
Interval<T> intersect(Interval<T> other) => new Interval.intersectAll([this,other]);

/// Returns minimal interval that [encloses] both `this` and [other].
Interval<T> enclose(Interval<T> other) => new Interval.encloseAll([this,other]);



/// Whether `this` contains [test].
bool contains(T test) {
if (lower != null) {
Expand Down Expand Up @@ -261,6 +322,9 @@ class Interval<T extends Comparable<T>> {
return true;
}

/// Whether the intersection of `this` and [other] is not empty.
bool intersects(Interval<T> other) => intersect(other)!=null;

/// Whether the union of `this` and [other] is connected (i.e. is an
/// [Interval]).
bool connectedTo(Interval<T> other) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ author: Sean Eagan <[email protected]>
description: Provdes an Interval class, a contiguous set of values.
homepage: https://github.com/seaneagan/interval
dev_dependencies:
unittest: any
test: any
46 changes: 45 additions & 1 deletion test/interval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
library interval.test;

import 'package:interval/interval.dart';
import 'package:unittest/unittest.dart';
import 'package:test/test.dart';

main() {
group('Interval', () {
Expand Down Expand Up @@ -111,6 +111,50 @@ main() {

});

group('intersectAll', () {

test('should return null if iterable is empty', () {
var interval = new Interval.intersectAll([]);
expect(interval, null);
});

test('should return null when input interval do not overlap', () {
var interval = new Interval.intersectAll([
new Interval.atMost(0),
new Interval.atLeast(1)]);
expect(interval, null);
});

test('should have bounds matching extreme input interval bounds', () {
var interval = new Interval.intersectAll([
new Interval.closed(0, 3),
new Interval.closed(-1, 1),
new Interval.closed(-8, 10),
new Interval.closed(-5, 7)]);
expect(interval.lower, 0);
expect(interval.upper, 1);
});

test('should have open bound when any corresponding extreme input '
'interval bound does', () {
var interval = new Interval.intersectAll([
new Interval.closedOpen(0, 1),
new Interval.openClosed(0, 1)]);
expect(interval.lowerClosed, isFalse);
expect(interval.upperClosed, isFalse);
});

test('should have closed bound when all extreme input interval bounds '
'do', () {
var interval = new Interval.intersectAll([
new Interval.closed(0, 1),
new Interval.closed(0, 1)]);
expect(interval.lowerClosed, isTrue);
expect(interval.upperClosed, isTrue);
});

});

});

group('contains', () {
Expand Down