kodkod.util.ints
Class IntBitSet

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

public final class IntBitSet
extends AbstractIntSet
implements java.lang.Cloneable

An implementation of the IntSet interface based on a bit map. An IntBitSet can store only numbers in the half-open range [0..capacity) where capacity is a user-specified value. The implementation will allocated enough bits to explicitly represent all allowed integers; it performs better than a tree set when the stored integers are not clustered.

Author:
Emina Torlak
invariant:
all i: this.ints | 0 <= i < capacity
specfield:
capacity: [0..Integer.MAX_VALUE]

Constructor Summary
IntBitSet(int capacity)
          Constructs an empty IntBitSet that can store up to capacity elements.
IntBitSet(int capacity, long[] data)
          Constructs an IntBitSet that can store up to capacity elements.
 
Method Summary
 boolean add(int i)
          Adds the given integer to this set if not already present and returns true.
 boolean addAll(IntCollection other)
          Adds all of the elements in the specified collection to this collection. Returns true if this collection has changed as a result of the call.
 int capacity()
          Returns the capacity of this int bit set
 int ceil(int i)
          Returns the smallest element in this set that is greater than or equal to i.
 void clear()
          Removes all elements from this set.
 IntBitSet clone()
          Returns a copy of this int bit set.
 boolean contains(int i)
          Returns true if i is in this set.
 boolean containsAll(IntCollection other)
          Returns true if this collection contains all of the elements in the specified collection.
 int floor(int i)
          Returns the largest element in this set that is smaller than or equal to i.
 boolean isEmpty()
          Returns true if this collection has no elements; otherwise returns false.
 IntIterator iterator()
          Returns an ascending iterator over all elements in 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 largest element in this set.
 int min()
          Returns the smallest element in this set.
 boolean remove(int i)
          Removes the given integer from this set if already present and returns true.
 boolean removeAll(IntCollection other)
          Removes all of this collection's elements that are also contained in the specified collection. After this call returns, this collection will contain no elements in common with the specified collection. Returns true if this collection has changed as a result of the call.
 boolean retainAll(IntCollection other)
          Retains only the elements in this collection that are contained in the specified collection. In other words, removes from this collection all of its elements that are not contained in the specified collection. Returns true if this collection has changed as a result of the call.
 int size()
          Returns the number of elements in this collection.
 
Methods inherited from class kodkod.util.ints.AbstractIntSet
equals, hashCode, toString
 
Methods inherited from class kodkod.util.ints.AbstractIntCollection
toArray, toArray
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface kodkod.util.ints.IntSet
toArray, toArray
 

Constructor Detail

IntBitSet

public IntBitSet(int capacity)
Constructs an empty IntBitSet that can store up to capacity elements.

Throws:
java.lang.IllegalArgumentException - - capacity < 0
effects:
no this.ints' && this.capacity' = capacity

IntBitSet

public IntBitSet(int capacity,
                 long[] data)
Constructs an IntBitSet that can store up to capacity elements. The set is initialized to contain all integers i such that data[i>>>6] & (1L<
Throws:
java.lang.IllegalArgumentException - - capacity is out of range
requires:
0 <= capacity < max({i: int | data[i>>>6] & (1L<>>6)+1 > data.length
Method Detail

min

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

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

max

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

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

ceil

public int ceil(int i)
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.

Specified by:
ceil in interface IntSet
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)
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.

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

iterator

public IntIterator iterator()
Returns an ascending iterator over all elements in this set. This method calls this.iterator(Integer.MIN_VALUE, Integer.MAX_VALUE).

Specified by:
iterator in interface IntCollection
Specified by:
iterator in interface IntSet
Overrides:
iterator in class AbstractIntSet
Returns:
an ascending iterator over all elements in this set.
See Also:
IntSet.iterator()

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.

Specified by:
iterator in interface IntSet
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.

Specified by:
size in interface IntCollection
Specified by:
size in interface IntSet
Returns:
number of elements in this collection.
See Also:
IntSet.size()

capacity

public int capacity()
Returns the capacity of this int bit set

Returns:
this.capacity

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)

add

public boolean add(int i)
Adds the given integer to this set if not already present and returns true. Otherwise does nothing and returns false.

Specified by:
add in interface IntCollection
Specified by:
add in interface IntSet
Overrides:
add in class AbstractIntCollection
Returns:
i in this.ints'
Throws:
java.lang.IllegalArgumentException - - i !in [0..this.capacity)
See Also:
IntSet.add(int)
effects:
this.ints' = this.ints + i

remove

public boolean remove(int i)
Removes the given integer from this set if already present and returns true. Otherwise does nothing and returns false.

Specified by:
remove in interface IntCollection
Specified by:
remove in interface IntSet
Overrides:
remove in class AbstractIntCollection
Returns:
i !in this.ints'
See Also:
IntSet.remove(int)
effects:
this.ints' = this.ints - i

isEmpty

public boolean isEmpty()
Returns true if this collection has no elements; otherwise returns false.

Specified by:
isEmpty in interface IntCollection
Specified by:
isEmpty in interface IntSet
Overrides:
isEmpty in class AbstractIntCollection
Returns:
true if this collection has no elements, false otherwise.
See Also:
IntCollection.isEmpty()

containsAll

public boolean containsAll(IntCollection other)
Returns true if this collection contains all of the elements in the specified collection.

Specified by:
containsAll in interface IntCollection
Specified by:
containsAll in interface IntSet
Overrides:
containsAll in class AbstractIntCollection
Returns:
true if this collection contains all of the elements in the specified collection.
See Also:
IntSet.containsAll(kodkod.util.ints.IntCollection)

addAll

public boolean addAll(IntCollection other)
Adds all of the elements in the specified collection to this collection. Returns true if this collection has changed as a result of the call.

Specified by:
addAll in interface IntCollection
Specified by:
addAll in interface IntSet
Overrides:
addAll in class AbstractIntCollection
Returns:
true if this collection has changed as a result of the call
See Also:
IntSet.addAll(kodkod.util.ints.IntCollection)

retainAll

public boolean retainAll(IntCollection other)
Retains only the elements in this collection that are contained in the specified collection. In other words, removes from this collection all of its elements that are not contained in the specified collection. Returns true if this collection has changed as a result of the call.

Specified by:
retainAll in interface IntCollection
Specified by:
retainAll in interface IntSet
Overrides:
retainAll in class AbstractIntCollection
Returns:
rue if this collection has changed as a result of the call
See Also:
IntSet.retainAll(kodkod.util.ints.IntCollection)

removeAll

public boolean removeAll(IntCollection other)
Removes all of this collection's elements that are also contained in the specified collection. After this call returns, this collection will contain no elements in common with the specified collection. Returns true if this collection has changed as a result of the call.

Specified by:
removeAll in interface IntCollection
Specified by:
removeAll in interface IntSet
Overrides:
removeAll in class AbstractIntCollection
Returns:
true if this collection has changed as a result of the call
See Also:
IntSet.removeAll(kodkod.util.ints.IntCollection)

clear

public void clear()
Removes all elements from this set.

Specified by:
clear in interface IntCollection
Specified by:
clear in interface IntSet
Overrides:
clear in class AbstractIntCollection
See Also:
IntCollection.clear()
effects:
no this.ints'

clone

public IntBitSet clone()
Returns a copy of this int bit set. The copy is independent of this IntSet.

Specified by:
clone in interface IntSet
Overrides:
clone in class AbstractIntSet
Returns:
a copy of this IntSet.
See Also:
IntSet.clone()