I started with my crazy hack to use bit strings to calculate combinations. I didn't want to resort to creating bit strings, as I wanted to minimize the amount of work I needed to do. Therefore, I thought about simply using bitwise operators.
Below is my Java algorithm for creating calculations using iteration and bitwise operators to create all combinations from an array.
Thoughts?
public static void generate(String[] list) {
int max = (int) Math.pow(list.length, 2)-1;
System.out.println(max + " combinations");
for (long i = 0; i < max; i++) {
String[] combo = new String[Long.bitCount(i)];
int comboPos = 0;
for (int j = 0; j < list.length; j++) {
if ((i & (1L< 0) {
combo[comboPos++] = list[j];
}
}
//System.out.println(Arrays.toString(combo));
}
}
Update: handle up to 64 slots. Will need to move to something like BigInteger to handle greater than 64 bits I suppose. Wonder how that will affect performance?
1 comments:
To make it support virtually any number of combination string length, one way is to generate the binary count string with a r-permutation engine and then use that string to filter out the combinations. I have implemented an r-permutation generator and a combination generator. You may check both here:
http://wp.me/pmGbq-8B
http://wp.me/smGbq-rpermrep
Although the unlimited length combination generator is not implemented, but with these two it is very easy. But ofcource there will be performance penalty cause after the bitstring is made there are no more bit operations, and also you need to generate r-permutations which costs you.
Post a Comment