Implement nextPermutation and nextEvenPermutation

This code has already been working in my personal projects for a while now; since Andrei asked for nextPermutation a long time ago, I thought I'd throw this out there for review. Hopefully this will have better luck than cartesianProduct. :)

I also included nextEvenPermutation because it is needed in some geometric applications, but one can hardly find a good algorithm for it out there. It's not that much different from nextPermutation. Note that it automatically subsumes nextOddPermutation (you just swap the last two elements of a sorted array and then call nextEvenPermutation repeatedly, and you get all the odd permutations).