| 1 |
NAME |
| 2 |
|
| 3 |
Algorithm::Permute - Handy and fast permutation with object oriented |
| 4 |
interface |
| 5 |
|
| 6 |
SYNOPSIS |
| 7 |
|
| 8 |
use Algorithm::Permute; |
| 9 |
|
| 10 |
# default is to create n of n objects permutation generator |
| 11 |
my $p = Algorithm::Permute->new(['a'..'d']); |
| 12 |
|
| 13 |
# but also you can create r of n objects permutation generator, where r <= n |
| 14 |
my $p = Algorithm::Permute->new([1..4], 3); |
| 15 |
|
| 16 |
while (my @res = $p->next) { |
| 17 |
print join(", ", @res), "\n"; |
| 18 |
} |
| 19 |
|
| 20 |
# and this one is the speed demon: |
| 21 |
my @array = (1..9); |
| 22 |
Algorithm::Permute::permute { print "@array\n" } @array; |
| 23 |
|
| 24 |
DESCRIPTION |
| 25 |
|
| 26 |
This handy module makes performing permutation in Perl easy and fast, |
| 27 |
although perhaps its algorithm is not the fastest on the earth. It |
| 28 |
supports permutation r of n objects where 0 < r <= n. |
| 29 |
|
| 30 |
METHODS |
| 31 |
|
| 32 |
new [@list] |
| 33 |
|
| 34 |
Returns a permutor object for the given items. |
| 35 |
|
| 36 |
next |
| 37 |
|
| 38 |
Returns a list of the items in the next permutation. The order of the |
| 39 |
resulting permutation is the same as of the previous version of |
| 40 |
Algorithm::Permute. |
| 41 |
|
| 42 |
peek |
| 43 |
|
| 44 |
Returns the list of items which will be returned by next(), but |
| 45 |
doesn't advance the sequence. Could be useful if you wished to skip |
| 46 |
over just a few unwanted permutations. |
| 47 |
|
| 48 |
reset |
| 49 |
|
| 50 |
Resets the iterator to the start. May be used at any time, whether |
| 51 |
the entire set has been produced or not. Has no useful return value. |
| 52 |
|
| 53 |
CALLBACK STYLE INTERFACE |
| 54 |
|
| 55 |
Starting with version 0.03, there is a function - not exported by |
| 56 |
default - which supports a callback style interface: |
| 57 |
|
| 58 |
permute BLOCK ARRAY |
| 59 |
|
| 60 |
A block of code is passed, which will be executed for each |
| 61 |
permutation. The array will be changed in place, and then changed |
| 62 |
back again before permute returns. During the execution of the |
| 63 |
callback, the array is read-only and you'll get an error if you try |
| 64 |
to change its length. (You can change its elements, but the |
| 65 |
consequences are liable to confuse you and may change in future |
| 66 |
versions.) |
| 67 |
|
| 68 |
You have to pass an array, it can't just be a list. It does work with |
| 69 |
special arrays and tied arrays, though unless you're doing something |
| 70 |
particularly abstruse you'd be better off copying the elements into a |
| 71 |
normal array first. Example: |
| 72 |
|
| 73 |
my @array = (1..9); |
| 74 |
permute { print "@array\n" } @array; |
| 75 |
|
| 76 |
The code is run inside a pseudo block, rather than as a normal |
| 77 |
subroutine. That means you can't use return, and you can't jump out |
| 78 |
of it using goto and so on. Also, caller won't tell you anything |
| 79 |
helpful from inside the callback. Such is the price of speed. |
| 80 |
|
| 81 |
The order in which the permutations are generated is not guaranteed, |
| 82 |
so don't rely on it. |
| 83 |
|
| 84 |
The low-level hack behind this function makes it currently the |
| 85 |
fastest way of doing permutation among others. |
| 86 |
|
| 87 |
COMPARISON |
| 88 |
|
| 89 |
I've collected some Perl routines and modules which implement |
| 90 |
permutation, and do some simple benchmark. The whole result is the |
| 91 |
following. |
| 92 |
|
| 93 |
Permutation of eight scalars: |
| 94 |
|
| 95 |
Abigail's : 9 wallclock secs ( 8.07 usr + 0.30 sys = 8.37 CPU) |
| 96 |
Algorithm::Permute : 5 wallclock secs ( 5.72 usr + 0.00 sys = 5.72 CPU) |
| 97 |
Algorithm::Permute qw(permute): 2 wallclock secs ( 1.65 usr + 0.00 sys = 1.65 CPU) |
| 98 |
List::Permutor : 27 wallclock secs (26.73 usr + 0.01 sys = 26.74 CPU) |
| 99 |
Memoization : 32 wallclock secs (32.55 usr + 0.02 sys = 32.57 CPU) |
| 100 |
perlfaq4 : 36 wallclock secs (35.27 usr + 0.02 sys = 35.29 CPU) |
| 101 |
|
| 102 |
Permutation of nine scalars (the Abigail's routine is commented out, |
| 103 |
because it stores all of the result in memory, swallows all of my |
| 104 |
machine's memory): |
| 105 |
|
| 106 |
Algorithm::Permute : 43 wallclock secs ( 42.93 usr + 0.04 sys = 42.97 CPU) |
| 107 |
Algorithm::Permute qw(permute): 15 wallclock secs ( 14.82 usr + 0.00 sys = 14.82 CPU) |
| 108 |
List::Permutor : 227 wallclock secs (226.46 usr + 0.22 sys = 226.68 CPU) |
| 109 |
Memoization : 307 wallclock secs (306.69 usr + 0.43 sys = 307.12 CPU) |
| 110 |
perlfaq4 : 272 wallclock secs (271.93 usr + 0.33 sys = 272.26 CPU) |
| 111 |
|
| 112 |
The benchmark script is included in the bench directory. I understand |
| 113 |
that speed is not everything. So here is the list of URLs of the |
| 114 |
alternatives, in case you hate this module. |
| 115 |
|
| 116 |
* Memoization is discussed in chapter 4 Perl Cookbook, so you can get |
| 117 |
it from O'Reilly: |
| 118 |
ftp://ftp.oreilly.com/published/oreilly/perl/cookbook |
| 119 |
|
| 120 |
* Abigail's: http://www.foad.org/~abigail/Perl |
| 121 |
|
| 122 |
* List::Permutor: http://www.cpan.org/modules/by-module/List |
| 123 |
|
| 124 |
* The classic way, usually used by Lisp hackers: perldoc perlfaq4 |
| 125 |
|
| 126 |
ACKNOWLEDGEMENT |
| 127 |
|
| 128 |
In Edwin's words: Yustina Sri Suharini - my ex-fiance-now-wife, for |
| 129 |
providing the permutation problem to me. |
| 130 |
|
| 131 |
SEE ALSO |
| 132 |
|
| 133 |
* Data Structures, Algorithms, and Program Style Using C - Korsh and |
| 134 |
Garrett |
| 135 |
|
| 136 |
* Algorithms from P to NP, Vol. I - Moret and Shapiro |
| 137 |
|
| 138 |
AUTHOR |
| 139 |
|
| 140 |
Edwin Pratomo <[email protected]> was the original author. |
| 141 |
|
| 142 |
Stephan Loyd <[email protected]> is co-maintainer after version 0.12. |
| 143 |
|
| 144 |
The object oriented interface is taken from Tom Phoenix's |
| 145 |
List::Permutor. Robin Houston <[email protected]> invented and |
| 146 |
contributed the callback style interface. |
| 147 |
|
| 148 |
COPYRIGHT AND LICENSE |
| 149 |
|
| 150 |
This software is copyright (c) 1999 by Edwin Pratomo. |
| 151 |
|
| 152 |
This is free software; you can redistribute it and/or modify it under |
| 153 |
the same terms as the Perl 5 programming language system itself. |
| 154 |
|