| 1 | package AI::Categorizer::Util; | ||||||||||
| 2 | |||||||||||
| 3 | 12 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 12 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 12 | @EXPORT_OK = qw(intersection average max min random_elements binary_search); | |||||||||
| 6 | |||||||||||
| 7 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # It's possible that this can be a class - something like | ||||||||||
| 10 | # | ||||||||||
| 11 | # $e = Evaluate->new(); $e->correct([...]); $e->assigned([...]); print $e->precision; | ||||||||||
| 12 | |||||||||||
| 13 | # A simple binary search | ||||||||||
| 14 | 5 | ▶ | sub binary_search { | ||||||||
|
Subroutine
binary_search: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 15 | 5 | my ($arr, $target) = @_; | |||||||||
| 16 | 5 | my ($low, $high) = (0, scalar @$arr); | |||||||||
| 17 | 12 | ▶ | use integer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | ▶ | while ( $low < $high ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 10 | my $cur = ($low + $high)/2; | |||||||||
| 20 | 10 | ▶ | if ( $arr->[$cur] < $target ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 4 | $low = $cur + 1; | |||||||||
| 22 | } else { | ||||||||||
| 23 | 6 | $high = $cur; | |||||||||
| 24 | } | ||||||||||
| 25 | } | ||||||||||
| 26 | 5 | return $low; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 4 | ▶ | sub max { | ||||||||
|
Subroutine
max: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 30 | 4 | ▶ | return undef unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 4 | my $max = shift; | |||||||||
| 32 | 4 | foreach (@_) { | |||||||||
| 33 | 22 | ▶ | $max = $_ if $_ > $max; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | } | ||||||||||
| 35 | 4 | return $max; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub min { | ||||||||
|
Subroutine
min: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 39 | 0 | ▶ | return undef unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | my $min = shift; | |||||||||
| 41 | 0 | foreach (@_) { | |||||||||
| 42 | 0 | ▶ | $min = $_ if $_ > $min; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | 0 | return $min; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | sub average { | ||||||||
|
Subroutine
average: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 48 | 0 | ▶ | return undef unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | my $total; | |||||||||
| 50 | 0 | $total += $_ foreach @_; | |||||||||
| 51 | 0 | return $total/@_; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub intersection { | ||||||||
|
Subroutine
intersection: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 55 | 0 | my ($one, $two) = @_; | |||||||||
| 56 | 0 | $two = _hashify($two); | |||||||||
| 57 | |||||||||||
| 58 | return UNIVERSAL::isa($one, 'HASH') ? # Accept hash or array for $one | ||||||||||
| 59 | 0 | grep {exists $two->{$_}} keys %$one : | |||||||||
| 60 | 0 | ▶ | grep {exists $two->{$_}} @$one; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub _hashify { | ||||||||
|
Subroutine
_hashify: not called CC=2 SCAR=17.9
| |||||||||||
| 64 | 0 | ▶ | return $_[0] if UNIVERSAL::isa($_[0], 'HASH'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | return {map {$_=>1} @{$_[0]}}; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 2 | ▶ | sub random_elements { | ||||||||
|
Subroutine
random_elements: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||
| 69 | 2 | my ($a_ref, $n) = @_; | |||||||||
| 70 | 2 | ▶ | return @$a_ref if $n >= @$a_ref; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | 2 | ▶ | my ($select, $mode) = ($n < @$a_ref/2) ? ($n, 'include') : (@$a_ref - $n, 'exclude'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 2 | my %i; | |||||||||
| 75 | 2 | ▶ | $i{int rand @$a_ref} = 1 while keys(%i) < $select; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 2 | ▶ | return @{$a_ref}[keys %i] if $mode eq 'include'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 1 | ▶ | return map {$i{$_} ? () : $a_ref->[$_]} 0..$#$a_ref; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 12 | 1; | |||||||||