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