| 1 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | package Set::CartesianProduct::Lazy; | ||||||||||||
| 4 | 1 | $Set::CartesianProduct::Lazy::VERSION = '0.004'; | |||||||||||
| 5 | { | ||||||||||||
| 6 | 1 | $Set::CartesianProduct::Lazy::DIST = 'Set-CartesianProduct-Lazy'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | |||||||||||||
| 9 | # ABSTRACT: lazily calculate the tuples of a cartesian-product | ||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use List::Util qw( reduce ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use Scalar::Util qw( reftype ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 5 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 17 | 5 | my $class = shift; | |||||||||||
| 18 | 5 | my %opts = map { %$_ } grep { reftype $_ eq 'HASH' } @_; | |||||||||||
| 19 | 5 | my @sets = grep { reftype $_ eq 'ARRAY' } @_; | |||||||||||
| 20 | |||||||||||||
| 21 | 5 | my $getsub = __make_getsub(\%opts, \@sets); | |||||||||||
| 22 | |||||||||||||
| 23 | 5 | return bless { %opts, sets => \@sets, getsub => $getsub }, $class; | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | 5 | ▶ | sub __make_getsub { | ||||||||||
|
Subroutine
__make_getsub: called CC=2 SCAR=6.9
| |||||||||||||
| 27 | 5 | my ($opts, $sets_ref) = @_; | |||||||||||
| 28 | |||||||||||||
| 29 | # TODO: eval code to produce the sub so the code isn't duplicated | ||||||||||||
| 30 | |||||||||||||
| 31 | 5 | ▶ | if ($opts->{less_lazy}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | |||||||||||||
| 33 | # pre-calculate some things, so we don't have to every time. | ||||||||||||
| 34 | 1 | my @sets = @$sets_ref; | |||||||||||
| 35 | my @info = map { | ||||||||||||
| 36 | 1 | ▶ | [ $_, (scalar @{$sets[$_]}), reduce { $a * @$b } 1, @sets[$_ + 1 .. $#sets] ]; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | } 0 .. $#sets; | ||||||||||||
| 38 | |||||||||||||
| 39 | 2 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 40 | 2 | my ($n) = @_; | |||||||||||
| 41 | |||||||||||||
| 42 | my @tuple = map { | ||||||||||||
| 43 | 2 | my ($set_num, $set_size, $factor) = @$_; | |||||||||||
| 44 | 6 | $sets[ $set_num ][ int( $n / $factor ) % $set_size ]; | |||||||||||
| 45 | } @info; | ||||||||||||
| 46 | |||||||||||||
| 47 | 2 | ▶ | return wantarray ? @tuple : \@tuple; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 1 | }; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 9 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 52 | 9 | my ($n) = @_; | |||||||||||
| 53 | |||||||||||||
| 54 | 9 | my @tuple; | |||||||||||
| 55 | |||||||||||||
| 56 | 9 | my @sets = @$sets_ref; | |||||||||||
| 57 | |||||||||||||
| 58 | 9 | for my $set_num (0 .. $#sets) { | |||||||||||
| 59 | 20 | my $set_size = @{ $sets[$set_num] }; | |||||||||||
| 60 | 20 | ▶ | my $factor = reduce { $a * @$b } 1, @sets[$set_num + 1 .. $#sets]; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 20 | my $idx = int( $n / $factor ) % $set_size; | |||||||||||
| 62 | |||||||||||||
| 63 | 20 | push @tuple, $sets[$set_num][$idx]; | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 9 | ▶ | return wantarray ? @tuple : \@tuple; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 4 | }; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | |||||||||||||
| 71 | 11 | ▶ | sub get { | ||||||||||
|
Subroutine
get: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 72 | 11 | my $self = shift; | |||||||||||
| 73 | 11 | ▶ | croak "no value passed to get method\n" unless defined $_[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 11 | $self->{getsub}->(@_); | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | |||||||||||||
| 78 | #sub get_faster { my $self = shift; $self->{getsub}->(@_); } | ||||||||||||
| 79 | |||||||||||||
| 80 | { | ||||||||||||
| 81 | 1 | ▶ | no warnings 'once'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 25 | ▶ | sub count { return reduce { $a * @$b } 1, @{ shift->{sets} || [] } } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Subroutine
count: called CC=2 SCAR=7.1
Pod
documented
Condition50%1/2
+(shift())->{'sets'} || []
MC/DC0%0/1
+(shift())->{'sets'} || []
+(shift())->{'sets'}
Truth table50%1/2
A
A= +(shift())->{'sets'}
| |||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 3 | ▶ | sub last_idx { return shift->count - 1 } | ||||||||||
|
Subroutine
last_idx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 86 | |||||||||||||
| 87 | |||||||||||||
| 88 | 1 | 1 && q{a set in time saves nine}; | |||||||||||
| 89 | |||||||||||||
| 90 | __END__ | ||||||||||||