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 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

      @Nonnull public BigInteger getCombinationsLeft()
      Returns:
      number of combinations not yet generated
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<DATATYPE>
      Returns:
      whether or not there are more combinations left
    • getTotalCombinations

      @Nonnull public BigInteger getTotalCombinations()
      Returns:
      total number of combinations
    • next

      Generate next combination (algorithm from Rosen p. 286)
      Specified by:
      next in interface Iterator<DATATYPE>
      Returns:
      the next combination as list of the size specified for slots filled with elements from the original list
    • 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