Class Combinations
- java.lang.Object
-
- org.apache.commons.numbers.combinatorics.Combinations
-
- All Implemented Interfaces:
java.lang.Iterable<int[]>
public final class Combinations extends java.lang.Object implements java.lang.Iterable<int[]>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classCombinations.LexicographicComparatorDefines a lexicographic ordering of the combinations.private static classCombinations.LexicographicIteratorLexicographic combinations iterator.private static classCombinations.SingletonIteratorIterator with just one element to handle degenerate cases (full array, empty array) for combination iterator.
-
Constructor Summary
Constructors Modifier Constructor Description privateCombinations(int n, int k)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Comparator<int[]>comparator()Creates a comparator.intgetK()Gets the number of elements in each combination.intgetN()Gets the size of the set from which combinations are drawn.java.util.Iterator<int[]>iterator()Creates an iterator whose range is the k-element subsets of {0, ..., n - 1} represented asint[]arrays.static Combinationsof(int n, int k)Create an instance.
-
-
-
Method Detail
-
of
public static Combinations of(int n, int k)
Create an instance.- Parameters:
n- Size of the set from which subsets are selected.k- Size of the subsets to be enumerated.- Returns:
- a new instance.
- Throws:
java.lang.IllegalArgumentException- ifn < 0.java.lang.IllegalArgumentException- ifk > nork < 0.
-
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
public java.util.Iterator<int[]> iterator()
Creates an iterator 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.- Specified by:
iteratorin interfacejava.lang.Iterable<int[]>
-
comparator
public java.util.Comparator<int[]> comparator()
Creates a comparator. When performing a comparison, if an element of the array is not within the interval [0,n), anIllegalArgumentExceptionwill be thrown.- Returns:
- a comparator.
-
-