Listing 2
typedef void(* perm_callback)(int*,int); // permutation callback function
// uses permutation Method 1, Knuth, Art of Computer Programming, vol 1
void permute(int* vals, int n, int r, perm_callback pfn,
unsigned long* pCount, int level=0)
{
// prologue
for( int x = 0; x < (n-level); ++x ) // rotate each possible value
// into the slot at vals[level]
{
// if not at lowest level, keep spinning down...
if( (level+1) < r )
permute(vals,n,r,pfn,pCount,level+1);
// ...otherwise, report bottom-level permutation
else
{
pfn(vals,r);
(*pCount)++;
}
// rotate all possible values in to this position
rotate(&vals[level],&vals[level+1],&vals[n]);
}
// epilogue
}