Class Combinations
- All Implemented Interfaces:
Iterable<int[]>
- Since:
- 3.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumDescribes the type of iteration performed by theiterator.private static classDefines the lexicographic ordering of combinations, using theCombinations.LexicographicComparator.lexNorm(int[])method.private static classLexicographic combinations iterator.private static classIterator with just one element to handle degenerate cases (full array, empty array) for combination iterator. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Combinations.IterationOrderIteration order.private final intNumber of elements in each combination.private final intSize of the set from which combinations are drawn. -
Constructor Summary
ConstructorsModifierConstructorDescriptionCombinations(int n, int k) Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented asint[]arrays.privateCombinations(int n, int k, Combinations.IterationOrder iterationOrder) Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented asint[]arrays. -
Method Summary
Modifier and TypeMethodDescriptionComparator<int[]> Defines a lexicographic ordering of combinations.intgetK()Gets the number of elements in each combination.intgetN()Gets the size of the set from which combinations are drawn.Iterator<int[]> iterator()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
n
private final int nSize of the set from which combinations are drawn. -
k
private final int kNumber of elements in each combination. -
iterationOrder
Iteration order.
-
-
Constructor Details
-
Combinations
public Combinations(int n, int k) Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented asint[]arrays.The iteration order is lexicographic: the arrays returned by the
Ifiteratorare sorted in descending order and they are visited in lexicographic order with significance from right to left. For example,new Combinations(4, 2).iterator()returns an iterator that will generate the following sequence of arrays on successive calls tonext():
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]k == 0an iterator containing an empty array is returned; ifk == nan iterator containing [0, ..., n - 1] is returned.- Parameters:
n- Size of the set from which subsets are selected.k- Size of the subsets to be enumerated.- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.
-
Combinations
Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented asint[]arrays.If the
IfiterationOrderargument is set toCombinations.IterationOrder.LEXICOGRAPHIC, the arrays returned by theiteratorare sorted in descending order and they are visited in lexicographic order with significance from right to left. For example,new Combinations(4, 2).iterator()returns an iterator that will generate the following sequence of arrays on successive calls tonext():
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]k == 0an iterator containing an empty array is returned; ifk == nan iterator containing [0, ..., n - 1] is returned.- Parameters:
n- Size of the set from which subsets are selected.k- Size of the subsets to be enumerated.iterationOrder- Specifies theiteration order.- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.
-
-
Method Details
-
getN
public int getN()Gets the size of the set from which combinations are drawn.- Returns:
- the size of the universe.
-
getK
public int getK()Gets the number of elements in each combination.- Returns:
- the size of the subsets to be enumerated.
-
iterator
-
comparator
Defines a lexicographic ordering of combinations. The returned comparator allows to compare any two combinations that can be produced by this instance'siterator. Itscompare(int[],int[])method will throw exceptions if passed combinations that are inconsistent with this instance:DimensionMismatchExceptionif the array lengths are not equal tok,OutOfRangeExceptionif an element of the array is not within the interval [0,n).
- Returns:
- a lexicographic comparator.
-