it.unimi.dsi.util
Class Interval

java.lang.Object
  extended by it.unimi.dsi.fastutil.ints.AbstractIntCollection
      extended by it.unimi.dsi.fastutil.ints.AbstractIntSet
          extended by it.unimi.dsi.fastutil.ints.AbstractIntSortedSet
              extended by it.unimi.dsi.util.Interval
All Implemented Interfaces:
IntCollection, IntIterable, IntSet, IntSortedSet, Serializable, Cloneable, Iterable<Integer>, Collection<Integer>, Set<Integer>, SortedSet<Integer>

public final class Interval
extends AbstractIntSortedSet
implements IntSortedSet, Serializable

An interval of integers. An interval is defined by two integers, called its left and right extremes, and contains all integers x such that left ≤ x ≤ right.

This class has no constructor: use the static factory methods valueOf(int, int) and valueOf(int), instead.

Instances of this class are immutable, and moreover implement the IntSortedSet interface. The equality method allows to check equality with both sorted and non-sorted sets of integers.

To reduce garbage collection, intervals made of one non-negative small points are precomputed and returned upon request.

See Also:
Serialized Form

Field Summary
 int left
          The left extreme of the interval.
 int right
          The right extreme of the interval.
 
Constructor Summary
protected Interval(int left, int right)
          Builds an interval with given extremes.
 
Method Summary
 IntComparator comparator()
           
 int compareTo(int x)
          Compares this interval to an integer.
 int compareTo(int x, int radius)
          Compares this interval to an integer with a specified radius.
 int compareTo(int x, int leftRadius, int rightRadius)
          Compares this interval to an integer with specified left and right radii.
 boolean contains(int x)
          Checks whether this interval contains the specified integer.
 boolean contains(Interval interval)
          Checks whether this interval contains the specified interval.
 boolean contains(int x, int radius)
          Checks whether this interval would contain the specified integer if enlarged in both directions by the specified radius.
 boolean contains(int x, int leftRadius, int rightRadius)
          Checks whether this interval would contain the specified integer if enlarged in each direction with the respective radius.
 boolean equals(Object o)
          Checks whether this interval is equal to another set of integers.
 int firstInt()
           
 int hashCode()
           
 IntSortedSet headSet(int to)
           
 IntBidirectionalIterator iterator()
          Returns an iterator over the integers in this interval.
 IntBidirectionalIterator iterator(int from)
          Returns an iterator over the integers in this interval larger than or equal to a given integer.
 int lastInt()
           
 int length()
          Returns the interval length, that is, the number of integers contained in the interval.
 int size()
          An alias for length().
 IntSortedSet subSet(int from, int to)
           
 IntSortedSet tailSet(int from)
           
 String toString()
           
static Interval valueOf(int point)
          Returns a one-point interval.
static Interval valueOf(int left, int right)
          Returns an interval with given extremes.
 
Methods inherited from class it.unimi.dsi.fastutil.ints.AbstractIntSortedSet
first, headSet, intIterator, last, subSet, tailSet
 
Methods inherited from class it.unimi.dsi.fastutil.ints.AbstractIntSet
rem, remove, remove
 
Methods inherited from class it.unimi.dsi.fastutil.ints.AbstractIntCollection
add, add, addAll, addAll, clear, contains, containsAll, containsAll, isEmpty, rem, removeAll, removeAll, retainAll, retainAll, toArray, toArray, toArray, toIntArray, toIntArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface it.unimi.dsi.fastutil.ints.IntSortedSet
headSet, intIterator, subSet, tailSet
 
Methods inherited from interface it.unimi.dsi.fastutil.ints.IntSet
remove
 
Methods inherited from interface it.unimi.dsi.fastutil.ints.IntCollection
add, addAll, containsAll, rem, removeAll, retainAll, toArray, toArray, toIntArray, toIntArray
 
Methods inherited from interface java.util.SortedSet
first, last
 
Methods inherited from interface java.util.Set
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

left

public final int left
The left extreme of the interval.


right

public final int right
The right extreme of the interval.

Constructor Detail

Interval

protected Interval(int left,
                   int right)
Builds an interval with given extremes.

You cannot generate an empty interval with this constructor. Use Intervals.EMPTY_INTERVAL instead.

Parameters:
left - the left extreme.
right - the right extreme (which must be greater than or equal to the left extreme).
Method Detail

valueOf

public static Interval valueOf(int left,
                               int right)
Returns an interval with given extremes.

You cannot obtain an empty interval with this factory method. Use Intervals.EMPTY_INTERVAL instead.

Parameters:
left - the left extreme.
right - the right extreme (which must be greater than or equal to the left extreme).
Returns:
an interval with the given extremes.

valueOf

public static Interval valueOf(int point)
Returns a one-point interval.

