#!/usr/bin/perl

use blib;
use Benchmark;
use Algorithm::Permute qw(permute);
use List::Permutor;                             # Tom Phoenix's
require './mjd_permute';    # MJD's
require './Permutations.pm';    # Abigail's

my @arr = (1..8);

$p = new Algorithm::Permute([@arr]);
$l = new List::Permutor (@arr);

#permute([split], []);
sub faq_permute{
    my @items = @{ $_[0] };
    my @perms = @{ $_[1] };
    unless (@items) {
#       print "@perms\n";
        @res = @perms;
    } else {
        my(@newitems,@newperms,$i);
        foreach $i (0 .. $#items) {
            @newitems = @items;
            @newperms = @perms;
            unshift(@newperms, splice(@newitems, $i, 1));
            faq_permute([@newitems], [@newperms]);
        }
    }
}

sub mjd_permute {
    my @data = @_;
    my $num_permutations = factorial(scalar @data);
    for (my $i=0; $i < $num_permutations; $i++) {
        my @permutation = @data[n2perm($i, $#data)];
#        print "@permutation\n";
    }
}

timethese(5, {
   'Abigail\'s'    => sub { 
       @res = Combinatorial::Permutations::permutate(@arr) },
    'MJD\'s'    => sub { mjd_permute(@arr) },
    'perlfaq4'  => sub { faq_permute([@arr], []) },
    'Algorithm::Permute' => sub { while (@res = $p->next) {}; $p->reset },
    'List::Permutor'         => sub { while (@res = $l->next) {}; $l->reset },
    'Algorithm::Permute qw(permute)' => sub { permute { @res = @arr } @arr },
    });
