Class IntPermutations

  • All Implemented Interfaces:
    CombinatorialIterator<int[]>, java.io.Serializable, java.lang.Iterable<int[]>, java.util.Iterator<int[]>

    public final class IntPermutations
    extends java.lang.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:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      IntPermutations​(int dimension)
      Construct iterator over all permutations with specified dimension starting with identity.
      IntPermutations​(int[] permutation)
      Construct iterator over permutations with specified permutation at the start.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] current()
      Returns the reference on the current iteration element.
      int getDimension()
      Returns dimension specified in the constructor
      boolean hasNext()  
      boolean hasPrevious()
      Returns true if the iteration has more elements, iterating in back order.
      private boolean isFirst()  
      private boolean isLast()  
      int[] next()
      Returns the next element in the iteration.
      int[] previous()
      Returns the previous element in the iteration.
      void remove()  
      void reset()
      Resets the iteration
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • permutation

        final int[] permutation
      • onFirst

        private boolean onFirst
      • size

        private final int size
    • Constructor Detail

      • 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:
        java.lang.IllegalArgumentException - if permutation is inconsistent with one-line notation
    • Method Detail

      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.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 java.util.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
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<int[]>
        Throws:
        java.lang.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