Code:
// First make a lookup table of all the 24 possible hand ordering permutations:
int PERMS[24][4]={{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {0, 3, 2, 1}, {1, 0, 2, 3}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 2, 3, 0}, {1, 3, 0, 2}, {1, 3, 2, 0}, {2, 0, 1, 3}, {2, 0, 3, 1}, {2, 1, 0, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 1, 2, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}};
// Next we need a function to map an unordered 4-card hand to a unique integer.
// NOTE: 140608=52^3, 2704=52^2, etc.
int getIndex(int c0, int c1, int c2, int c3) {
return c0*140608 + c1*2704 + c2*52 + c3;
}
// Next create a lookup table of size 52^4, which we will populate so as to map from unordered --> ordered index.
int lookup[7311616];
// You can now populate this as you create your ordering:
int handIndex=0;
for (int c0 = 0; c0 < 49; c0++) {
for (int c1 = c0+1; c1 < 50; c1++) {
for (int c2 = c1+1; c2 < 51; c2++) {
for (int c3 = c2+1; c3 < 52; c3++) {
int hand[4]={c0, c1, c2, c3};
for (int i=0; i<24; i++) {
lookup[getIndex(hand[PERMS[i][0]],hand[PERMS[i][1]],hand[PERMS[i][2]],hand[PERMS[i][3]])]=handIndex;
}
handIndex++;
}
}
}
}
.
.
.
// Now you can lookup your ordered hand index in O(1) from an unordered hand using:
int handIndex=lookup[getIndex(c0,c1,c2,c3)];