| 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 ||= {}
MC/DC0%0/1
$opt ||= {}
$opt
Truth table50%1/2
A
A= $opt
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$a->lower <=> $b->lower || $a->upper <=> $b->upper
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
| |||||||||||||||||||||||||||||
| 21 | ]; | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | return $opt->{no_verify} ? $sorted : $class->_verify($sorted); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | 18 | my $point = int($#ranges / 2); | |||||||||||||||||||||||||||
| 42 | 18 | my $range = $ranges[$point]; | |||||||||||||||||||||||||||
| 43 | 18 | ▶ | if ($value < $range->lower) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||