« blib/lib/Language/Farnsworth/Value/Date.pm
↑ Summary
blib/lib/Language/Farnsworth/FunctionDispatch.pm »
| 1 | package Language::Farnsworth::Dimension; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Language::Farnsworth::Output; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new | ||||||||||||||||||||||||||||
| 12 | 25137 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 25137 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 25137 | my $dimers = shift; | |||||||||||||||||||||||||||
| 15 | 25137 | my $scope = shift; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 25137 | my $dims; | |||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | 25137 | ▶ | if (defined($dimers)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | 25137 | $dims = +{%{$dimers}}; #make a shallow copy, i don't feel like debugging this later if somehow i ended up with two objects blessing the same hashref | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | else | ||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | #none, use empty hash | ||||||||||||||||||||||||||||
| 26 | 0 | $dims = {}; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 25137 | my $self = {dimen =>$dims, scope => $scope}; #so i don't have to rewrite a lot of code... NTS: this should probably go away later | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 25137 | bless $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub compare | ||||||||||||||||||||||||||||
| 36 | 18809 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
compare: called CC=9 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 18809 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | 18809 | my $target = shift; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # print "DIMENWARN: ".join("::", caller())."\n"; | ||||||||||||||||||||||||||||
| 41 | # print Dumper($self, $target); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ((!ref($target)) && keys %{$self->{dimen}} == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $target and keys %{$$self{"dimen"};} == 0
MC/DC0%0/2
not ref $target and keys %{$$self{"dimen"};} == 0
not ref $target
keys %{$$self{"dimen"};} == 0
Truth table33%1/3
not(A) and B
A= not ref $targetB= keys %{$$self{"dimen"};} == 0
| |||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 0 | return 1; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 18809 | ▶ | if (keys %{$target->{dimen}} == keys %{$self->{dimen}}) #check lengths of keys | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | 1522 | my $v = 1; | |||||||||||||||||||||||||||
| 51 | 1522 | for my $k (keys %{$self->{dimen}}) | |||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 401 | ▶ | return 0 if (!exists($target->{dimen}{$k})); #optimization fixes bug! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 117 | ▶ | $v = 0 if (($self->{dimen}{$k} != $target->{dimen}{$k}) && ($k ne "string")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"dimen"}{$k} != $target->{"dimen"}{$k} and $k ne "string"
MC/DC50%1/2
$self->{"dimen"}{$k} != $target->{"dimen"}{$k} and $k ne "string"
$self->{"dimen"}{$k} != $target->{"dimen"}{$k}
$k ne "string"
Truth table66%2/3
A and B
A= $self->{"dimen"}{$k} != $target->{"dimen"}{$k}B= $k ne "string"
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1238 | ▶ | if ($v) #also check if there are no dimensions | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 1180 | return 1; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 17345 | return 0; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub invert | ||||||||||||||||||||||||||||
| 67 | 2795 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
invert: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 2795 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | #i CAN'T modify myself in this! | ||||||||||||||||||||||||||||
| 70 | 2795 | my $atom = $self->new($self->{dimen}); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2795 | for (keys %{$atom->{dimen}}) | |||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | #turn all positives to negatives and vice versa | ||||||||||||||||||||||||||||
| 75 | 1904 | $atom->{dimen}{$_} = -$atom->{dimen}{$_}; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2795 | return $atom->prune(); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub merge | ||||||||||||||||||||||||||||
| 82 | 11319 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 11319 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | #i CAN'T modify myself in this! | ||||||||||||||||||||||||||||
| 85 | 11319 | my $atom = Language::Farnsworth::Dimension->new($self->{dimen}); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 11319 | my $partner = shift; | |||||||||||||||||||||||||||
| 88 | 11319 | my $pd = {}; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 11319 | ▶ | if (ref($partner) eq "Language::Farnsworth::Dimension") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 11319 | $pd = $partner->{dimen}; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 11319 | for (uniq (keys %{$atom->{dimen}}, keys %{$pd})) | |||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 4 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 13475 | $atom->{dimen}{$_} += $partner->{dimen}{$_}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 11319 | return $atom->prune(); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub mult | ||||||||||||||||||||||||||||
| 105 | 982 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
mult: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 982 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | #i CAN'T modify myself in this! | ||||||||||||||||||||||||||||
| 108 | 982 | my $atom = Language::Farnsworth::Dimension->new($self->{dimen}); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 982 | my $value = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 982 | for (keys %{$atom->{dimen}}) | |||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 4 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 859 | $atom->{dimen}{$_} *= $value; #this might turn them into Math::PARI objects? does it matter? | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 982 | return $atom->prune(); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub prune | ||||||||||||||||||||||||||||
| 123 | 15096 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prune: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 15096 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 15096 | for (keys %{$self->{dimen}}) | |||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 16238 | ▶ | if (!$self->{dimen}{$_}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 372 | delete $self->{dimen}{$_}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 15096 | return $self; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub Dump | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
Dump: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 0 | my $scope = shift; | |||||||||||||||||||||||||||
| 141 | 0 | my @returns; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | #added a sort so its stable, i'll need this... | ||||||||||||||||||||||||||||
| 144 | 0 | for my $d (sort {$a cmp $b} keys %{$self->{dimen}}) | |||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||
| 146 | 0 | my $exp = ""; | |||||||||||||||||||||||||||
| 147 | #print Dumper($dimen->{dimen}, $exp); | ||||||||||||||||||||||||||||
| 148 | 0 | my $dv = "".($self->{dimen}{$d}); | |||||||||||||||||||||||||||
| 149 | 0 | my $realdv = "".(0.0+$self->{dimen}{$d}); #use this for comparing below, that way i can keep rational exponents when possible | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | $dv =~ s/([.]\d+?)0+$/$1/; | |||||||||||||||||||||||||||
| 152 | 0 | $dv =~ s/E/e/; #make floating points clearer | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $exp = "^".($dv =~ /^[\d\.]+$/? $dv :"(".$dv.")") unless ($realdv eq "1"); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | push @returns, $scope->{units}->getdimen($d).$exp; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (my $combo = $self->findcombo($scope)) #this should be a method? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | 0 | @returns = $combo; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my $return =join " ", @returns; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | $return = "($return)"if ($return =~ /\s/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $return = "dimensionless value" if ($return =~ /^\s*$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | return $return; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub findcombo | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
findcombo: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 175 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | 0 | my $scope = shift; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | #HACK: THIS NEEDS TO MOVE SOMEWHERE MORE APPROPRIATE AS I DEPRECIATE THE OUTPUT CLASS! | ||||||||||||||||||||||||||||
| 179 | 0 | my $combos = \%Language::Farnsworth::Output::combos; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | for my $combo (keys %$combos) | |||||||||||||||||||||||||||
| 182 | { | ||||||||||||||||||||||||||||
| 183 | #print "TRY COMBO: $combo\n"; | ||||||||||||||||||||||||||||
| 184 | 0 | my $cv = $combos->{$combo}; #grab the value | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | return $combo if ($self->compare($cv->getdimen())); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | return undef; #none found | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # vim: filetype=perl | ||||||||||||||||||||||||||||