org.metasyntactic.utilities
Class Odometer

java.lang.Object
  |
  +--org.metasyntactic.utilities.Odometer
All Implemented Interfaces:
java.util.Iterator

public class Odometer
extends java.lang.Object
implements java.util.Iterator

This class enumerates all values of an odometer for you. The values are returned to you as an int[]. In the zeroth place of the array is the most significant digit.

The class also can act as a permutation generator. The permutations are returned in lexicographic order. For example, if you want to generate all the 3-permutations of the numbers 0-3:

		Odometer o = new Odometer(3, 4, true);

	   while (o.hasMoreElements()) {
		   System.out.println(ArrayUtilities.toString(o.nextElement()));
	   }
 produces:
	  {0, 1, 2}
	  {0, 1, 3}
	  {0, 2, 1}
	  {0, 2, 3}
	  {0, 3, 1}
	  {0, 3, 2}
	  {1, 0, 2}
	  {1, 0, 3}
	  {1, 2, 0}
	  {1, 2, 3}
	  {1, 3, 0}
	  {1, 3, 2}
	  {2, 0, 1}
	  {2, 0, 3}
	  {2, 1, 0}
	  {2, 1, 3}
	  {2, 3, 0}
	  {2, 3, 1}
	  {3, 0, 1}
	  {3, 0, 2}
 


Field Summary
protected  int[] odometer
          The current states of the odometer.
protected  boolean ongoing
          We are done when we have generated the last valid odometer setting before a complete rollover
protected  boolean permute
          Are we generating only valid permutations? Or are we a normal odometer?
protected  int rollover
          The digit to rollover on.
 
Constructor Summary
Odometer(int digits, int rollover)
          Constructs a normal (non-permuting) odometer.
Odometer(int digits, int rollover, boolean permute)
           
 
Method Summary
 boolean hasNext()
           
static void main(java.lang.String[] args)
           
 java.lang.Object next()
           
 int[] nextPosition()
           
 void remove()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

odometer

protected final int[] odometer
The current states of the odometer. A copy of this will be returned on every call to 'nextElement'


rollover

protected final int rollover
The digit to rollover on. On a car speedometer this would be 10


permute

protected final boolean permute
Are we generating only valid permutations? Or are we a normal odometer?


ongoing

protected boolean ongoing
We are done when we have generated the last valid odometer setting before a complete rollover

Constructor Detail

Odometer

public Odometer(int digits,
                int rollover)
Constructs a normal (non-permuting) odometer.


Odometer

public Odometer(int digits,
                int rollover,
                boolean permute)
Method Detail

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator

remove

public void remove()
Specified by:
remove in interface java.util.Iterator

next

public java.lang.Object next()
Specified by:
next in interface java.util.Iterator

nextPosition

public int[] nextPosition()

main

public static void main(java.lang.String[] args)