| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
scalar @letters and exists $$node{$letters[0]}
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]}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||