Skip to content

Commit

Permalink
Support KPermutation
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Feng committed Feb 12, 2021
1 parent dfc2c23 commit f1eca99
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/main/java/org/paukov/combinatorics3/KPermutationGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Combinatorics Library 3
* Copyright 2009-2016 Dmytro Paukov d.paukov@gmail.com
*/
package org.paukov.combinatorics3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* This generator generates k-permutations of the specified initial vector
* <p>
* A k-permutation is an ordering of a subset in the subset of all possible
* orderings. For example, the set containing 2 digits of 123, has
* six permutations: 12, 21, 13, 31, 23 and 32.
* <p>
* This is an example of the k-permutations of 3 string items (apple, orange,
* cherry):
* <p>
* <blockquote>
*
* <pre>
*
* List<List<String>> permutations =
* Generator.permutation(Arrays.asList("apple", "orange", "cherry"))
* .k(2)
* .stream()
* .collect(toList());
* permutations.stream().forEach(System.out::println);
*
* </pre>
*
* </blockquote>
* <p>
* And the result
* <p>
* <blockquote>
*
* <pre>
* [apple, orange]
* [orange, apple]
* [apple, cherry]
* [cherry, apple]
* [orange, cherry]
* [cherry, orange]
* </pre>
*
* </blockquote>
* <p>
*
* @param <T> Type of the elements in the permutations
* @author Charlie Feng
* @version 3.4
*/
class KPermutationGenerator<T> implements IGenerator<List<T>> {

final List<T> originalVector;
final int length;

/**
* Constructor
*
* @param vector Vector which is used for k-permutation generation
* @param length number of elements in k-permutation
*/
KPermutationGenerator(Collection<T> vector,
int length) {
this.originalVector = new ArrayList<>(vector);
this.length = length;
}

@Override
public Iterator<List<T>> iterator() {
return Generator.combination(originalVector)
.simple(length)
.stream()
.flatMap(combination -> Generator.permutation(combination).simple().stream() )
.distinct()
.iterator();
}

@Override
public Stream<List<T>> stream() {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), 0), false);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public IGenerator<List<T>> simple(TreatDuplicatesAs treatAsIdentical) {
TreatDuplicatesAs.IDENTICAL.equals(treatAsIdentical));
}

/**
* Generate KPermutationGenerator with treat duplicates as different.
* @param length the value of "k" in KPermutationGenerator
* @return KPermutationGenerator
*/
public IGenerator<List<T>> k(int length) {
return new KPermutationGenerator<>(originalVector, length);
}

public IGenerator<List<T>> withRepetitions(int permutationLength) {
return new PermutationWithRepetitionGenerator<>(originalVector, permutationLength);
}
Expand Down
Loading

0 comments on commit f1eca99

Please sign in to comment.