You cannot obtain an empty interval with this factory method. Use Intervals.EMPTY_INTERVAL instead.

Parameters:
point - a point.
Returns:
a one-point interval

length

public int length()
Returns the interval length, that is, the number of integers contained in the interval.

Returns:
the interval length.

size

public int size()
An alias for length().

Specified by:
size in interface Collection<Integer>
Specified by:
size in interface Set<Integer>
Returns:
the interval length.

iterator

public IntBidirectionalIterator iterator()
Returns an iterator over the integers in this interval.

Specified by:
iterator in interface IntCollection
Specified by:
iterator in interface IntIterable
Specified by:
iterator in interface IntSet
Specified by:
iterator in interface IntSortedSet
Specified by:
iterator in interface Iterable<Integer>
Specified by:
iterator in interface Collection<Integer>
Specified by:
iterator in interface Set<Integer>
Overrides:
iterator in class AbstractIntSortedSet
Returns:
an integer iterator over the elements in this interval.

iterator

public IntBidirectionalIterator iterator(int from)
Returns an iterator over the integers in this interval larger than or equal to a given integer.

Specified by:
iterator in interface IntSortedSet
Parameters:
from - the starting integer.
Returns:
an integer iterator over the elements in this interval.

contains

public boolean contains(int x)
Checks whether this interval contains the specified integer.

Specified by:
contains in interface IntCollection
Parameters:
x - an integer.
Returns:
whether this interval contains x, that is, whether leftxright.

contains

public boolean contains(Interval interval)
Checks whether this interval contains the specified interval.

Parameters:
interval - an interval.
Returns:
whether this interval contains (as a set) interval.

contains

public boolean contains(int x,
                        int radius)
Checks whether this interval would contain the specified integer if enlarged in both directions by the specified radius.

Parameters:
x - an integer.
radius - the radius.
Returns:
whether this interval enlarged by radius would contain x, e.g., whether leftradiusxright+radius.

contains

public boolean contains(int x,
                        int leftRadius,
                        int rightRadius)
Checks whether this interval would contain the specified integer if enlarged in each direction with the respective radius. directions by the specified radius.

Parameters:
x - an integer.
leftRadius - the left radius.
rightRadius - the right radius.
Returns:
whether this interval enlarged to the left by leftRadius and to the right by rightRadius would contain x, e.g., whether leftleftRadiusxright+rightRadius.

compareTo

public int compareTo(int x)
Compares this interval to an integer.

Parameters:
x - an integer.
Returns:
a negative integer, zero, or a positive integer as x is positioned at the left, belongs, or is positioned to the right of this interval, e.g., as x < left, leftxright or right < x.

compareTo

public int compareTo(int x,
                     int radius)
Compares this interval to an integer with a specified radius.

Parameters:
x - an integer.
radius - the radius.
Returns:
a negative integer, zero, or a positive integer as x is positioned at the left, belongs, or is positioned to the right of this interval enlarged by radius, that is, as x < leftradius, leftradiusxright+radius or right+radius < x.

compareTo

public int compareTo(int x,
                     int leftRadius,
                     int rightRadius)
Compares this interval to an integer with specified left and right radii.

Parameters:
x - an integer.
leftRadius - the left radius.
rightRadius - the right radius.
Returns:
a negative integer, zero, or a positive integer as x is positioned at the left, belongs, or is positioned to the right of this interval enlarged by leftRadius on the left and rightRadius in the right, that is, as x < leftleftRadius, leftleftRadiusxright+rightRadius or right+rightRadius < x.

comparator

public IntComparator comparator()
Specified by:
comparator in interface IntSortedSet
Specified by:
comparator in interface SortedSet<Integer>

headSet

public IntSortedSet headSet(int to)
Specified by:
headSet in interface IntSortedSet

tailSet

public IntSortedSet tailSet(int from)
Specified by:
tailSet in interface IntSortedSet

subSet

public IntSortedSet subSet(int from,
                           int to)
Specified by:
subSet in interface IntSortedSet

firstInt

public int firstInt()
Specified by:
firstInt in interface IntSortedSet

lastInt

public int lastInt()
Specified by:
lastInt in interface IntSortedSet

toString

public String toString()
Overrides:
toString in class AbstractIntCollection

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<Integer>
Specified by:
hashCode in interface Set<Integer>
Overrides:
hashCode in class AbstractIntSet

equals

public boolean equals(Object o)
Checks whether this interval is equal to another set of integers.

Specified by:
equals in interface Collection<Integer>
Specified by:
equals in interface Set<Integer>
Overrides:
equals in class AbstractIntSet
Parameters:
o - an object.
Returns:
true if o is an ordered set of integer containing the same element of this interval in the same order, or if o is a set of integers containing the same elements of this interval.