File Coverage

blib/lib/Permute/Named/Iter.pm
Criterion Covered Total %
statement 49 53 92.4
branch 15 20 75.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 71 80 88.7


line stmt bran cond sub pod time code
1             package Permute::Named::Iter;
2              
3             our $DATE = '2016-09-26'; # DATE
4             our $VERSION = '0.04'; # VERSION
5              
6 1     1   421 use 5.010001;
  1         3  
7 1     1   3 use strict;
  1         1  
  1         14  
8 1     1   3 use warnings;
  1         1  
  1         18  
9              
10 1     1   3 use Exporter qw(import);
  1         0  
  1         309  
11             our @EXPORT_OK = qw(
12             permute_named_iter
13             );
14              
15             sub permute_named_iter {
16 3 50   3 1 1553 die "Please supply a non-empty list of key-specification pairs" unless @_;
17 3 50       9 die "Please supply an even-sized list" unless @_ % 2 == 0;
18              
19 3         2 my @keys;
20             my @values;
21 3         13 while (my ($key, $values) = splice @_, 0, 2) {
22 5         6 push @keys, $key;
23 5 100       11 $values = [$values] unless ref($values) eq 'ARRAY';
24 5 50       10 die "$key cannot contain empty values" unless @$values;
25 5         13 push @values, $values;
26             }
27              
28 3         5 my $state = [(0) x @keys];
29 3         5 my $state2 = 0; # 0,1,2
30             my $iter = sub {
31 13 100   13   45 if (!$state2) { # starting the first time, don't increment state yet
    50          
32 3         1 $state2 = 1;
33 3         10 goto L2;
34             } elsif ($state2 == 2) { # all permutation exhausted
35 0         0 return undef;
36             }
37 10         9 my $i = $#{$state};
  10         9  
38             L1:
39 10         12 while ($i >= 0) {
40 10 100       6 if ($state->[$i] >= $#{$values[$i]}) {
  10         15  
41 4 100       6 if ($i == 0) {
42 1         1 $state2 = 2;
43 1         2 return undef;
44             }
45 3         2 $state->[$i] = 0;
46 3         3 my $j = $i-1;
47 3         5 while ($j >= 0) {
48 3 100       3 if ($state->[$j] >= $#{$values[$j]}) {
  3         4  
49 2 50       3 if ($j == 0) { # all permutation exhausted
50 2         2 $state2 = 2;
51 2         4 return undef;
52             }
53 0         0 $state->[$j] = 0;
54 0         0 $j--;
55             } else {
56 1         1 $state->[$j]++;
57 1         1 last L1;
58             }
59             }
60 0         0 $i--;
61             } else {
62 6         4 $state->[$i]++;
63 6         6 last;
64             }
65             }
66             L2:
67 17         30 return { map { ($keys[$_] => $values[$_][ $state->[$_] ]) }
68 10         7 0..$#{$state} };
  10         14  
69 3         13 };
70 3         6 $iter;
71             }
72              
73             1;
74             # ABSTRACT: Permute multiple-valued key-value pairs
75              
76             __END__