kodkod.instance
Class TupleSet

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<Tuple>
          extended by kodkod.instance.TupleSet
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<Tuple>, java.util.Collection<Tuple>, java.util.Set<Tuple>

public final class TupleSet
extends java.util.AbstractSet<Tuple>
implements java.lang.Cloneable

Represents a set of tuples of a given arity, constructed over a given universe. All polymorphic methods throw a ClassCastException when passed an element that is not a Tuple. All methods throw a NullPointerException when passed null. The iterator of a TupleSet returns tuples in the order of their indeces.

Author:
Emina Torlak
invariant:
tuples.arity = arity && tuples.universe = universe
specfield:
tuples: set Tuple
universe: Universe
arity: int

Method Summary
 boolean add(Tuple t)
          Adds the specified tuple to this tupleset.
 boolean addAll(java.util.Collection<? extends Tuple> c)
          Adds all tuples from c to this, if not present, and returns true.
 int arity()
          Returns this.arity
 int capacity()
          Returns the capacity of this set -- the maximum number of tuples that it can hold, given its universe and arity.
 void clear()
          Removes all tuples from this tupleset.
 TupleSet clone()
          Returns a deep copy of this tuple set.
 boolean contains(java.lang.Object o)
          Returns true if this contains the given object.
 boolean containsAll(java.util.Collection<?> c)
          Returns true if this contains all tuples from c.
 boolean equals(java.lang.Object o)
          Returns true if o contains the same tuples as this.
 int hashCode()
          
 IntSet indexView()
          Returns an unmodifiable int set view of the tuples stored in this set.
 java.util.Iterator<Tuple> iterator()
          Returns an iterator over the tuples in this tupleset.
 TupleSet product(TupleSet s)
          Returns a tuple set that is the cross product of this and the specified set.
 TupleSet project(int dimension)
          Projects this TupleSet onto the given dimension.
 boolean remove(java.lang.Object o)
          Removes the given object from this tupleset, if present, and returns true.
 boolean removeAll(java.util.Collection<?> c)
          Removes all tuples in c from this, if present, and returns true.
 boolean retainAll(java.util.Collection<?> c)
          Removes all tuples from this that are not in c, if any, and returns true.
 int size()
          Returns the size of this tupleset.
 Universe universe()
          Returns this.universe.
 TupleSet unmodifiableView()
          Returns an unmodifiable view of the this tupleset.
 
Methods inherited from class java.util.AbstractCollection
isEmpty, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
isEmpty, toArray, toArray
 

Method Detail

capacity

public final int capacity()
Returns the capacity of this set -- the maximum number of tuples that it can hold, given its universe and arity.

Returns:
this.universe.size() ^ this.arity

universe

public Universe universe()
Returns this.universe.

Returns:
this.universe

arity

public int arity()
Returns this.arity

Returns:
this.arity

indexView

public IntSet indexView()
Returns an unmodifiable int set view of the tuples stored in this set. Specifically, the returned int set contains an integer i iff this set contains a tuple with the index i. The view is backed by this set, so changes to this set are reflected in the index set.

