Documentation of 'cc.redberry.core.combinatorics.IntPermutationsGenerator' Java class
IntPermutationsGenerator
cc.redberry.core.combinatorics

Class IntPermutationsGenerator

  • All Implemented Interfaces:
    IntCombinatorialPort, OutputPort<int[]>, java.lang.Iterable<int[]>, java.util.Iterator<int[]>


    public final class IntPermutationsGenerator
    extends IntCombinatorialGenerator
    implements IntCombinatorialPort
    This class represents iterator over all possible permutations of specified dimension written in one-line notation. Number of all permutations of dimension D is D!.

    Example

          IntPermutationsGenerator ig = new IntPermutationsGenerator(3);
          while (ig.hasNext())
              System.out.println(Arrays.toString(ig.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. So, if it is needed not only to obtain the information from next(), but also save the result, it is necessary to clone the returned array.

    Since:
    1.0
    • Constructor Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      int getDimension()
      Returns dimension specified in the constructor
      int[] getReference()
      Returns the reference on the current iteration element.
      boolean hasNext() 
      boolean hasPrevious()
      Returns true if the iteration has more elements, iterating in back order.
      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
      int[] take()
      Calculates and returns the next combination or null, if no more combinations exist.
      • Methods inherited from class java.lang.Object

        equals, 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
    • Constructor Detail

      • IntPermutationsGenerator

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

        public IntPermutationsGenerator(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

      • take

        public int[] take()
        Description copied from interface: IntCombinatorialPort
        Calculates and returns the next combination or null, if no more combinations exist.
        Specified by:
        take in interface IntCombinatorialPort
        Specified by:
        take in interface OutputPort<int[]>
        Returns:
        the next combination or null, if no more combinations exist
      • 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
      • 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

DataMelt 3.0 © DataMelt by jWork.ORG

Ads help maintain this website.