Skip to content

Instantly share code, notes, and snippets.

@rastadrian
Last active May 12, 2017 18:47
Show Gist options
  • Select an option

  • Save rastadrian/ca466f6bcfd3598ecb60aff11d8409c2 to your computer and use it in GitHub Desktop.

Select an option

Save rastadrian/ca466f6bcfd3598ecb60aff11d8409c2 to your computer and use it in GitHub Desktop.
Permuterator
public class Permuterator {
public void permute(List<String> values, int depth, Validator validator) {
values.parallelStream().forEach(value -> {
//add fist value
final String[] permutation = new String[depth];
permutation[0] = value;
final int indexLimit = depth - 1;
traverse(permutation, values, indexLimit, 0, validator);
});
}
private void traverse(String[] permutation, List<String> values, int indexLimit, int index, Validator validator) {
index ++;
if (index > indexLimit) {
//finished a permutation, ready for validation
validator.validatePermutation(permutation);
return;
}
for (String value : values) {
permutation[index] = value;
traverse(permutation, values, indexLimit, index, validator);
}
}
interface Validator {
void validatePermutation(String[] permutation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment