blib/lib/Games/Dice/Probability.pm

filter: Statementstmt 80.9% 149 / 184 Branchbran 70.4% 69 / 98 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 10 totaltotal 71.8% 240 / 334 CC 64 SCAR 22.3
CC
64
Cov
75%
distribution
30.0
travel
12.3
debug
12.0
CRAP 9.3
↑ Summary
1 package Games::Dice::Probability;
2  
3 29 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Be a Good Module
6 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use diagnostics;
9  
10 # Our version number.
11 29 our $VERSION = '0.02';
12  
13 # Required Modules
14 # Math::Sumbolic::AuxFunctions is for the calcs of binomial coefficients.
15 #
16 # Parse::RecDescent parses the dice expressions.
17 29 ▶use Math::Symbolic::AuxFunctions;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 29 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Optional Modules
21 # Debug::ShowStuff is used by $self->debug() to descend and display hashes
22 # in the object.
23 #
24 # Memoize is used by $self->new() to speed up calculations on individual
25 # nodes in the expression tree. If there are duplicate nodes, the second
26 # and all subsequent calc_distribution calls with the same parameters will
27 # just return the cached values.
28 #
29 # Thanks to Mark Mills for the optional-module code snippet, and general
30 # ideas, tips, and tricks for the whole module. He's my local Perl
31 # Monk...you can email him thanks and offers of cookies at:
32 # extremely{plus}pm{at}hostile{dot}org
33 29 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=14.2
34 # Is Debug::ShowStuff available?
35 29 ▶ if ( eval q/ require Debug::ShowStuff / ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval ' require Debug::ShowStuff ') { } 0 29 29
36 # If so, import the routines.
37 0 Debug::ShowStuff->import("showref");
38 } else {
39 # Not available. Place stub in its place.
40 0 ▶ eval q/ sub showref { print @_; print "\n"; } /;
Subroutine showref: not called CC=1 SCAR=2.6
Pod undocumented
41 }
42  
43 # Is Memoize available?
44 29 ▶ if ( eval q/ require Memoize / ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval ' require Memoize ') { } 29 0 29
45 # If so, import the routines.
46 29 Memoize->import();
47 } else {
48 # Not available. Place stub in its place.
49 0 eval q/ sub memoize { return; } /;
50 }
51 }
52  
53 # Binomial Coefficient Shortcut
54 # Non-polluted namespaces are cool and all, but sometimes it's too much to
55 # type a 44 character long subroutine name multiple times in a simple
56 # equation. Sheesh.
57 29 my $binco = \&Math::Symbolic::AuxFunctions::binomial_coeff;
58  
59 # Recursive Parsing Grammar
60 # Parsing grammar and tree code outright stolen from Sam Holden's
61 # DiceDistribution.pm at http://sam.holden.id.au/junk/DICEDISTRIBUTION/.
62 # Added a divide expression for division calculations. Changed the way
63 # Fudge Dice are expressed to be #d[fF] which is more inline with how
64 # they are represented elsewhere. Added mid# dice expression as part
65 # of dicenode.
66 29 my $DiceGrammar = <<'END_GRAMMAR';
67 expression: add_sub end { $item[1] }
68 add_sub: mult_div '+' add_sub { { left => $item[1], op => '+', right => $item[3] } }
69 add_sub: mult_div '-' add_sub { { left => $item[1], op => '-', right => $item[3] } }
70 add_sub: mult_div
71 mult_div: bracket '/' mult_div { { left => $item[1], op => '/', right => $item[3] } }
72 mult_div: bracket '*' mult_div { { left => $item[1], op => '*', right => $item[3] } }
73 mult_div: bracket
74 bracket: '(' add_sub ')' { $item[2] }
75 bracket: dicenode
76 dicenode: /(\d+|mi)d(\d+|f)/i
77 dicenode: /\d+/
78 end: /\s*$/
79 END_GRAMMAR
80  
81 # Dice Parsing Object
82 29 ▶my $DiceParser = Parse::RecDescent->new($DiceGrammar) || die("bad grammar");
Condition50%1/2
'Parse::RecDescent'->new($DiceGrammar) || die('bad grammar')
l!l
290
MC/DC0%0/1
'Parse::RecDescent'->new($DiceGrammar) || die('bad grammar')
'Parse::RecDescent'->new($DiceGrammar)
Truth table50%1/2
A
A= 'Parse::RecDescent'->new($DiceGrammar)
Aresult
11
00
83  
84 # import()
85 # Faux import function that either memoizes the calc portions of the
86 # module (default) or doesn't.
87 29 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
88 # Default is to memoize. Saving cycles is a Good Thing. However, if
89 # someone passes an unmemoize argument, then we will respect their wishes.
90 29 ▶ if ( ! grep(/(un|no)memo(ize)*/i,@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /(un|no)memo(ize)*/i, @_) 29 0 29
91 # Attempt to memoize the calculation subroutines. This will either
92 # truly memoize, or the stub memoize function will simply return having
93 # done nothing.
94 29 memoize('calc_distribution');
95 # Not certain if calc_combination will benefit from Memoize. Need to
96 # test further.
97 29 memoize('calc_combination');
98 }
99  
100 # All is well. Return.
101 29 return;
102 }
103  
104 # debug()
105 # Print debugging information about object.
106 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod undocumented
107 # The object of our attention.
108 0 my $self = shift;
109  
110 # For every piece of the object...
111 0 foreach my $key ( sort(keys(%$self)) ) {
112 # Output the name...
113 0 print "self->{$key}=";
114 # And if it is a reference...
115 0 ▶ if ( ref($self->{$key}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key}) { } 0 0 0
116 # Print the contents of the reference...
117 0 print "\n";
118 0 showref($self->{$key});
119 } else {
120 # Or, print the value.
121 0 print $self->{$key} . "\n";
122 }
123 }
124  
125 # All is well.
126 0 return(0);
127 }
128  
129 # new(expression)
130 # Creates a new object based on the provided dice expression.
131 29 ▶sub new {
Subroutine new: called CC=6 SCAR=20.2
Pod undocumented
132 29 my $invocant = shift;
133 29 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
0290
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
134 29 my $self = {};
135  
136 29 ▶ die("must provide dice expression") unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 29 29
137  
138 29 ▶ if (@_ != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 1) { } 0 29 29
139 0 die("new() called with too many arguments");
140 } else {
141 29 ▶ $self->{EXPRESSION} = $DiceParser->expression(@_) || die "could not parse expression";
Condition50%1/2
$DiceParser->expression(@_) || die('could not parse expression')
l!l
290
MC/DC0%0/1
$DiceParser->expression(@_) || die('could not parse expression')
$DiceParser->expression(@_)
Truth table50%1/2
A
A= $DiceParser->expression(@_)
Aresult
11
00
142 29 ▶ $self->{DISTRIBUTION} = travel($self->{EXPRESSION}) || die "could not travel parsed expression";
Condition50%1/2
travel($self->{'EXPRESSION'}) || die('could not travel parsed expression')
l!l
290
MC/DC0%0/1
travel($self->{'EXPRESSION'}) || die('could not travel parsed expression')
travel($self->{'EXPRESSION'})
Truth table50%1/2
A
A= travel($self->{'EXPRESSION'})
Aresult
11
00
143 }
144  
145 29 return(bless($self, $class));
146 }
147  
148 # travel(expression)
149 # Travel the parsed expression, returning hash of value => permutations.
150 61 ▶sub travel {
Subroutine travel: called CC=12 SCAR=25.1
Pod undocumented
151 61 ▶ my $node = shift || die "invalid or missing expression node";
Condition50%1/2
shift() || die('invalid or missing expression node')
l!l
610
MC/DC0%0/1
shift() || die('invalid or missing expression node')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
152  
153 61 ▶ if ( ref($node) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) 16 45 61
154 16 for ($node->{op}) {
155 16 ▶ /(\+)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) );
Branch: 1 branch
BranchTrueFalseTotal
/(\+)/ and return calc_combination($1, travel($node->{"left"}), travel($node->{"right"})) 7 9 16
156 9 ▶ /(\-)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) );
Branch: 1 branch
BranchTrueFalseTotal
/(\-)/ and return calc_combination($1, travel($node->{"left"}), travel($node->{"right"})) 3 6 9
157 6 ▶ /(\*)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) );
Branch: 1 branch
BranchTrueFalseTotal
/(\*)/ and return calc_combination($1, travel($node->{"left"}), travel($node->{"right"})) 4 2 6
158 2 ▶ /(\/)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) );
Branch: 1 branch
BranchTrueFalseTotal
m[(/)] and return calc_combination($1, travel($node->{"left"}), travel($node->{"right"})) 2 0 2
159 }
160 }
161  
162 45 for ($node) {
163 45 ▶ /^(\d+)(d)(\d+|f)/i && return( calc_distribution($2,$1,$3) );
Branch: 1 branch
BranchTrueFalseTotal
/^(\d+)(d)(\d+|f)/i and return calc_distribution($2, $1, $3) 37 8 45
164 8 ▶ /^(mi)(d)(\d+|f)/i && return( calc_distribution("m",3,$3) );
Branch: 1 branch
BranchTrueFalseTotal
/^(mi)(d)(\d+|f)/i and return calc_distribution("m", 3, $3) 6 2 8
165 2 ▶ /^\d+$/ && return( {$node => 1} );
Branch: 1 branch
BranchTrueFalseTotal
/^\d+$/ and return {$node, 1} 2 0 2
166 }
167  
168 0 die("invalid token in expression.");
169 }
170  
171 # calc_distribution(method,numdice,numsides)
172 # Calculate distribution of values/permutations given: method, number of
173 # dice, and number of sides per die.
174 41 ▶sub calc_distribution {
Subroutine calc_distribution: called CC=11 SCAR=24.0
175 # The dice method requested.
176 41 my $method = shift;
177 # The number of dice.
178 41 my $n = shift; $n += 0;
179 # The number of sides/faces on each die.
180 # f/F = Fudge dice = d3.
181 41 ▶ my $s = shift; $s += 0 unless $s =~ /f/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /f/i 35 6 41
182 # Loop value based on total/face.
183 41 my $t1;
184 # Hash to return containing the distribution.
185 my %dist;
186  
187 # If dice node method is simple-sum nDs or nDf...
188 41 ▶ if ( $method =~ /d/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method =~ /d/i) { } 35 6 41
elsif ($method =~ /m/i) { } 6 0 6
189 35 ▶ if ( $s =~ /f/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ /f/i) { } 6 29 35
elsif ($n eq 1) { } 4 25 29
190 # If dice node method is fudge nDf...
191  
192 # First, get the distribution for nDs...
193 6 my $tempdist = calc_distribution("d",$n,3);
194 # Then loop to build the new distribution from (-n..0)...
195 6 my $origkey = $n;
196 6 ▶ for ($t1=-1 * $n; $t1 <= 0; $t1++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t1 <= 0) 27 6 33
197 # Changing the original values in (n..ns) to (-n..n),
198 # copy the original distribution to the new one with
199 # the correct values...
200 27 $dist{$t1} = $$tempdist{$origkey};
201 27 $dist{$t1*-1} = $$tempdist{$origkey};
202 27 $origkey++;
203 }
204 } elsif ( $n eq 1 ) {
205 # Save compute cycles if only one die...
206  
207 # Each face has a single chance...
208 4 ▶ for ($t1=1; $t1 <= $s; $t1++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t1 <= $s) 19 4 23
209 19 $dist{$t1} = 1;
210 }
211 } else {
212 # Else compute number of combinations for a total on the face of n
213 # dice.
214  
215 # Minimum and maximum sums.
216 25 my $tmin = $n;
217 25 my $tmax = $n*$s;
218 # Peak sum is the sum around which the bell-curve mirrors, saving
219 # compute time.
220 25 my $tpeak = ($tmin+$tmax)/2;
221  
222 # For each total (t1) in $tmin..$tmax, calculate the number of
223 # combinations giving that total.
224 25 ▶ for ($t1=$tmin; $t1 <= $tpeak; $t1++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t1 <= $tpeak) 188 25 213
225 # Total (t2) that is the mirror point in the bell curve.
226 188 my $t2 = $tmin + $tmax - $t1;
227 # Ceiling for the sum function.
228 188 my $ceil = int( ($t1-$n) / $s );
229 # Result.
230 188 my $res = 0;
231 # Loop control for the sum funtion.
232 188 my $k;
233  
234 # Sum Function: For each k in 0 to $ceil...
235 188 ▶ for ($k=0; $k <= $ceil; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $ceil) 246 188 434
236 # Calculate and add to previous results.
237 246 $res += ((-1)**$k) * &$binco($n,$k) * &$binco(($t1-($s*$k)-1),($n-1));
238 }
239  
240 # Set the mirrored points of the distribution.
241 # Note: $t1 and $t2 can be equal once at
242 # $t1=$tpeak when s is even.
243 188 $dist{$t1} = $res;
244 188 $dist{$t2} = $res;
245 }
246 }
247 } elsif ( $method =~ /m/i ) {
248 # If dice method is take-the-middle-value nMs...
249  
250 # Minimum and maximum values.
251 6 my $tmin = 1;
252 6 my $tmax = $s;
253  
254 # For each value (t1) in $tmin..$tmax, calculate the number of
255 # combinations giving t1 as the middle face value.
256 6 ▶ for ($t1=$tmin; $t1 <= $tmax; $t1++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t1 <= $tmax) 60 6 66
257 60 $dist{$t1} = 1 + ( 3 * ($s - 1) ) + ( 6 * ($t1 - 1) * ($s - $t1) );
258 }
259 }
260  
261 # Return the value=>combinations distribution hash for this node.
262 41 return({%dist});
263 }
264  
265 # calc_combination(operand,distribution1,distribution2)
266 # Combine two distributions with the method provided.
267 16 ▶sub calc_combination {
Subroutine calc_combination: called CC=8 SCAR=20.8
268 # Calculation to perform on values.
269 16 my $op = shift;
270 # Distributions to combine.
271 16 my $dist1 = shift;
272 16 my $dist2 = shift;
273 # The combined distribution.
274 16 my %cdist;
275  
276 # For each value in the first distribution...
277 16 foreach my $val1 ( sort {$a+0 <=> $b+0} keys(%$dist1) ) {
278 # Combine it with every value in the second distribution...
279 206 foreach my $val2 ( sort {$a+0 <=> $b+0} keys(%$dist2) ) {
280 # The new value of which is calculated based on combine method...
281 28724 my $newval;
282 28724 for ($op) {
283 28724 ▶ /\+/ && do { $newval = $val1 + $val2 };
Branch: 1 branch
BranchTrueFalseTotal
/\+/ and do { $newval = $val1 + $val2 } 20649 8075 28724
284 28724 ▶ /\-/ && do { $newval = $val1 - $val2 };
Branch: 1 branch
BranchTrueFalseTotal
/\-/ and do { $newval = $val1 - $val2 } 405 28319 28724
285 28724 ▶ /\*/ && do { $newval = $val1 * $val2 };
Branch: 1 branch
BranchTrueFalseTotal
/\*/ and do { $newval = $val1 * $val2 } 7293 21431 28724
286 28724 ▶ /\// && do { $newval = int($val1 / $val2) };
Branch: 1 branch
BranchTrueFalseTotal
m[/] and do { $newval = int $val1 / $val2 } 377 28347 28724
287 }
288 # Calculate the new combined combinations and set it to the new
289 # value in the new distribution.
290 28724 $cdist{$newval} += $$dist1{$val1} * $$dist2{$val2};
291 }
292 }
293  
294 # Return the combined distribution.
295 16 return({%cdist});
296 }
297  
298 # combinations(targetvalue)
299 # Calculate number of combinations for a target value.
300 303 ▶sub combinations {
Subroutine combinations: called CC=7 SCAR=20.4
Pod undocumented
301 303 my $self = shift;
302 303 my $targetvalue = shift;
303  
304 303 ▶ $targetvalue = "ALL" unless defined($targetvalue);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $targetvalue 28 275 303
305  
306 303 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 303 0 303
307 303 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 303 303
308 0 die("combinations() called incorrectly");
309 } else {
310 303 ▶ if ( $targetvalue eq "ALL" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($targetvalue eq 'ALL') { } 28 275 303
311 28 ▶ if ( $self->{COMBINATIONS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'COMBINATIONS'}) { } 0 28 28
312 0 return ( $self->{COMBINATIONS} );
313 } else {
314 28 foreach my $value ( values(%{$self->{DISTRIBUTION}}) ) {
315 12201 $self->{COMBINATIONS} += $value;
316 }
317 28 return($self->{COMBINATIONS});
318 }
319 } else {
320 275 return( $self->{DISTRIBUTION}->{$targetvalue} );
321 }
322 }
323 } else {
324 0 die("combinations() called on non-object");
325 }
326 }
327  
328 # distribution()
329 # Returns a hash containing the distribution in value=>combinations format.
330 0 ▶sub distribution {
Subroutine distribution: not called CC=5 SCAR=34.0
Pod undocumented
331 0 my $self = shift;
332  
333 0 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
334 0 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 0 0
335 0 die("distribution() called with argument on object");
336 } else {
337 0 return( $self->{DISTRIBUTION} );
338 }
339 } else {
340 0 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 0 0
341 0 ▶ my $expression = $DiceParser->expression(@_) || die "could not parse expression";
Condition0%0/2
$DiceParser->expression(@_) || die('could not parse expression')
l!l
00
MC/DC0%0/1
$DiceParser->expression(@_) || die('could not parse expression')
$DiceParser->expression(@_)
Truth table0%0/2
A
A= $DiceParser->expression(@_)
Aresult
11
00
342 0 return( travel($expression) );
343 } else {
344 0 die("no expression provided for non-object distribution() call");
345 }
346 }
347 }
348  
349 # probability(targetvalue)
350 # Returns the probability for targetvalue, or a hash of probabilities in
351 # value=>probability format.
352 12201 ▶sub probability {
Subroutine probability: called CC=8 SCAR=22.3
Pod undocumented
353 12201 my $self = shift;
354 12201 my $targetvalue = shift;
355  
356 12201 ▶ $targetvalue = "ALL" unless defined($targetvalue);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $targetvalue 0 12201 12201
357  
358 12201 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 12201 0 12201
359 12201 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 12201 12201
360 0 die("probability() called incorrectly");
361 } else {
362 12201 ▶ if ( ! exists($self->{PROBABILITIES}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"PROBABILITIES"}) 28 12173 12201
363 28 my $combs = $self->combinations();
364 28 my %probs;
365 28 foreach my $value ( keys(%{$self->{DISTRIBUTION}}) ) {
366 12201 $probs{$value} = $self->{DISTRIBUTION}->{$value} / $combs;
367 }
368 28 $self->{PROBABILITIES} = {%probs};
369 }
370 12201 ▶ if ( $targetvalue eq "ALL" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($targetvalue eq 'ALL') { } 0 12201 12201
371 0 return( $self->{PROBABILITIES} );
372 } else {
373 12201 ▶ return( $self->{PROBABILITIES}->{$targetvalue} || undef );
Condition50%1/2
$self->{'PROBABILITIES'}{$targetvalue} || undef
l!l
122010
MC/DC0%0/1
$self->{'PROBABILITIES'}{$targetvalue} || undef
$self->{'PROBABILITIES'}{$targetvalue}
Truth table50%1/2
A
A= $self->{'PROBABILITIES'}{$targetvalue}
Aresult
11
00
374 }
375 }
376 } else {
377 0 die("probability() called on non-object");
378 }
379 }
380  
381 # bounds()
382 # Returns the min and max values of the valueset.
383 56 ▶sub bounds {
Subroutine bounds: called CC=3 SCAR=12.7
Pod undocumented
384 56 my $self = shift;
385  
386 56 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 56 0 56
387 56 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 56 56
388 0 die("bounds() called with argument on object");
389 } else {
390 56 return( [ $self->min(), $self->max() ] );
391 }
392 } else {
393 0 die("bounds() called on non-object");
394 }
395 }
396  
397 # max()
398 # Returns the max value of the valueset.
399 84 ▶sub max {
Subroutine max: called CC=4 SCAR=14.3
Pod undocumented
400 84 my $self = shift;
401  
402 84 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 84 0 84
403 84 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 84 84
404 0 die("max() called with argument on object");
405 } else {
406 84 ▶ if ( $self->{MAX} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'MAX'}) { } 56 28 84
407 56 return( $self->{MAX} );
408 } else {
409 28 my @values = sort {$b+0 <=> $a+0} keys(%{$self->{DISTRIBUTION}});
410 28 $self->{MAX} = shift(@values);
411 28 return( $self->{MAX} );
412 }
413 }
414 } else {
415 0 die("max() called on non-object");
416 }
417 }
418  
419 # min()
420 # Returns the min value of the valueset.
421 84 ▶sub min {
Subroutine min: called CC=4 SCAR=14.3
Pod undocumented
422 84 my $self = shift;
423  
424 84 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 84 0 84
425 84 ▶ if (@_ != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) { } 0 84 84
426 0 die("min() called with argument on object");
427 } else {
428 84 ▶ if ( $self->{MIN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'MIN'}) { } 52 32 84
429 52 return( $self->{MIN} );
430 } else {
431 32 my @values = sort {$a+0 <=> $b+0} keys(%{$self->{DISTRIBUTION}});
432 32 $self->{MIN} = shift(@values);
433 32 return( $self->{MIN} );
434 }
435 }
436 } else {
437 0 die("min() called on non-object");
438 }
439 }
440  
441 29 1;
442  
443 __END__
↑ Summary