kodkod.util.ints
Class ArrayIntSet

java.lang.Object
  extended by kodkod.util.ints.AbstractIntCollection
      extended by kodkod.util.ints.AbstractIntSet
          extended by kodkod.util.ints.ArrayIntSet
All Implemented Interfaces:
java.lang.Cloneable, IntCollection, IntSet

public final class ArrayIntSet
extends AbstractIntSet

An immutable set of integers, stored in a sorted array.

Author:
Emina Torlak
specfield:
ints: set int

Constructor Summary
ArrayIntSet(int[] ints)
          Constructs a set view for the given array.
ArrayIntSet(IntSet s)
          Constructs an ArrayIntSet that is equal to the given set.
 
Method Summary
 int ceil(int i)
          Returns the smallest element in this set that is greater than or equal to i.
 boolean contains(int i)
          Returns true if i is in this set.
 boolean equals(java.lang.Object o)
          Compares the specified object with this set for equality.
 int floor(int i)
          Returns the largest element in this set that is smaller than or equal to i.
 int hashCode()
          Returns the hash code value for this set.
 IntIterator iterator(int from, int to)
          Returns an iterator over the elements of this set that are in the closed range [from..to].
 int max()
          Returns the smallest element in this set.
 int min()
          Returns the largest element in this set.
 int size()
          Returns the number of elements in this collection.
 int[] toArray()
          Returns the result of calling AbstractIntCollection.toArray(int[]) with a freshly constructed array of length this.size().
 int[] toArray(int[] array)
          Copies the elements of this collection into the specified array, provided that it is large enough, and returns it. If the array is not large enough, the effect of this method is the same as calling IntCollection.toArray().
 
Methods inherited from class kodkod.util.ints.AbstractIntSet
clone, iterator, toString
 
Methods inherited from class kodkod.util.ints.AbstractIntCollection
add, addAll, clear, containsAll, isEmpty, remove, removeAll, retainAll
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface kodkod.util.ints.IntSet
add, addAll, clear, containsAll, isEmpty, remove, removeAll, retainAll
 

Constructor Detail

ArrayIntSet

public ArrayIntSet(int[] ints)
Constructs a set view for the given array. The array must contain no duplicates, its elements must be sorted in the ascending order, and its contents must not be changed while it is in use by this set

effects:
this.ints' = ints
requires:
all i, j: [0..ints.length) | i < j => array[i] <= array[j]

ArrayIntSet

public ArrayIntSet(IntSet s)
Constructs an ArrayIntSet that is equal to the given set.

effects:
this.ints' = s.ints
Method Detail

iterator

public IntIterator iterator(int from,
                            int to)
Returns an iterator over the elements of this set that are in the closed range [from..to]. If from < to, the elements are returned in the ascending order. Otherwise, they are returned in the descending order.

Returns:
an iterator over the elements in this set that are in the closed range [from..to].
See Also:
IntSet.iterator(int, int)

size

public int size()
Returns the number of elements in this collection.

Returns:
number of elements in this collection.
See Also:
IntSet.size()

ceil

public int ceil(int i)
Description copied from interface: IntSet
Returns the smallest element in this set that is greater than or equal to i. If this is emtpy or i is greater than this.max(), NoSuchElementException is thrown.

Returns:
{j: this.ints | j >= i && no k: this.ints - j | k < j && k >= i}
See Also:
IntSet.ceil(int)

floor

public int floor(int i)
Description copied from interface: IntSet
Returns the largest element in this set that is smaller than or equal to i. If this is emtpy or i is less than this.min(), NoSuchElementException is thrown.

Returns:
{j: this.ints | j <= i && no k: this.ints - j | k > j && k <= i}
See Also:
IntSet.floor(int)

contains

public boolean contains(int i)
Returns true if i is in this set.

Specified by:
contains in interface IntCollection
Specified by:
contains in interface IntSet
Overrides:
contains in class AbstractIntCollection
Returns:
i in this.ints
See Also:
IntSet.contains(int)

max

public int max()
Returns the smallest element in this set. Throws a NoSuchElementException if this set is empty.

Specified by:
max in interface IntSet
Overrides:
max in class AbstractIntSet
Returns:
min(this.ints)
Throws:
java.util.NoSuchElementException - - no this.ints
See Also:
IntSet.max()

min

public int min()
Returns the largest element in this set. Throws a NoSuchElementException if this set is empty.

Specified by:
min in interface IntSet
Overrides:
min in class AbstractIntSet
Returns:
max(this.ints)
Throws:
java.util.NoSuchElementException - - no this.ints
See Also:
IntSet.min()

toArray

public int[] toArray()
Returns the result of calling AbstractIntCollection.toArray(int[]) with a freshly constructed array of length this.size().

Specified by:
toArray in interface IntCollection
Specified by:
toArray in interface IntSet
Overrides:
toArray in class AbstractIntCollection
Returns:
this.toArray(new int[size()])
See Also:
IntCollection.toArray()

toArray

public int[] toArray(int[] array)
Copies the elements of this collection into the specified array, provided that it is large enough, and returns it. If the array is not large enough, the effect of this method is the same as calling IntCollection.toArray().

Specified by:
toArray in interface IntCollection
Specified by:
toArray in interface IntSet
Overrides:
toArray in class AbstractIntCollection
Returns:
the given array, filled with the elements from this collection, if the it is large enough; otherwise a new array containing all of the elements in this collection.
See Also:
IntCollection.toArray(int[])

hashCode

public int hashCode()
Returns the hash code value for this set. The hash code of a set is defined to be the Ints.superFastHash(int[]) of the elements in the set, taken in the ascending order of values. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two IntSets s1 and s2, as required by the general contract of the Object.hashCode method.

Specified by:
hashCode in interface IntSet
Overrides:
hashCode in class AbstractIntSet
Returns:
Ints.superFastHash(this.toArray())
See Also:
IntSet.hashCode()

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this set for equality. Returns true if the specified object is also an IntSet, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the IntSet interface.

Specified by:
equals in interface IntSet
Overrides:
equals in class AbstractIntSet
Returns:
o instanceof IntSet and o.size() = this.size() and this.containsAll(o)
See Also:
IntSet.equals(java.lang.Object)