|
Uncommons Maths API (Version 1.2.3) |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.uncommons.maths.combinatorics.CombinationGenerator<T>
T
- The type of element that the combinations are made from.public class CombinationGenerator<T>
Combination generator for generating all combinations of a given size from the specified set of elements. For performance reasons, this implementation is restricted to operating with set sizes and combination lengths that produce no more than 2^63 different combinations.
PermutationGenerator
Constructor Summary | |
---|---|
CombinationGenerator(Collection<T> elements,
int combinationLength)
Create a combination generator that generates all combinations of a specified length from the given set. |
|
CombinationGenerator(T[] elements,
int combinationLength)
Create a combination generator that generates all combinations of a specified length from the given set. |
Method Summary | |
---|---|
long |
getRemainingCombinations()
|
long |
getTotalCombinations()
|
boolean |
hasMore()
Are there more combinations? |
Iterator<List<T>> |
iterator()
Provides a read-only iterator for iterating over the combinations generated by this object. |
T[] |
nextCombinationAsArray()
Generate the next combination and return an array containing the appropriate elements. |
T[] |
nextCombinationAsArray(T[] destination)
Generate the next combination and return an array containing the appropriate elements. |
List<T> |
nextCombinationAsList()
Generate the next combination and return a list containing the appropriate elements. |
List<T> |
nextCombinationAsList(List<T> destination)
Generate the next combination and return a list containing the appropriate elements. |
void |
reset()
Reset the combination generator. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public CombinationGenerator(T[] elements, int combinationLength)
elements
- The set from which to generate combinations.combinationLength
- The length of the combinations to be generated.public CombinationGenerator(Collection<T> elements, int combinationLength)
elements
- The set from which to generate combinations.combinationLength
- The length of the combinations to be generated.Method Detail |
---|
public final void reset()
public long getRemainingCombinations()
public boolean hasMore()
public long getTotalCombinations()
public T[] nextCombinationAsArray()
nextCombinationAsArray(Object[])
,
nextCombinationAsList()
public T[] nextCombinationAsArray(T[] destination)
nextCombinationAsArray()
method is
used it will create a new array every time. When iterating over
combinations this will result in lots of short-lived objects that
have to be garbage collected. This method allows a single array
instance to be reused in such circumstances.
destination
- Provides an array to use to create the
combination. The specified array must be the same length as a
combination.
public List<T> nextCombinationAsList()
nextCombinationAsList(List)
,
nextCombinationAsArray()
public List<T> nextCombinationAsList(List<T> destination)
nextCombinationAsList()
method is
used it will create a new list every time. When iterating over
combinations this will result in lots of short-lived objects that
have to be garbage collected. This method allows a single list
instance to be reused in such circumstances.
destination
- Provides a list to use to create the
combination.
public Iterator<List<T>> iterator()
Provides a read-only iterator for iterating over the combinations
generated by this object. This method is the implementation of the
Iterable
interface that permits instances of this class to be
used with the new-style for loop.
For example:
List<Integer> elements = Arrays.asList(1, 2, 3); CombinationGenerator<Integer> combinations = new CombinationGenerator(elements, 2); for (List<Integer> c : combinations) { // Do something with each combination. }
iterator
in interface Iterable<List<T>>
|
Uncommons Maths API (Version 1.2.3) |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |