Top Description Fields Constructors Methods
org.h2.util

public Class Permutations<T>

extends Object
Class Inheritance
Type Parameters
<T>
the element type
Imports
org.h2.message.DbException

A class to iterate over all permutations of an array. The algorithm is from Applied Combinatorics, by Alan Tucker as implemented in http://www.koders.com/java/fidD3445CD11B1DC687F6B8911075E7F01E23171553.aspx

Field Summary

Modifier and TypeField and Description
private boolean
private final T[]
private final int[]
private final int
m

private final int
n

private final T[]

Constructor Summary

AccessConstructor and Description
private
Permutations(T[] in, T[] out, int m)

Method Summary

Modifier and TypeMethod and Description
public static <
the type
T
>
Permutations<T>

Returns:

the generated permutations object
create
(T[]
the source array
in
,
T[]
the target array
out
)

Create a new permutations object.

public static <
the type
T
>
Permutations<T>

Returns:

the generated permutations object
create
(T[]
the source array
in
,
T[]
the target array
out
,
int
the number of output elements to generate
m
)

Create a new permutations object.

private void
moveIndex()

Move the index forward a notch.

public boolean

Returns:

if a new lineup is available
next
()

Go to the next lineup, and if available, fill the target array.

private void
reverseAfter(int
the index
i
)

Reverse the elements to the right of the specified index.

private int

Returns:

the index or -1 if non is found
rightmostDip
()

Get the index of the first element from the right that is less than its neighbor on the right.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hasNextback to summary
private boolean hasNext
inback to summary
private final T[] in
indexback to summary
private final int[] index
mback to summary
private final int m
nback to summary
private final int n
outback to summary
private final T[] out

Constructor Detail

Permutationsback to summary
private Permutations(T[] in, T[] out, int m)

Method Detail

createback to summary
public static <T> Permutations<T> create(T[] in, T[] out)

Create a new permutations object.

Parameters
<T>
the type
in:T[]

the source array

out:T[]

the target array

Returns:Permutations<T>

the generated permutations object

createback to summary
public static <T> Permutations<T> create(T[] in, T[] out, int m)

Create a new permutations object.

Parameters
<T>
the type
in:T[]

the source array

out:T[]

the target array

m:int

the number of output elements to generate

Returns:Permutations<T>

the generated permutations object

moveIndexback to summary
private void moveIndex()

Move the index forward a notch. The algorithm first finds the rightmost index that is less than its neighbor to the right. This is the dip point. The algorithm next finds the least element to the right of the dip that is greater than the dip. That element is switched with the dip. Finally, the list of elements to the right of the dip is reversed. For example, in a permutation of 5 items, the index may be {1, 2, 4, 3, 0}. The dip is 2 the rightmost element less than its neighbor on its right. The least element to the right of 2 that is greater than 2 is 3. These elements are swapped, yielding {1, 3, 4, 2, 0}, and the list right of the dip point is reversed, yielding {1, 3, 0, 2, 4}.

nextback to summary
public boolean next()

Go to the next lineup, and if available, fill the target array.

Returns:boolean

if a new lineup is available

reverseAfterback to summary
private void reverseAfter(int i)

Reverse the elements to the right of the specified index.

Parameters
i:int

the index

rightmostDipback to summary
private int rightmostDip()

Get the index of the first element from the right that is less than its neighbor on the right.

Returns:int

the index or -1 if non is found