All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.paukov.combinatorics3.MultiCombinationGenerator Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/**
 * 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 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);
 * }
 * 
* *
*

* * @author Dmytro Paukov * @version 3.0 * @see MultiCombinationIterator * @param * Type of elements in the combination */ class MultiCombinationGenerator implements IGenerator> { final List originalVector; final int combinationLength; MultiCombinationGenerator(Collection originalVector, int combinationsLength) { this.originalVector = new ArrayList<>(originalVector); if (combinationsLength < 0) combinationLength = 0; else combinationLength = combinationsLength; } @Override public Iterator> iterator() { return new MultiCombinationIterator<>(this); } @Override public Stream> stream() { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), 0), false); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy