L
- type of labels in this set, must be immutablepublic interface IntervalSet<L>
L
and must implement the Object
contract: they are compared for equality using Object.equals(java.lang.Object)
.
For example, { "A"=[0,10), "B"=[20,30) } is an interval set where the labels are Strings "A" and "B". We could add "C"=[10,20) to such a set, but not "D"=[25,35) since that interval overlaps with "B"=[20,30).
PS2 instructions: this is a required ADT interface. You may not change the specifications or add new methods.
Modifier and Type | Method | Description |
---|---|---|
static <L> IntervalSet<L> |
empty() |
Create an empty interval set.
|
long |
end(L label) |
Get the end of an interval.
|
void |
insert(long start,
long end,
L label) |
Add a labeled interval (if not present) to this set, if it does not conflict
with existing intervals.
|
Set<L> |
labels() |
Get the labels in this set.
|
boolean |
remove(L label) |
Remove a labeled interval from this set, if present.
|
long |
start(L label) |
Get the start of an interval.
|
static <L> IntervalSet<L> empty()
L
- type of labels in the set, must be immutablevoid insert(long start, long end, L label) throws IntervalConflictException
Labeled intervals conflict if:
For example, if this set is { "A"=[0,10), "B"=[20,30) },
start
- low end of the interval, inclusiveend
- high end of the interval, exclusive, must be greater than startlabel
- label to addIntervalConflictException
- if label is already in this set and its
interval is not [start,end), or if an interval in this set with
a different label overlaps [start,end)boolean remove(L label)
label
- label to removelong start(L label) throws NoSuchElementException
label
- the labelNoSuchElementException
- if label is not in this setlong end(L label) throws NoSuchElementException
label
- the labelNoSuchElementException
- if label is not in this set