/** * Create a combination generator that generates all combinations of * a specified length from the given set. * @param elements The set from which to generate combinations; will be used directly (not copied) * @param combinationLength The length of the combinations to be generated. */ public CombinationGenerator(T[] elements, int combinationLength) { if (combinationLength > elements.length) { throw new IllegalArgumentException("Combination length cannot be greater than set size."); } this.elements = elements; this.combinationIndices = new int[combinationLength]; BigInteger sizeFactorial = MathExtras.bigFactorial(elements.length); BigInteger lengthFactorial = MathExtras.bigFactorial(combinationLength); BigInteger differenceFactorial = MathExtras.bigFactorial(elements.length - combinationLength); BigInteger total = sizeFactorial.divide(differenceFactorial.multiply(lengthFactorial)); if (total.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { throw new IllegalArgumentException("Total number of combinations must not be more than 2^63."); } totalCombinations = total.longValue(); reset(); }