blib/lib/Tie/Hash/Interpolate.pm

filter: Statementstmt 100% 99 / 99 Branchbran 90.4% 38 / 42 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 23 / 23 Podpod 100% 1 / 1 totaltotal 94.4% 170 / 180 CC 26 SCAR 22.5
CC
26
Cov
95%
FETCH
20.1
STORE
5.0
TIEHASH
3.0
CRAP 9.5
↑ Summary
1 package Tie::Hash::Interpolate;
2
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw/ looks_like_number blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶use constant EX_LINEAR => 'linear';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant EX_CONSTANT => 'constant';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant EX_FATAL => 'fatal';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant EX_UNDEF => 'undef';
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 1 ▶use constant ONE_KEY_FATAL => 'fatal';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant ONE_KEY_CONSTANT => 'constant';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant ONE_KEY_UNDEF => 'undef';
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 1 our $VERSION = '0.08';
20
21 sub new
22 1 ▶ {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 1 my ($class, %opts) = @_;
24 1 my $tied = {};
25 1 tie %{$tied}, $class, %opts;
26 1 return $tied;
27 }
28
29 sub TIEHASH
30 8 ▶ {
Subroutine TIEHASH: called CC=3 SCAR=11.0
31 8 my ($class, %opts) = (shift, extrapolate => EX_LINEAR, one_key => ONE_KEY_FATAL, @_);
32 croak "invalid value for 'extrapolate' option ($opts{'extrapolate'})"
33 8 ▶ unless grep $_ eq $opts{'extrapolate'}, EX_LINEAR, EX_UNDEF, EX_FATAL, EX_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $opts{"extrapolate"}), "linear", "undef", "fatal", "constant") 1 7 8
34 croak "invalid value for 'one_key' option ($opts{'one_key'})"
35 7 ▶ unless grep $_ eq $opts{'one_key'}, ONE_KEY_UNDEF, ONE_KEY_FATAL, ONE_KEY_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $opts{"one_key"}), "undef", "fatal", "constant") 0 7 7
36 7 my $self = { _DATA => {}, _KEYS => [], _SORT => 1, _OPTS => \%opts };
37 7 bless $self, $class;
38 }
39
40 sub FIRSTKEY
41 2 ▶ {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
42 2 my $a = scalar keys %{$_[0]->{'_DATA'}};
43 2 return each %{$_[0]->{'_DATA'}};
44 }
45
46 sub NEXTKEY
47 3 ▶ {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
48 3 return each %{$_[0]->{'_DATA'}};
49 }
50
51 sub EXISTS
52 2 ▶ {
Subroutine EXISTS: called CC=1 SCAR=0.0
53 2 return exists $_[0]->{'_DATA'}->{$_[1]};
54 }
55
56 sub DELETE
57 1 ▶ {
Subroutine DELETE: called CC=1 SCAR=0.0
58 ## force a re-sort on next fetch
59 1 $_[0]->{'_SORT'} = 1;
60 1 delete $_[0]->{'_DATA'}->{$_[1]};
61 }
62
63 sub CLEAR
64 5 ▶ {
Subroutine CLEAR: called CC=1 SCAR=0.0
65 ## force a re-sort on next fetch
66 5 $_[0]->{'_SORT'} = 1;
67 5 %{$_[0]->{'_DATA'}} = ();
68 }
69
70 sub STORE
71 19 ▶ {
Subroutine STORE: called CC=5 SCAR=16.2
72 19 my ($self, $key, $val) = @_;
73
74 ## the key must be a number
75 19 ▶ croak "key ($key) must be a number" if ref $key ||
Branch: 1 branch
BranchTrueFalseTotal
if ref $key or not &looks_like_number($key) 2 17 19
Condition66%2/3
ref $key or not &looks_like_number($key)
l!l&&r!l&&!r
0217
MC/DC50%1/2
ref $key or not &looks_like_number($key)
ref $key not &looks_like_number($key)
Truth table66%2/3
A or not(B)
A= ref $keyB= not &looks_like_number($key)
ABresult
1X1
011
000
76 ! looks_like_number($key);
77
78 ## the value must be a number
79 17 ▶ croak "val ($val) must be a number" if ref $val ||
Branch: 1 branch
BranchTrueFalseTotal
if ref $val or not &looks_like_number($val) 2 15 17
Condition66%2/3
ref $val or not &looks_like_number($val)
l!l&&r!l&&!r
0215
MC/DC50%1/2
ref $val or not &looks_like_number($val)
ref $val not &looks_like_number($val)
Truth table66%2/3
A or not(B)
A= ref $valB= not &looks_like_number($val)
ABresult
1X1
011
000
80 ! looks_like_number($val);
81
82 ## force key to number
83 15 $key += 0;
84
85 ## force a re-sort on next fetch
86 15 $self->{'_SORT'} = 1;
87
88 15 $self->{'_DATA'}{$key} = $val;
89
90 }
91
92 sub FETCH
93 45 ▶ {
Subroutine FETCH: called CC=20 SCAR=30.0
94 45 my ($self, $key) = @_;
95
96 45 ▶ croak "key ($key) must be a number" if ref $key ||
Branch: 1 branch
BranchTrueFalseTotal
if ref $key or not &looks_like_number($key) 2 43 45
Condition66%2/3
ref $key or not &looks_like_number($key)
l!l&&r!l&&!r
0243
MC/DC50%1/2
ref $key or not &looks_like_number($key)
ref $key not &looks_like_number($key)
Truth table66%2/3
A or not(B)
A= ref $keyB= not &looks_like_number($key)
ABresult
1X1
011
000
97 ! looks_like_number($key);
98
99 ## force key to number
100 43 $key += 0;
101
102 ## return right away for direct hits
103 43 ▶ return $self->{'_DATA'}{$key} if exists $self->{'_DATA'}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_DATA"}{$key} 3 40 43
104
105 ## re-sort keys if necessary
106 40 ▶ _sort_keys($self) if $self->{'_SORT'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_SORT"} 13 27 40
107
108 40 my @keys = @{ $self->{'_KEYS'} };
109
110 ## be sure we have at least 1 key
111 40 ▶ croak "cannot interpolate/extrapolate with less than two keys"
Branch: 1 branch
BranchTrueFalseTotal
if @keys < 1 2 38 40
112 if @keys < 1;
113
114 ## return constant if only 1 key
115 38 ▶ if (@keys == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 1) 4 34 38
116 {
117
118 ## determine whether we should die, return undef, or extrapolate
119 4 my $one_key_opt = $self->{'_OPTS'}{'one_key'};
120
121 4 ▶ $one_key_opt eq ONE_KEY_FATAL ? croak "cannot extrapolate with only one key" :
Branch: 2 branches
BranchTrueFalseTotal
$one_key_opt eq 'fatal' ? : 0 4 4
$one_key_opt eq 'undef' ? : 0 4 4
122 $one_key_opt eq ONE_KEY_UNDEF ? return undef : ();
123
124 4 return $self->{'_DATA'}{$keys[0]};
125 }
126
127 ## begin interpolation/extrapolation search
128 34 my ($lower, $upper);
129
130 ## key is below range of known keys
131 34 ▶ if ($key < $keys[0])
Branch: 2 branches
BranchTrueFalseTotal
if ($key < $keys[0]) { } 15 19 34
elsif ($key > $keys[-1]) { } 9 10 19
132 {
133
134 15 my $extrap_opt = $self->{'_OPTS'}{'extrapolate'};
135
136 15 ▶ $extrap_opt eq EX_CONSTANT ? return $self->{'_DATA'}{$keys[0]} :
Branch: 3 branches
BranchTrueFalseTotal
$extrap_opt eq 'constant' ? : 1 14 15
$extrap_opt eq 'fatal' ? : 1 13 14
$extrap_opt eq 'undef' ? : 1 12 13
137 $extrap_opt eq EX_FATAL ? croak "fatal extrapolation with key ($key)" :
138 $extrap_opt eq EX_UNDEF ? return undef : ();
139
140 12 ($lower, $upper) = @keys[0, 1];
141
142 }
143 ## key is above range of known keys
144 elsif ($key > $keys[-1])
145 {
146
147 9 my $extrap_opt = $self->{'_OPTS'}{'extrapolate'};
148
149 9 ▶ $extrap_opt eq EX_CONSTANT ? return $self->{'_DATA'}{$keys[-1]} :
Branch: 3 branches
BranchTrueFalseTotal
$extrap_opt eq 'constant' ? : 1 8 9
$extrap_opt eq 'fatal' ? : 1 7 8
$extrap_opt eq 'undef' ? : 1 6 7
150 $extrap_opt eq EX_FATAL ? croak "fatal extrapolation with key ($key)" :
151 $extrap_opt eq EX_UNDEF ? return undef : ();
152
153 6 ($lower, $upper) = @keys[-2, -1];
154
155 }
156 ## key is within range of known keys
157 else
158 {
159
160 10 for my $i (0 .. $#keys - 1)
161 {
162 11 ($lower, $upper) = @keys[$i, $i+1];
163 11 ▶ last if $key <= $upper;
Branch: 1 branch
BranchTrueFalseTotal
if $key <= $upper 10 1 11
164 1 ▶ croak "unable to find bracketing keys" if $i == $#keys - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#keys - 1 0 1 1
165 }
166
167 }
168
169 return _mx_plus_b($key, $lower, $upper, $self->{'_DATA'}{$lower},
170 28 $self->{'_DATA'}{$upper});
171
172 }
173
174 ## sort keys and reset flag
175 sub _sort_keys
176 13 ▶ {
Subroutine _sort_keys: called CC=1 SCAR=0.0
177 13 my ($self) = @_;
178 13 $self->{'_KEYS'} = [ sort { $a <=> $b } keys %{ $self->{'_DATA'} } ];
179 13 $self->{'_SORT'} = 0;
180 }
181
182 ## basic equation for a line given 2 points
183 sub _mx_plus_b
184 28 ▶ {
Subroutine _mx_plus_b: called CC=1 SCAR=0.0
185 28 my ($x, $x1, $x2, $y1, $y2) = @_;
186 28 my $slope = ($y2 - $y1) / ($x2 - $x1);
187 28 my $intercept = $y2 - ($slope * $x2);
188 28 return $slope * $x + $intercept;
189 }
190
191 1 1;
192 __END__
↑ Summary