Class IntPermutationsGenerator
- java.lang.Object
-
- cc.redberry.core.combinatorics.IntCombinatorialGenerator
-
- cc.redberry.core.combinatorics.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
The result will beIntPermutationsGenerator ig = new IntPermutationsGenerator(3); while (ig.hasNext()) System.out.println(Arrays.toString(ig.next()))It is also possible to iterate in the opposite direction via[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]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 fromnext(), but also save the result, it is necessary to clone the returned array.- Since:
- 1.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface cc.redberry.core.utils.OutputPort
OutputPort.PortIterable<T>, OutputPort.PortIterator<T>, OutputPort.Singleton<T>
-
-
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 intgetDimension()Returns dimension specified in the constructorint[]getReference()Returns the reference on the current iteration element.booleanhasNext()booleanhasPrevious()Returnstrueif 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.voidremove()voidreset()Resets the iterationint[]take()Calculates and returns the next combination or null, if no more combinations exist.-
Methods inherited from class cc.redberry.core.combinatorics.IntCombinatorialGenerator
iterator
-
-
-
-
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
permutationis 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:IntCombinatorialPortCalculates and returns the next combination or null, if no more combinations exist.- Specified by:
takein interfaceIntCombinatorialPort- Specified by:
takein interfaceOutputPort<int[]>- Returns:
- the next combination or null, if no more combinations exist
-
hasNext
public boolean hasNext()
- Specified by:
hasNextin interfacejava.util.Iterator<int[]>
-
hasPrevious
public boolean hasPrevious()
Returnstrueif the iteration has more elements, iterating in back order. (In other words, returnstrueifprevious()would return an element rather than throwing an exception.)- Returns:
trueif the iteration has more elements
-
next
public int[] next()
Returns the next element in the iteration.- Specified by:
nextin interfacejava.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
-
reset
public void reset()
Description copied from class:IntCombinatorialGeneratorResets the iteration- Specified by:
resetin interfaceIntCombinatorialPort- Specified by:
resetin classIntCombinatorialGenerator
-
remove
public void remove()
- Specified by:
removein interfacejava.util.Iterator<int[]>- Throws:
java.lang.UnsupportedOperationException- always
-
getDimension
public int getDimension()
Returns dimension specified in the constructor- Returns:
- dimension specified in the constructor
-
getReference
public int[] getReference()
Description copied from class:IntCombinatorialGeneratorReturns the reference on the current iteration element.- Specified by:
getReferencein interfaceIntCombinatorialPort- Specified by:
getReferencein classIntCombinatorialGenerator- Returns:
- the reference on the current iteration element
-
-
DataMelt 3.0 © DataMelt by jWork.ORG