blib/lib/Iterator/Flex/Zip.pm

filter: Statementstmt 96.7% 147 / 152 Branchbran 76.3% 58 / 76 Conditioncond 39.1% 9 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 100% 21 / 21 Podpod 50.0% 1 / 2 totaltotal 81.6% 236 / 289 CC 48 SCAR 21.1
CC
48
Cov
85%
__ANON__
12.6
new
12.3
__ANON__
10.4
CRAP 8.2
« blib/lib/Iterator/Flex/Flatten.pm ↑ Summary blib/lib/Iterator/Flex/ArrayLike.pm »
1 package Iterator::Flex::Zip;
2  
3 # ABSTRACT: Zip Iterator Class
4  
5 3 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use experimental 'signatures', 'declared_refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.33';
11  
12 3 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Iterator::Flex::Utils qw[ THROW STATE EXHAUSTION :IterAttrs :IterStates throw_failure ];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Ref::Util 'is_ref', 'is_hashref', 'is_blessed_ref';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use parent 'Iterator::Flex::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use constant { map { $_ => lc } qw( ON_EXHAUSTION TRUNCATE THROW INSERT ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94 ▶sub new ( $class, @args ) {
Subroutine new: called CC=12 SCAR=25.1
Pod documented
95 13 ▶ my $pars = is_hashref( $args[-1] ) ? pop @args : {};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($args[-1]) ? : 8 5 13
96  
97 13 ▶ throw_failure( parameter => 'not enough parameters' )
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 13 13
98 unless @args;
99  
100 13 my @iterators;
101 my @keys;
102  
103 # distinguish between ( key => iterator, key =>iterator ) and ( iterator, iterator );
104 13 ▶ if ( is_ref( $args[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_ref($args[0])) { } 8 5 13
105 8 @iterators = @args;
106 }
107 else {
108 5 ▶ throw_failure( parameter => 'expected an even number of arguments' )
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 5 5
109 if @args % 2;
110  
111 5 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 15 5 20
112 15 push @keys, shift @args;
113 15 push @iterators, shift @args;
114 }
115 }
116  
117 ## no critic (AmbiguousNames)
118 13 my ( @set, @insert );
119  
120 13 my %attr = (
121 keys => \@keys,
122 depends => \@iterators,
123 set => \@set,
124 insert => \@insert,
125 value => [],
126 );
127  
128 13 ▶ if ( defined( my $on_exhaustion = delete $pars->{ +ON_EXHAUSTION } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $on_exhaustion = delete $pars->{'on_exhaustion'})) { } 8 5 13
129  
130 8 ▶ if ( is_hashref( $on_exhaustion ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_hashref($on_exhaustion)) { } 6 2 8
elsif (Ref::Util::XS::is_ref($on_exhaustion) or not &first(sub { $on_exhaustion eq $_; } , 'truncate', 'throw')) { } 0 2 2
131 6 my @ekeys = keys $on_exhaustion->%*;
132  
133 6 my %idx;
134 6 @idx{ 0 .. $#iterators } = 0 .. $#iterators;
135 6 ▶ @idx{@keys} = 0 .. $#iterators
Branch: 1 branch
BranchTrueFalseTotal
if @keys 3 3 6
136 if @keys;
137  
138 6 my @iset = @idx{@ekeys};
139  
140 throw_failure( parameter => ON_EXHAUSTION . ' illegal iterator label or index' )
141 6 ▶ if defined first { !defined } @iset;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if defined &first(sub { not defined $_; } , @iset) 0 6 6
142  
143 6 @set[@iset] = ( !!1 ) x @iset;
144 6 @insert[@iset] = $on_exhaustion->@{@ekeys};
145  
146 6 $attr{set} = \@set;
147 6 $attr{insert} = \@insert;
148 6 $attr{ +ON_EXHAUSTION } = INSERT;
149 }
150 4 ▶ elsif ( is_ref( $on_exhaustion ) or !first { $on_exhaustion eq $_ } TRUNCATE, THROW ) {
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%1/3
Ref::Util::XS::is_ref($on_exhaustion) or not &first(sub { $on_exhaustion eq $_; } , "truncate", "throw")
l!l&&r!l&&!r
002
MC/DC0%0/2
Ref::Util::XS::is_ref($on_exhaustion) or not &first(sub { $on_exhaustion eq $_; } , "truncate", "throw")
Ref::Util::XS::is_ref($on_exhaustion) not &first(sub { $on_exhaustion eq $_; } , "truncate", "throw")
Truth table33%1/3
A or not(B)
A= Ref::Util::XS::is_ref($on_exhaustion)B= not &first(sub { $on_exhaustion eq $_; } , "truncate", "throw")
ABresult
1X1
011
000
151 0 throw_failure( parameter => ON_EXHAUSTION . ": unexpected value: $on_exhaustion" );
152 }
153 else {
154 2 $attr{ +ON_EXHAUSTION } = $on_exhaustion;
155 }
156 }
157 else {
158 5 $attr{ +ON_EXHAUSTION } = TRUNCATE;
159 }
160  
161 13 $class->SUPER::new( \%attr, $pars );
162 }
163  
164  
165 ## no critic (ExcessComplexity)
166 ▶sub construct ( $class, $state ) {
Subroutine construct: called CC=9 SCAR=22.5
Pod undocumented
167 13 ▶ throw_failure( parameter => q{state must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 13 13
168 unless is_hashref( $state );
169  
170 13 ▶ $state->{value} //= [];
Condition50%1/2
$state->{"value"} //= []
l!l
130
MC/DC0%0/1
$state->{"value"} //= []
$state->{"value"}
Truth table50%1/2
A
A= $state->{"value"}
Aresult
11
00
171  
172 ## no critic (AmbiguousNames)
173 my ( \@depends, \@keys, \@value, \@insert, \@set, $on_exhaustion, $thaw )
174 13 = @{$state}{qw[ depends keys value insert set on_exhaustion thaw ]};
175  
176 # transform into iterators if required.
177  
178 my @iterators
179 13 = map { to_iterator( $_, { ( +EXHAUSTION ) => THROW } ) } @depends;
180  
181 13 ▶ throw_failure( parameter => q{number of keys not equal to number of iterators} )
Branch: 1 branch
BranchTrueFalseTotal
if @keys and @keys != @iterators 0 13 13
Condition66%2/3
@keys and @keys != @iterators
!ll&&!rl&&r
850
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
182 if @keys && @keys != @iterators;
183  
184 13 ▶ @value = map { $_->current } @iterators
Branch: 1 branch
BranchTrueFalseTotal
if $thaw 0 13 13
185 if $thaw;
186  
187 13 my $self;
188 my $iterator_state;
189 my %params = (
190  
191 ( +_NAME ) => 'izip',
192  
193 ( +_SELF ) => \$self,
194  
195 ( +STATE ) => \$iterator_state,
196  
197  
198 ( +CURRENT ) => sub {
199 2 ▶ return undef if !@value;
Subroutine __ANON__: called CC=4 SCAR=15.9
Branch: 1 branch
BranchTrueFalseTotal
unless @value 1 1 2
200 1 ▶ return $self->signal_exhaustion if $iterator_state eq IterState_EXHAUSTED;
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state eq 1 0 1 1
201 1 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 0 1 1
202 0 my %value;
203 0 @value{@keys} = @value;
204 0 return \%value;
205 }
206 else {
207 1 return [@value];
208 }
209 },
210  
211 3 ▶ ( +RESET ) => sub { @value = () },
Subroutine __ANON__: called CC=1 SCAR=0.0
212 ▶ ( +REWIND ) => sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
213 13 ( +_DEPENDS ) => \@iterators,
214 );
215  
216 13 ▶ if ( $on_exhaustion eq TRUNCATE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($on_exhaustion eq 'truncate') { } 5 8 13
elsif ($on_exhaustion eq 'throw') { } 2 6 8
elsif ($on_exhaustion eq 'insert') { } 6 0 6
217  
218 $params{ +NEXT } = sub {
219 26 ▶ return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED;
Subroutine __ANON__: called CC=6 SCAR=18.2
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 0 26 26
220  
221 26 my @nvalue;
222  
223 eval {
224 26 @nvalue = map { $_->() } @iterators;
225 21 1;
226 26 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @nvalue = map({&$_();} @iterators); 1 } } or do { die $@ unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted"); return $self->signal_exhaustion } 5 21 26
227 5 ▶ die $@
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted") 0 5 5
Condition33%1/3
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
!ll&&!rl&&r
005
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
Ref::Util::XS::is_blessed_ref($@) $@->isa("Iterator::Flex::Failure::Exhausted")
Truth table33%1/3
A and B
A= Ref::Util::XS::is_blessed_ref($@)B= $@->isa("Iterator::Flex::Failure::Exhausted")
ABresult
0X0
100
111
228 unless is_blessed_ref( $@ )
229 && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
230 5 return $self->signal_exhaustion;
231 };
232  
233 21 @value = @nvalue;
234  
235 21 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 6 15 21
236 6 my %value;
237 6 @value{@keys} = @value;
238 6 return \%value;
239 }
240 else {
241 15 return [@value];
242 }
243 5 };
244 }
245  
246 elsif ( $on_exhaustion eq THROW ) {
247  
248 $params{ +NEXT } = sub {
249 8 ▶ return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED;
Subroutine __ANON__: called CC=12 SCAR=25.4
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 0 8 8
250  
251 8 my @nvalue;
252  
253 8 my $idx = -1;
254 eval {
255 ## no critic (ComplexMappings)
256 8 @nvalue = map { $idx++; $_->() } @iterators;
257 6 1;
258 8 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @nvalue = map({++$idx; &$_();} @iterators); 1 } } or do { die $@ unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted"); my(@exhausted) = $idx; while (++$idx < @iterators) { unless (eval { do { $iterators[$idx]->(); 1 } }) { die $@ unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted"); push @exhausted, $idx; } ; } ; return $self->signal_exhaustion if @exhausted == @iterators; @exhausted = @keys[@exhausted] if @keys; throw_failure("Truncated", \@exhausted) } 2 6 8
259 2 ▶ die $@
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted") 0 2 2
Condition33%1/3
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
!ll&&!rl&&r
002
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
Ref::Util::XS::is_blessed_ref($@) $@->isa("Iterator::Flex::Failure::Exhausted")
Truth table33%1/3
A and B
A= Ref::Util::XS::is_blessed_ref($@)B= $@->isa("Iterator::Flex::Failure::Exhausted")
ABresult
0X0
100
111
260 unless is_blessed_ref( $@ )
261 && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
262  
263 # if all of the iterators have been exhausted,
264 # then we're ok. Otherwise, find out which ones
265 # are
266  
267 2 my @exhausted = ( $idx );
268 2 ▶ while ( ++$idx < @iterators ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$idx < @iterators) 4 2 6
269 4 ▶ if ( !eval { $iterators[$idx]->(); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $iterators[$idx]->(); 1 } }) 2 2 4
270 2 ▶ die $@
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted") 0 2 2
Condition33%1/3
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
!ll&&!rl&&r
002
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
Ref::Util::XS::is_blessed_ref($@) $@->isa("Iterator::Flex::Failure::Exhausted")
Truth table33%1/3
A and B
A= Ref::Util::XS::is_blessed_ref($@)B= $@->isa("Iterator::Flex::Failure::Exhausted")
ABresult
0X0
100
111
271 unless is_blessed_ref( $@ )
272 && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
273 2 push @exhausted, $idx;
274 }
275 }
276 2 ▶ return $self->signal_exhaustion
Branch: 1 branch
BranchTrueFalseTotal
if @exhausted == @iterators 0 2 2
277 if @exhausted == @iterators;
278  
279 2 ▶ @exhausted = @keys[@exhausted]
Branch: 1 branch
BranchTrueFalseTotal
if @keys 1 1 2
280 if @keys;
281  
282 2 throw_failure( Truncated => \@exhausted );
283 };
284  
285 6 @value = @nvalue;
286  
287 6 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 3 3 6
288 3 my %value;
289 3 @value{@keys} = @value;
290 3 return \%value;
291 }
292 else {
293 3 return [@value];
294 }
295 2 };
296 }
297  
298 elsif ( $on_exhaustion eq INSERT ) {
299  
300 6 my @exhausted = ( !!0 ) x @iterators;
301 6 my $nexhausted = 0;
302  
303 $params{ +NEXT } = sub {
304 28 ▶ return $self->signal_exhaustion
Subroutine __ANON__: called CC=10 SCAR=23.4
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 or $nexhausted == @iterators 0 28 28
Condition33%1/3
$iterator_state == 1 or $nexhausted == @iterators
l!l&&r!l&&!r
0028
MC/DC0%0/2
$iterator_state == 1 or $nexhausted == @iterators
$iterator_state == 1 $nexhausted == @iterators
Truth table33%1/3
A or B
A= $iterator_state == 1B= $nexhausted == @iterators
ABresult
1X1
011
000
305 if $iterator_state == IterState_EXHAUSTED
306 || $nexhausted == @iterators;
307  
308 28 my @nvalue;
309  
310 28 for my $idx ( 0 .. $#iterators ) {
311  
312 80 my $value;
313  
314 80 ▶ if ( $exhausted[$idx] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exhausted[$idx]) { } 4 76 80
315 4 $value = $insert[$idx];
316 }
317 else {
318 76 ▶ eval { $value = $iterators[$idx]->(); 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $value = $iterators[$idx]->(); 1 } } or 14 62 76
319 14 ▶ die $@
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted") 0 14 14
Condition33%1/3
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
!ll&&!rl&&r
0014
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
Ref::Util::XS::is_blessed_ref($@) $@->isa("Iterator::Flex::Failure::Exhausted")
Truth table33%1/3
A and B
A= Ref::Util::XS::is_blessed_ref($@)B= $@->isa("Iterator::Flex::Failure::Exhausted")
ABresult
0X0
100
111
320 unless is_blessed_ref( $@ )
321 && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
322  
323 # if insert value not provided for this iterator,
324 # abort
325 14 ▶ return $self->signal_exhaustion if !$set[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless $set[$idx] 6 8 14
326  
327 8 $nexhausted++;
328 8 $exhausted[$idx] = !!1;
329 8 $value = $insert[$idx];
330 }
331 }
332  
333 74 push @nvalue, $value;
334 }
335  
336 22 @value = @nvalue;
337  
338 22 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 11 11 22
339 11 my %value;
340 11 @value{@keys} = @value;
341 11 return \%value;
342 }
343 else {
344 11 return [@value];
345 }
346 6 };
347 }
348  
349  
350 13 return \%params;
351 }
352  
353 3 __PACKAGE__->_add_roles( qw[
354 State::Closure
355 Next::ClosedSelf
356 Current::Closure
357 Reset::Closure
358 Rewind::Closure
359 ] );
360  
361 3 1;
362  
363 #
364 # This file is part of Iterator-Flex
365 #
366 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
367 #
368 # This is free software, licensed under:
369 #
370 # The GNU General Public License, Version 3, June 2007
371 #
372  
373 __END__
« blib/lib/Iterator/Flex/Flatten.pm ↑ Summary blib/lib/Iterator/Flex/ArrayLike.pm »