| 1 | package Text::Trie; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | require 5.000; | |||||||||||||||||||||||||||
| 5 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 8 | 1 | @EXPORT_OK = qw(Trie walkTrie); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | $step = 1 unless defined $step; # Length of unit. All the arguments should | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 11 | # have length that is multiple of this. | ||||||||||||||||||||||||||||
| 12 | # Length of any cell in trie will be multiple | ||||||||||||||||||||||||||||
| 13 | # too. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | sub Trie { | ||||||||||||||||||||||||||
|
Subroutine
Trie: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 8 | my @list = @_; | |||||||||||||||||||||||||||
| 17 | 8 | ▶ | return shift if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 8 | my %first; | |||||||||||||||||||||||||||
| 19 | my @ans; | ||||||||||||||||||||||||||||
| 20 | 8 | foreach (@list) { | |||||||||||||||||||||||||||
| 21 | 24 | $c = substr $_, 0, $step; | |||||||||||||||||||||||||||
| 22 | 24 | ▶ | $first{$c} = [] unless defined $first{$c}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 24 | push @{$first{$c}}, $_; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 8 | foreach (keys %first) { | |||||||||||||||||||||||||||
| 26 | # Find common substring | ||||||||||||||||||||||||||||
| 27 | 17 | my $substr = $first{$_}->[0]; | |||||||||||||||||||||||||||
| 28 | 17 | ▶ | (push @ans, $substr), next if @{$first{$_}} == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 5 | $l = length($substr) / $step * $step; | |||||||||||||||||||||||||||
| 30 | 5 | foreach (@{$first{$_}}) { | |||||||||||||||||||||||||||
| 31 | 12 | ▶ | $l -= $step while substr($_, 0, $l) ne substr($substr, 0, $l); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 5 | $substr = substr $substr, 0, $l; | |||||||||||||||||||||||||||
| 34 | # Return value | ||||||||||||||||||||||||||||
| 35 | 5 | @list = map {substr $_, $l} @{$first{$_}}; | |||||||||||||||||||||||||||
| 36 | 5 | push @ans, [$substr, Trie(@list)]; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 8 | @ans; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 8 | ▶ | sub walkTrie { | ||||||||||||||||||||||||||
|
Subroutine
walkTrie: called CC=12 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 8 | my ($singlesub,$headsub,$notsinglesub,$sepsub,$opensub,$closesub,@trie) = @_; | |||||||||||||||||||||||||||
| 43 | 8 | my $num = 0; | |||||||||||||||||||||||||||
| 44 | 8 | foreach (@trie) { | |||||||||||||||||||||||||||
| 45 | 17 | ▶ | &$sepsub($_) if $num++ and defined $sepsub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$num++ and defined $sepsub
MC/DC50%1/2
$num++ and defined $sepsub
$num++
defined $sepsub
Truth table66%2/3
A and B
A= $num++B= defined $sepsub
| |||||||||||||||||||||||||||||
| 46 | 17 | ▶ | if (defined ref $_ and ref $_ eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined ref $_ and ref $_ eq "ARRAY"
MC/DC50%1/2
defined ref $_ and ref $_ eq "ARRAY"
defined ref $_
ref $_ eq "ARRAY"
Truth table66%2/3
A and B
A= defined ref $_B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | &$opensub($_) if defined $opensub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 5 | ▶ | &$headsub(@$_[0]) if defined $headsub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 5 | ▶ | if ($#$_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 5 | ▶ | &$notsinglesub($_) if defined $notsinglesub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | walkTrie($singlesub, $headsub, $notsinglesub, $sepsub, $opensub, | ||||||||||||||||||||||||||||
| 52 | 5 | $closesub, @{$_}[1 .. $#$_]); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 5 | ▶ | &$closesub($_) if defined $closesub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 12 | ▶ | &$singlesub($_) if defined $singlesub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | 1; | |||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||