File Coverage

blib/lib/List/Permutor.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 6 6 100.0
pod 4 4 100.0
total 48 48 100.0


line stmt bran cond sub pod time code
1             package List::Permutor;
2              
3 1     1   716 use strict;
  1         2  
  1         34  
4 1     1   4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         1  
  1         356  
5              
6             require Exporter;
7              
8             @ISA = qw(Exporter);
9             @EXPORT = qw();
10             $VERSION = '0.022';
11              
12             sub new {
13 5     5 1 169 my $class = shift;
14 5         12 my $items = [ @_ ];
15 5         28 bless [ $items, [ 0..$#$items ] ], $class;
16             }
17              
18             sub reset {
19 2     2 1 11 my $self = shift;
20 2         3 my $items = $self->[0];
21 2         6 $self->[1] = [ 0..$#$items ];
22 2         4 1; # No useful return value
23             }
24              
25             sub peek {
26 6     6 1 19 my $self = shift;
27 6         10 my $items = $self->[0];
28 6         5 my $rv = $self->[1];
29 6         18 @$items[ @$rv ];
30             }
31              
32             sub next {
33 142     142 1 872 my $self = shift;
34 142         157 my $items = $self->[0];
35 142         151 my $rv = $self->[1]; # return value array
36 142 100       349 return unless @$rv;
37 139         199 my @next = @$rv;
38             # The last N items in @next (for 1 <= N <= @next) are each
39             # smaller than the one before. Move those into @tail.
40 139         180 my @tail = pop @next;
41 139   100     580 while (@next and $next[-1] > $tail[-1]) {
42 98         354 push @tail, pop @next;
43             }
44             # Then there's one more. Right?
45 139 100       248 if (defined(my $extra = pop @next)) {
46             # The extra one exchanges with the next larger one in @tail
47 135         410 my($place) = grep $extra < $tail[$_], 0..$#tail;
48 135         229 ($extra, $tail[$place]) = ($tail[$place], $extra);
49             # And the next order is what you get by assembling the three
50 135         327 $self->[1] = [ @next, $extra, @tail ];
51             } else {
52             # Guess that's all....
53 4         6 $self->[1] = [];
54             }
55 139         563 return @$items[ @$rv ];
56             }
57              
58             1;
59             __END__