NAME
Algorithm::PermuteInPlace - Fast permutations
SYNOPSIS
use Algorithm::PermuteInPlace;
my @array = (1..9);
my $permutor = Algorithm::PermuteInPlace->new(\@array);
print "@array\n";
print "@array\n" while $permutor->next();
DESCRIPTION
This module will generate all the permutations of an array, modifying the array in-place. That allows it to be very fast.
Each time you call $p->next()
, two adjacent elements of the array are swapped. This is done in such a way that every permutation will eventually be generated. When the last permutation has been generated (so that the list has been restored to its original order), next
returns false.
It's not recommended to change the elements of the array while a permutor is attached to it. If the length changes, next()
will die.
Algorithm::PermuteInPlace currently uses the so-called Johnson-Trotter algorithm. This may change, if a different algorithm proves to be faster.
AUTHOR
Robin Houston, <robin@cpan.org>