| 1 | package Sub::Throttler::Rate::AnyEvent; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | our @CARP_NOT = qw( Sub::Throttler ); | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = 'v0.2.10'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use parent qw( Sub::Throttler::algo ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Sub::Throttler qw( throttle_flush ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Time::HiRes qw( clock_gettime CLOCK_MONOTONIC time sleep ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use List::Util qw( min ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Storable qw( dclone ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 40 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use warnings FATAL => qw( misc ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 40 | my ($class, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | limit => delete $opt{limit} // 1, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 39 | ▶ | period => delete $opt{period} // 1, | ||||||||||||||||||||||||||||||||||||||||
|
Condition71%5/7
delete $opt{"limit"} // 1
delete $opt{"period"} // 1
ref $class || $class
MC/DC100%1/1
delete $opt{"limit"} // 1
delete $opt{"limit"}
MC/DC100%1/1
delete $opt{"period"} // 1
delete $opt{"period"}
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table100%2/2
A
A= delete $opt{"limit"}
Truth table100%2/2
A
A= delete $opt{"period"}
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | acquired=> {}, # { $id => { $key => [$time, $quantity], … }, … } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | used => {}, # { $key => { next => $idx, data => [ $time, … ] }, … } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | _cb => undef, # callback for timer | ||||||||||||||||||||||||||||||||||||||||||
| 29 | _t => undef, # undef or AE::timer | ||||||||||||||||||||||||||||||||||||||||||
| 30 | }, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 39 | ▶ | croak 'limit must be an unsigned integer' if $self->{limit} !~ /\A\d+\z/ms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 37 | ▶ | croak 'period must be a positive number' if $self->{period} <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 37 | ▶ | croak 'period is too large' if $self->{period} >= -Sub::Throttler::Rate::rr::EMPTY(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 37 | ▶ | croak 'bad param: '.(keys %opt)[0] if keys %opt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 35 | weaken(my $this = $self); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 35 | ▶ | $self->{_cb} = sub { $this && $this->_tick() }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 35 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 11 | ▶ | sub acquire { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
acquire: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 11 | my ($self, $id, $key, $quantity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 11 | ▶ | if (!$self->try_acquire($id, $key, $quantity)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 11 | ▶ | if ($quantity <= $self->{limit}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 | my $now = clock_gettime(CLOCK_MONOTONIC); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 11 | my $delay = $self->{used}{$key}->get($quantity) + $self->{period} - $now; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 11 | $delay = sprintf '%.6f', $delay; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 47 | # resource may expire between try_acquire() and clock_gettime() | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 11 | ▶ | if ($delay > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 11 | sleep $delay; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 11 | ▶ | if (!$self->try_acquire($id, $key, $quantity)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | croak "$self: unable to acquire $quantity of resource '$key'"; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 11 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 11 | ▶ | sub limit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
limit: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 11 | my ($self, $limit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 11 | ▶ | if (1 == @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | return $self->{limit}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 9 | ▶ | croak 'limit must be an unsigned integer' if $limit !~ /\A\d+\z/ms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | # OPTIMIZATION call throttle_flush() only if amount of available | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # resources increased (i.e. limit was increased) | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 9 | my $resources_increases = $self->{limit} < $limit; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | $self->{limit} = $limit; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | for my $rr (values %{ $self->{used} }) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | $rr->resize($self->{limit}); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 9 | ▶ | if ($resources_increases) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | throttle_flush(); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 9 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | my ($class, $state) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | croak 'bad state: wrong algorithm' if $state->{algo} ne __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | my $v = version->parse($state->{version}); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | if ($v > $VERSION) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | carp 'restoring state saved by future version'; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | my $self = $class->new(limit=>$state->{limit}, period=>$state->{period}); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | $self->{used} = dclone($state->{used}); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | my ($time, $now) = (time, clock_gettime(CLOCK_MONOTONIC)); | |||||||||||||||||||||||||||||||||||||||||
| 88 | # time jump backward, no matter how much, handled like we still is in | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # current period, to be safe | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | if ($state->{at} > $time) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | $time = $state->{at}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my $diff = $time - $now; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | for my $data (map {$_->{data}} values %{ $self->{used} }) { | |||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | for (@{ $data }) { | |||||||||||||||||||||||||||||||||||||||||
| 96 | 12 | ▶ | if ($_ != Sub::Throttler::Rate::rr::EMPTY()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 12 | $_ -= $diff; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 12 | $_ = sprintf '%.6f', $_; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | for (values %{ $self->{used} }) { | |||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | bless $_, 'Sub::Throttler::Rate::rr'; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | $self->{_t} = AE::timer 0, 0, $self->{_cb}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | ▶ | sub period { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
period: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | my ($self, $period) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | ▶ | if (1 == @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | return $self->{period}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | croak 'period must be a positive number' if $period <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | croak 'period is too large' if $self->{period} >= -Sub::Throttler::Rate::rr::EMPTY(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | # OPTIMIZATION call throttle_flush() only if amount of available | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # resources increased (i.e. period was decreased) | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | my $resources_increases = $self->{period} > $period; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | $self->{period} = $period; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | ▶ | if ($resources_increases) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | if ($self->{_t}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | $self->{_t} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | $self->_tick(); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | throttle_flush(); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 28 | ▶ | sub release { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
release: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 28 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 28 | ▶ | croak sprintf '%s not acquired anything', $id if !$self->{acquired}{$id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 25 | delete $self->{acquired}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 25 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 26 | ▶ | sub release_unused { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
release_unused: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 26 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 26 | ▶ | croak sprintf '%s not acquired anything', $id if !$self->{acquired}{$id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 23 | my $now = clock_gettime(CLOCK_MONOTONIC); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 23 | for my $key (grep {$self->{used}{$_}} keys %{ $self->{acquired}{$id} }) { | |||||||||||||||||||||||||||||||||||||||||
| 145 | 22 | my ($time, $quantity) = @{ $self->{acquired}{$id}{$key} }; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 22 | $self->{used}{$key}->del($time, $quantity); | |||||||||||||||||||||||||||||||||||||||||
| 147 | # clean up (avoid memory leak in long run with unique keys) | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 22 | ▶ | if ($self->{used}{$key}->get($self->{limit}) + $self->{period} <= $now) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 16 | delete $self->{used}{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 23 | delete $self->{acquired}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 23 | throttle_flush(); | |||||||||||||||||||||||||||||||||||||||||
| 154 | 23 | ▶ | if (!keys %{ $self->{used} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 14 | $self->{_t} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 23 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | my ($time, $now) = (time, clock_gettime(CLOCK_MONOTONIC)); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | my $diff = $time - $now; | |||||||||||||||||||||||||||||||||||||||||
| 164 | my $state = { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | algo => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | version => version->declare($VERSION)->numify, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | limit => $self->{limit}, | ||||||||||||||||||||||||||||||||||||||||||
| 168 | period => $self->{period}, | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | used => dclone($self->{used}), | |||||||||||||||||||||||||||||||||||||||||
| 170 | at => $time, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | for my $data (map {$_->{data}} values %{ $state->{used} }) { | |||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | for (@{ $data }) { | |||||||||||||||||||||||||||||||||||||||||
| 174 | 3 | ▶ | if ($_ != Sub::Throttler::Rate::rr::EMPTY()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | $_ += $diff; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 3 | $_ = sprintf '%.6f', $_; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | for (values %{ $state->{used} }) { | |||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | $_ = {%{ $_ }}; # unbless | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | return $state; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 166 | ▶ | sub try_acquire { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
try_acquire: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 166 | my ($self, $id, $key, $quantity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 188 | croak sprintf '%s already acquired %s', $id, $key | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 166 | ▶ | if $self->{acquired}{$id} && exists $self->{acquired}{$id}{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key}
MC/DC100%2/2
$self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key}
$self->{"acquired"}{$id}
exists $self->{"acquired"}{$id}{$key}
Truth table100%3/3
A and B
A= $self->{"acquired"}{$id}B= exists $self->{"acquired"}{$id}{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 164 | ▶ | croak 'quantity must be positive' if $quantity <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 162 | my $now = clock_gettime(CLOCK_MONOTONIC); | |||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 162 | ▶ | $self->{used}{$key} ||= Sub::Throttler::Rate::rr->new($self->{limit}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"used"}{$key} ||= "Sub::Throttler::Rate::rr"->new($self->{"limit"})
MC/DC0%0/2
$self->{"used"}{$key} ||= "Sub::Throttler::Rate::rr"->new($self->{"limit"})
$self->{"used"}{$key}
"Sub::Throttler::Rate::rr"->new($self->{"limit"})
Truth table66%2/3
A ||= B
A= $self->{"used"}{$key}B= "Sub::Throttler::Rate::rr"->new($self->{"limit"})
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 162 | ▶ | if (!$self->{used}{$key}->add($self->{period}, $now, $quantity)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 68 | return; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 94 | $self->{acquired}{$id}{$key} = [$now, $quantity]; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 94 | ▶ | if (!$self->{_t}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 45 | $self->{_t} = AE::timer $self->{period}, 0, $self->{_cb}; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 94 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 14 | ▶ | sub _tick { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tick: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 14 | my $now = clock_gettime(CLOCK_MONOTONIC); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 14 | my $when = 0; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 14 | for my $key (keys %{ $self->{used} }) { | |||||||||||||||||||||||||||||||||||||||||
| 211 | 20 | my $after = $self->{used}{$key}->after($now - $self->{period}); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 20 | ▶ | if (!$after) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 17 | delete $self->{used}{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | ▶ | elsif (!$when || $when > $after) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
not $when or $when > $after
MC/DC0%0/2
not $when or $when > $after
not $when
$when > $after
Truth table33%1/3
not(A) or B
A= not $whenB= $when > $after
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 3 | $when = $after; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 14 | ▶ | $self->{_t} = !$when ? undef : AE::timer $when + $self->{period} - $now, 0, $self->{_cb}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 14 | throttle_flush(); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 14 | return; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | package Sub::Throttler::Rate::rr; ## no critic (ProhibitMultiplePackages) | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 3 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 3 | ▶ | use constant EMPTY => -1_000_000_000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 63 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 63 | my ($class, $len) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 63 | ▶ | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | next => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 239 | data => [ (EMPTY) x $len ], | ||||||||||||||||||||||||||||||||||||||||||
| 240 | }, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 63 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 162 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 162 | my ($self, $period, $time, $quantity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 162 | $time = sprintf '%.6f', $time; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 247 | 162 | my $len = @{ $self->{data} }; | |||||||||||||||||||||||||||||||||||||||||
| 248 | # try_acquire() guarantee $quantity > 0, so we continue only if $len > 0 | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # (thus avoid division by zero on % $len) and there is a chance to add | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # $quantity elements | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 162 | ▶ | if ($quantity > $len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 159 | my $required = ($self->{next} + $quantity - 1) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 255 | # {data} is sorted, last added element ($self->{next}-1) is guaranteed | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # to be largest of all elements, so all elements between (inclusive) | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # $self->{next} and $required are guaranteed to be either EMPTY | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # or <= $self->{next}-1 element, and $required element is largest of them | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 159 | my $since = sprintf '%.6f', $time - $period; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 260 | 159 | ▶ | if ($self->{data}[$required] > $since) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 65 | return; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 94 | for (1 .. $quantity) { | |||||||||||||||||||||||||||||||||||||||||
| 264 | 188 | $self->{data}[ $self->{next} ] = $time; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 188 | ($self->{next} += 1) %= $len; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 94 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | # Return time of acquiring first resource after $time or nothing. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 20 | ▶ | sub after { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
after: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 20 | my ($self, $time) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 20 | $time = sprintf '%.6f', $time; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 274 | # _tick() guarantee $time > EMPTY | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 20 | my $len = @{ $self->{data} }; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 20 | for (1 .. $len) { | |||||||||||||||||||||||||||||||||||||||||
| 277 | 46 | $_ = ($self->{next} + $_ - 1) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 46 | ▶ | return $self->{data}[ $_ ] if $self->{data}[ $_ ] > $time; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 17 | return; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 22 | ▶ | sub del { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
del: called CC=15 SCAR=40.4
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 22 | my ($self, $time, $quantity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 22 | $time = sprintf '%.6f', $time; # work around floating point precision issues | |||||||||||||||||||||||||||||||||||||||||
| 286 | # try_acquire() guarantee $quantity > 0 | ||||||||||||||||||||||||||||||||||||||||||
| 287 | # even if $time is already outdated, these elements should be removed | ||||||||||||||||||||||||||||||||||||||||||
| 288 | # anyway in case {period} will be increased later | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 22 | my $len = @{ $self->{data} }; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 22 | ▶ | if (!$len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 22 | ▶ | if ($quantity > $len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $quantity = $len; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | # OPTIMIZATION not in {data} | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 22 | ▶ | if ($self->{data}[ $self->{next} ] > $time) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # OPTIMIZATION oldest | ||||||||||||||||||||||||||||||||||||||||||
| 301 | elsif ($self->{data}[ $self->{next} ] == $time) { | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 16 | for (map { ($self->{next} + $_ - 1) % $len } 1 .. $quantity) { | |||||||||||||||||||||||||||||||||||||||||
| 303 | # part of $quantity may be not in {data} (if {limit} was decreased) | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 26 | ▶ | return if $self->{data}[ $_ ] != $time; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 26 | $self->{data}[ $_ ] = EMPTY; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # OPTIMIZATION newest | ||||||||||||||||||||||||||||||||||||||||||
| 309 | elsif ($self->{data}[ $self->{next} - 1 ] == $time) { | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | for (map { $self->{next} - $_ } 1 .. $quantity) { | |||||||||||||||||||||||||||||||||||||||||
| 311 | 6 | ▶ | croak 'assert: newest: no time' if $self->{data}[ $_ ] != $time; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 6 | $self->{data}[ $_ ] = EMPTY; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 3 | $self->{next} = ($self->{next} - $quantity) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | # middle (actually it support any case, not just middle) | ||||||||||||||||||||||||||||||||||||||||||
| 317 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 318 | my $i = _binsearch($time, $self->{data}, $self->{next}, $len - 1) | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | // _binsearch($time, $self->{data}, 0, $self->{next} - 1); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
_binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1) // _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
MC/DC0%0/2
_binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1) // _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
_binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1)
_binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
Truth table0%0/3
A // B
A= _binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1)B= _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | croak 'assert: middle: not found' if !defined $i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | for (map { ($i + $_ - 1) % $len } 1 .. $quantity) { | |||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | croak 'assert: middle: no time' if $self->{data}[ $_ ] != $time; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | $self->{data}[ $_ ] = EMPTY; | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | # OPTIMIZATION move minimum amount of elements | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $count_rew = ($self->{next} - $i) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | my $count_fwd = ($i + $quantity - $self->{next}) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 328 | # move oldest elements forward | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | if ($count_fwd <= $count_rew) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | @{ $self->{data} }[ map { ($self->{next}+$_-1) % $len } 1 .. $count_fwd ] = | |||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | @{ $self->{data} }[ map { ($self->{next}+$_-1) % $len } $count_fwd-$quantity+1 .. $count_fwd, 1 .. $count_fwd-$quantity ]; | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | # move newest elements backward | ||||||||||||||||||||||||||||||||||||||||||
| 334 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | @{ $self->{data} }[ map { ($i+$_-1) % $len } 1 .. $count_rew ] = | |||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | @{ $self->{data} }[ map { ($i+$_-1) % $len } 1+$quantity .. $count_rew, 1 .. $quantity]; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | $self->{next} = ($self->{next} - $quantity) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 19 | return; | |||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 33 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 33 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 345 | # $id is number of required element, counting from oldest one ($id = 1) | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 33 | my $len = @{ $self->{data} }; | |||||||||||||||||||||||||||||||||||||||||
| 347 | # acquire() guarantee 0 < $id <= $len | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 33 | my $i = ($self->{next} + $id - 1) % $len; | |||||||||||||||||||||||||||||||||||||||||
| 349 | 33 | return $self->{data}[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 7 | ▶ | sub resize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 7 | my ($self, $newlen) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 354 | # limit() guarantee $newlen >= 0 | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 7 | my $len = @{ $self->{data} }; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 7 | my $d = $self->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 7 | $self->{data} = [ @{$d}[ $self->{next} .. $#{$d} ], @{$d}[ 0 .. $self->{next} - 1 ] ]; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 7 | ▶ | if ($newlen < $len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 6 | $self->{next} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 360 | 6 | splice @{ $self->{data} }, 0, $len - $newlen; | |||||||||||||||||||||||||||||||||||||||||
| 361 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | $self->{next} = $len % $newlen; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 1 | push @{ $self->{data} }, (EMPTY) x ($newlen - $len); | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | # From List::BinarySearch::PP version 0.23. | ||||||||||||||||||||||||||||||||||||||||||
| 369 | # Modified to support slices and work with array of numbers, without callback. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | sub _binsearch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_binsearch: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | my ( $target, $aref, $min, $max ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | $min //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$min //= 0
MC/DC0%0/1
$min //= 0
$min
Truth table0%0/2
A
A= $min
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | $max //= $#{$aref}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$max //= $#{$aref;}
MC/DC0%0/1
$max //= $#{$aref;}
$max
Truth table0%0/2
A
A= $max
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | ▶ | croak 'bad slice' if $min < 0 || $#{$aref} < $max || $min > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$min < 0 or $#{$aref;} < $max or $min > $max
$min < 0 or $#{$aref;} < $max
MC/DC0%0/3
$min < 0 or $#{$aref;} < $max or $min > $max
$min < 0
$#{$aref;} < $max
$min > $max
Truth table0%0/4
A or B or C
A= $min < 0B= $#{$aref;} < $maxC= $min > $max
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | while ( $max > $min ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | my $mid = int( ( $min + $max ) / 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ( $target > $aref->[$mid] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $min = $mid + 1; | |||||||||||||||||||||||||||||||||||||||||
| 379 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | $max = $mid; | |||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | return $min if $target == $aref->[$min]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | 3 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||||||||||||
| 389 | __END__ | ||||||||||||||||||||||||||||||||||||||||||