User:Margusmartsepp/Contributions/Java/Utils.java: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "<lang java> import java.util.ArrayList; import java.util.Collections; public class Utils<T extends Comparable<T>> { private void swap(ArrayList<T> data, int i, int j) { T t =...")
 
No edit summary
Line 3: Line 3:
import java.util.Collections;
import java.util.Collections;


public class Utils<T extends Comparable<T>> {
public class Utils {
private void swap(ArrayList<T> data, int i, int j) {
private static <T> void swap(ArrayList<T> data, int i, int j) {
T t = data.get(i);
T t = data.get(i);
data.set(i, data.get(j));
data.set(i, data.get(j));
Line 10: Line 10:
}
}


public ArrayList<Integer> mRange(int from, int to) {
public static ArrayList<Integer> mRange(int from, int to) {
ArrayList<Integer> result = new ArrayList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = from; i <= to; i++)
for (int i = from; i <= to; i++)
Line 17: Line 17:
}
}


public boolean nextPerm(ArrayList<T> data) {
public static <T extends Comparable<? super T>> boolean nextPerm(ArrayList<T> data) {
// find the swaps
// find the swaps
int c = -1, d = data.size();
int c = -1, d = data.size();
Line 43: Line 43:
}
}


public static <T extends Comparable<? super T>> ArrayList<ArrayList<T>> Permutations(ArrayList<T> d) {
@SuppressWarnings("unchecked")
public ArrayList<ArrayList<T>> Permutations(ArrayList<T> d) {
ArrayList<ArrayList<T>> result = new ArrayList<ArrayList<T>>();
ArrayList<ArrayList<T>> result = new ArrayList<ArrayList<T>>();
Collections.sort(d);
Collections.sort(d);
do {
do {
result.add((ArrayList<T>) d.clone());
result.add(new ArrayList<T>(d));
} while (this.nextPerm(d));
} while (nextPerm(d));
return result;
return result;
}
}

Revision as of 05:17, 28 October 2010

<lang java> import java.util.ArrayList; import java.util.Collections;

public class Utils { private static <T> void swap(ArrayList<T> data, int i, int j) { T t = data.get(i); data.set(i, data.get(j)); data.set(j, t); }

public static ArrayList<Integer> mRange(int from, int to) { ArrayList<Integer> result = new ArrayList<Integer>(); for (int i = from; i <= to; i++) result.add(i); return result; }

public static <T extends Comparable<? super T>> boolean nextPerm(ArrayList<T> data) { // find the swaps int c = -1, d = data.size(); for (int i = d - 2; i >= 0; i--) if (data.get(i).compareTo(data.get(i + 1)) < 0) { c = i; break; }

if (c < 0) return false;

int s = c + 1; for (int j = c + 2; j < d; j++) if (data.get(j).compareTo(data.get(s)) < 0 && // data.get(j).compareTo(data.get(c)) > 0) s = j;

// do the swaps swap(data, c, s); while (--d > ++c) swap(data, c, d);

return true; }

public static <T extends Comparable<? super T>> ArrayList<ArrayList<T>> Permutations(ArrayList<T> d) { ArrayList<ArrayList<T>> result = new ArrayList<ArrayList<T>>(); Collections.sort(d); do { result.add(new ArrayList<T>(d)); } while (nextPerm(d)); return result; } } </lang>