| 1 | package Petal::Utils::Limitr; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use warnings::register; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 4 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 4 | ▶ | use base qw( Petal::Utils::Base ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 4 | ▶ | use constant name => 'limitr'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use constant aliases => qw(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | our $VERSION = ((require Petal::Utils), $Petal::Utils::VERSION)[1]; | |||||||||||
| 14 | 4 | our $REVISION = (split(/ /, ' $Revision: 1.2 $ '))[2]; | |||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | sub process { | ||||||||||
|
Subroutine
process: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||
| 17 | 2 | my $class = shift; | |||||||||||
| 18 | 2 | my $hash = shift; | |||||||||||
| 19 | 2 | ▶ | my $args = shift || confess( "'limitr' expects 2 variables (got nothing)!" ); | ||||||||||
|
Condition50%1/2
shift() || confess(q['limitr' expects 2 variables (got nothing)!])
MC/DC0%0/1
shift() || confess(q['limitr' expects 2 variables (got nothing)!])
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 20 | |||||||||||||
| 21 | 2 | my @args = $class->split_args( $args ); | |||||||||||
| 22 | 2 | ▶ | my $key = $args[0] || confess( "1st arg to 'limit' should be an array (got nothing)!" ); | ||||||||||
|
Condition50%1/2
$args[0] || confess(q[1st arg to 'limit' should be an array (got nothing)!])
MC/DC0%0/1
$args[0] || confess(q[1st arg to 'limit' should be an array (got nothing)!])
$args[0]
Truth table50%1/2
A
A= $args[0]
| |||||||||||||
| 23 | 2 | ▶ | my $count = $args[1] || confess( "2nd arg to 'limit' should be a variable (got nothing)!" ); | ||||||||||
|
Condition50%1/2
$args[1] || confess(q[2nd arg to 'limit' should be a variable (got nothing)!])
MC/DC0%0/1
$args[1] || confess(q[2nd arg to 'limit' should be a variable (got nothing)!])
$args[1]
Truth table50%1/2
A
A= $args[1]
| |||||||||||||
| 24 | |||||||||||||
| 25 | 2 | my $arrayref = $hash->fetch($key); | |||||||||||
| 26 | # Shuffle full array | ||||||||||||
| 27 | 2 | fisher_yates_shuffle($arrayref); | |||||||||||
| 28 | 2 | $count--; | |||||||||||
| 29 | # trim $count to max size of array | ||||||||||||
| 30 | 2 | ▶ | $count = $#$arrayref if $#$arrayref < $count; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 2 | ▶ | return [] if $count < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 2 | return [@{$arrayref}[0 .. $count]]; | |||||||||||
| 33 | |||||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | |||||||||||||
| 37 | # Generate a random permutation of @array in place | ||||||||||||
| 38 | # Usage: fisher_yates_shuffle( \@array ) : | ||||||||||||
| 39 | 2 | ▶ | sub fisher_yates_shuffle { | ||||||||||
|
Subroutine
fisher_yates_shuffle: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 40 | 2 | my $array = shift; | |||||||||||
| 41 | 2 | ▶ | return unless $#$array >= 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 2 | my $i; | |||||||||||
| 43 | 2 | ▶ | for ($i = @$array; --$i; ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 4 | my $j = int rand ($i+1); | |||||||||||
| 45 | 4 | ▶ | next if $i == $j; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 2 | @$array[$i,$j] = @$array[$j,$i]; | |||||||||||
| 47 | } | ||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | |||||||||||||
| 51 | 4 | 1; | |||||||||||
| 52 | |||||||||||||
| 53 | __END__ | ||||||||||||