| 1 | package Math::Calc::Units::Convert::Multi; | ||||||||||||
| 2 | 1 | ▶ | use base 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use vars qw(@EXPORT_OK); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | @EXPORT_OK = qw(to_canonical simple_convert singular | |||||||||||
| 6 | variants major_variants | ||||||||||||
| 7 | major_pref range_score pref_score | ||||||||||||
| 8 | get_class construct); | ||||||||||||
| 9 | }; | ||||||||||||
| 10 | 1 | require Math::Calc::Units::Convert::Time; | |||||||||||
| 11 | 1 | require Math::Calc::Units::Convert::Byte; | |||||||||||
| 12 | 1 | require Math::Calc::Units::Convert::Date; | |||||||||||
| 13 | 1 | require Math::Calc::Units::Convert::Distance; | |||||||||||
| 14 | 1 | require Math::Calc::Units::Convert::Combo; | |||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 1 | ▶ | use vars qw(@UnitClasses); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 1 | @UnitClasses = qw(Math::Calc::Units::Convert::Time | |||||||||||
| 19 | Math::Calc::Units::Convert::Byte | ||||||||||||
| 20 | Math::Calc::Units::Convert::Date | ||||||||||||
| 21 | Math::Calc::Units::Convert::Distance | ||||||||||||
| 22 | Math::Calc::Units::Convert::Combo); | ||||||||||||
| 23 | |||||||||||||
| 24 | # to_canonical : unit -> value | ||||||||||||
| 25 | # | ||||||||||||
| 26 | 488 | ▶ | sub to_canonical { | ||||||||||
|
Subroutine
to_canonical: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 27 | 488 | my ($unit) = @_; | |||||||||||
| 28 | |||||||||||||
| 29 | 488 | my $val = 1; | |||||||||||
| 30 | 488 | my %newUnit; | |||||||||||
| 31 | |||||||||||||
| 32 | 488 | ▶ | while (my ($unitName, $power) = each %$unit) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 555 | my ($mult, $canon) = name_to_canonical($unitName); | |||||||||||
| 34 | 555 | $val *= $mult ** $power; | |||||||||||
| 35 | |||||||||||||
| 36 | 555 | ▶ | if (ref $canon) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | # Uh oh, it was a combination of basic types | ||||||||||||
| 38 | 7 | my $c = to_canonical($canon); | |||||||||||
| 39 | 7 | $val *= $c->[0] ** $power; | |||||||||||
| 40 | 7 | ▶ | while (my ($name, $subPower) = each %{ $c->[1] }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 14 | ▶ | if (($newUnit{$name} += $subPower * $power) == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 1 | delete $newUnit{$name}; | |||||||||||
| 43 | } | ||||||||||||
| 44 | } | ||||||||||||
| 45 | } else { | ||||||||||||
| 46 | 548 | ▶ | if (($newUnit{$canon} += $power) == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 3 | delete $newUnit{$canon}; | |||||||||||
| 48 | } | ||||||||||||
| 49 | } | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 488 | return [ $val, \%newUnit ]; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | # name_to_canonical : unitName -> value x baseUnit | ||||||||||||
| 56 | # | ||||||||||||
| 57 | # Memoizing this doubles the speed of the test suite. | ||||||||||||
| 58 | # | ||||||||||||
| 59 | 1 | my %CANON_CACHE; | |||||||||||
| 60 | 1260 | ▶ | sub name_to_canonical { | ||||||||||
|
Subroutine
name_to_canonical: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 61 | 1260 | my $unitName = shift; | |||||||||||
| 62 | 1260 | ▶ | $CANON_CACHE{$unitName} ||= [ _name_to_canonical($unitName) ]; | ||||||||||
|
Condition100%2/2
$CANON_CACHE{$unitName} ||= [_name_to_canonical($unitName)]
MC/DC100%1/1
$CANON_CACHE{$unitName} ||= [_name_to_canonical($unitName)]
$CANON_CACHE{$unitName}
Truth table100%2/2
A
A= $CANON_CACHE{$unitName}
| |||||||||||||
| 63 | 1260 | return @{ $CANON_CACHE{$unitName} }; | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 76 | ▶ | sub _name_to_canonical { | ||||||||||
|
Subroutine
_name_to_canonical: called CC=4 SCAR=13.9
| |||||||||||||
| 67 | 76 | my ($unitName) = @_; | |||||||||||
| 68 | |||||||||||||
| 69 | # First, check for compound units | ||||||||||||
| 70 | 76 | ▶ | if (my $v = Math::Calc::Units::Convert::Combo->lookup_compound($unitName)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 2 | return @$v; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 74 | foreach my $uclass (@UnitClasses) { | |||||||||||
| 75 | 201 | ▶ | if (my ($val, $base) = $uclass->to_canonical($unitName)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 63 | return ($val, $base); | |||||||||||
| 77 | } | ||||||||||||
| 78 | } | ||||||||||||
| 79 | 11 | return Math::Calc::Units::Convert::Base->to_canonical($unitName); | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 705 | ▶ | sub get_class { | ||||||||||
|
Subroutine
get_class: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 83 | 705 | my ($unitName) = @_; | |||||||||||
| 84 | 705 | my (undef, $canon) = name_to_canonical($unitName); | |||||||||||
| 85 | 705 | foreach my $uclass (@UnitClasses) { | |||||||||||
| 86 | 1524 | my $canon_unit = $uclass->canonical_unit(); | |||||||||||
| 87 | 1524 | ▶ | next if ! defined $canon_unit; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 1500 | ▶ | return $uclass if $canon_unit eq $canon; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | } | ||||||||||||
| 90 | 24 | return 'Math::Calc::Units::Convert::Base'; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | sub simple_convert { | ||||||||||
|
Subroutine
simple_convert: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 94 | 0 | my ($u, $v) = @_; | |||||||||||
| 95 | 0 | foreach my $uclass (@UnitClasses) { | |||||||||||
| 96 | 0 | my $c; | |||||||||||
| 97 | 0 | ▶ | return $c if $c = $uclass->simple_convert($u, $v); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | } | ||||||||||||
| 99 | 0 | return; | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | sub singular { | ||||||||||
|
Subroutine
singular: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 103 | 0 | my ($unitName) = @_; | |||||||||||
| 104 | 0 | return get_class($unitName)->singular($unitName); | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 22 | ▶ | sub variants { | ||||||||||
|
Subroutine
variants: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 108 | 22 | my ($base) = @_; | |||||||||||
| 109 | 22 | return get_class($base)->variants($base); | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 4 | ▶ | sub major_variants { | ||||||||||
|
Subroutine
major_variants: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 113 | 4 | my ($base) = @_; | |||||||||||
| 114 | 4 | return get_class($base)->major_variants($base); | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 9 | ▶ | sub major_pref { | ||||||||||
|
Subroutine
major_pref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 118 | 9 | my ($base) = @_; | |||||||||||
| 119 | 9 | return get_class($base)->major_pref($base); | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 282 | ▶ | sub range_score { | ||||||||||
|
Subroutine
range_score: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 123 | 282 | my ($val, $unitName) = @_; | |||||||||||
| 124 | 282 | ▶ | die if ref $unitName; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 282 | return get_class($unitName)->range_score($val, $unitName); | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 296 | ▶ | sub pref_score { | ||||||||||
|
Subroutine
pref_score: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 129 | 296 | my ($unitName) = @_; | |||||||||||
| 130 | 296 | ▶ | die if ref $unitName; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 296 | return get_class($unitName)->pref_score($unitName); | |||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | 7 | ▶ | sub construct { | ||||||||||
|
Subroutine
construct: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||
| 135 | 7 | my ($constructor, $args) = @_; | |||||||||||
| 136 | 7 | foreach my $uclass (@UnitClasses) { | |||||||||||
| 137 | 21 | my $c; | |||||||||||
| 138 | 21 | ▶ | return $c if $c = $uclass->construct($constructor, $args); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | } | ||||||||||||
| 140 | 0 | return; | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 1 | 1; | |||||||||||