blib/lib/Language/Farnsworth/Value/Array.pm

filter: Statementstmt 48.6% 55 / 113 Branchbran 10.8% 8 / 74 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.8% 14 / 23 Podpod 0.0% 0 / 13 totaltotal 33.1% 79 / 238 CC 42 SCAR 30.8
CC
42
Cov
33%
compare
56.0
subtract
30.0
modulus
30.0
CRAP 21.8
« blib/lib/Language/Farnsworth/Units/Currency.pm ↑ Summary blib/lib/Language/Farnsworth/Value/Undef.pm »
1 package Language::Farnsworth::Value::Array;
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 Language::Farnsworth::Dimension;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base 'Language::Farnsworth::Value';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::MoreUtils 'each_array';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = 0.6;
14  
15 use overload
16 4 ▶ '+' => \&add,
Subroutine BEGIN: called CC=1 SCAR=0.0
17 '-' => \&subtract,
18 '*' => \&mult,
19 '/' => \&div,
20 '%' => \&mod,
21 '**' => \&pow,
22 '<=>' => \&compare,
23 4 'bool' => \&bool;
24  
25 4 ▶use base qw(Language::Farnsworth::Value);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #this is the REQUIRED fields for Language::Farnsworth::Value subclasses
28 #
29 #dimen => a Language::Farnsworth::Dimension object
30 #
31 #this is so i can make a -> conforms in Language::Farnsworth::Value, to replace the existing code, i'm also planning on adding some definitions such as, TYPE_PARI, TYPE_STRING, TYPE_LAMBDA, TYPE_DATE, etc. to make certain things easier
32  
33 sub new
34 275 ▶{
Subroutine new: called CC=4 SCAR=14.4
Pod undocumented
35 275 my $class = shift;
36 275 my $value = shift;
37 275 my $outmagic = shift; #i'm still not sure on this one
38  
39 275 ▶ error "Non array reference given as \$value to constructor" unless ref($value) eq "ARRAY" && defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" and defined $value 0 275 275
Condition33%1/3
ref $value eq "ARRAY" and defined $value
!ll&&!rl&&r
00275
MC/DC0%0/2
ref $value eq "ARRAY" and defined $value
ref $value eq "ARRAY" defined $value
Truth table33%1/3
A and B
A= ref $value eq "ARRAY"B= defined $value
ABresult
0X0
100
111
40  
41 275 my $self = {};
42  
43 275 bless $self, $class;
44  
45 275 $self->{outmagic} = $outmagic;
46  
47 275 ▶ $self->{array} = [@{$value}] || [];
Condition50%1/2
[@{$value;}] || []
l!l
2750
MC/DC0%0/1
[@{$value;}] || []
[@{$value;}]
Truth table50%1/2
A
A= [@{$value;}]
Aresult
11
00
48
49 275 return $self;
50 }
51  
52 sub type
53 0 ▶{
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
54 0 return "Array";
55 }
56  
57 sub sanitizeself
58 0 ▶{
Subroutine sanitizeself: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my $self=shift;
60 #this is to deal with a single bug i've got, it slows things down a lot :(
61
62 0 for (@{$self->{array}})
63 {
64 0 $_->setref(undef);
65 }
66 }
67  
68 sub getarray
69 427 ▶{
Subroutine getarray: called CC=1 SCAR=0.0
Pod undocumented
70 427 my $self = shift;
71 427 return @{$self->{array}};
72 }
73  
74 sub getarrayref
75 387 ▶{
Subroutine getarrayref: called CC=1 SCAR=0.0
Pod undocumented
76 387 return $_[0]->{array};
77 }
78  
79 sub add
80 13 ▶{
Subroutine add: called CC=7 SCAR=21.3
Pod undocumented
81 13 my ($one, $two, $rev) = @_;
82  
83 13 ▶ error "Non reference given to addition of array" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 13 13
84  
85 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
86 13 ▶ error "Scalar value given to addition to array, <this may become push/unshift>" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 13 13
87 13 ▶ return $two->add($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 13 13
88 13 ▶ if (!$two->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Array")) 0 13 13
89 {
90 0 error "Given non array to array addition";
91 }
92  
93 13 my $order;
94 13 ▶ $order = [$one->getarray(), $two->getarray()] unless $rev;
Branch: 1 branch
BranchTrueFalseTotal
unless $rev 13 0 13
95 13 ▶ $order = [$two->getarray(), $one->getarray()] if $rev;
Branch: 1 branch
BranchTrueFalseTotal
if $rev 0 13 13
96 13 my $arr = new Language::Farnsworth::Value::Array($order); #concatenate the arrays
97 #$arr->sanitizeself();
98 13 return $arr;
99 }
100  
101 sub subtract
102 0 ▶{
Subroutine subtract: not called CC=5 SCAR=34.0
Pod undocumented
103 0 my ($one, $two, $rev) = @_;
104  
105 0 ▶ error "Non reference given to subtraction of array" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
106  
107 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
108 0 ▶ error "Scalar value given to subtraction of array" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
109 0 ▶ return $two->subtract($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
110 0 ▶ if (!$two->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Array")) 0 0 0
111 {
112 0 error "Given non array to array subtraction";
113 }
114  
115 0 error "Subtracting arrays? what did you think this would do, create a black hole?";
116 }
117  
118 sub modulus
119 0 ▶{
Subroutine modulus: not called CC=5 SCAR=34.0
Pod undocumented
120 0 my ($one, $two, $rev) = @_;
121  
122 0 ▶ error "Non reference given to modulus of array" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
123  
124 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
125 0 ▶ error "Scalar value given to modulus of array" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
126 0 ▶ return $two->mod($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
127 0 ▶ if (!$two->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Array")) 0 0 0
128 {
129 0 error "Given non array to array modulus";
130 }
131  
132 0 error "Modulusing arrays? what did you think this would do, create a black hole?";
133 }
134  
135 sub mult
136 0 ▶{
Subroutine mult: not called CC=5 SCAR=34.0
Pod undocumented
137 0 my ($one, $two, $rev) = @_;
138  
139 0 ▶ error "Non reference given to multiplication of array" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
140  
141 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
142 0 ▶ error "Scalar value given to multiplcation of array" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
143 0 ▶ return $two->mult($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
144 0 ▶ if (!$two->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Array")) 0 0 0
145 {
146 0 error "Given non array to array multiplication of array";
147 }
148  
149 0 error "Multiplying arrays? what did you think this would do, create a black hole?";
150 }
151  
152 sub div
153 0 ▶{
Subroutine div: not called CC=5 SCAR=34.0
Pod undocumented
154 0 my ($one, $two, $rev) = @_;
155  
156 0 ▶ error "Non reference given to division of array" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
157  
158 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
159 0 ▶ error "Scalar value given to division of array" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
160 0 ▶ return $two->div($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
161 0 ▶ if (!$two->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Array")) 0 0 0
162 {
163 0 error "Given non array to array division";
164 }
165  
166 0 error "Dividing arrays? what did you think this would do, create a black hole?";
167 }
168  
169 sub bool
170 4 ▶{
Subroutine bool: called CC=2 SCAR=7.1
Pod undocumented
171 4 my $self = shift;
172  
173 #boolean for array is the same as it is in perl, empty or not
174 4 ▶ return @{$self->getarrayref()}?1:0;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->getarrayref;} ? : 0 4 4
175 }
176  
177 sub pow
178 0 ▶{
Subroutine pow: not called CC=5 SCAR=34.0
Pod undocumented
179 0 my ($one, $two, $rev) = @_;
180  
181 0 ▶ error "Non reference given to exponentiation of array" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
182  
183 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
184 0 ▶ error "Scalar value given to division of array" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
185 0 ▶ return $two->pow($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
186 0 ▶ if (!$two->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Array")) 0 0 0
187 {
188 0 error "Given non array to array exponentiation";
189 }
190  
191 0 error "Exponentiating arrays? what did you think this would do, create a black hole?";
192 }
193  
194 sub __compare
195 0 ▶{
Subroutine __compare: not called CC=5 SCAR=34.0
196 0 my ($a1, $a2) = @_;
197 0 my $same = 0;
198 0 my $ea = each_array(@$a1, @$a2);
199
200 0 ▶ while(my ($first, $second) = $ea->())
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 0 0 0
201 {
202 0 ▶ $same = $first > $second ? 1 : -1 and last if $first != $second
Branch: 2 branches
BranchTrueFalseTotal
if $first != $second 0 0 0
$first > $second ? : 0 0 0
Condition0%0/5
$first != $second && (($same = $first > $second ? 1 : -1) && (last))
!ll&&!rl&&r
000
$same = $first > $second ? 1 : -1 and last
!ll
00
MC/DC0%0/2
$first != $second && (($same = $first > $second ? 1 : -1) && (last))
$first != $second $same = $first > $second ? 1 : -1
Truth table0%0/3
A && B
A= $first != $secondB= $same = $first > $second ? 1 : -1
ABresult
0X0
100
111
203 } # shortcircuits
204  
205 0 return $same;
206 }
207  
208 sub compare
209 0 ▶{
Subroutine compare: not called CC=7 SCAR=40.3
Pod undocumented
210 0 my ($one, $two, $rev) = @_;
211  
212 0 ▶ error "Non reference given to compare" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
213  
214 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
215 0 ▶ error "Can't compare scalar with array" if ($two->istype("Pari")); #this should be brough into ALL higher level classes
Branch: 1 branch
BranchTrueFalseTotal
if $two->istype("Pari") 0 0 0
216 0 ▶ return $two->compare($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
217 0 ▶ error "Can't compare two things that aren't arrays!" unless $two->isa("Language::Farnsworth::Value::Array");
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa("Language::Farnsworth::Value::Array") 0 0 0
218  
219 0 ▶ my $rv = $rev ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 0 0
220 0 my $tv = $two->getarray();
221 0 my $ov = $one->getarray();
222  
223 #i also need to check the units, but that will come later
224 #NOTE TO SELF this needs to be more helpful, i'll probably do something by adding stuff in ->new to be able to fetch more about the processing
225 0 ▶ error "Unable to process different array types in compare\n" unless $one->conforms($two); #always call this on one, since $two COULD be some other object
Branch: 1 branch
BranchTrueFalseTotal
unless $one->conforms($two) 0 0 0
226  
227 #moving this down so that i don't do any math i don't have to
228 0 my $new = __compare($tv, $ov);
229
230 0 return $new * $rv;
231 }
232  
« blib/lib/Language/Farnsworth/Units/Currency.pm ↑ Summary blib/lib/Language/Farnsworth/Value/Undef.pm »