Package com.helger.commons.math
Class CombinationGenerator<DATATYPE>
- java.lang.Object
-
- com.helger.commons.math.CombinationGenerator<DATATYPE>
-
- Type Parameters:
DATATYPE- Element type to be combined
- All Implemented Interfaces:
ICommonsIterable<ICommonsList<DATATYPE>>,IIterableIterator<ICommonsList<DATATYPE>>,Iterable<ICommonsList<DATATYPE>>,Iterator<ICommonsList<DATATYPE>>
public class CombinationGenerator<DATATYPE> extends Object implements IIterableIterator<ICommonsList<DATATYPE>>
Utility class for generating all possible combinations of elements for a specified number of available slots. Duplicates in the passed elements will be treated as different individuals and hence deliver duplicate result solutions. This generator will only return complete result sets filling all slots.- Author:
- Boris Gregorcic, Philip Helger
-
-
Constructor Summary
Constructors Constructor Description CombinationGenerator(ICommonsList<DATATYPE> aElements, int nSlotCount)Ctor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <DATATYPE> voidaddAllPermutations(ICommonsList<DATATYPE> aInput, int nSlotCount, Collection<ICommonsList<DATATYPE>> aResultList)Fill a list with all permutations of the input elements.static <DATATYPE> ICommonsList<ICommonsList<DATATYPE>>getAllPermutations(ICommonsList<DATATYPE> aInput, int nSlotCount)Get a list of all permutations of the input elements.BigIntegergetCombinationsLeft()BigIntegergetTotalCombinations()booleanhasNext()ICommonsList<DATATYPE>next()Generate next combination (algorithm from Rosen p. 286)voidreset()Reset the generator-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.helger.commons.collection.impl.ICommonsIterable
containsAny, containsNone, containsOnly, findAll, findAllInstanceOf, findAllMapped, findAllMapped, findAllMapped, findFirst, findFirst, findFirstIndex, findFirstMapped, findFirstMapped, findLastIndex, forEachBreakable, forEachByIndex, forEachThrowing, getCount, getCount
-
Methods inherited from interface com.helger.commons.collection.iterate.IIterableIterator
iterator, withFilter, withMapper
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Constructor Detail
-
CombinationGenerator
public CombinationGenerator(@Nonnull @Nonempty ICommonsList<DATATYPE> aElements, @Nonnegative int nSlotCount)
Ctor- Parameters:
aElements- the elements to fill into the slots for creating all combinations (must not be empty!)nSlotCount- the number of slots to use (must not be greater than the element count!)
-
-
Method Detail
-
reset
public final void reset()
Reset the generator
-
getCombinationsLeft
@Nonnull public BigInteger getCombinationsLeft()
- Returns:
- number of combinations not yet generated
-
hasNext
public boolean hasNext()
-
getTotalCombinations
@Nonnull public BigInteger getTotalCombinations()
- Returns:
- total number of combinations
-
next
@Nonnull @ReturnsMutableCopy public ICommonsList<DATATYPE> next()
Generate next combination (algorithm from Rosen p. 286)
-
getAllPermutations
@Nonnull public static <DATATYPE> ICommonsList<ICommonsList<DATATYPE>> getAllPermutations(@Nonnull @Nonempty ICommonsList<DATATYPE> aInput, @Nonnegative int nSlotCount)
Get a list of all permutations of the input elements.- Type Parameters:
DATATYPE- Element type to be combined- Parameters:
aInput- Input list.nSlotCount- Slot count.- Returns:
- The list of all permutations. Beware: the resulting list may be quite large and may contain duplicates if the input list contains duplicate elements!
-
addAllPermutations
public static <DATATYPE> void addAllPermutations(@Nonnull @Nonempty ICommonsList<DATATYPE> aInput, @Nonnegative int nSlotCount, @Nonnull Collection<ICommonsList<DATATYPE>> aResultList)
Fill a list with all permutations of the input elements.- Type Parameters:
DATATYPE- Element type to be combined- Parameters:
aInput- Input list.nSlotCount- Slot count.aResultList- The list to be filled with all permutations. Beware: this list may be quite large and may contain duplicates if the input list contains duplicate elements! Note: this list is not cleared before filling
-
-