Commit bb3f6673 authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Added enclose intervals operation

parent f73e135c
......@@ -28,27 +28,24 @@ import java.util.List;
*/
public class IntegerIntervalOperations implements IntervalOperations<Integer> {
@Override
public boolean beginsWith(Interval<Integer> fst, Interval<Integer> snd) {
return fst.as(Interval.Type.ZeroOpen).getStart() == snd.as(Interval.Type.ZeroOpen).getStart();
}
@Override
public boolean endsWith(Interval<Integer> fst, Interval<Integer> snd) {
return fst.as(Interval.Type.ZeroOpen).getEnd() == snd.as(Interval.Type.ZeroOpen).getEnd();
}
@Override
public boolean contains(Interval<Integer> fst, Interval<Integer> snd) {
fst = fst.as(Interval.Type.ZeroOpen);
snd = snd.as(Interval.Type.ZeroOpen);
return fst.getStart() <= snd.getStart() && fst.getEnd() >= snd.getEnd();
}
@Override
public boolean overlap(Interval<Integer> fst, Interval<Integer> snd) {
if (fst.isEmpty() || snd.isEmpty()) {
......@@ -76,7 +73,7 @@ public class IntegerIntervalOperations implements IntervalOperations<Integer> {
if (overlap(fst, snd)) {
return false;
}
// sort by start
if (fst.getStart() > snd.getStart()) {
Interval<Integer> tmp = fst;
......@@ -183,7 +180,7 @@ public class IntegerIntervalOperations implements IntervalOperations<Integer> {
@Override
public boolean leftOf(Interval<Integer> fst, Interval<Integer> snd) {
// normalize
// normalize
fst = fst.as(Interval.Type.OneClosed);
snd = snd.as(Interval.Type.OneClosed);
if (overlap(fst, snd)) {
......@@ -240,4 +237,20 @@ public class IntegerIntervalOperations implements IntervalOperations<Integer> {
return output;
}
@Override
public Interval<Integer> enclose(Iterable<? extends Interval<Integer>> interval) {
if (Iterables.isEmpty(interval)) {
return Intervals.EMPTY;
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (Interval<Integer> i : interval) {
i = i.as(Interval.Type.ZeroOpen);
min = Math.min(min, i.getStart());
max = Math.max(max, i.getEnd());
}
return Intervals.createInterval(min, max);
}
}
......@@ -22,6 +22,7 @@ import java.util.List;
* This class defines operations on intervals. Internally the operations should operate on ZeroOpen intervals to allow
* the handling of empty intervals.
*
* @param <L> The number type the interval uses.
* @author Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.de>
*/
public interface IntervalOperations<L extends Number> {
......@@ -159,4 +160,12 @@ public interface IntervalOperations<L extends Number> {
*/
List<Interval<L>> complement(Interval<L> fst, Iterable<? extends Interval<L>> snd);
/**
* Creates the smallest interval that encloses all intervals in the iterable.
*
* @param intervals
* @return
*/
Interval<Integer> enclose(Iterable<? extends Interval<Integer>> intervals);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment