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 -
Method Summary
Modifier and TypeMethodDescriptionstatic <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.booleanhasNext()next()Generate next combination (algorithm from Rosen p. 286)final voidreset()Reset the generatorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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, getCountMethods inherited from interface com.helger.commons.collection.iterate.IIterableIterator
iterator, withFilter, withMapperMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Constructor Details
-
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 Details
-
reset
public final void reset()Reset the generator -
getCombinationsLeft
- Returns:
- number of combinations not yet generated
-
hasNext
public boolean hasNext() -
getTotalCombinations
- Returns:
- total number of combinations
-
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
-