public class Combinations
extends java.lang.Object
implements java.lang.Iterable<int[]>
Modifier and Type | Class and Description |
---|---|
private static class |
Combinations.IterationOrder
Describes the type of iteration performed by the
iterator . |
private static class |
Combinations.LexicographicComparator
Defines the lexicographic ordering of combinations, using
the
Combinations.LexicographicComparator.lexNorm(int[]) method. |
private static class |
Combinations.LexicographicIterator
Lexicographic combinations iterator.
|
private static class |
Combinations.SingletonIterator
Iterator with just one element to handle degenerate cases (full array,
empty array) for combination iterator.
|
Modifier and Type | Field and Description |
---|---|
private Combinations.IterationOrder |
iterationOrder
Iteration order.
|
private int |
k
Number of elements in each combination.
|
private int |
n
Size of the set from which combinations are drawn.
|
Modifier | Constructor and Description |
---|---|
|
Combinations(int n,
int k)
Creates an instance whose range is the k-element subsets of
{0, ..., n - 1} represented as
int[] arrays. |
private |
Combinations(int n,
int k,
Combinations.IterationOrder iterationOrder)
Creates an instance whose range is the k-element subsets of
{0, ..., n - 1} represented as
int[] arrays. |
Modifier and Type | Method and Description |
---|---|
java.util.Comparator<int[]> |
comparator()
Defines a lexicographic ordering of combinations.
|
int |
getK()
Gets the number of elements in each combination.
|
int |
getN()
Gets the size of the set from which combinations are drawn.
|
java.util.Iterator<int[]> |
iterator() |
private final int n
private final int k
private final Combinations.IterationOrder iterationOrder
public Combinations(int n, int k)
int[]
arrays.
The iteration order is lexicographic: the arrays returned by the
iterator
are 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 to
next()
:
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]
k == 0
an iterator containing an empty array is returned;
if k == n
an iterator containing [0, ..., n - 1] is returned.n
- Size of the set from which subsets are selected.k
- Size of the subsets to be enumerated.NotPositiveException
- if n < 0
.NumberIsTooLargeException
- if k > n
.private Combinations(int n, int k, Combinations.IterationOrder iterationOrder)
int[]
arrays.
If the iterationOrder
argument is set to
Combinations.IterationOrder.LEXICOGRAPHIC
, the arrays returned by the
iterator
are 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 to
next()
:
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]
k == 0
an iterator containing an empty array is returned;
if k == n
an iterator containing [0, ..., n - 1] is returned.n
- Size of the set from which subsets are selected.k
- Size of the subsets to be enumerated.iterationOrder
- Specifies the iteration order
.NotPositiveException
- if n < 0
.NumberIsTooLargeException
- if k > n
.public int getN()
public int getK()
public java.util.Iterator<int[]> iterator()
iterator
in interface java.lang.Iterable<int[]>
public java.util.Comparator<int[]> comparator()
iterator
.
Its compare(int[],int[])
method will throw exceptions if
passed combinations that are inconsistent with this instance:
DimensionMismatchException
if the array lengths are not
equal to k
,OutOfRangeException
if an element of the array is not
within the interval [0, n
).Copyright (c) 2003-2014 Apache Software Foundation