-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Charlie Feng
committed
Feb 12, 2021
1 parent
dfc2c23
commit f1eca99
Showing
3 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/main/java/org/paukov/combinatorics3/KPermutationGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.