blib/lib/Iterator/Flex/Stack.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 70.0% 14 / 20 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 22 / 22 Podpod 50.0% 1 / 2 totaltotal 89.0% 122 / 137 CC 13 SCAR 7.8
CC
13
Cov
89%
__ANON__
5.0
construct
3.0
__ANON__
2.5
CRAP 2.2
« blib/lib/Iterator/Flex/Cache.pm ↑ Summary blib/lib/Iterator/Flex/Role/Error/Throw.pm »
1 package Iterator::Flex::Stack;
2  
3 # ABSTRACT: An iterator which concatenates a set 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.33';
11  
12 2 ▶use Iterator::Flex::Utils qw( RETURN STATE EXHAUSTION :IterAttrs :IterStates );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use parent 'Iterator::Flex::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use List::Util 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
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  
64  
65  
66  
67 ▶sub new ( $class, @args ) {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
68 3 ▶ my $pars = Ref::Util::is_hashref( $args[-1] ) ? pop @args : {};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($args[-1]) ? : 0 3 3
69  
70 3 $class->SUPER::new( {
71 depends => \@args,
72 current_iterator_index => undef,
73 },
74 $pars,
75 );
76 }
77  
78 ▶sub construct ( $class, $state ) {
Subroutine construct: called CC=3 SCAR=11.1
Pod undocumented
79 3 ▶ throw_failure( parameter => q{state must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 3 3
80 unless Ref::Util::is_hashref( $state );
81  
82 3 ▶ $state->{value} //= [];
Condition50%1/2
$state->{"value"} //= []
l!l
03
MC/DC0%0/1
$state->{"value"} //= []
$state->{"value"}
Truth table50%1/2
A
A= $state->{"value"}
Aresult
11
00
83  
84 my ( \@depends, $prev, $current, undef, undef )
85 3 = @{$state}{ 'depends', 'prev', 'current', 'next', 'thaw' };
86  
87 # transform into iterators if required.
88 my @stack
89 3 = map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @depends;
90 3 my @snapshot = @stack;
91  
92 # --- cargo cult??
93 # my $value;
94 # $value = $current
95 # if $thaw;
96  
97 3 my $self;
98  
99 my $iter;
100 3 my $is_exhausted;
101 3 my $iterator_state;
102  
103 my sub init {
104 15 ▶ $iterator_state = IterState_EXHAUSTED;
Subroutine init: called CC=2 SCAR=6.9
105 15 ▶ if ( @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 14 1 15
106 14 $iter = $stack[0];
107 14 $is_exhausted = $iter->can( 'is_exhausted' );
108 14 $iterator_state = IterState_CLEAR;
109 }
110 }
111 3 init();
112  
113 my %params = (
114  
115 ( +_NAME ) => 'istack',
116  
117 ( +_SELF ) => \$self,
118  
119 ( +RESET ) => sub {
120 3 ▶ $prev = $current = undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 3 @stack = @snapshot;
122 3 init();
123 },
124  
125 ( +REWIND ) => sub {
126 3 ▶ @stack = @snapshot;
Subroutine __ANON__: called CC=1 SCAR=0.0
127 3 init();
128 },
129  
130 ( +STATE ) => \$iterator_state,
131  
132 ( +NEXT ) => sub {
133 70 ▶ return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED;
Subroutine __ANON__: called CC=5 SCAR=16.2
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 0 70 70
134  
135 70 while ( 1 ) {
136 92 my $value = $iter->();
137  
138 92 ▶ if ( defined( $value ) || !$iter->$is_exhausted ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value or not $iter->$is_exhausted) 59 33 92
Condition66%2/3
defined $value or not $iter->$is_exhausted
l!l&&r!l&&!r
59033
MC/DC50%1/2
defined $value or not $iter->$is_exhausted
defined $value not $iter->$is_exhausted
Truth table66%2/3
A or not(B)
A= defined $valueB= not $iter->$is_exhausted
ABresult
1X1
011
000
139 59 $prev = $current;
140 59 return $current = $value;
141 }
142  
143 33 ▶ last unless defined( $iter = shift @stack );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($iter = shift @stack) 11 22 33
144 22 $is_exhausted = $iter->can( 'is_exhausted' );
145 }
146  
147 11 $prev = $current;
148 11 return $current = $self->signal_exhaustion;
149 },
150  
151 ( +PREV ) => sub {
152 7 ▶ return $prev;
Subroutine __ANON__: called CC=1 SCAR=0.0
153 },
154  
155 ( +CURRENT ) => sub {
156 3 ▶ return $self->signal_exhaustion if !@stack;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 0 3 3
157 3 return $current;
158 },
159  
160 ▶ ( +_ROLES ) => [],
Subroutine __ANON__: called CC=2 SCAR=6.9
161  
162 ( +_DEPENDS ) => \@snapshot,
163  
164 ( +METHODS ) => {
165 push => sub ( $, @iters ) {
166 6 push @stack, map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @iters;
167 6 $self->_clear_state;
168 6 ▶ init() if @stack == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @stack == 1 3 3 6
169 },
170  
171 ▶ pop => sub ($) {
Subroutine __ANON__: called CC=2 SCAR=9.2
172 1 ▶ if ( @stack == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack == 1) 0 1 1
173 0 $iterator_state = IterState_EXHAUSTED;
174 0 $iter = $is_exhausted = undef;
175 }
176 1 return CORE::pop( @stack );
177 },
178  
179 ▶ unshift => sub ( $, @iters ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 2 unshift @stack, map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @iters;
181 2 init();
182 },
183  
184 ▶ shift => sub ($) {
Subroutine __ANON__: called CC=2 SCAR=7.9
185 1 ▶ if ( @stack == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack == 1) 0 1 1
186 0 $iterator_state = IterState_EXHAUSTED;
187 0 $iter = $is_exhausted = undef;
188 }
189 1 my $ret = CORE::shift( @stack );
190 1 init();
191 1 return $ret;
192 },
193  
194 ▶ snapshot => sub ($) {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 2 @snapshot = @stack;
196 },
197 },
198  
199 3 );
200  
201 3 return \%params;
202 }
203  
204  
205 2 __PACKAGE__->_add_roles( qw[
206 State::Closure
207 Next::ClosedSelf
208 Rewind::Closure
209 Reset::Closure
210 Current::Closure
211 Prev::Closure
212 ] );
213  
214 2 1;
215  
216 #
217 # This file is part of Iterator-Flex
218 #
219 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
220 #
221 # This is free software, licensed under:
222 #
223 # The GNU General Public License, Version 3, June 2007
224 #
225  
226 __END__
« blib/lib/Iterator/Flex/Cache.pm ↑ Summary blib/lib/Iterator/Flex/Role/Error/Throw.pm »