Class RangeSet
java.lang.Object
io.github.mahmoudimus.quic.connection.RangeSet
- All Implemented Interfaces:
Iterable<RangeSet.Range>
A set of non-negative
long values stored as disjoint, non-adjacent half-open ranges, for
long runs with random insertion and removal: received packet numbers, ACK ranges, received and
acknowledged stream offsets.
Adjacent and overlapping ranges are always merged, so iteration yields the smallest possible set of ranges, in ascending order.
Threads. Not thread-safe; a set belongs to one connection, which serializes access.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA half-open range[start, end). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds every value of another set.voidclear()Removes every value.booleancontains(long x) Whether a value is in the set.The ranges in descending order, as ACK frames list them.elements()Every value, in ascending order.booleaninthashCode()booleaninsert(long start, long end) Adds the values[start, end), merging with overlapping and adjacent ranges.booleaninsertOne(long x) Adds one value.booleanisEmpty()Whether the set is empty.iterator()The ranges in ascending order.longmax()The largest value.longmin()The smallest value.peekMin()The first range.popMin()Removes and returns the first range.booleanremove(long start, long end) Removes the values[start, end).replace(long start, long end) Adds[start, end)and returns the parts of it that were already present, in ascending order.intsize()The number of disjoint ranges.voidRemoves every value of another set.toString()The ranges, as[a..b, c..d].Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
RangeSet
public RangeSet()An empty set. -
RangeSet
A copy of another set.- Parameters:
other- the set to copy
-
-
Method Details
-
contains
public boolean contains(long x) Whether a value is in the set.- Parameters:
x- the value- Returns:
- true if a range contains it
-
insertOne
public boolean insertOne(long x) Adds one value.- Parameters:
x- the value- Returns:
- true if the set changed
-
insert
public boolean insert(long start, long end) Adds the values[start, end), merging with overlapping and adjacent ranges.- Parameters:
start- the first valueend- one past the last value- Returns:
- true if the set changed; false for an empty range or one already contained
-
remove
public boolean remove(long start, long end) Removes the values[start, end).- Parameters:
start- the first valueend- one past the last value- Returns:
- true if the set changed
-
replace
Adds[start, end)and returns the parts of it that were already present, in ascending order. Used to deduplicate stream data received out of order.- Parameters:
start- the first valueend- one past the last value- Returns:
- the intersection of the previous set with the range
-
add
Adds every value of another set.- Parameters:
other- the values to add
-
subtract
Removes every value of another set.- Parameters:
other- the values to remove
-
isEmpty
public boolean isEmpty()Whether the set is empty.- Returns:
- true if it holds no values
-
size
public int size()The number of disjoint ranges.- Returns:
- the range count
-
min
public long min()The smallest value.- Returns:
- the start of the first range
- Throws:
NoSuchElementException- if the set is empty
-
max
public long max()The largest value.- Returns:
- one less than the end of the last range
- Throws:
NoSuchElementException- if the set is empty
-
peekMin
The first range.- Returns:
- the lowest range, or null if the set is empty
-
popMin
Removes and returns the first range.- Returns:
- the lowest range, or null if the set is empty
-
clear
public void clear()Removes every value. -
iterator
The ranges in ascending order.- Specified by:
iteratorin interfaceIterable<RangeSet.Range>- Returns:
- an iterator that does not support removal
-
descending
The ranges in descending order, as ACK frames list them.- Returns:
- the ranges, highest first
-
elements
Every value, in ascending order.- Returns:
- the values
-
equals
-
hashCode
public int hashCode() -
toString
The ranges, as[a..b, c..d].
-