L
- type of labels in this set, must be immutablepublic class MultiIntervalSet<L> extends Object
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 a multi-interval set where the labels are Strings "A" and "B". We could add "A"=[10,20) to that set to obtain { "A"=[[0,10),[10,20)], "B"=[[20,30)] }.
PS2 instructions: this is a required ADT class. You may not change the specifications or add new public methods. You must use IntervalSet in your rep, but otherwise the implementation of this class is up to you.
Constructor | Description |
---|---|
MultiIntervalSet() |
Create an empty multi-interval set.
|
MultiIntervalSet(IntervalSet<L> initial) |
Create a new multi-interval set containing the given labeled intervals.
|
Modifier and Type | Method | Description |
---|---|---|
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.
|
IntervalSet<Integer> |
intervals(L label) |
Get all the intervals in this set associated with a given label.
|
Set<L> |
labels() |
Get the labels in this set.
|
boolean |
removeAll(L label) |
Remove all intervals of the given label from this set, if any.
|
String |
toString() |
public MultiIntervalSet()
public MultiIntervalSet(IntervalSet<L> initial)
initial
- initial contents of the new setpublic void insert(long start, long end, L label) throws IntervalConflictException
Labeled intervals conflict if:
For example, if this set is { "A"=[[0,10),[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
associated with an interval other than [start,end) that overlaps
[start,end), or if an interval in this set with a different label
overlaps [start,end)public boolean removeAll(L label)
label
- label to removepublic IntervalSet<Integer> intervals(L label) throws NoSuchElementException
For example, if this set is { "A"=[[0,10),[20,30)], "B"=[[10,20)] },
label
- the labelNoSuchElementException
- if label is not in this set