Class IntPermutations

java.lang.Object
cc.redberry.combinatorics.IntPermutations
All Implemented Interfaces:
CombinatorialIterator<int[]>, Serializable, Iterable<int[]>, Iterator<int[]>

public final class IntPermutations extends Object implements CombinatorialIterator<int[]>
Iterator over all possible permutations.

Example

 IntPermutations its = new IntPermutations(3);
 while (its.hasNext())
     System.out.println(Arrays.toString(its.next()))
 
The result will be
      [0, 1, 2]
      [0, 2, 1]
      [1, 0, 2]
      [1, 2, 0]
      [2, 0, 1]
      [2, 1, 0]
 
It is also possible to iterate in the opposite direction via previous() method.

The iterator is implemented such that each next combination will be calculated only on the invocation of method next().

Note: method next() returns the same reference on each invocation.

Since:
1.0
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • permutation

      final int[] permutation
    • onFirst

      private boolean onFirst
    • size

      private final int size
  • Constructor Details

    • IntPermutations

      public IntPermutations(int dimension)
      Construct iterator over all permutations with specified dimension starting with identity.
      Parameters:
      dimension - dimension of permutations
    • IntPermutations

      public IntPermutations(int[] permutation)
      Construct iterator over permutations with specified permutation at the start. If starting permutation is not identity, the iterator will not iterate over all possible permutations, but only from starting permutation up to the last permutation, which is [size-1,size-2,....1,0].

      Note: parameter permutation is not copied in constructor and the same instance will be used during iteration.

      Parameters:
      permutation - starting permutation
      Throws:
      IllegalArgumentException - if permutation is inconsistent with one-line notation
  • Method Details

    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<int[]>
    • hasPrevious

      public boolean hasPrevious()
      Returns true if the iteration has more elements, iterating in back order. (In other words, returns true if previous() would return an element rather than throwing an exception.)
      Returns:
      true if the iteration has more elements
    • isLast

      private boolean isLast()
    • isFirst

      private boolean isFirst()
    • next

      public int[] next()
      Returns the next element in the iteration.
      Specified by:
      next in interface Iterator<int[]>
      Returns:
      the next element in the iteration
    • previous

      public int[] previous()
      Returns the previous element in the iteration.
      Returns:
      the previous element in the iteration
    • reset

      public void reset()
      Description copied from interface: CombinatorialIterator
      Resets the iteration
      Specified by:
      reset in interface CombinatorialIterator<int[]>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<int[]>
      Throws:
      UnsupportedOperationException - always
    • getDimension

      public int getDimension()
      Returns dimension specified in the constructor
      Returns:
      dimension specified in the constructor
    • current

      public int[] current()
      Description copied from interface: CombinatorialIterator
      Returns the reference on the current iteration element.
      Specified by:
      current in interface CombinatorialIterator<int[]>
      Returns:
      the reference on the current iteration element