| 1 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | package Math::VarRate; | ||||||||||||
| 4 | { | ||||||||||||
| 5 | 4 | $Math::VarRate::VERSION = '0.100000'; | |||||||||||
| 6 | } | ||||||||||||
| 7 | # ABSTRACT: deal with linear, variable rates of increase | ||||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 4 | ▶ | use Scalar::Util (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | |||||||||||||
| 13 | sub new { | ||||||||||||
| 14 | 15 | ▶ | my ($class, $arg) = @_; | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 15 | |||||||||||||
| 16 | 15 | ▶ | my $changes = $arg->{rate_changes} || {}; | ||||||||||
|
Condition100%2/2
$arg->{'rate_changes'} || {}
MC/DC100%1/1
$arg->{'rate_changes'} || {}
$arg->{'rate_changes'}
Truth table100%2/2
A
A= $arg->{'rate_changes'}
| |||||||||||||
| 17 | my $self = bless { | ||||||||||||
| 18 | rate_changes => $changes, | ||||||||||||
| 19 | 15 | ▶ | starting_value => $arg->{starting_value} || 0, | ||||||||||
|
Condition100%2/2
$arg->{'starting_value'} || 0
MC/DC100%1/1
$arg->{'starting_value'} || 0
$arg->{'starting_value'}
Truth table100%2/2
A
A= $arg->{'starting_value'}
| |||||||||||||
| 20 | } => $class; | ||||||||||||
| 21 | |||||||||||||
| 22 | 15 | $self->_sanity_check_rate_changes; | |||||||||||
| 23 | 11 | $self->_precompute_offsets; | |||||||||||
| 24 | |||||||||||||
| 25 | 11 | return $self; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | sub _sanity_check_rate_changes { | ||||||||||||
| 29 | 15 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
_sanity_check_rate_changes: called CC=4 SCAR=13.9
| |||||||||||||
| 30 | 15 | my $rc = $self->{rate_changes}; | |||||||||||
| 31 | |||||||||||||
| 32 | 15 | my %check = ( | |||||||||||
| 33 | rates => [ values %$rc ], | ||||||||||||
| 34 | offsets => [ keys %$rc ], | ||||||||||||
| 35 | ); | ||||||||||||
| 36 | |||||||||||||
| 37 | 15 | ▶ | while (my ($k, $v) = each %check) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | Carp::confess("non-numeric $k are not allowed") | ||||||||||||
| 39 | 28 | ▶ | if grep { ! Scalar::Util::looks_like_number("$_") } @$v; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 26 | ▶ | Carp::confess("negative $k are not allowed") if grep { $_ < 0 } @$v; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | } | ||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | |||||||||||||
| 45 | 40 | ▶ | sub starting_value { $_[0]->{starting_value} || 0 } | ||||||||||
|
Subroutine
starting_value: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 46 | |||||||||||||
| 47 | |||||||||||||
| 48 | sub offset_for { | ||||||||||||
| 49 | 22 | ▶ | my ($self, $value) = @_; | ||||||||||
|
Subroutine
offset_for: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 50 | |||||||||||||
| 51 | 22 | ▶ | Carp::croak("illegal value: non-numeric") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | unless Scalar::Util::looks_like_number("$value"); | ||||||||||||
| 53 | |||||||||||||
| 54 | 21 | ▶ | Carp::croak("illegal value: negative") unless $value >= 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | |||||||||||||
| 56 | 20 | $value += 0; | |||||||||||
| 57 | |||||||||||||
| 58 | 20 | ▶ | return 0 if $value == $self->starting_value; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | |||||||||||||
| 60 | 16 | my $ko = $self->{known_offsets}; | |||||||||||
| 61 | 16 | my ($offset) = sort { $b <=> $a } grep { $ko->{ $_ } < $value } keys %$ko; | |||||||||||
| 62 | |||||||||||||
| 63 | 16 | ▶ | return unless defined $offset; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | |||||||||||||
| 65 | 14 | my $rate = $self->{rate_changes}{ $offset }; | |||||||||||
| 66 | |||||||||||||
| 67 | # If we stopped for good, we can never reach the target. -- rjbs, 2009-05-11 | ||||||||||||
| 68 | 14 | ▶ | return undef if $rate == 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | |||||||||||||
| 70 | 12 | my $to_go = $value - $ko->{ $offset }; | |||||||||||
| 71 | 12 | my $dur = $to_go / $rate; | |||||||||||
| 72 | |||||||||||||
| 73 | 12 | return $offset + $dur; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | |||||||||||||
| 77 | sub value_at { | ||||||||||||
| 78 | 32 | ▶ | my ($self, $offset) = @_; | ||||||||||
|
Subroutine
value_at: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 79 | |||||||||||||
| 80 | 32 | ▶ | Carp::croak("illegal offset: non-numeric") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | unless Scalar::Util::looks_like_number("$offset"); | ||||||||||||
| 82 | |||||||||||||
| 83 | 31 | ▶ | Carp::croak("illegal offset: negative") unless $offset >= 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | |||||||||||||
| 85 | 30 | $offset += 0; | |||||||||||
| 86 | |||||||||||||
| 87 | 30 | my $known_offsets = $self->{known_offsets}; | |||||||||||
| 88 | |||||||||||||
| 89 | 30 | ▶ | return $known_offsets->{ $offset } if exists $known_offsets->{ $offset }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | |||||||||||||
| 91 | 22 | my ($max) = sort { $b <=> $a } grep { $_ < $offset } keys %$known_offsets; | |||||||||||
| 92 | |||||||||||||
| 93 | 22 | ▶ | return $self->starting_value unless defined $max; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | |||||||||||||
| 95 | 16 | my $start = $known_offsets->{ $max }; | |||||||||||
| 96 | 16 | my $rate = $self->{rate_changes}{ $max }; | |||||||||||
| 97 | 16 | my $dur = $offset - $max; | |||||||||||
| 98 | |||||||||||||
| 99 | 16 | return $start + $rate * $dur; | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | sub _precompute_offsets { | ||||||||||||
| 103 | 11 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
_precompute_offsets: called CC=2 SCAR=6.9
| |||||||||||||
| 104 | |||||||||||||
| 105 | 11 | my $value = $self->starting_value; | |||||||||||
| 106 | 11 | my $v_at_o = {}; | |||||||||||
| 107 | 11 | my %changes = %{ $self->{rate_changes} }; | |||||||||||
| 108 | 11 | my $prev = 0; | |||||||||||
| 109 | 11 | my $rate = 0; | |||||||||||
| 110 | |||||||||||||
| 111 | 11 | for my $offset (sort { $a <=> $b } keys %changes) { | |||||||||||
| 112 | 19 | my $duration = $offset - $prev; | |||||||||||
| 113 | |||||||||||||
| 114 | 19 | $value += $duration * $rate; | |||||||||||
| 115 | 19 | $v_at_o->{ $offset } = $value; | |||||||||||
| 116 | |||||||||||||
| 117 | 19 | $rate = $changes{ $offset }; | |||||||||||
| 118 | 19 | $prev = $offset; | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 11 | $self->{known_offsets} = $v_at_o; | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | 4 | 1; | |||||||||||
| 125 | |||||||||||||
| 126 | __END__ | ||||||||||||