diff --git a/permute.hh b/permute.hh index 27b8906..715b905 100644 --- a/permute.hh +++ b/permute.hh @@ -11,24 +11,30 @@ Copyright 2023 Ahmet Inan namespace CODE { template -class ReversibleFisherYatesShuffle +struct FisherYatesShuffle +{ + template + void operator()(TYPE *array) + { + CODE::Xorshift32 prng; + for (int i = 0; i < SIZE-1; ++i) + std::swap(array[i], array[i + prng() % (SIZE - i)]); + } +}; + +template +class ReverseFisherYatesShuffle { int seq[SIZE-1]; public: - ReversibleFisherYatesShuffle() + ReverseFisherYatesShuffle() { CODE::Xorshift32 prng; for (int i = 0; i < SIZE-1; ++i) seq[i] = i + prng() % (SIZE - i); } template - void forward(TYPE *array) - { - for (int i = 0; i < SIZE-1; ++i) - std::swap(array[i], array[seq[i]]); - } - template - void reverse(TYPE *array) + void operator()(TYPE *array) { for (int i = SIZE-2; i >= 0; --i) std::swap(array[i], array[seq[i]]);