blib/lib/Tree/BK.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 90.0% 18 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 94.0% 94 / 100 SCAR 26.4
CC
14
Cov
94%
_find
6.0
insert
5.0
insert_all
3.1
CRAP 14.0
↑ Summary Build »
1 #
2 # This file is part of Tree-BK
3 #
4 # This software is copyright (c) 2014 by Nathan Glenn.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Tree::BK;
10 2 $Tree::BK::VERSION = '0.01';
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Text::Levenshtein::XS qw(distance);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: Structure for efficient fuzzy matching
17  
18  
19 sub new {
20 4 ▶ my ($class, $metric) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
21 4 ▶ if(defined $metric){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metric) { } 2 2 4
22 2 ▶ if((ref $metric) ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $metric ne "CODE") 1 1 2
23 1 croak 'argument to new() should be ' .
24 'a code reference implementing a metric';
25 }
26 }else{
27 2 $metric = \&Text::Levenshtein::XS::distance;
28 }
29 3 my $tree = bless {
30 metric => $metric,
31 root => undef,
32 size => 0,
33 }, $class;
34 3 return $tree;
35 }
36  
37  
38 sub insert {
39 18 ▶ my ($self, $object) = @_;
Subroutine insert: called CC=5 SCAR=16.1
Pod documented
40 18 ▶ if(!defined $self->{root}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"root"}) 3 15 18
41 3 $self->{root} = { object=>$object };
42 3 $self->{size}++;
43 3 return $object;
44 }
45  
46 15 my $current = $self->{root};
47 15 my $dist = $self->{metric}->($current->{object}, $object);
48 15 ▶ while(exists $current->{$dist}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $current->{$dist}) 2 15 17
49 # object was already in the tree
50 2 ▶ if($dist == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($dist == 0) 0 2 2
51 0 return;
52 }
53 2 $current = $current->{$dist};
54 2 $dist = $self->{metric}->($current->{object}, $object);
55 }
56 # prevent adding the root node multiple times
57 15 ▶ if($dist == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($dist == 0) 2 13 15
58 2 return;
59 }
60 13 $current->{$dist} = {object => $object};
61 13 $self->{size}++;
62 13 return $object;
63 }
64  
65 sub insert_all {
66 3 ▶ my ($self, @objects) = @_;
Subroutine insert_all: called CC=3 SCAR=11.4
Pod documented
67 3 ▶ if(@objects < 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@objects < 1) 0 3 3
68 0 croak 'Must pass at least one object to insert_all method';
69 }
70 3 my $size_before = $self->size;
71 3 $self->insert($_) for @objects;
72 3 return $self->size - $size_before;
73 }
74  
75 sub find {
76 2 ▶ my ($self, $target, $threshold) = @_;
Subroutine find: called CC=1 SCAR=0.0
Pod documented
77 2 my @return;
78 2 $self->_find($self->{root}, \@return, $target, $threshold);
79 2 return \@return;
80 }
81  
82 sub _find {
83 8 ▶ my ($self, $node, $current_list, $target, $threshold) = @_;
Subroutine _find: called CC=6 SCAR=17.9
84 8 my $distance = $self->{metric}->($node->{object}, $target);
85 8 my $min_dist = $distance - $threshold;
86 8 my $max_dist = $distance + $threshold;
87 8 ▶ if($distance <= $threshold){
Branch: 1 branch
BranchTrueFalseTotal
if ($distance <= $threshold) 7 1 8
88 7 push @$current_list, $node->{object};
89 }
90 # recursively search the children where nodes with the threshold
91 # distance might reside
92 8 for(keys %$node){
93 18 ▶ next if $_ eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "object" 8 10 18
94 10 ▶ next unless $_ >= $min_dist && $_ <= $max_dist;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ >= $min_dist and $_ <= $max_dist 4 6 10
Condition66%2/3
$_ >= $min_dist and $_ <= $max_dist
!ll&&!rl&&r
046
MC/DC50%1/2
$_ >= $min_dist and $_ <= $max_dist
$_ >= $min_dist $_ <= $max_dist
Truth table66%2/3
A and B
A= $_ >= $min_distB= $_ <= $max_dist
ABresult
0X0
100
111
95 6 $self->_find($node->{$_}, $current_list, $target, $threshold);
96 }
97 }
98  
99 sub size {
100 9 ▶ my ($self) = @_;
Subroutine size: called CC=1 SCAR=0.0
Pod documented
101 9 return $self->{size};
102 }
103  
104 2 1;
105  
106 __END__
↑ Summary Build »