|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcommon.TupleIterator
public class TupleIterator
This class is an iterator over all tuples in a finite cartesian product of finite sets. Some special cases: if the number of sets passed in is zero, then the Cartesian product contains one element, the empty tuple. If an empty set is passed in, then the Cartesian product is empty.
Constructor Summary | |
---|---|
TupleIterator(java.util.List cartproduct)
Constructor. |
Method Summary | |
---|---|
boolean |
hasNext()
Returns true if there are any more elements in the Cartesian product to return. |
static void |
main(java.lang.String[] args)
Test program. |
java.lang.Object |
next()
Returns another tuple not returned previously. |
void |
remove()
OPTIONAL METHOD -- NOT IMPLEMENTED. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TupleIterator(java.util.List cartproduct)
cartproduct
- a List of Collections from which the
tuples' components are to be draw.Method Detail |
---|
public boolean hasNext()
hasNext
in interface java.util.Iterator
public java.lang.Object next()
The iterator stores its state in the private member
currstate
-- an ArrayList of the iterators of the
individual Collections in the cartesian product. In the start state,
each iterator returns a single element. Afterwards, while iterator #1
has anything to return, we replace the first element of the previous
tuple to obtain a new tuple. Once iterator #1 runs out of elements we
replace it and advance iterator #2. We keep on advancing iterator #1
until it runs out of elements for the second time, reinitialize it
again, and advance iterator #2 once more. We repeat these operations
until iterator #2 runs out of elements and we start advancing
iterator #3, and so on, until all iterators run out of elements.
This method of generating the m-tuples is very similar to producing all numbers of m "digits" in the order of their magnitude, where the i-th "digit" is in basei = #(i-th Collection in the cartesian product) and we assume that the "numbers" in the i-th Collection are ordered in some way.
next
in interface java.util.Iterator
public void remove()
remove
in interface java.util.Iterator
public static void main(java.lang.String[] args)
> java -cp . common.TupleIterator "a b" "c d"
[a, c]
[a, d]
[b, c]
[b, d]
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |