Range/Search/Binary.pm

filter: Statementstmt 100% 36 / 36 Branchbran 75.0% 9 / 12 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 86.1% 56 / 65 CC 10 SCAR 11.6
CC
10
Cov
89%
_normalize
4.8
find
4.0
_verify
3.0
CRAP 3.2
« Range/Set.pm ↑ Summary Range.pm »
1 package List::Range::Search::Binary;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Class::Accessor::Lite ro => [qw/ranges/];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
8 1 my ($class, $set, $opt) = @_;
9 1 ▶ $opt ||= {};
Condition50%1/2
$opt ||= {}
l!l
01
MC/DC0%0/1
$opt ||= {}
$opt
Truth table50%1/2
A
A= $opt
Aresult
11
00
10  
11 1 my $ranges = $class->_normalize($set->ranges, $opt);
12 1 return bless {
13 ranges => $ranges,
14 } => $class;
15 }
16  
17 1 ▶sub _normalize {
Subroutine _normalize: called CC=4 SCAR=15.6
18 1 my ($class, $ranges, $opt) = @_;
19 my $sorted = $opt->{no_sort} ? $ranges : [
20 1 ▶ sort { $a->lower <=> $b->lower || $a->upper <=> $b->upper } @$ranges
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'no_sort'} ? : 0 1 1
Condition33%1/3
$a->lower <=> $b->lower || $a->upper <=> $b->upper
l!l&&r!l&&!r
900
MC/DC0%0/2
$a->lower <=> $b->lower || $a->upper <=> $b->upper
$a->lower <=> $b->lower $a->upper <=> $b->upper
Truth table33%1/3
A || B
A= $a->lower <=> $b->lowerB= $a->upper <=> $b->upper
ABresult
1X1
011
000
21 ];
22 1 ▶ return $opt->{no_verify} ? $sorted : $class->_verify($sorted);
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'no_verify'} ? : 0 1 1
23 }
24  
25 1 ▶sub _verify {
Subroutine _verify: called CC=3 SCAR=11.0
26 1 my ($class, $ranges) = @_;
27 1 for my $i (0..$#{$ranges}-1) {
28 5 my $before = $ranges->[$i];
29 5 my $after = $ranges->[$i+1];
30 5 ▶ die "Binary search does not support to search in the crossed ranges"
Branch: 1 branch
BranchTrueFalseTotal
if $after->lower <= $before->upper 0 5 5
31 if $after->lower <= $before->upper;
32 }
33 1 return $ranges;
34 }
35  
36 7 ▶sub find {
Subroutine find: called CC=4 SCAR=13.9
Pod documented
37 7 my ($self, $value) = @_;
38  
39 7 my @ranges = @{ $self->{ranges} };
40 7 ▶ while (@ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ranges) 18 1 19
41 18 my $point = int($#ranges / 2);
42 18 my $range = $ranges[$point];
43 18 ▶ if ($value < $range->lower) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value < $range->lower) { } 4 14 18
elsif ($value > $range->upper) { } 8 6 14
44 4 splice @ranges, $point;
45 }
46 elsif ($value > $range->upper) {
47 8 splice @ranges, 0, $point + 1;
48 }
49 else {
50 # includes
51 6 return $range;
52 }
53 }
54  
55 # not found
56 1 return undef; ## no critic
57 }
58  
59 2 1;
60 __END__
« Range/Set.pm ↑ Summary Range.pm »