Flex/Cache.pm

filter: Statementstmt 100% 60 / 60 Branchbran 71.4% 10 / 14 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 19 / 19 Podpod 50.0% 1 / 2 totaltotal 95.5% 108 / 113 CC 14 SCAR 11.1
CC
14
Cov
95%
new
7.0
construct
4.0
__ANON__
3.0
CRAP 3.0
« Flex/Cat.pm ↑ Summary Flex/Grep.pm »
1 package Iterator::Flex::Cache;
2  
3 # ABSTRACT: Cache 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 qw( signatures postderef );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.34';
11  
12 3 ▶use parent 'Iterator::Flex::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Iterator::Flex::Utils qw( STATE :IterAttrs :IterStates throw_failure );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use namespace::clean;
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  
58  
59  
60  
61 13 ▶sub new ( $class, $iterable, $pars = {} ) {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
62  
63 13 ▶ throw_failure( parameter => 'q{pars} argument must be a hash' )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($pars) 0 13 13
64 unless Ref::Util::is_hashref( $pars );
65  
66 13 my %pars = $pars->%*;
67  
68 13 ▶ my $capacity = delete $pars{capacity} // 2;
Condition100%2/2
delete $pars{"capacity"} // 2
l!l
67
MC/DC100%1/1
delete $pars{"capacity"} // 2
delete $pars{"capacity"}
Truth table100%2/2
A
A= delete $pars{"capacity"}
Aresult
11
00
69 13 ▶ 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 integer") 9 4 13
Condition100%6/6
&Scalar::Util::looks_like_number($capacity) and int $capacity == $capacity and $capacity > 0
!ll&&!rl&&r
229
&Scalar::Util::looks_like_number($capacity) and int $capacity == $capacity
!ll&&!rl&&r
1111
MC/DC100%3/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 table100%4/4
A and B and C
A= &Scalar::Util::looks_like_number($capacity)B= int $capacity == $capacityC= $capacity > 0
ABCresult
0XX0
10X0
1100
1111
70 or throw_failure( parameter => "parameter 'capacity' ($capacity) is not a positive integer" );
71  
72 9 $class->SUPER::new( {
73 capacity => $capacity,
74 depends => [ to_iterator( $iterable ) ],
75 },
76 \%pars,
77 );
78 }
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89 11 ▶sub construct ( $class, $state ) {
Subroutine construct: called CC=4 SCAR=13.9
Pod undocumented
90  
91 11 ▶ throw_failure( parameter => q{state must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 11 11
92 unless Ref::Util::is_hashref( $state );
93  
94 11 my ( $src, $capacity, $idx, $cache ) = @{$state}{qw[ depends capacity idx cache ]};
95 11 $src = $src->[0];
96 11 ▶ $idx //= -1;
Condition100%2/2
$idx //= -1
l!l
29
MC/DC100%1/1
$idx //= -1
$idx
Truth table100%2/2
A
A= $idx
Aresult
11
00
97 11 ▶ $cache //= [];
Condition100%2/2
$cache //= []
l!l
29
MC/DC100%1/1
$cache //= []
$cache
Truth table100%2/2
A
A= $cache
Aresult
11
00
98  
99 11 my $self;
100 11 my $is_exhausted = $src->can( 'is_exhausted' );
101 11 my $iterator_state;
102  
103 return {
104  
105 ( +_SELF ) => \$self,
106  
107 ( +STATE ) => \$iterator_state,
108  
109 2 ▶ ( +RESET ) => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
110 2 $idx = -1;
111 2 @{$cache} = ();
112 },
113  
114 2 ▶ ( +REWIND ) => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 },
116  
117 34 ▶ ( +PREV ) => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
118 34 ▶ return defined $idx ? $cache->[ ( $idx - 1 ) % $capacity ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $idx ? : 34 0 34
119 },
120  
121 34 ▶ ( +CURRENT ) => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
122 34 ▶ return defined $idx ? $cache->[ $idx % $capacity ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $idx ? : 34 0 34
123 },
124  
125 95 ▶ ( +NEXT ) => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
126  
127 95 ▶ return $self->signal_exhaustion
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 12 83 95
128 if $iterator_state == IterState_EXHAUSTED;
129  
130 83 $idx = ++$idx % $capacity;
131 83 my $current = $cache->[$idx] = $src->();
132  
133 83 ▶ return $self->signal_exhaustion
Branch: 1 branch
BranchTrueFalseTotal
if $src->$is_exhausted 11 72 83
134 if $src->$is_exhausted;
135  
136 72 return $current;
137 },
138  
139 ( +METHODS ) => {
140 110 ▶ at => sub ( $, $at ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 110 $cache->[ ( $idx - $at ) % $capacity ];
142 },
143 },
144  
145 2 ▶ ( +FREEZE ) => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
146 2 return [ $class, { idx => $idx, capacity => $capacity, cache => $cache } ];
147 },
148  
149 11 ( +_DEPENDS ) => $src,
150 };
151 }
152  
153  
154  
155 3 __PACKAGE__->_add_roles( qw[
156 State::Closure
157 Next::ClosedSelf
158 Rewind::Closure
159 Reset::Closure
160 Prev::Closure
161 Current::Closure
162 Freeze
163 ] );
164  
165  
166 3 1;
167  
168 #
169 # This file is part of Iterator-Flex
170 #
171 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
172 #
173 # This is free software, licensed under:
174 #
175 # The GNU General Public License, Version 3, June 2007
176 #
177  
178 __END__
« Flex/Cat.pm ↑ Summary Flex/Grep.pm »