org.paukov.combinatorics3.MultiCombinationGenerator 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 java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* This generator generates multi-combinations (with repetitions) from specified core set by
* specified length. Core set and length are specified in the constructor of generator
*
* A k-multicombination or k-combination with repetition of a finite set S is given by a sequence of
* k not necessarily distinct elements of S, where order is not taken into account.
*
* As an example. Suppose there are 2 types of fruits (apple and orange) at a grocery store, and you
* want to buy 3 pieces of fruit. You could select
*
* - (apple, apple, apple)
*
- (apple, apple, orange)
*
- (apple, orange, orange)
*
- (orange, orange, orange)
*
*
* Generate 3-combinations with repetitions of the set (apple, orange).
*
*
*
*
*
*
* // Create the initial vector of (apple, orange)
* ICombinatoricsVector<String> initialVector = Factory.createVector(new String[] {
* "apple", "orange" });
*
* // Create a multi-combination generator to generate 3-combinations of
* // the initial vector
* Generator<String> gen = Factory.createMultiCombinationGenerator(initialVector,
* 3);
*
* // Print all possible combinations
* for (ICombinatoricsVector<String> combination : gen) {
* System.out.println(combination);
* }
*
*
*
*
*
* @param Type of elements in the combination.
* @author Dmytro Paukov
* @version 3.0
* @see MultiCombinationIterator
*/
class MultiCombinationGenerator implements IGenerator> {
final List originalVector;
final int combinationLength;
MultiCombinationGenerator(Collection originalVector, int combinationsLength) {
this.originalVector = new ArrayList<>(originalVector);
this.combinationLength = Math.max(combinationsLength, 0);
}
@Override
public Iterator> iterator() {
return new MultiCombinationIterator<>(this);
}
}