Returns:
{ s: IntSet | s.ints = {i: int | some t: this.tuples | t.index = i}

unmodifiableView

public TupleSet unmodifiableView()
Returns an unmodifiable view of the this tupleset. This method allows modules to provide "read-only" access to internal tuple sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.

Returns:
an unmodifiable view of the this tupleset

product

public TupleSet product(TupleSet s)
Returns a tuple set that is the cross product of this and the specified set.

Returns:
{t: TupleSet | t.arity = this.arity + s.arity && t.universe = this.universe && t.tuples = this.tuples->s.tuples }
Throws:
java.lang.NullPointerException - - s = null
java.lang.IllegalArgumentException - - s.universe != this.universe

project

public TupleSet project(int dimension)
Projects this TupleSet onto the given dimension.

Returns:
{s: TupleSet | s.arity = 1 && s.universe = this.universe && s.tuples = { t: Tuple | some q: this.tuples | q.atoms[dimension] = t.atoms[int] } }
Throws:
java.lang.IllegalArgumentException - - dimension < 0 || dimension >= this.arity

clone

public TupleSet clone()
Returns a deep copy of this tuple set.

Overrides:
clone in class java.lang.Object
Returns:
{s: TupleSet - this | s.universe = this.universe && s.tuples = this.tuples }

iterator

public java.util.Iterator<Tuple> iterator()
Returns an iterator over the tuples in this tupleset.

Specified by:
iterator in interface java.lang.Iterable<Tuple>
Specified by:
iterator in interface java.util.Collection<Tuple>
Specified by:
iterator in interface java.util.Set<Tuple>
Specified by:
iterator in class java.util.AbstractCollection<Tuple>
Returns:
an iterator over the tuples in this tupleset.

contains

public boolean contains(java.lang.Object o)
Returns true if this contains the given object.

Specified by:
contains in interface java.util.Collection<Tuple>
Specified by:
contains in interface java.util.Set<Tuple>
Overrides:
contains in class java.util.AbstractCollection<Tuple>
Returns:
o in this.tuples
Throws:
java.lang.IllegalArgumentException - o.arity != this.arity || o.universe != this.universe

size

public int size()
Returns the size of this tupleset.

Specified by:
size in interface java.util.Collection<Tuple>
Specified by:
size in interface java.util.Set<Tuple>
Specified by:
size in class java.util.AbstractCollection<Tuple>
Returns:
#this.tuples

clear

public void clear()
Removes all tuples from this tupleset.

Specified by:
clear in interface java.util.Collection<Tuple>
Specified by:
clear in interface java.util.Set<Tuple>
Overrides:
clear in class java.util.AbstractCollection<Tuple>
effects:
no this.tuples'

add

public boolean add(Tuple t)
Adds the specified tuple to this tupleset. Returns true if this set was changed as the result of the operation.

Specified by:
add in interface java.util.Collection<Tuple>
Specified by:
add in interface java.util.Set<Tuple>
Overrides:
add in class java.util.AbstractCollection<Tuple>
Returns:
o !in this.tuples
Throws:
java.lang.IllegalArgumentException - - t.universe != this.universe || t.arity != this.arity
effects:
this.tuples' = this.tuples + t

remove

public boolean remove(java.lang.Object o)
Removes the given object from this tupleset, if present, and returns true. Otherwise does nothing and returns false.

Specified by:
remove in interface java.util.Collection<Tuple>
Specified by:
remove in interface java.util.Set<Tuple>
Overrides:
remove in class java.util.AbstractCollection<Tuple>
Returns:
o in this.tuples
Throws:
java.lang.IllegalArgumentException - - o.universe != this.universe || o.arity != this.arity
effects:
this.tuples' = this.tuples - o

containsAll

public boolean containsAll(java.util.Collection<?> c)
Returns true if this contains all tuples from c. Otherwise returns false.

Specified by:
containsAll in interface java.util.Collection<Tuple>
Specified by:
containsAll in interface java.util.Set<Tuple>
Overrides:
containsAll in class java.util.AbstractCollection<Tuple>
Returns:
c.elements in this.tuples
Throws:
java.lang.IllegalArgumentException - - some t: c.elements | t.universe != this.universe || t.arity != this.arity

addAll

public boolean addAll(java.util.Collection<? extends Tuple> c)
Adds all tuples from c to this, if not present, and returns true. Otherwise does nothing and returns false.

Specified by:
addAll in interface java.util.Collection<Tuple>
Specified by:
addAll in interface java.util.Set<Tuple>
Overrides:
addAll in class java.util.AbstractCollection<Tuple>
Returns:
c.elements !in this.tuples
Throws:
java.lang.IllegalArgumentException - - some t: c.elements | t.universe != this.universe || t.arity != this.arity
effects:
this.tuples' = this.tuples + c.elements

removeAll

public boolean removeAll(java.util.Collection<?> c)
Removes all tuples in c from this, if present, and returns true. Otherwise does nothing and returns false.

Specified by:
removeAll in interface java.util.Collection<Tuple>
Specified by:
removeAll in interface java.util.Set<Tuple>
Overrides:
removeAll in class java.util.AbstractSet<Tuple>
Returns:
some c.elements & this.tuples
Throws:
java.lang.IllegalArgumentException - - some t: c.elements | t.universe != this.universe || t.arity != this.arity
effects:
this.tuples' = this.tuples - c.elements

retainAll

public boolean retainAll(java.util.Collection<?> c)
Removes all tuples from this that are not in c, if any, and returns true. Otherwise does nothing and returns false.

Specified by:
retainAll in interface java.util.Collection<Tuple>
Specified by:
retainAll in interface java.util.Set<Tuple>
Overrides:
retainAll in class java.util.AbstractCollection<Tuple>
Returns:
this.tuples !in c.elements
Throws:
java.lang.IllegalArgumentException - - some t: c.elements | t.universe != this.universe || t.arity != this.arity
effects:
this.tuples' = this.tuples & c.elements

equals

public boolean equals(java.lang.Object o)
Returns true if o contains the same tuples as this.

Specified by:
equals in interface java.util.Collection<Tuple>
Specified by:
equals in interface java.util.Set<Tuple>
Overrides:
equals in class java.util.AbstractSet<Tuple>
Returns:
this.tuples = o.elements

hashCode

public int hashCode()

Specified by:
hashCode in interface java.util.Collection<Tuple>
Specified by:
hashCode in interface java.util.Set<Tuple>
Overrides:
hashCode in class java.util.AbstractSet<Tuple>