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 CC 14 SCAR 13.1
CC
14
Cov
94%
_find
6.0
insert
5.0
insert_all
3.1
CRAP 3.7
↑ Summary
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.02';
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 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
20 4 my ($class, $metric) = @_;
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 18 ▶sub insert {
Subroutine insert: called CC=5 SCAR=16.1
Pod documented
39 18 my ($self, $object) = @_;
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 3 ▶sub insert_all {
Subroutine insert_all: called CC=3 SCAR=11.4
Pod documented
66 3 my ($self, @objects) = @_;
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 2 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
76 2 my ($self, $target, $threshold) = @_;
77 2 my @return;
78 2 $self->_find($self->{root}, \@return, $target, $threshold);
79 2 return \@return;
80 }
81  
82 8 ▶sub _find {
Subroutine _find: called CC=6 SCAR=17.9
83 8 my ($self, $node, $current_list, $target, $threshold) = @_;
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 9 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
100 9 my ($self) = @_;
101 9 return $self->{size};
102 }
103  
104  
105 2 1;
106  
107 __END__
↑ Summary