blib/lib/Iterator/Flex/Buffer.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 76.9% 20 / 26 Conditioncond 46.6% 7 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 94.1% 16 / 17 Podpod 50.0% 1 / 2 totaltotal 83.5% 122 / 146 CC 22 SCAR 17.8
CC
22
Cov
87%
__ANON__
11.0
new
9.6
construct
4.0
CRAP 6.0
« blib/lib/Iterator/Flex/Cycle.pm ↑ Summary blib/lib/Iterator/Flex/Product.pm »
1 package Iterator::Flex::Buffer;
2  
3 # ABSTRACT: Buffer 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  
9 3 our $VERSION = '0.33';
10  
11 3 ▶use Iterator::Flex::Utils ':IterAttrs', ':IterStates', ':SignalParameters', ':ExhaustionActions';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use experimental 'signatures';
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  
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 8 ▶sub new ( $class, $iterable, $capacity = 0, $pars = {} ) {
Subroutine new: called CC=7 SCAR=22.6
Pod documented
57  
58 8 my %pars = $pars->%*;
59 8 ▶ $capacity //= 0;
Condition50%1/2
$capacity //= 0
l!l
80
MC/DC0%0/1
$capacity //= 0
$capacity
Truth table50%1/2
A
A= $capacity
Aresult
11
00
60  
61 8 ▶ Scalar::Util::looks_like_number( $capacity ) && int( $capacity ) == $capacity && $capacity >= 0
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($capacity) and int $capacity == $capacity and $capacity >= 0 or throw_failure("parameter", "parameter 'capacity' ($capacity) is not a positive or zero integer") 0 8 8
Condition33%2/6
&Scalar::Util::looks_like_number($capacity) and int $capacity == $capacity and $capacity >= 0
!ll&&!rl&&r
008
&Scalar::Util::looks_like_number($capacity) and int $capacity == $capacity
!ll&&!rl&&r
008
MC/DC0%0/3
&Scalar::Util::looks_like_number($capacity) and int $capacity == $capacity and $capacity >= 0
&Scalar::Util::looks_like_number($capacity) int $capacity == $capacity $capacity >= 0
Truth table25%1/4
A and B and C
A= &Scalar::Util::looks_like_number($capacity)B= int $capacity == $capacityC= $capacity >= 0
ABCresult
0XX0
10X0
1100
1111
62 or throw_failure(
63 parameter => "parameter 'capacity' ($capacity) is not a positive or zero integer" );
64  
65 8 $class->SUPER::new( {
66 capacity => $capacity,
67 src => $iterable,
68 },
69 \%pars,
70 );
71 }
72  
73 ▶sub construct ( $class, $state ) {
Subroutine construct: called CC=4 SCAR=13.9
Pod undocumented
74  
75 8 ▶ throw_failure( parameter => q{'state' parameter must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 8 8
76 unless Ref::Util::is_hashref( $state );
77  
78 my ( $src, $prev, $current, $next, $array, $capacity )
79 8 = @{$state}{qw[ src prev current next array capacity ]};
80  
81 8 $src
82 = to_iterator( $src, { ( +EXHAUSTION ) => THROW } );
83  
84 8 ▶ my $nread = defined $array ? $array->@* - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $array ? : 0 8 8
85 8 ▶ $next //= 1;
Condition50%1/2
$next //= 1
l!l
08
MC/DC0%0/1
$next //= 1
$next
Truth table50%1/2
A
A= $next
Aresult
11
00
86 8 my $self;
87  
88 my $iterator_state;
89 8 my $src_is_exhausted = !!0;
90  
91 return {
92  
93 ( +_NAME ) => 'ibuffer',
94  
95 ( +_SELF ) => \$self,
96  
97 ( +STATE ) => \$iterator_state,
98  
99 ( +RESET ) => sub {
100 4 ▶ $src_is_exhausted = !!0;
Subroutine __ANON__: called CC=1 SCAR=0.0
101 4 $prev = $current = undef;
102 4 $next = 1;
103 4 $nread = 0;
104 },
105  
106 ( +REWIND ) => sub {
107 4 ▶ $src_is_exhausted = !!0;
Subroutine __ANON__: called CC=1 SCAR=0.0
108 4 $next = 1;
109 4 $nread = 0;
110 },
111  
112 ( +PREV ) => sub {
113 50 ▶ return defined $prev ? $array->[$prev] : undef;
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $prev ? : 50 0 50
114 },
115  
116 ( +CURRENT ) => sub {
117 40 ▶ return defined $current ? $array->[$current] : undef;
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $current ? : 40 0 40
118 },
119  
120 ( +NEXT ) => sub {
121  
122 166 ▶ return $self->signal_exhaustion
Subroutine __ANON__: called CC=11 SCAR=24.0
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 10 156 166
123 if $iterator_state == IterState_EXHAUSTED;
124  
125 # load buffer; need to handle both initial and recurrent loads,
126 # keeping track of the previous value.
127 156 ▶ if ( $next > $nread ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $nread) 50 106 156
128  
129 50 ▶ if ( $src_is_exhausted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_is_exhausted) 7 43 50
130 7 $prev = $current;
131 7 return $self->signal_exhaustion;
132 }
133  
134 43 ▶ $array //= [];
Condition100%2/2
$array //= []
l!l
358
MC/DC100%1/1
$array //= []
$array
Truth table100%2/2
A
A= $array
Aresult
11
00
135 43 ▶ my $prev_value = defined $current ? $array->[$current] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $current ? : 31 12 43
136  
137 # $array has one more element than $capacity to
138 # ensure there's room for the previous value when
139 # we load the next buffer.
140 43 $array->@* = ( $prev_value );
141  
142 eval {
143 # preload $array
144 43 push $array->@*, $src->next;
145  
146 # postfix until/while check conditional first,
147 # which would be a problem if $array->@* and
148 # capacity == 0, in which case it would never
149 # call $src->next
150 38 ▶ push( $array->@*, $src->next ) until $array->@* == $capacity + 1;
Branch: 1 branch
BranchTrueFalseTotal
@$array == $capacity + 1 or do { push @$array, $src->next } 117 31 148
151 31 1;
152 43 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { push @$array, $src->next; push @$array, $src->next until @$array == $capacity + 1; 1 } } or 12 31 43
153 12 ▶ die $@
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted") 0 12 12
Condition33%1/3
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
!ll&&!rl&&r
0012
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
154 unless Ref::Util::is_blessed_ref( $@ )
155 && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
156 12 $src_is_exhausted = !!1;
157 12 ▶ if ( $array->@* == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$array == 1) 5 7 12
158 5 $current = $prev = 0; # setup for rewind
159 5 return $self->signal_exhaustion;
160 }
161 };
162  
163 38 $nread = $array->@* - 1;
164 38 $current = 0;
165 38 $next = 1;
166 }
167  
168 144 $prev = $current;
169 144 $current = $next;
170 144 $next++;
171 144 return $array->[$current];
172 },
173  
174 # this iterator only depends on $src if it hasn't drained it.
175 # currently _DEPENDS must be a list of iterators, not a
176 # coderef, so a dynamic dependency is no tpossible
177 ( +_DEPENDS ) => $src,
178 ( +FREEZE ) => sub {
179 return [
180 0 ▶ $class,
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 {
182 src => $src,
183 prev => $prev,
184 current => $current,
185 next => $next,
186 array => $array,
187 capacity => $capacity,
188 },
189 ];
190 },
191 8 };
192 }
193  
194  
195 3 __PACKAGE__->_add_roles( qw[
196 State::Closure
197 Next::ClosedSelf
198 Rewind::Closure
199 Reset::Closure
200 Prev::Closure
201 Current::Closure
202 Freeze
203 ] );
204  
205 3 1;
206  
207 #
208 # This file is part of Iterator-Flex
209 #
210 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
211 #
212 # This is free software, licensed under:
213 #
214 # The GNU General Public License, Version 3, June 2007
215 #
216  
217 __END__
« blib/lib/Iterator/Flex/Cycle.pm ↑ Summary blib/lib/Iterator/Flex/Product.pm »