Flex/Product.pm

filter: Statementstmt 99.0% 100 / 101 Branchbran 81.8% 36 / 44 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 94.7% 18 / 19 Podpod 50.0% 1 / 2 totaltotal 91.9% 160 / 174 CC 26 SCAR 17.5
CC
26
Cov
93%
__ANON__
11.0
construct
8.0
new
6.1
CRAP 5.8
« Flex/Buffer.pm ↑ Summary Flex/Freeze.pm »
1 package Iterator::Flex::Product;
2  
3 # ABSTRACT: An iterator which produces a Cartesian product of iterators
4  
5 2 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use experimental qw( signatures declared_refs refaliasing );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.34';
11  
12 use Iterator::Flex::Utils
13 2 ▶ qw( RETURN STATE EXHAUSTION :IterAttrs :IterStates can_meth throw_failure );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use parent 'Iterator::Flex::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63 6 ▶sub new ( $class, @args ) {
Subroutine new: called CC=6 SCAR=18.1
Pod documented
64 6 ▶ my $pars = Ref::Util::is_hashref( $args[-1] ) ? pop @args : {};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($args[-1]) ? : 0 6 6
65  
66 6 ▶ throw_failure( parameter => 'not enough parameters' )
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 6 6
67 unless @args;
68  
69 6 my @iterators;
70 my @keys;
71  
72 # distinguish between ( key => iterator, key =>iterator ) and ( iterator, iterator );
73 6 ▶ if ( Ref::Util::is_ref( $args[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_ref($args[0])) { } 4 2 6
74 4 @iterators = @args;
75 }
76 else {
77 2 ▶ throw_failure( parameter => 'expected an even number of arguments' )
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 2 2
78 if @args % 2;
79  
80 2 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 4 2 6
81 4 push @keys, shift @args;
82 4 push @iterators, shift @args;
83 }
84 }
85  
86 6 $class->SUPER::new( { keys => \@keys, depends => \@iterators, value => [] }, $pars );
87 }
88  
89 8 ▶sub construct ( $class, $state ) { ## no critic (ExcessComplexity)
Subroutine construct: called CC=8 SCAR=20.8
Pod undocumented
90 8 ▶ throw_failure( parameter => q{state must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 8 8
91 unless Ref::Util::is_hashref( $state );
92  
93 8 ▶ $state->{value} //= [];
Condition100%2/2
$state->{"value"} //= []
l!l
62
MC/DC100%1/1
$state->{"value"} //= []
$state->{"value"}
Truth table100%2/2
A
A= $state->{"value"}
Aresult
11
00
94  
95 my ( \@depends, \@keys, \@value, $thaw )
96 8 = @{$state}{qw[ depends keys value thaw ]};
97  
98 # transform into iterators if required.
99 my @iterators
100 8 = map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @depends;
101  
102 # can only work if the iterators support a rewind method
103 throw_failure( parameter => q{all iterables must provide a rewind method} )
104 8 ▶ unless List::Util::all { defined can_meth( $_, 'rewind' ) } @iterators;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &List::Util::all(sub { defined can_meth($_, "rewind"); } , @iterators) 0 8 8
105  
106 8 ▶ throw_failure( parameter => q{number of keys not equal to number of iterators} )
Branch: 1 branch
BranchTrueFalseTotal
if @keys and @keys != @iterators 0 8 8
Condition66%2/3
@keys and @keys != @iterators
!ll&&!rl&&r
530
MC/DC0%0/2
@keys and @keys != @iterators
@keys @keys != @iterators
Truth table66%2/3
A and B
A= @keysB= @keys != @iterators
ABresult
0X0
100
111
107 if @keys && @keys != @iterators;
108  
109 8 ▶ @value = map { $_->current } @iterators
Branch: 1 branch
BranchTrueFalseTotal
if $thaw 2 6 8
110 if $thaw;
111  
112 ## no critic ( AmbiguousNames )
113 8 my @set = ( 1 ) x @value;
114  
115 8 my @src = map { [ $_, $_->can( 'is_exhausted' ) ] } @iterators;
116  
117 8 my $self;
118 my $iterator_state;
119 my %params = (
120  
121 ( +_SELF ) => \$self,
122  
123 ( +STATE ) => \$iterator_state,
124  
125 44 ▶ ( +NEXT ) => sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
126 44 ▶ return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED;
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 11 33 44
127  
128 # first time through
129 33 ▶ if ( !@value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @value) { } 7 26 33
130  
131 7 for my $src ( @src ) {
132 14 my ( $iter, $is_exhausted ) = $src->@*;
133 14 push @value, $iter->();
134  
135 14 ▶ return $self->signal_exhaustion
Branch: 1 branch
BranchTrueFalseTotal
if $iter->$is_exhausted 0 14 14
136 if $iter->$is_exhausted;
137 }
138  
139 7 @set = ( 1 ) x @value;
140 }
141  
142 else {
143 26 my ( $iter, $is_exhausted ) = $src[-1]->@*;
144  
145 26 $value[-1] = $iter->();
146 26 ▶ if ( $iter->$is_exhausted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->$is_exhausted) 13 13 26
147 13 $set[-1] = 0;
148 13 my $idx = @src - 1;
149  
150 13 ▶ while ( --$idx >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$idx >= 0) 13 7 20
151 13 ( $iter, $is_exhausted ) = $src[$idx]->@*;
152 13 $value[$idx] = $iter->();
153 13 ▶ last unless $iter->$is_exhausted;
Branch: 1 branch
BranchTrueFalseTotal
unless $iter->$is_exhausted 6 7 13
154 7 $set[$idx] = 0;
155 }
156  
157 13 ▶ return $self->signal_exhaustion
Branch: 1 branch
BranchTrueFalseTotal
unless $set[0] 7 6 13
158 if !$set[0];
159  
160 6 ▶ while ( ++$idx < @src ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$idx < @src) 6 6 12
161 6 $src[$idx][0]->rewind;
162 6 $value[$idx] = $src[$idx][0]->();
163 6 $set[$idx] = 1;
164 }
165 }
166  
167 }
168 26 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 8 18 26
169 8 my %value;
170 8 @value{@keys} = @value;
171 8 return \%value;
172 }
173 else {
174 18 return [@value];
175 }
176 },
177  
178 33 ▶ ( +CURRENT ) => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
179 33 ▶ return undef if !@value;
Branch: 1 branch
BranchTrueFalseTotal
unless @value 6 27 33
180 27 ▶ return $self->signal_exhaustion if $iterator_state eq IterState_EXHAUSTED;
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state eq 1 5 22 27
181 22 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 8 14 22
182 8 my %value;
183 8 @value{@keys} = @value;
184 8 return \%value;
185 }
186 else {
187 14 return [@value];
188 }
189 },
190  
191 0 ▶ ( +RESET ) => sub { @value = () },
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 1 ▶ ( +REWIND ) => sub { @value = () },
Subroutine __ANON__: called CC=1 SCAR=0.0
193 8 ( +_DEPENDS ) => \@iterators,
194 );
195  
196 # can only freeze if the iterators support a current method
197 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::all(sub { defined can_meth($_, "current"); } , @iterators)) 8 0 8
198 16 ▶ List::Util::all { defined can_meth( $_, 'current' ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
199 @iterators
200 )
201 {
202  
203 2 ▶ $params{ +FREEZE } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
204 2 return [ $class, { keys => \@keys } ];
205 8 };
206 8 $params{ +_ROLES } = ['Freeze'];
207 }
208  
209 8 $params{ +_NAME } = 'iproduct';
210 8 return \%params;
211 }
212  
213  
214 2 __PACKAGE__->_add_roles( qw[
215 State::Closure
216 Next::ClosedSelf
217 Current::Closure
218 Reset::Closure
219 Rewind::Closure
220 ] );
221  
222 2 1;
223  
224 #
225 # This file is part of Iterator-Flex
226 #
227 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
228 #
229 # This is free software, licensed under:
230 #
231 # The GNU General Public License, Version 3, June 2007
232 #
233  
234 __END__
« Flex/Buffer.pm ↑ Summary Flex/Freeze.pm »