Package cc.redberry.combinatorics
Class IntPermutations
- java.lang.Object
-
- cc.redberry.combinatorics.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
The result will beIntPermutations its = new IntPermutations(3); while (its.hasNext()) System.out.println(Arrays.toString(its.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.- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private booleanonFirst(package private) int[]permutationprivate static longserialVersionUIDprivate intsize
-
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.intgetDimension()Returns dimension specified in the constructorbooleanhasNext()booleanhasPrevious()Returnstrueif the iteration has more elements, iterating in back order.private booleanisFirst()private booleanisLast()int[]next()Returns the next element in the iteration.int[]previous()Returns the previous element in the iteration.voidremove()voidreset()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 cc.redberry.combinatorics.CombinatorialIterator
iterator, stream, toList
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
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
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
-
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
-
isLast
private boolean isLast()
-
isFirst
private boolean isFirst()
-
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 interface:CombinatorialIteratorResets the iteration- Specified by:
resetin interfaceCombinatorialIterator<int[]>
-
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
-
current
public int[] current()
Description copied from interface:CombinatorialIteratorReturns the reference on the current iteration element.- Specified by:
currentin interfaceCombinatorialIterator<int[]>- Returns:
- the reference on the current iteration element
-
-