提交 11679d44 authored 作者: Thomas Mueller's avatar Thomas Mueller

Use generics

上级 32955f2c
...@@ -104,7 +104,7 @@ public class Optimizer { ...@@ -104,7 +104,7 @@ public class Optimizer {
private void calculateBruteForceAll() throws SQLException { private void calculateBruteForceAll() throws SQLException {
TableFilter[] list = new TableFilter[filters.length]; TableFilter[] list = new TableFilter[filters.length];
Permutations p = new Permutations(filters, list); Permutations<TableFilter> p = Permutations.create(filters, list);
for (int x = 0; !canStop(x) && p.next(); x++) { for (int x = 0; !canStop(x) && p.next(); x++) {
testPlan(list); testPlan(list);
} }
...@@ -113,7 +113,7 @@ public class Optimizer { ...@@ -113,7 +113,7 @@ public class Optimizer {
private void calculateBruteForceSome() throws SQLException { private void calculateBruteForceSome() throws SQLException {
int bruteForce = getMaxBruteForceFilters(filters.length); int bruteForce = getMaxBruteForceFilters(filters.length);
TableFilter[] list = new TableFilter[filters.length]; TableFilter[] list = new TableFilter[filters.length];
Permutations p = new Permutations(filters, list, bruteForce); Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce);
for (int x = 0; !canStop(x) && p.next(); x++) { for (int x = 0; !canStop(x) && p.next(); x++) {
// find out what filters are not used yet // find out what filters are not used yet
for (TableFilter f : filters) { for (TableFilter f : filters) {
......
...@@ -12,33 +12,18 @@ import org.h2.message.Message; ...@@ -12,33 +12,18 @@ import org.h2.message.Message;
* A class to iterate over all permutations of an array. * A class to iterate over all permutations of an array.
* The algorithm is from Applied Combinatorics, by Alan Tucker as implemented in * The algorithm is from Applied Combinatorics, by Alan Tucker as implemented in
* http://www.koders.com/java/fidD3445CD11B1DC687F6B8911075E7F01E23171553.aspx * http://www.koders.com/java/fidD3445CD11B1DC687F6B8911075E7F01E23171553.aspx
*
* @param <T> the element type
*/ */
public class Permutations { public class Permutations<T> {
private Object[] in; private T[] in;
private Object[] out; private T[] out;
private int n, m; private int n, m;
private int[] index; private int[] index;
private boolean hasNext = true; private boolean hasNext = true;
/** private Permutations(T[] in, T[] out, int m) {
* Create a new permutations object.
*
* @param in the source array
* @param out the target array
*/
public Permutations(Object[] in, Object[] out) {
this(in, out, in.length);
}
/**
* Create a new permutations object.
*
* @param in the source array
* @param out the target array
* @param m the number of output elements to generate
*/
public Permutations(Object[] in, Object[] out, int m) {
this.n = in.length; this.n = in.length;
this.m = m; this.m = m;
if (n < m || m < 0) { if (n < m || m < 0) {
...@@ -56,6 +41,31 @@ public class Permutations { ...@@ -56,6 +41,31 @@ public class Permutations {
reverseAfter(m - 1); reverseAfter(m - 1);
} }
/**
* Create a new permutations object.
*
* @param <T> the type
* @param in the source array
* @param out the target array
* @return the generated permutations object
*/
public static <T> Permutations<T> create(T[] in, T[] out) {
return new Permutations<T>(in, out, in.length);
}
/**
* Create a new permutations object.
*
* @param <T> the type
* @param in the source array
* @param out the target array
* @param m the number of output elements to generate
* @return the generated permutations object
*/
public static <T> Permutations<T> create(T[] in, T[] out, int m) {
return new Permutations<T>(in, out, m);
}
/** /**
* Move the index forward a notch. The algorithm first finds the rightmost * 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. * index that is less than its neighbor to the right. This is the dip point.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论