| 1 | package Search::Binary; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | our @EXPORT = qw(binary_search); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.96_02'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub binary_search { | ||||||||||||||||||||||||||||
| 12 | 24 | ▶ | my ($posmin, $posmax, $target, $readfn, $handle, $smallblock) = @_; | ||||||||||||||||||||||||||
|
Subroutine
binary_search: called CC=11 SCAR=24.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 24 | ▶ | $smallblock ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$smallblock ||= 0
MC/DC0%0/1
$smallblock ||= 0
$smallblock
Truth table50%1/2
A
A= $smallblock
| |||||||||||||||||||||||||||||
| 14 | 24 | ▶ | if ($posmin > $posmax) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 1 | carp 'First argument must be less then or equal to second argument' | |||||||||||||||||||||||||||
| 16 | . " (min: $posmin, max: $posmax)"; | ||||||||||||||||||||||||||||
| 17 | 1 | return 0; # some libraries rely on this behavior | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 23 | my ($x, $compare, $mid); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # assert $posmin <= $posmax | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 23 | my $seeks = my $reads = 0; | |||||||||||||||||||||||||||
| 25 | 23 | my $lastmid = int($posmin + (($posmax - $posmin) / 2)) - 1; | |||||||||||||||||||||||||||
| 26 | 23 | ▶ | while ($posmax - $posmin > $smallblock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # assert: $posmin is the beginning of a record | ||||||||||||||||||||||||||||
| 29 | # and $target >= index value for that record | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 97 | $seeks++; | |||||||||||||||||||||||||||
| 32 | 97 | $x = int($posmin + (($posmax - $posmin) / 2)); | |||||||||||||||||||||||||||
| 33 | 97 | ($compare, $mid) = $readfn->($handle, $target, $x); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 97 | ▶ | unless (defined($compare)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $posmax = $mid; | |||||||||||||||||||||||||||
| 37 | 0 | next; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 97 | ▶ | last if ($mid == $lastmid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 78 | ▶ | if ($compare > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 37 | $posmin = $mid; | |||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 41 | $posmax = $mid; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 78 | $lastmid = $mid; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Switch to sequential search. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 23 | $x = $posmin; | |||||||||||||||||||||||||||
| 51 | 23 | ▶ | while ($posmin <= $posmax) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # same loop invarient as above applies here | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 44 | $reads++; | |||||||||||||||||||||||||||
| 56 | 44 | ($compare, $posmin) = $readfn->($handle, $target, $x); | |||||||||||||||||||||||||||
| 57 | 44 | ▶ | last unless (defined($compare) && $compare > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $compare and $compare > 0
MC/DC50%1/2
defined $compare and $compare > 0
defined $compare
$compare > 0
Truth table66%2/3
A and B
A= defined $compareB= $compare > 0
| |||||||||||||||||||||||||||||
| 58 | 21 | $x = undef; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 23 | ▶ | return wantarray ? ($posmin, $seeks, $reads) : $posmin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | 1; | |||||||||||||||||||||||||||
| 64 | __END__ | ||||||||||||||||||||||||||||