|
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.PermutationGenerator<T>
T
- The type of element that the permutation will consist of.public class PermutationGenerator<T>
Permutation generator for generating all permutations for all sets up to
20 elements in size. While 20 may seem a low limit, bear in mind that
the number of permutations of a set of size n is n! For a set of 21
items, the number of permutations is bigger than can be stored in Java's
64-bit long integer data type. Therefore it seems unlikely that you
could ever generate, let alone process, all of the permutations in a
reasonable time frame. For this reason the implementation is optimised for
sets of size 20 or less (this affords better performance by allowing primitive
numeric types to be used for calculations rather than
BigInteger
).
CombinationGenerator
Constructor Summary | |
---|---|
PermutationGenerator(Collection<T> elements)
Permutation generator that generates all possible orderings of the elements in the specified set. |
|
PermutationGenerator(T[] elements)
Permutation generator that generates all possible orderings of the elements in the specified set. |
Method Summary | |
---|---|
long |
getRemainingPermutations()
Returns the number of permutations not yet generated. |
long |
getTotalPermutations()
Returns the total number of unique permutations that can be generated for the given set of elements. |
boolean |
hasMore()
Are there more permutations that have not yet been returned? |
Iterator<List<T>> |
iterator()
Provides a read-only iterator for iterating over the permutations generated by this object. |
T[] |
nextPermutationAsArray()
Generate the next permutation and return an array containing the elements in the appropriate order. |
T[] |
nextPermutationAsArray(T[] destination)
Generate the next permutation and return an array containing the elements in the appropriate order. |
List<T> |
nextPermutationAsList()
Generate the next permutation and return a list containing the elements in the appropriate order. |
List<T> |
nextPermutationAsList(List<T> destination)
Generate the next permutation and return a list containing the elements in the appropriate order. |
void |
reset()
Resets the generator state. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public PermutationGenerator(T[] elements)
elements
- The elements to permute.public PermutationGenerator(Collection<T> elements)
elements
- The elements to permute.Method Detail |
---|
public final void reset()
public long getRemainingPermutations()
public long getTotalPermutations()
public boolean hasMore()
public T[] nextPermutationAsArray()
nextPermutationAsArray(Object[])
,
nextPermutationAsList()
public T[] nextPermutationAsArray(T[] destination)
nextPermutationAsArray()
method is
used it will create a new array every time. When iterating over
permutations 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
permutation. The specified array must be the same length as a
permutation. This is the array that will be returned, once
it has been filled with the elements in the appropriate order.
public List<T> nextPermutationAsList()
nextPermutationAsList(java.util.List)
,
nextPermutationAsArray()
public List<T> nextPermutationAsList(List<T> destination)
nextPermutationAsList()
method is
used it will create a new list every time. When iterating over
permutations 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
permutation. This is the list that will be returned, once
it has been filled with the elements in the appropriate order.
public Iterator<List<T>> iterator()
Provides a read-only iterator for iterating over the permutations
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); PermutationGenerator<Integer> permutations = new PermutationGenerator(elements); for (List<Integer> p : permutations) { // Do something with each permutation. }
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 |