org.paukov.combinatorics3.SimplePermutationGenerator Maven / Gradle / Ivy
Show all versions of combinatoricslib3 Show documentation
/*
* Combinatorics Library 3
* Copyright 2009-2016 Dmytro Paukov [email protected]
*/
package org.paukov.combinatorics3;
import static org.paukov.combinatorics3.PermutationGenerator.hasDuplicates;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* This generator generates all possible permutations of the specified initial
* vector
*
* A permutation is an ordering of a set in the context of all possible
* orderings. For example, the set containing the first three digits, 123, has
* six permutations: 123, 132, 213, 231, 312, and 321.
*
* This is an example of the permutations of 3 string items (apple, orange,
* cherry):
*
*
*
*
*
* // Create the initial vector of 3 elements (apple, orange, cherry)
* ICombinatoricsVector<String> originalVector = Factory
* .createVector(new String[] { "apple", "orange", "cherry" });
*
* // Create the permutation generator by calling the appropriate method in the
* // Factory class
* Generator<String> gen = Factory.createPermutationGenerator(originalVector);
*
* // Print the result
* for (ICombinatoricsVector<String> perm : gen)
* System.out.println(perm);
*
*
*
*
*
* And the result
*
*
*
*
* CombinatoricsVector=([apple, orange, cherry], size=3)
* CombinatoricsVector=([apple, cherry, orange], size=3)
* CombinatoricsVector=([cherry, apple, orange], size=3)
* CombinatoricsVector=([cherry, orange, apple], size=3)
* CombinatoricsVector=([orange, cherry, apple], size=3)
* CombinatoricsVector=([orange, apple, cherry], size=3)
*
*
*
*
*
* @param Type of the elements in the permutations
* @author Dmytro Paukov
* @version 2.0
*/
class SimplePermutationGenerator implements IGenerator> {
final boolean hasDuplicates;
final boolean treatAsIdentical;
final List originalVector;
/**
* Constructor
*
* @param vector Vector which is used for permutation generation
* @param treatAsIdentical True if the generator should treat the vector as identical
*/
SimplePermutationGenerator(Collection vector,
boolean treatAsIdentical) {
this.hasDuplicates = hasDuplicates(vector);
this.treatAsIdentical = treatAsIdentical;
this.originalVector = new ArrayList<>(vector);
}
@Override
public Iterator> iterator() {
if (isDuplicateIterator()) {
return new DuplicatedPermutationIterator<>(this);
} else {
return new SimplePermutationIterator<>(this);
}
}
private boolean isDuplicateIterator() {
return (!treatAsIdentical && hasDuplicates);
}
}