| 1 | package PLS::Node; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | PLS::Node | ||||||||||
| 9 | |||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||
| 11 | |||||||||||
| 12 | Node within a L<PLS::Trie>. | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | sub new | ||||||||||
| 17 | { | ||||||||||
| 18 | 0 | ▶ | my ($class) = @_; | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||
| 19 | |||||||||||
| 20 | 0 | my %self = ( | |||||||||
| 21 | children => {}, | ||||||||||
| 22 | value => '' | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 0 | return bless \%self, $class; | |||||||||
| 26 | } ## end sub new | ||||||||||
| 27 | |||||||||||
| 28 | sub collect | ||||||||||
| 29 | { | ||||||||||
| 30 | 0 | ▶ | my ($self, $prefix) = @_; | ||||||||
|
Subroutine
collect: not called CC=3 SCAR=24.8
| |||||||||||
| 31 | |||||||||||
| 32 | 0 | my @results; | |||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | if (length $self->{value}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | { | ||||||||||
| 36 | 0 | push @results, (join '', @$prefix); | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | foreach my $char (keys %{$self->{children}}) | |||||||||
| 40 | { | ||||||||||
| 41 | 0 | push @$prefix, $char; | |||||||||
| 42 | 0 | push @results, @{$self->{children}{$char}->collect($prefix)}; | |||||||||
| 43 | 0 | pop @$prefix; | |||||||||
| 44 | } ## end foreach my $char (keys %{$self...}) | ||||||||||
| 45 | |||||||||||
| 46 | 0 | return \@results; | |||||||||
| 47 | } ## end sub collect | ||||||||||
| 48 | |||||||||||
| 49 | package PLS::Trie; | ||||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | |||||||||||
| 54 | =head1 NAME | ||||||||||
| 55 | |||||||||||
| 56 | PLS::Trie | ||||||||||
| 57 | |||||||||||
| 58 | =head2 DESCRIPTION | ||||||||||
| 59 | |||||||||||
| 60 | A trie for fast searching of a word by prefix. | ||||||||||
| 61 | |||||||||||
| 62 | =head3 SYNOPSIS | ||||||||||
| 63 | |||||||||||
| 64 | my $trie = PLS::Trie->new(); | ||||||||||
| 65 | $trie->insert('me', 'me'); | ||||||||||
| 66 | $trie->insert('met', 'met'); | ||||||||||
| 67 | $trie->insert('method', 'method'); | ||||||||||
| 68 | $trie->find('me') # ['me', 'met', 'method'] | ||||||||||
| 69 | $trie->find('met') # ['met', 'method'] | ||||||||||
| 70 | $trie->find('meth') # ['method'] | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | sub new | ||||||||||
| 75 | { | ||||||||||
| 76 | 0 | ▶ | my ($class) = @_; | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 77 | |||||||||||
| 78 | 0 | my %self = (root => PLS::Node->new()); | |||||||||
| 79 | |||||||||||
| 80 | 0 | return bless \%self, $class; | |||||||||
| 81 | } ## end sub new | ||||||||||
| 82 | |||||||||||
| 83 | sub find | ||||||||||
| 84 | { | ||||||||||
| 85 | 0 | ▶ | my ($self, $prefix) = @_; | ||||||||
|
Subroutine
find: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 86 | |||||||||||
| 87 | 0 | my @prefix = split //, $prefix; | |||||||||
| 88 | 0 | my $node = $self->_get_node(\@prefix); | |||||||||
| 89 | 0 | ▶ | return unless (ref $node eq 'PLS::Node'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | return $node->collect(\@prefix); | |||||||||
| 91 | } ## end sub find | ||||||||||
| 92 | |||||||||||
| 93 | sub _get_node | ||||||||||
| 94 | { | ||||||||||
| 95 | 0 | ▶ | my ($self, $key) = @_; | ||||||||
|
Subroutine
_get_node: not called CC=3 SCAR=24.8
| |||||||||||
| 96 | |||||||||||
| 97 | 0 | my $node = $self->{root}; | |||||||||
| 98 | |||||||||||
| 99 | 0 | foreach my $char (@$key) | |||||||||
| 100 | { | ||||||||||
| 101 | 0 | ▶ | if (ref $node->{children}{$char} eq 'PLS::Node') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | { | ||||||||||
| 103 | 0 | $node = $node->{children}{$char}; | |||||||||
| 104 | } | ||||||||||
| 105 | else | ||||||||||
| 106 | { | ||||||||||
| 107 | 0 | return; | |||||||||
| 108 | } | ||||||||||
| 109 | } ## end foreach my $char (@$key) | ||||||||||
| 110 | |||||||||||
| 111 | 0 | return $node; | |||||||||
| 112 | } ## end sub _get_node | ||||||||||
| 113 | |||||||||||
| 114 | sub find_node | ||||||||||
| 115 | { | ||||||||||
| 116 | 0 | ▶ | my ($self, $key) = @_; | ||||||||
|
Subroutine
find_node: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 117 | |||||||||||
| 118 | 0 | my @chars = split //, $key; | |||||||||
| 119 | 0 | return $self->_get_node(\@chars); | |||||||||
| 120 | } ## end sub find_node | ||||||||||
| 121 | |||||||||||
| 122 | sub insert | ||||||||||
| 123 | { | ||||||||||
| 124 | 0 | ▶ | my ($self, $key, $value) = @_; | ||||||||
|
Subroutine
insert: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 125 | |||||||||||
| 126 | 0 | my $node = $self->{root}; | |||||||||
| 127 | |||||||||||
| 128 | 0 | foreach my $char (split //, $key) | |||||||||
| 129 | { | ||||||||||
| 130 | 0 | ▶ | if (ref $node->{children}{$char} ne 'PLS::Node') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | { | ||||||||||
| 132 | 0 | $node->{children}{$char} = PLS::Node->new(); | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | $node = $node->{children}{$char}; | |||||||||
| 136 | } ## end foreach my $char (split //,...) | ||||||||||
| 137 | |||||||||||
| 138 | 0 | $node->{value} = $value; | |||||||||
| 139 | } ## end sub insert | ||||||||||
| 140 | |||||||||||
| 141 | sub delete | ||||||||||
| 142 | { | ||||||||||
| 143 | 0 | ▶ | my ($self, $key) = @_; | ||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 144 | |||||||||||
| 145 | 0 | my @chars = split //, $key; | |||||||||
| 146 | 0 | my $last_char = pop @chars; | |||||||||
| 147 | 0 | my $node = $self->_get_node(\@chars); | |||||||||
| 148 | 0 | ▶ | return unless (ref $node eq 'PLS::Node'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 0 | delete $node->{children}{$last_char}; | |||||||||
| 150 | } ## end sub delete | ||||||||||
| 151 | |||||||||||
| 152 | 1 | 1; | |||||||||