org.paukov.combinatorics3.SimpleCombinationGenerator Maven / Gradle / Ivy
Show all versions of combinatoricslib3 Show documentation
/**
* Combinatorics Library 3
* Copyright 2016 Dmytro Paukov [email protected]
*/
package org.paukov.combinatorics3;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* This generator generates simple combinations from specified core set by
* specified length. Core set and length are specified in the constructor of
* generator
*
* A simple k-combination of a finite set S is a subset of k distinct elements
* of S. Specifying a subset does not arrange them in a particular order. As an
* example, a poker hand can be described as a 5-combination of cards from a
* 52-card deck: the 5 cards of the hand are all distinct, and the order of the
* cards in the hand does not matter.
*
* Example. Generate 3-combination of the set (red, black, white, green, blue).
*
*
*
*
*
* // Create the initial vector
* ICombinatoricsVector<String> initialVector = Factory.createVector(new String[] {
* "red", "black", "white", "green", "blue" });
*
* // Create a simple combination generator to generate 3-combinations of the
* // initial vector
* Generator<String> gen = Factory.createSimpleCombinationGenerator(initialVector,
* 3);
*
* // Print all possible combinations
* for (ICombinatoricsVector<String> combination : gen) {
* System.out.println(combination);
* }
*
*
*
*
*
* @author Dmytro Paukov
* @version 3.0
* @see SimpleCombinationIterator
* @param
* Type of elements in the combination
*/
class SimpleCombinationGenerator implements IGenerator> {
final List originalVector;
final int combinationLength;
/**
* Constructor
*
* @param originalVector
* Original vector which is used for generating the combination
* @param combinationsLength
* Length of the combinations
*/
SimpleCombinationGenerator(Collection originalVector,
int combinationsLength) {
this.originalVector = new ArrayList<>(originalVector);
this.combinationLength = combinationsLength;
}
/**
* Creates an iterator of the simple combinations (without repetitions)
*/
@Override
public Iterator> iterator() {
return new SimpleCombinationIterator(this);
}
@Override
public Stream> stream() {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), 0), false);
}
}