lib/App/Sandy/Role/BSearch.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« lib/App/Sandy/Read/SingleEnd.pm ↑ Summary lib/App/Sandy/Role/Counter.pm »
1 package App::Sandy::Role::BSearch;
2 # ABSTRACT: Binary search role
3  
4 1 ▶use App::Sandy::Base 'role';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.25'; # VERSION
7  
8 sub with_bsearch {
9 my ($self, $key, $base, $nmemb, $func) = @_;
10 return $self->_bsearch($key, $base, 0, $nmemb - 1, $func);
11 }
12  
13 sub _bsearch {
14 my ($self, $key1, $base, $start, $end, $func) = @_;
15  
16 if ($start > $end) {
17 # Not found!
18 return;
19 }
20  
21 my $index = int(($start + $end) / 2);
22 my $key2 = $base->[$index];
23  
24 # $key1 <=> $key2
25 my $rc = $func->($key1, $key2);
26  
27 if ($rc > 0) {
28 return $self->_bsearch($key1, $base, $index + 1, $end, $func);
29 } elsif ($rc < 0) {
30 return $self->_bsearch($key1, $base, $start, $index - 1, $func);
31 } else {
32 return $index;
33 }
34 }
35  
36 __END__
« lib/App/Sandy/Read/SingleEnd.pm ↑ Summary lib/App/Sandy/Role/Counter.pm »