blib/lib/Simple/Trie.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 94.5% 69 / 73 CC 12 SCAR 9.1
CC
12
Cov
95%
_add_node
4.0
smart_find
3.1
find
3.0
CRAP 2.5
↑ Summary
1 package Simple::Trie;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $Simple::Trie::AUTHORITY = 'cpan:LOGIE';
4 }
5 {
6 2 $Simple::Trie::VERSION = '0.01';
7 }
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use constant '_END' => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Trie, it's simple
12  
13 has words => (
14 is => 'rw',
15 initializer => 'words',
16 4 ▶ trigger => sub { (shift)->_make_trie(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 ▶ coerce => sub { ref $_[0] eq 'ARRAY' ? $_[0] : [split(/\s/,$_[0])] }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 2 4
18 2 );
19  
20 4 ▶has _trie => ( is => 'rw', default => sub {{}} );
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 2 ▶sub add { (shift)->_add_node(@_) }
Subroutine add: called CC=1 SCAR=0.0
Pod documented
23  
24 4 ▶sub find {
Subroutine find: called CC=3 SCAR=11.0
Pod documented
25 4 my ($self, $word) = @_;
26 4 my $node = $self->_trie;
27 4 $node = $node->{$_} for split "", $word;
28 4 ▶ return exists $node->{_END()} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $node->{''} ? : 2 2 4
29 }
30  
31 1 ▶sub smart_find {
Subroutine smart_find: called CC=3 SCAR=11.2
Pod documented
32 1 my ($self, $prefix) = @_;
33 1 my @letters = split "", $prefix;
34 1 my $node = $self->_trie;
35  
36 1 $node = $node->{$_} for @letters;
37  
38 1 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 1 0 1
39 1 my @found;
40 1 $self->_find_all($prefix, $node, \@found);
41 1 return @found;
42 }
43 0 return;
44 }
45  
46 10 ▶sub _add_node {
Subroutine _add_node: called CC=4 SCAR=13.9
47 10 my ($self, $word) = @_;
48 10 my $head;
49 10 my $node = $head = $self->_trie;
50  
51 10 my @letters = split "", $word;
52 10 ▶ while ( scalar @letters && exists($node->{$letters[0]}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @letters and exists $$node{$letters[0]}) 7 10 17
Condition66%2/3
scalar @letters and exists $$node{$letters[0]}
!ll&&!rl&&r
0107
MC/DC50%1/2
scalar @letters and exists $$node{$letters[0]}
scalar @letters exists $$node{$letters[0]}
Truth table66%2/3
A and B
A= scalar @lettersB= exists $$node{$letters[0]}
ABresult
0X0
100
111
53 7 $node = $node->{shift(@letters)};
54 }
55 10 for my $letter (@letters) {
56 27 $node = $node->{$letter} = {};
57 }
58 10 $node->{_END()} = undef;
59 }
60  
61 8 ▶sub _find_all {
Subroutine _find_all: called CC=3 SCAR=11.0
62 8 my ($self, $prefix, $node, $found) = @_;
63 8 ▶ push @$found, $prefix if exists $node->{_END()};
Branch: 1 branch
BranchTrueFalseTotal
if exists $node->{""} 3 5 8
64 8 $self->_find_all($prefix . $_, $node->{$_}, $found) for keys %$node;
65 }
66  
67 4 ▶sub _make_trie {
Subroutine _make_trie: called CC=2 SCAR=6.9
68 4 my ($self, $words) = @_;
69 4 $self->_add_node($_) for @$words;
70 }
71  
72  
73 2 1;
74  
75 __END__
↑ Summary