File Coverage

lib/App/Sandy/Role/BSearch.pm
Criterion Covered Total %
statement 14 15 93.3
branch 5 6 83.3
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 22 25 88.0


line stmt bran cond sub pod time code
1             package App::Sandy::Role::BSearch;
2             # ABSTRACT: Binary search role
3              
4 6     6   5725 use App::Sandy::Base 'role';
  6         13  
  6         47  
5              
6             our $VERSION = '0.22'; # VERSION
7              
8             sub with_bsearch {
9 3903     3903 0 9522 my ($self, $key, $base, $nmemb, $func) = @_;
10 3903         10846 return $self->_bsearch($key, $base, 0, $nmemb - 1, $func);
11             }
12              
13             sub _bsearch {
14 5904     5904   11006 my ($self, $key1, $base, $start, $end, $func) = @_;
15              
16 5904 50       11621 if ($start > $end) {
17             # Not found!
18 0         0 return;
19             }
20              
21 5904         11952 my $index = int(($start + $end) / 2);
22 5904         10329 my $key2 = $base->[$index];
23              
24             # $key1 <=> $key2
25 5904         13628 my $rc = $func->($key1, $key2);
26              
27 5904 100       14238 if ($rc > 0) {
    100          
28 1433         4080 return $self->_bsearch($key1, $base, $index + 1, $end, $func);
29             } elsif ($rc < 0) {
30 568         1583 return $self->_bsearch($key1, $base, $start, $index - 1, $func);
31             } else {
32 3903         12459 return $index;
33             }
34             }
35              
36             __END__
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             App::Sandy::Role::BSearch - Binary search role
45              
46             =head1 VERSION
47              
48             version 0.22
49              
50             =head1 AUTHORS
51              
52             =over 4
53              
54             =item *
55              
56             Thiago L. A. Miller <tmiller@mochsl.org.br>
57              
58             =item *
59              
60             J. Leonel Buzzo <lbuzzo@mochsl.org.br>
61              
62             =item *
63              
64             Felipe R. C. dos Santos <fsantos@mochsl.org.br>
65              
66             =item *
67              
68             Helena B. Conceição <hconceicao@mochsl.org.br>
69              
70             =item *
71              
72             Gabriela Guardia <gguardia@mochsl.org.br>
73              
74             =item *
75              
76             Fernanda Orpinelli <forpinelli@mochsl.org.br>
77              
78             =item *
79              
80             Pedro A. F. Galante <pgalante@mochsl.org.br>
81              
82             =back
83              
84             =head1 COPYRIGHT AND LICENSE
85              
86             This software is Copyright (c) 2018 by Teaching and Research Institute from Sírio-Libanês Hospital.
87              
88             This is free software, licensed under:
89              
90             The GNU General Public License, Version 3, June 2007
91              
92             =cut