## Introduction

This class can perform permutations on a set, but not the kind of permutations that the STL next_permutation() function does, which are permutations of *n*, but *k*-permutations of *n*.

## Background

There is some confusion over the meaning of "permutation" so for clarity, you might want to read the Wikipedia article on permutations. Note that this does not calculate the number of permutations there are in given parameters of *k* and *n* but returns all the subsets that form the permutations of a set.

## Using the Code

Currently, you do not get to choose the set. `AiPermutations`

presumes that your set will be all integers between 0 and *n* inclusive. If that does not suit your data, then you can treat the arrays returned as holding the indexes to the array of your set. So, if your set would look something like:

char myset[] = { 'A', 'B', 'C', 'D' };

then you can convert the returned subset into a subset of your liking with:

for(int i = 0; i < k; i++)
mysubset[i] = myset[ returnedsubset[i] ];

Other than that, usage is very simple. Just plug the *k* and *n* parameters into the constructor, allocate an `elementtype`

-array of size *k* and pass it to `AiPermutations::next()`

, which you repeatedly call until it returns `false`

. Here's an example:

#include <span class="code-keyword"><iostream>
</span>
using namespace std;
#include <span class="code-string">"aipermutations.h"
</span>#include <span class="code-keyword"><boost/scoped_array.hpp>
</span>#include <span class="code-keyword"><stdlib.h>
</span>
#define K 3
#define N 6
int fact(int n) {
int nAccumulator = 1;
for(int i = 2; i <= n; i++)
nAccumulator *= i;
return nAccumulator;
}
int main(int argc, char **argv) {
AiPermutations perm(K, N);
boost::scoped_array<AiPermutations::elementtype>
permutation(new AiPermutations::elementtype[K]);
int nPermutationCount = 0;
while (perm.next(permutation.get())) {
nPermutationCount++;
cout << "got: (";
for(int i = 0; i < K - 1; i++)
cout << permutation[i] << ", ";
cout << permutation[K - 1] << ')' << endl;
}
cout << "Finally, " << nPermutationCount << "
permutations when it should have been: " << fact(N)/fact(N - K) << endl;
return EXIT_SUCCESS;
}

### Robustness

This class is not designed around it. It will not throw exceptions or return errors (when built in release mode) for absurd parameters or inadequately allocated arrays in method parameters. Furthermore, the code will behave unpredictably when processing trivial examples, such as a *k* = 0, even if they are mathematically sound.

### Portability

It should be portable in ANSI C and requires STL. So far I've built it under:

- gcc 4.4.3 (Ubuntu 10.04)
- Visual Studio 2005 (WinXP)

## Watch for...

...a template version that allows the user to use sets of his own type. This should not be difficult, since the concept is as difficult as the example above, however if a set was provided with duplicate elements, then non-unique subsets would be produced, which probably could not be managed easily.