GenOO/Module/Search/Binary.pm

filter: Statementstmt 100% 27 / 27 Branchbran 92.8% 13 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 2 / 2 Podpod 0.0% 0 / 1 totaltotal 88.8% 48 / 54 CC 10 SCAR 22.2
CC
10
Cov
94%
binary_search_for_value_greater_or_equal
10.0
BEGIN
1.0
CRAP 9.2
« GenOO/Data/File/SAM/CigarAndMDZ.pm ↑ Summary GenOO/Gene.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 GenOO::Module::Search::Binary - Module that offers methods for searching in an array using binary search
6  
7 =head1 SYNOPSIS
8  
9 use GenOO::Module::Search::Binary
10 my $pos = GenOO::Module::Search::Binary->binary_search_for_value_greater_or_equal(3.2, [0,1,2,3,4,4,4,5,6], sub {return $_[0]});
11  
12  
13 =head1 DESCRIPTION
14  
15 Implements binary search algorithms for scanning an array.
16
17 =cut
18  
19 # Let the code begin...
20  
21 package GenOO::Module::Search::Binary;
22 1 $GenOO::Module::Search::Binary::VERSION = '1.4.9';
23  
24 #######################################################################
25 ####################### Load External modules #####################
26 #######################################################################
27 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 #######################################################################
31 ####################### Interface Functions #######################
32 #######################################################################
33 =head2 binary_search_for_value_greater_or_equal
34 Arg [1] : number. The target value
35 Arg [2] : array reference. Array in which the target value is searched.
36 No assumption is made about the entries in the array and how to extract the actual values from them.
37 The array must be sorted by value though.
38 Arg [3] : code reference. Code that extracts the actual value given an entry of the array
39 Example : binary_search_for_value_greater_or_equal(3.2, [0,1,2,3,4,4,4,5,6], sub {return $_[0]})
40 Description: Implements a binary search algorithm returning the I<position> of the first I<entry>
41 whose I<value> is greater than or equal to C<target value>. The search routine does
42 not make any assumption about the entries in the array, but leaves the implementation
43 to the user supplied code function.
44 During the search the user supplied code function will be called with a single arguments:
45 an entry of the array and should return the value that corresponds to this entry.
46 Return : Integer index of the first entry whose value is greater or equal to the target value
47 =cut
48 sub binary_search_for_value_greater_or_equal {
49 30 ▶ my ($class, $target_value, $sorted_array, $code) = @_;
Subroutine binary_search_for_value_greater_or_equal: called CC=10 SCAR=23.1
Pod undocumented
50
51 30 my $index;
52 30 my $low_index = 0;
53 30 my $up_index = $#{$sorted_array};
54
55 30 ▶ while ($low_index <= $up_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low_index <= $up_index) 68 19 87
56
57 68 $index = int(($low_index + $up_index) / 2);
58
59 68 ▶ if ($code->($sorted_array->[$index]) < $target_value) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$code($sorted_array->[$index]) < $target_value) { } 26 42 68
elsif (&$code($sorted_array->[$index]) > $target_value) { } 31 11 42
60 26 $low_index = $index+1; # move to the up half
61 }
62 elsif ($code->($sorted_array->[$index]) > $target_value) {
63 31 $up_index = $index-1; # move to the low half
64 }
65 else {
66 # exact match found -> search upstream for the first of the exact matches
67 11 ▶ while (($index-1 >= 0) and ($code->($sorted_array->[$index-1]) == $target_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index - 1 >= 0 and &$code($$sorted_array[$index - 1]) == $target_value) 3 11 14
Condition100%3/3
$index - 1 >= 0 and &$code($$sorted_array[$index - 1]) == $target_value
!ll&&!rl&&r
563
MC/DC100%2/2
$index - 1 >= 0 and &$code($$sorted_array[$index - 1]) == $target_value
$index - 1 >= 0 &$code($$sorted_array[$index - 1]) == $target_value
Truth table100%3/3
A and B
A= $index - 1 >= 0B= &$code($$sorted_array[$index - 1]) == $target_value
ABresult
0X0
100
111
68 3 $index--;
69 }
70 11 last;
71 }
72
73 }
74
75 # check if got outside the array without finding a bigger value than the one requested
76 30 ▶ if ($low_index > $#{$sorted_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low_index > $#{$sorted_array;}) 5 25 30
77 5 return undef;
78 }
79
80 # under certain conditions index might point to the value immediatelly preceding the greater value
81 25 ▶ if ($code->($sorted_array->[$index]) < $target_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$code($sorted_array->[$index]) < $target_value) 3 22 25
82 3 ▶ if (($index+1 <= $#{$sorted_array}) and ($code->($sorted_array->[$index+1]) > $target_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index + 1 <= $#{$sorted_array;} and &$code($$sorted_array[$index + 1]) > $target_value) 3 0 3
Condition33%1/3
$index + 1 <= $#{$sorted_array;} and &$code($$sorted_array[$index + 1]) > $target_value
!ll&&!rl&&r
003
MC/DC0%0/2
$index + 1 <= $#{$sorted_array;} and &$code($$sorted_array[$index + 1]) > $target_value
$index + 1 <= $#{$sorted_array;} &$code($$sorted_array[$index + 1]) > $target_value
Truth table33%1/3
A and B
A= $index + 1 <= $#{$sorted_array;}B= &$code($$sorted_array[$index + 1]) > $target_value
ABresult
0X0
100
111
83 3 $index++;
84 }
85 3 return $index;
86 }
87
88 22 return $index;
89 }
90  
91 1 1;
« GenOO/Data/File/SAM/CigarAndMDZ.pm ↑ Summary GenOO/Gene.pm »