| 1 | =head1 Name | ||||||||||
| 2 | |||||||||||
| 3 | Math::Permute::List - Generate all permutations of a list. | ||||||||||
| 4 | |||||||||||
| 5 | =head1 Synopsis | ||||||||||
| 6 | |||||||||||
| 7 | use Math::Permute::List; | ||||||||||
| 8 | |||||||||||
| 9 | permute {say "@_"} qw(a b c); | ||||||||||
| 10 | |||||||||||
| 11 | # a b c | ||||||||||
| 12 | # a c b | ||||||||||
| 13 | # b a c | ||||||||||
| 14 | # c a b | ||||||||||
| 15 | # b c a | ||||||||||
| 16 | # c b a | ||||||||||
| 17 | |||||||||||
| 18 | =cut | ||||||||||
| 19 | |||||||||||
| 20 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | package Math::Permute::List; | ||||||||||
| 23 | |||||||||||
| 24 | sub permute(&@) # Generate all permutations of a list | ||||||||||
| 25 | 10 | ▶ | {my $s = shift; # Subroutine to call to process each permutation | ||||||||
|
Subroutine
permute: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 26 | 10 | my $n = scalar(@_); # Size of array to be permuted | |||||||||
| 27 | # return 0 unless $n; # Empty lists cannot be permuted - removed per Philipp Rumpf | ||||||||||
| 28 | 10 | my $l = 0; # Item being permuted | |||||||||
| 29 | 10 | my @p = (); # Current permutations | |||||||||
| 30 | 10 | my @P = @_; # Array to permute | |||||||||
| 31 | 10 | my @Q = (); # Permuted array | |||||||||
| 32 | |||||||||||
| 33 | 10 | my $p; $p = sub # Generate each permutation | |||||||||
| 34 | 9864551 | ▶ | {if ($l < $n) | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||
| 35 | 6235584 | {for(0..$n-1) | |||||||||
| 36 | 62354304 | ▶ | {if (!$p[$_]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 9864541 | {$Q[$_] = $P[$l]; | |||||||||
| 38 | 9864541 | $p[$_] = ++$l; | |||||||||
| 39 | 9864541 | &$p(); | |||||||||
| 40 | 9864539 | --$l; | |||||||||
| 41 | 9864539 | $p[$_] = 0; | |||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | else | ||||||||||
| 46 | 3628967 | {&$s(@Q); | |||||||||
| 47 | } | ||||||||||
| 48 | 10 | }; | |||||||||
| 49 | |||||||||||
| 50 | 10 | &$p; | |||||||||
| 51 | |||||||||||
| 52 | 9 | $p = undef; # Break memory loop per Philipp Rumpf | |||||||||
| 53 | |||||||||||
| 54 | 9 | my $i = 1; $i *= $_ for 2..$n; | |||||||||
| 55 | 9 | $i # Number of permutations | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | # Export details | ||||||||||
| 59 | |||||||||||
| 60 | 10 | require 5; | |||||||||
| 61 | 10 | require Exporter; | |||||||||
| 62 | |||||||||||
| 63 | 10 | ▶ | use vars qw(@ISA @EXPORT $VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 64 | |||||||||||
| 65 | 10 | @ISA = qw(Exporter); | |||||||||
| 66 | 10 | @EXPORT = qw(permute); | |||||||||
| 67 | 10 | $VERSION = '1.007'; | |||||||||
| 68 | |||||||||||
| 69 | =head1 Description | ||||||||||
| 70 | |||||||||||
| 71 | Generate and process all the permutations of a list using the standard | ||||||||||
| 72 | Perl metaphor. | ||||||||||
| 73 | |||||||||||
| 74 | C<permute()> returns the number of permutations in both scalar and array | ||||||||||
| 75 | context. | ||||||||||
| 76 | |||||||||||
| 77 | C<permute()> is easy to use and fast. It is written in 100% Pure Perl. | ||||||||||
| 78 | |||||||||||
| 79 | Please note that the order in which the permutations are generated is | ||||||||||
| 80 | not guaranteed, so please do not rely on it. | ||||||||||
| 81 | |||||||||||
| 82 | =head1 Export | ||||||||||
| 83 | |||||||||||
| 84 | The C<permute()> function is exported. | ||||||||||
| 85 | |||||||||||
| 86 | =head1 Installation | ||||||||||
| 87 | |||||||||||
| 88 | Standard Module::Build process for building and installing modules: | ||||||||||
| 89 | |||||||||||
| 90 | perl Build.PL | ||||||||||
| 91 | ./Build | ||||||||||
| 92 | ./Build test | ||||||||||
| 93 | ./Build install | ||||||||||
| 94 | |||||||||||
| 95 | Or, if you're on a platform (like DOS or Windows) that doesn't require | ||||||||||
| 96 | the "./" notation, you can do this: | ||||||||||
| 97 | |||||||||||
| 98 | perl Build.PL | ||||||||||
| 99 | Build | ||||||||||
| 100 | Build test | ||||||||||
| 101 | Build install | ||||||||||
| 102 | |||||||||||
| 103 | =head1 Author | ||||||||||
| 104 | |||||||||||
| 105 | PhilipRBrenan@appaapps.com | ||||||||||
| 106 | |||||||||||
| 107 | http://www.appaapps.com | ||||||||||
| 108 | |||||||||||
| 109 | =head1 Acknowledgements | ||||||||||
| 110 | |||||||||||
| 111 | With considerable, cogent and unfailing help from Philipp Rumpf for which I am indebted. | ||||||||||
| 112 | |||||||||||
| 113 | http://www.appaapps.com | ||||||||||
| 114 | |||||||||||
| 115 | =head1 See Also | ||||||||||
| 116 | |||||||||||
| 117 | =over | ||||||||||
| 118 | |||||||||||
| 119 | =item L<Math::Cartesian::Product> | ||||||||||
| 120 | |||||||||||
| 121 | =item L<Math::Disarrange::List> | ||||||||||
| 122 | |||||||||||
| 123 | =item L<Math::Subsets::List> | ||||||||||
| 124 | |||||||||||
| 125 | =item L<Algorithm::Permute> | ||||||||||
| 126 | |||||||||||
| 127 | =item L<Algorithm::FastPermute> | ||||||||||
| 128 | |||||||||||
| 129 | =back | ||||||||||
| 130 | |||||||||||
| 131 | =head1 Copyright | ||||||||||
| 132 | |||||||||||
| 133 | Copyright (c) 2009 Philip R Brenan. | ||||||||||
| 134 | |||||||||||
| 135 | This module is free software. It may be used, redistributed and/or | ||||||||||
| 136 | modified under the same terms as Perl itself. | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||