| 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.33'; | |||||||||||
| 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 | 9 | ▶ | sub new ( $class, $iterable, $pars = {} ) { | ||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 62 | |||||||||||||
| 63 | 9 | ▶ | throw_failure( parameter => 'q{pars} argument must be a hash' ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | unless Ref::Util::is_hashref( $pars ); | ||||||||||||
| 65 | |||||||||||||
| 66 | 9 | my %pars = $pars->%*; | |||||||||||
| 67 | |||||||||||||
| 68 | 9 | ▶ | my $capacity = delete $pars{capacity} // 2; | ||||||||||
|
Condition100%2/2
delete $pars{"capacity"} // 2
MC/DC100%1/1
delete $pars{"capacity"} // 2
delete $pars{"capacity"}
Truth table100%2/2
A
A= delete $pars{"capacity"}
| |||||||||||||
| 69 | |||||||||||||
| 70 | 9 | $class->SUPER::new( { | |||||||||||
| 71 | capacity => $capacity, | ||||||||||||
| 72 | depends => [ to_iterator( $iterable ) ], | ||||||||||||
| 73 | }, | ||||||||||||
| 74 | \%pars, | ||||||||||||
| 75 | ); | ||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | |||||||||||||
| 79 | |||||||||||||
| 80 | |||||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | |||||||||||||
| 84 | |||||||||||||
| 85 | |||||||||||||
| 86 | |||||||||||||
| 87 | ▶ | sub construct ( $class, $state ) { | |||||||||||
|
Subroutine
construct: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 88 | |||||||||||||
| 89 | 11 | ▶ | throw_failure( parameter => q{state must be a HASH reference} ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | unless Ref::Util::is_hashref( $state ); | ||||||||||||
| 91 | |||||||||||||
| 92 | 11 | my ( $src, $capacity, $idx, $cache ) = @{$state}{qw[ depends capacity idx cache ]}; | |||||||||||
| 93 | 11 | $src = $src->[0]; | |||||||||||
| 94 | 11 | ▶ | $idx //= -1; | ||||||||||
|
Condition100%2/2
$idx //= -1
MC/DC100%1/1
$idx //= -1
$idx
Truth table100%2/2
A
A= $idx
| |||||||||||||
| 95 | 11 | ▶ | $cache //= []; | ||||||||||
|
Condition100%2/2
$cache //= []
MC/DC100%1/1
$cache //= []
$cache
Truth table100%2/2
A
A= $cache
| |||||||||||||
| 96 | |||||||||||||
| 97 | 11 | my $self; | |||||||||||
| 98 | 11 | my $is_exhausted = $src->can( 'is_exhausted' ); | |||||||||||
| 99 | 11 | my $iterator_state; | |||||||||||
| 100 | |||||||||||||
| 101 | return { | ||||||||||||
| 102 | |||||||||||||
| 103 | ( +_SELF ) => \$self, | ||||||||||||
| 104 | |||||||||||||
| 105 | ( +STATE ) => \$iterator_state, | ||||||||||||
| 106 | |||||||||||||
| 107 | ( +RESET ) => sub { | ||||||||||||
| 108 | 2 | ▶ | $idx = -1; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 109 | 2 | @{$cache} = (); | |||||||||||
| 110 | }, | ||||||||||||
| 111 | |||||||||||||
| 112 | ▶ | ( +REWIND ) => sub { | |||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 113 | }, | ||||||||||||
| 114 | |||||||||||||
| 115 | ( +PREV ) => sub { | ||||||||||||
| 116 | 34 | ▶ | return defined $idx ? $cache->[ ( $idx - 1 ) % $capacity ] : undef; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 117 | }, | ||||||||||||
| 118 | |||||||||||||
| 119 | ( +CURRENT ) => sub { | ||||||||||||
| 120 | 34 | ▶ | return defined $idx ? $cache->[ $idx % $capacity ] : undef; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 121 | }, | ||||||||||||
| 122 | |||||||||||||
| 123 | ( +NEXT ) => sub { | ||||||||||||
| 124 | |||||||||||||
| 125 | 95 | ▶ | return $self->signal_exhaustion | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||
| 126 | if $iterator_state == IterState_EXHAUSTED; | ||||||||||||
| 127 | |||||||||||||
| 128 | 83 | $idx = ++$idx % $capacity; | |||||||||||
| 129 | 83 | my $current = $cache->[$idx] = $src->(); | |||||||||||
| 130 | |||||||||||||
| 131 | 83 | ▶ | return $self->signal_exhaustion | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | if $src->$is_exhausted; | ||||||||||||
| 133 | |||||||||||||
| 134 | 72 | return $current; | |||||||||||
| 135 | }, | ||||||||||||
| 136 | |||||||||||||
| 137 | ▶ | ( +METHODS ) => { | |||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 138 | at => sub ( $, $at ) { | ||||||||||||
| 139 | 110 | $cache->[ ( $idx - $at ) % $capacity ]; | |||||||||||
| 140 | }, | ||||||||||||
| 141 | }, | ||||||||||||
| 142 | |||||||||||||
| 143 | ( +FREEZE ) => sub { | ||||||||||||
| 144 | 2 | ▶ | return [ $class, { idx => $idx, capacity => $capacity, cache => $cache } ]; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 145 | }, | ||||||||||||
| 146 | |||||||||||||
| 147 | 11 | ( +_DEPENDS ) => $src, | |||||||||||
| 148 | }; | ||||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | |||||||||||||
| 152 | |||||||||||||
| 153 | 3 | __PACKAGE__->_add_roles( qw[ | |||||||||||
| 154 | State::Closure | ||||||||||||
| 155 | Next::ClosedSelf | ||||||||||||
| 156 | Rewind::Closure | ||||||||||||
| 157 | Reset::Closure | ||||||||||||
| 158 | Prev::Closure | ||||||||||||
| 159 | Current::Closure | ||||||||||||
| 160 | Freeze | ||||||||||||
| 161 | ] ); | ||||||||||||
| 162 | |||||||||||||
| 163 | |||||||||||||
| 164 | 3 | 1; | |||||||||||
| 165 | |||||||||||||
| 166 | # | ||||||||||||
| 167 | # This file is part of Iterator-Flex | ||||||||||||
| 168 | # | ||||||||||||
| 169 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||
| 170 | # | ||||||||||||
| 171 | # This is free software, licensed under: | ||||||||||||
| 172 | # | ||||||||||||
| 173 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||
| 174 | # | ||||||||||||
| 175 | |||||||||||||
| 176 | __END__ | ||||||||||||