| 1 | # Tree::Trie, a module implementing a trie data structure. | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | # A formal description of tries can be found at: | ||||||||||||||||||||||||||||||||||||||||||||
| 3 | # http://www.cs.queensu.ca/home/daver/235/Notes/Tries.pdf | ||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||
| 5 | package Tree::Trie; | ||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | our $VERSION = "1.9"; | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | # A handful of helpful constants | ||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use constant DEFAULT_END_MARKER => ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use constant BOOLEAN => 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use constant CHOOSE => 1; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use constant COUNT => 2; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use constant PREFIX => 3; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use constant EXACT => 4; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | ## Public methods begin here | ||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | # The constructor method. It's very simple. | ||||||||||||||||||||||||||||||||||||||||||||
| 24 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 16 | my($proto) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 16 | my($options) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 16 | ▶ | my($class) = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 16 | my($self) = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 16 | bless($self, $class); | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 16 | $self->{_MAINHASHREF} = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # These are default values | ||||||||||||||||||||||||||||||||||||||||||||
| 32 | 16 | $self->{_END} = &DEFAULT_END_MARKER; | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 16 | $self->{_DEEPSEARCH} = CHOOSE; | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 16 | $self->{_FREEZE_END} = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 16 | ▶ | unless ( defined($options) && (ref($options) eq "HASH") ) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options and ref $options eq "HASH"
MC/DC50%1/2
defined $options and ref $options eq "HASH"
defined $options
ref $options eq "HASH"
Truth table66%2/3
A and B
A= defined $optionsB= ref $options eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | $options = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | 16 | $self->deepsearch($options->{'deepsearch'}); | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 16 | ▶ | if (exists $options->{end_marker}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | $self->end_marker($options->{end_marker}); | |||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 42 | 16 | ▶ | if (exists $options->{freeze_end_marker}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $self->freeze_end_marker($options->{freeze_end_marker}); | |||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | 16 | return($self); | |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Sets the value of the end marker, for those people who think they know | ||||||||||||||||||||||||||||||||||||||||||||
| 49 | # better than Tree::Trie. Note it does not allow the setting of single | ||||||||||||||||||||||||||||||||||||||||||||
| 50 | # character end markers. | ||||||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | ▶ | sub end_marker { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
end_marker: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | ▶ | if ($_[0] && length $_[0] > 1) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and length $_[0] > 1
MC/DC50%1/2
$_[0] and length $_[0] > 1
$_[0]
length $_[0] > 1
Truth table66%2/3
A and B
A= $_[0]B= length $_[0] > 1
| |||||||||||||||||||||||||||||||||||||||||||||
| 54 | # If they decide to set a new end marker, we have to be sure to | ||||||||||||||||||||||||||||||||||||||||||||
| 55 | # go through and update all existing markers. | ||||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | my $newend = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | my @refs = ($self->{_MAINHASHREF}); | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | while (@refs) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 34 | my $ref = shift @refs; | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 34 | for my $key (keys %{$ref}) { | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 38 | ▶ | if ($key eq $self->{_END}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 9 | $ref->{$newend} = $ref->{$key}; | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 9 | delete $ref->{$key}; | |||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 66 | 29 | push(@refs, $ref->{$key}); | |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | $self->{_END} = $newend; | |||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | return $self->{_END}; | |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | # Sets the option to not attempt to update the end marker based on added | ||||||||||||||||||||||||||||||||||||||||||||
| 76 | # letters. | ||||||||||||||||||||||||||||||||||||||||||||
| 77 | # The above is the most awkward sentence I have ever written. | ||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub freeze_end_marker { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
freeze_end_marker: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | if (scalar @_) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if (shift) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | $self->{_FREEZE_END} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | $self->{_FREEZE_END} = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | return $self->{_FREEZE_END}; | |||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Sets the value of the deepsearch parameter. Can be passed either words | ||||||||||||||||||||||||||||||||||||||||||||
| 92 | # describing the parameter, or their numerical equivalents. Legal values | ||||||||||||||||||||||||||||||||||||||||||||
| 93 | # are: | ||||||||||||||||||||||||||||||||||||||||||||
| 94 | # boolean => 0 | ||||||||||||||||||||||||||||||||||||||||||||
| 95 | # choose => 1 | ||||||||||||||||||||||||||||||||||||||||||||
| 96 | # count => 2 | ||||||||||||||||||||||||||||||||||||||||||||
| 97 | # prefix => 3 | ||||||||||||||||||||||||||||||||||||||||||||
| 98 | # exact => 4 | ||||||||||||||||||||||||||||||||||||||||||||
| 99 | # See the POD for the 'lookup' method for details on this option. | ||||||||||||||||||||||||||||||||||||||||||||
| 100 | 28 | ▶ | sub deepsearch { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deepsearch: called CC=12 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 28 | my($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 28 | my($option) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 28 | ▶ | if(defined($option)) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 12 | ▶ | if ($option eq BOOLEAN || $option eq 'boolean') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition66%2/3
$option eq 0 or $option eq "boolean"
MC/DC50%1/2
$option eq 0 or $option eq "boolean"
$option eq 0
$option eq "boolean"
Truth table66%2/3
A or B
A= $option eq 0B= $option eq "boolean"
| |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | $self->{_DEEPSEARCH} = BOOLEAN; | |||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 107 | ▶ | elsif ($option eq CHOOSE || $option eq 'choose') { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$option eq 1 or $option eq "choose"
MC/DC50%1/2
$option eq 1 or $option eq "choose"
$option eq 1
$option eq "choose"
Truth table66%2/3
A or B
A= $option eq 1B= $option eq "choose"
| |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | $self->{_DEEPSEARCH} = CHOOSE; | |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 110 | ▶ | elsif ($option eq COUNT || $option eq 'count') { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$option eq 2 or $option eq "count"
MC/DC50%1/2
$option eq 2 or $option eq "count"
$option eq 2
$option eq "count"
Truth table66%2/3
A or B
A= $option eq 2B= $option eq "count"
| |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | $self->{_DEEPSEARCH} = COUNT; | |||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | ▶ | elsif ($option eq PREFIX || $option eq 'prefix') { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$option eq 3 or $option eq "prefix"
MC/DC50%1/2
$option eq 3 or $option eq "prefix"
$option eq 3
$option eq "prefix"
Truth table66%2/3
A or B
A= $option eq 3B= $option eq "prefix"
| |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | $self->{_DEEPSEARCH} = PREFIX; | |||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 116 | ▶ | elsif ($option eq EXACT || $option eq 'exact') { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$option eq 4 or $option eq "exact"
MC/DC0%0/2
$option eq 4 or $option eq "exact"
$option eq 4
$option eq "exact"
Truth table33%1/3
A or B
A= $option eq 4B= $option eq "exact"
| |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | $self->{_DEEPSEARCH} = EXACT; | |||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 120 | 28 | return $self->{_DEEPSEARCH}; | |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | # The add() method takes a list of words as arguments and attempts to add | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | # them to the trie. In list context, returns a list of words successfully | ||||||||||||||||||||||||||||||||||||||||||||
| 125 | # added. In scalar context, returns a count of these words. As of this | ||||||||||||||||||||||||||||||||||||||||||||
| 126 | # version, the only reason a word can fail to be added is if it is already | ||||||||||||||||||||||||||||||||||||||||||||
| 127 | # in the trie. Or, I suppose, if there was a bug. :) | ||||||||||||||||||||||||||||||||||||||||||||
| 128 | 13 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 13 | my($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 13 | my(@words) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | 13 | my @retarray; | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 13 | my $retnum = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Process each word... | ||||||||||||||||||||||||||||||||||||||||||||
| 136 | 13 | for my $word (@words) { | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # And just call the internal thingy for it. | ||||||||||||||||||||||||||||||||||||||||||||
| 138 | 53 | ▶ | if ($self->_add_internal($word, undef)) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Updating return values as needed | ||||||||||||||||||||||||||||||||||||||||||||
| 140 | 50 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | push(@retarray,$word); | |||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 144 | 50 | $retnum++; | |||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 148 | # When done, return results. | ||||||||||||||||||||||||||||||||||||||||||||
| 149 | 13 | ▶ | return (wantarray ? @retarray : $retnum); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | # add_data() takes a hash of word => data pairs, adds the words to the trie and | ||||||||||||||||||||||||||||||||||||||||||||
| 153 | # associates the data to those words. | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | ▶ | sub add_data { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_data: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 7 | my($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 7 | my($retnum, @retarray); | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 7 | my $word = ""; | |||||||||||||||||||||||||||||||||||||||||||
| 158 | # Making sure that we've gotten data in pairs. Can't just turn @_ | ||||||||||||||||||||||||||||||||||||||||||||
| 159 | # into %data, because that would stringify arrayrefs | ||||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | ▶ | while(defined($word = shift) && @_) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined($word = shift()) and @_
MC/DC50%1/2
defined($word = shift()) and @_
defined($word = shift())
@_
Truth table66%2/3
A and B
A= defined($word = shift())B= @_
| |||||||||||||||||||||||||||||||||||||||||||||
| 161 | # This also just uses the internal add method. | ||||||||||||||||||||||||||||||||||||||||||||
| 162 | 13 | ▶ | if ($self->_add_internal($word, shift())) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 12 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | push(@retarray, $word); | |||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 167 | 12 | $retnum++; | |||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | ▶ | return @retarray if wantarray; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 7 | return $retnum; | |||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | # add_all() takes one or more other tries and adds all of their entries | ||||||||||||||||||||||||||||||||||||||||||||
| 176 | # to the trie. If both tries have data stored for the same key, the data | ||||||||||||||||||||||||||||||||||||||||||||
| 177 | # from the trie on which this method was invoked will be overwritten. I can't | ||||||||||||||||||||||||||||||||||||||||||||
| 178 | # think of anything useful to return from this method, so it has no return | ||||||||||||||||||||||||||||||||||||||||||||
| 179 | # value. If you can think of anything that would make sense, please let me | ||||||||||||||||||||||||||||||||||||||||||||
| 180 | # know. | ||||||||||||||||||||||||||||||||||||||||||||
| 181 | # This idea and most of its implementation come from Aaron Stone. | ||||||||||||||||||||||||||||||||||||||||||||
| 182 | # Thanks! | ||||||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | ▶ | sub add_all { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_all: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | for my $trie (@_) { | |||||||||||||||||||||||||||||||||||||||||||
| 186 | my $ignore_end = ( | ||||||||||||||||||||||||||||||||||||||||||||
| 187 | $self->{_FREEZE_END} || | ||||||||||||||||||||||||||||||||||||||||||||
| 188 | ($self->{_END} eq $trie->{_END}) | ||||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'_FREEZE_END'} || $self->{'_END'} eq $trie->{'_END'}
MC/DC50%1/2
$self->{'_FREEZE_END'} || $self->{'_END'} eq $trie->{'_END'}
$self->{'_FREEZE_END'}
$self->{'_END'} eq $trie->{'_END'}
Truth table66%2/3
A || B
A= $self->{'_FREEZE_END'}B= $self->{'_END'} eq $trie->{'_END'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 190 | my @nodepairs = ({ | ||||||||||||||||||||||||||||||||||||||||||||
| 191 | from => $trie->{_MAINHASHREF}, | ||||||||||||||||||||||||||||||||||||||||||||
| 192 | to => $self->{_MAINHASHREF}, | ||||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | }); | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | ▶ | while (scalar @nodepairs) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 34 | my $np = pop @nodepairs; | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 34 | for my $letter (keys %{$np->{from}}) { | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 39 | ▶ | unless ($ignore_end) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 9 | ▶ | if ($letter eq $self->{_END}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | $self->end_marker($self->_gen_new_marker( | |||||||||||||||||||||||||||||||||||||||||||
| 200 | bad => [$letter], | ||||||||||||||||||||||||||||||||||||||||||||
| 201 | )); | ||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 204 | 39 | ▶ | if ($letter eq $trie->{_END}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 7 | $np->{to}{$self->{_END}} = $np->{from}{$trie->{_END}}; | |||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 207 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 208 | 32 | ▶ | unless (exists $np->{to}{$letter}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 209 | 24 | $np->{to}{$letter} = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 211 | push @nodepairs, { | ||||||||||||||||||||||||||||||||||||||||||||
| 212 | from => $np->{from}{$letter}, | ||||||||||||||||||||||||||||||||||||||||||||
| 213 | 32 | to => $np->{to}->{$letter}, | |||||||||||||||||||||||||||||||||||||||||||
| 214 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | # delete_data() takes a list of words in the trie and deletes the associated | ||||||||||||||||||||||||||||||||||||||||||||
| 222 | # data from the internal data store. In list context, returns a list of words | ||||||||||||||||||||||||||||||||||||||||||||
| 223 | # whose associated data have been removed -- in scalar context, returns a count | ||||||||||||||||||||||||||||||||||||||||||||
| 224 | # thereof. | ||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | sub delete_data { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_data: called CC=8 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | my($self, @words) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | my($retnum, @retarray) = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | my @letters; | |||||||||||||||||||||||||||||||||||||||||||
| 229 | # Process each word... | ||||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | for my $word (@words) { | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 3 | ▶ | if (ref($word) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | @letters = (@{$word}); | |||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 234 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | @letters = split(//, $word); | |||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 237 | 3 | my $ref = $self->{_MAINHASHREF}; | |||||||||||||||||||||||||||||||||||||||||||
| 238 | # Walk down the tree... | ||||||||||||||||||||||||||||||||||||||||||||
| 239 | 3 | for my $letter (@letters) { | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 8 | ▶ | if ($ref->{$letter}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 7 | $ref = $ref->{$letter}; | |||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 243 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 244 | # This will cause the test right after this loop to fail and | ||||||||||||||||||||||||||||||||||||||||||||
| 245 | # skip the the next word -- we want that because if we're here | ||||||||||||||||||||||||||||||||||||||||||||
| 246 | # it means the word isn't in the trie. | ||||||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | $ref = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | next unless (exists $ref->{$self->{_END}}); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 252 | # This is all we need to do to clear out the data | ||||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | $ref->{$self->{_END}} = undef; | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | push(@retarray, $word); | |||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 257 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | $retnum++; | |||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | return @retarray; | |||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 264 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | return $retnum; | |||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | # The lookup() method searches for words (or beginnings of words) in the trie. | ||||||||||||||||||||||||||||||||||||||||||||
| 270 | # It takes a single word as an argument and, in list context, returns a list | ||||||||||||||||||||||||||||||||||||||||||||
| 271 | # of all the words in the trie which begin with the given word. In scalar | ||||||||||||||||||||||||||||||||||||||||||||
| 272 | # context, the return value depends on the value of the deepsearch parameter. | ||||||||||||||||||||||||||||||||||||||||||||
| 273 | # An optional second argument is available: This should be a numerical | ||||||||||||||||||||||||||||||||||||||||||||
| 274 | # argument, and specifies 2 things: first, that you want only word suffixes | ||||||||||||||||||||||||||||||||||||||||||||
| 275 | # to be returned, and second, the maximum length of those suffices. All | ||||||||||||||||||||||||||||||||||||||||||||
| 276 | # other configurations still apply. See the POD on this method for more | ||||||||||||||||||||||||||||||||||||||||||||
| 277 | # details. | ||||||||||||||||||||||||||||||||||||||||||||
| 278 | 37 | ▶ | sub lookup { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 37 | my($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 37 | my($word) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 281 | # This is the argument for doing suffix lookup. | ||||||||||||||||||||||||||||||||||||||||||||
| 282 | 37 | my($suff_length) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | # Abstraction is kind of cool | ||||||||||||||||||||||||||||||||||||||||||||
| 285 | 37 | return $self->_lookup_internal( | |||||||||||||||||||||||||||||||||||||||||||
| 286 | word => $word, | ||||||||||||||||||||||||||||||||||||||||||||
| 287 | suff_len => $suff_length, | ||||||||||||||||||||||||||||||||||||||||||||
| 288 | want_arr => wantarray(), | ||||||||||||||||||||||||||||||||||||||||||||
| 289 | data => 0, | ||||||||||||||||||||||||||||||||||||||||||||
| 290 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||
| 293 | # lookup_data() works basically the same as lookup, with the following | ||||||||||||||||||||||||||||||||||||||||||||
| 294 | # exceptions -- in list context, returns a hash of ward => data pairings, | ||||||||||||||||||||||||||||||||||||||||||||
| 295 | # and in scalar context, wherever it would return a word, it will instead | ||||||||||||||||||||||||||||||||||||||||||||
| 296 | # return the datum associated with that word. Note that, depending on | ||||||||||||||||||||||||||||||||||||||||||||
| 297 | # the deepsearch setting, lookup_data and lookup may return exactly the | ||||||||||||||||||||||||||||||||||||||||||||
| 298 | # same scalar context. | ||||||||||||||||||||||||||||||||||||||||||||
| 299 | 11 | ▶ | sub lookup_data { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 300 | 11 | my($self, $word) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | 11 | return $self->_lookup_internal( | |||||||||||||||||||||||||||||||||||||||||||
| 303 | word => $word, | ||||||||||||||||||||||||||||||||||||||||||||
| 304 | want_arr => wantarray(), | ||||||||||||||||||||||||||||||||||||||||||||
| 305 | data => 1, | ||||||||||||||||||||||||||||||||||||||||||||
| 306 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | # The remove() method takes a list of words and, surprisingly, removes them | ||||||||||||||||||||||||||||||||||||||||||||
| 310 | # from the trie. It returns, in scalar context, the number of words removed. | ||||||||||||||||||||||||||||||||||||||||||||
| 311 | # In list context, returns a list of the words removed. As of now, the only | ||||||||||||||||||||||||||||||||||||||||||||
| 312 | # reason a word would fail to be removed is if it's not in the trie in the | ||||||||||||||||||||||||||||||||||||||||||||
| 313 | # first place. Or, again, if there's a bug... :) | ||||||||||||||||||||||||||||||||||||||||||||
| 314 | 3 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=17 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 315 | 3 | my($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 3 | my(@words) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | 3 | my($letter,$ref) = ("","",""); | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | my(@letters,@ldn,@retarray); | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | my($retnum) = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 321 | # The basic strategy here is as follows: | ||||||||||||||||||||||||||||||||||||||||||||
| 322 | ## | ||||||||||||||||||||||||||||||||||||||||||||
| 323 | # We walk down the trie one node at a time. If at any point, we see that a | ||||||||||||||||||||||||||||||||||||||||||||
| 324 | # node can be deleted (that is, its only child is the one which continues the | ||||||||||||||||||||||||||||||||||||||||||||
| 325 | # word we're deleting) then we mark it as the 'last deleteable'. If at any | ||||||||||||||||||||||||||||||||||||||||||||
| 326 | # point we find a node which *cannot* be deleted (it has more children other | ||||||||||||||||||||||||||||||||||||||||||||
| 327 | # than the one for the word we're working on), then we unmark our 'last | ||||||||||||||||||||||||||||||||||||||||||||
| 328 | # deleteable' from before. Once done, delete from the last deleteable node | ||||||||||||||||||||||||||||||||||||||||||||
| 329 | # down. | ||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||
| 331 | 3 | for my $word (@words) { | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 5 | ▶ | if (ref($word) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | @letters = (@{$word}); | |||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 335 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 336 | 4 | @letters = split('',$word); | |||||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 338 | # For each word, we need to put the leaf node entry at the end of the list | ||||||||||||||||||||||||||||||||||||||||||||
| 339 | # of letters. We then reset the starting ref, and @ldn, which stands for | ||||||||||||||||||||||||||||||||||||||||||||
| 340 | # 'last deleteable node'. It contains the ref of the hash and the key to | ||||||||||||||||||||||||||||||||||||||||||||
| 341 | # be deleted. It does not seem possible to store a value passable to | ||||||||||||||||||||||||||||||||||||||||||||
| 342 | # the 'delete' builtin in a scalar, so we're forced to do this. | ||||||||||||||||||||||||||||||||||||||||||||
| 343 | 5 | push(@letters,$self->{_END}); | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 5 | $ref = $self->{_MAINHASHREF}; | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 5 | @ldn = (); | |||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||
| 347 | # This is a special case, if the first letter of the word is the only | ||||||||||||||||||||||||||||||||||||||||||||
| 348 | # key of the main hash. I might not really need it, but this works as | ||||||||||||||||||||||||||||||||||||||||||||
| 349 | # it is. | ||||||||||||||||||||||||||||||||||||||||||||
| 350 | 5 | ▶ | if (((scalar keys(%{ $ref })) == 1) && (exists $ref->{$letters[0]})) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar keys %{$ref;} == 1 and exists $$ref{$letters[0]}
MC/DC50%1/2
scalar keys %{$ref;} == 1 and exists $$ref{$letters[0]}
scalar keys %{$ref;} == 1
exists $$ref{$letters[0]}
Truth table66%2/3
A and B
A= scalar keys %{$ref;} == 1B= exists $$ref{$letters[0]}
| |||||||||||||||||||||||||||||||||||||||||||||
| 351 | 1 | @ldn = ($ref); | |||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 353 | # And now we go down the trie, as described above. | ||||||||||||||||||||||||||||||||||||||||||||
| 354 | 5 | ▶ | while (defined($letter = shift(@letters))) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 355 | # We break out if we're at the end, or if we're run out of trie before | ||||||||||||||||||||||||||||||||||||||||||||
| 356 | # finding the end of the word -- that is, if the word isn't in the | ||||||||||||||||||||||||||||||||||||||||||||
| 357 | # trie. | ||||||||||||||||||||||||||||||||||||||||||||
| 358 | 25 | ▶ | last if ($letter eq $self->{_END}); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 359 | 20 | ▶ | last unless exists($ref->{$letter}); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 360 | 20 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar keys %{$$ref{$letter};} == 1 and exists $ref->{$letter}{$letters[0]}
MC/DC50%1/2
scalar keys %{$$ref{$letter};} == 1 and exists $ref->{$letter}{$letters[0]}
scalar keys %{$$ref{$letter};} == 1
exists $ref->{$letter}{$letters[0]}
Truth table66%2/3
A and B
A= scalar keys %{$$ref{$letter};} == 1B= exists $ref->{$letter}{$letters[0]}
| |||||||||||||||||||||||||||||||||||||||||||||
| 361 | 20 | scalar keys(%{ $ref->{$letter} }) == 1 && | |||||||||||||||||||||||||||||||||||||||||||
| 362 | exists $ref->{$letter}{$letters[0]} | ||||||||||||||||||||||||||||||||||||||||||||
| 363 | ) { | ||||||||||||||||||||||||||||||||||||||||||||
| 364 | 18 | ▶ | unless (scalar @ldn) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 365 | 4 | @ldn = ($ref,$letter); | |||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 368 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 369 | 2 | @ldn = (); | |||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 371 | 20 | $ref = $ref->{$letter}; | |||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 373 | # If we broke out and there were still letters left in @letters, then the | ||||||||||||||||||||||||||||||||||||||||||||
| 374 | # word must not be in the trie. Furthermore, if we got all the way to | ||||||||||||||||||||||||||||||||||||||||||||
| 375 | # the end, but there's no leaf node, the word must not be in the trie. | ||||||||||||||||||||||||||||||||||||||||||||
| 376 | 5 | ▶ | next if (scalar @letters); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 377 | 5 | ▶ | next unless (exists($ref->{$self->{_END}})); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 378 | # If @ldn is empty, then the only deleteable node is the leaf node, so | ||||||||||||||||||||||||||||||||||||||||||||
| 379 | # we set this up. | ||||||||||||||||||||||||||||||||||||||||||||
| 380 | 5 | ▶ | if (scalar @ldn == 0) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 381 | 1 | @ldn = ($ref,$self->{_END}); | |||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 383 | # If there's only one entry in @ldn, then it's the ref of the top of our | ||||||||||||||||||||||||||||||||||||||||||||
| 384 | # Trie. If that's marked as deleteable, then we can just nuke the entire | ||||||||||||||||||||||||||||||||||||||||||||
| 385 | # hash. | ||||||||||||||||||||||||||||||||||||||||||||
| 386 | 5 | ▶ | if (scalar @ldn == 1) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 387 | 1 | %{ $ldn[0] } = (); | |||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 389 | # Otherwise, we just delete the key we want to. | ||||||||||||||||||||||||||||||||||||||||||||
| 390 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 391 | 4 | delete($ldn[0]->{$ldn[1]}); | |||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 393 | # And then just return stuff. | ||||||||||||||||||||||||||||||||||||||||||||
| 394 | 5 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | push (@retarray,$word); | |||||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 397 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 398 | 5 | $retnum++; | |||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 401 | 3 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | return @retarray; | |||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 404 | 3 | return $retnum; | |||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | ## These are PRIVATE METHODS. Don't call them directly unless you really | ||||||||||||||||||||||||||||||||||||||||||||
| 408 | # know what you're doing, or you enjoy things working funny. | ||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||
| 410 | # The _walktree() sub takes a word beginning and a hashref (hopefully to a trie) | ||||||||||||||||||||||||||||||||||||||||||||
| 411 | # and walks down the trie, gathering all of the word endings and retuning them | ||||||||||||||||||||||||||||||||||||||||||||
| 412 | # appended to the word beginning. | ||||||||||||||||||||||||||||||||||||||||||||
| 413 | 238 | ▶ | sub _walktree { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walktree: called CC=13 SCAR=25.6
| |||||||||||||||||||||||||||||||||||||||||||||
| 414 | 238 | my($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 238 | my $word = $args{word}; | |||||||||||||||||||||||||||||||||||||||||||
| 416 | 238 | my $ref = $args{ref}; | |||||||||||||||||||||||||||||||||||||||||||
| 417 | # These 2 arguments are used to control how far down the tree this | ||||||||||||||||||||||||||||||||||||||||||||
| 418 | # path will go. | ||||||||||||||||||||||||||||||||||||||||||||
| 419 | # This first argument is passed in by external subs | ||||||||||||||||||||||||||||||||||||||||||||
| 420 | 238 | ▶ | my $suffix_length = $args{suf_len} || 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'suf_len'} || 0
MC/DC100%1/1
$args{'suf_len'} || 0
$args{'suf_len'}
Truth table100%2/2
A
A= $args{'suf_len'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 421 | # And this one is used only by the recursive calls. | ||||||||||||||||||||||||||||||||||||||||||||
| 422 | 238 | ▶ | my $walked_suffix_length = $args{walked} || 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'walked'} || 0
MC/DC100%1/1
$args{'walked'} || 0
$args{'walked'}
Truth table100%2/2
A
A= $args{'walked'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||
| 424 | 238 | my $wantref = ref($word) eq 'ARRAY'; | |||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | 238 | my($key) = ""; | |||||||||||||||||||||||||||||||||||||||||||
| 427 | 238 | my(@retarray) = (); | |||||||||||||||||||||||||||||||||||||||||||
| 428 | 238 | my($ret) = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||
| 430 | # For some reason, I used to think this was complicated and had a lot of | ||||||||||||||||||||||||||||||||||||||||||||
| 431 | # stupid, useless code here. It's a lot simpler now. If the key we find | ||||||||||||||||||||||||||||||||||||||||||||
| 432 | # is our magic reference, then we just give back the word. Otherwise, we | ||||||||||||||||||||||||||||||||||||||||||||
| 433 | # walk down the new subtree we've discovered. | ||||||||||||||||||||||||||||||||||||||||||||
| 434 | 238 | foreach $key (keys %{ $ref }) { | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 291 | ▶ | if ($key eq $self->{_END}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 436 | 61 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 437 | 36 | push(@retarray,$word); | |||||||||||||||||||||||||||||||||||||||||||
| 438 | 36 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 439 | 3 | push(@retarray, $ref->{$key}); | |||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 442 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 443 | 25 | $ret++; | |||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 445 | 61 | next; | |||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 447 | 230 | ▶ | my $nextval = $wantref ? [(@{$word}), $key] : $word . $key; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 448 | # If we've reached the max depth we need to travel for the suffix (if | ||||||||||||||||||||||||||||||||||||||||||||
| 449 | # specified), then stop and collect everything up. | ||||||||||||||||||||||||||||||||||||||||||||
| 450 | 230 | ▶ | if ($suffix_length > 0 && ($suffix_length - $walked_suffix_length == 1)) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$suffix_length > 0 and $suffix_length - $walked_suffix_length == 1
MC/DC100%2/2
$suffix_length > 0 and $suffix_length - $walked_suffix_length == 1
$suffix_length > 0
$suffix_length - $walked_suffix_length == 1
Truth table100%3/3
A and B
A= $suffix_length > 0B= $suffix_length - $walked_suffix_length == 1
| |||||||||||||||||||||||||||||||||||||||||||||
| 451 | 11 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 452 | 1 | push(@retarray, $nextval); | |||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 454 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 455 | 10 | $ret++; | |||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 458 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 459 | # Look, recursion! | ||||||||||||||||||||||||||||||||||||||||||||
| 460 | my %arguments = ( | ||||||||||||||||||||||||||||||||||||||||||||
| 461 | word => $nextval, | ||||||||||||||||||||||||||||||||||||||||||||
| 462 | 'ref' => $ref->{$key}, | ||||||||||||||||||||||||||||||||||||||||||||
| 463 | suf_len => $suffix_length, | ||||||||||||||||||||||||||||||||||||||||||||
| 464 | walked => $walked_suffix_length + 1, | ||||||||||||||||||||||||||||||||||||||||||||
| 465 | data => $args{data}, | ||||||||||||||||||||||||||||||||||||||||||||
| 466 | 219 | ); | |||||||||||||||||||||||||||||||||||||||||||
| 467 | 219 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 468 | 142 | push(@retarray, $self->_walktree(%arguments)); | |||||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 470 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 471 | 77 | $ret += scalar $self->_walktree(%arguments); | |||||||||||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 475 | 238 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 476 | 151 | return @retarray; | |||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 478 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 479 | 87 | return $ret; | |||||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||
| 483 | # This code used to use some fairly hoary recursive code which caused it to | ||||||||||||||||||||||||||||||||||||||||||||
| 484 | # run fairly slowly, mainly due to the relatively slow way that perl handles | ||||||||||||||||||||||||||||||||||||||||||||
| 485 | # OO method invocation. This was pointed out to me by Justin Hicks, and he | ||||||||||||||||||||||||||||||||||||||||||||
| 486 | # helped me fix it up, to be quite a bit more reasonable now. | ||||||||||||||||||||||||||||||||||||||||||||
| 487 | 48 | ▶ | sub _lookup_internal { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lookup_internal: called CC=46 SCAR=40.7
| |||||||||||||||||||||||||||||||||||||||||||||
| 488 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 48 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 490 | 48 | my($ref) = $self->{_MAINHASHREF}; | |||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | 48 | my($letter, $nextletter) = ("", ""); | |||||||||||||||||||||||||||||||||||||||||||
| 493 | 48 | my(@letters) = (); | |||||||||||||||||||||||||||||||||||||||||||
| 494 | 48 | my(@retarray) = (); | |||||||||||||||||||||||||||||||||||||||||||
| 495 | 48 | my($wantref) = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||
| 497 | 48 | my $word = $args{word}; | |||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||
| 499 | # Here we split the word up into letters in the appropriate way. | ||||||||||||||||||||||||||||||||||||||||||||
| 500 | 48 | ▶ | if (ref($word) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 501 | 5 | @letters = (@{$word}); | |||||||||||||||||||||||||||||||||||||||||||
| 502 | # Keeping track of what kind of word it was. | ||||||||||||||||||||||||||||||||||||||||||||
| 503 | 5 | $wantref = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 505 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 506 | 43 | @letters = split('',$word); | |||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||
| 509 | # These three are to keep hold of possibly returned values. | ||||||||||||||||||||||||||||||||||||||||||||
| 510 | 48 | ▶ | my $lastword = $wantref ? [] : ""; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 511 | 48 | my $lastwordref = undef; | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 48 | ▶ | my $pref = $wantref ? [] : ""; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||||
| 514 | # Like everything else, we step across each letter. | ||||||||||||||||||||||||||||||||||||||||||||
| 515 | 48 | ▶ | while(defined($letter = shift(@letters))) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 516 | # This is to keep track of stuff for the "prefix" version of deepsearch. | ||||||||||||||||||||||||||||||||||||||||||||
| 517 | 130 | ▶ | if ($self->{_DEEPSEARCH} == PREFIX && !$args{want_arr}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"_DEEPSEARCH"} == 3 and not $args{"want_arr"}
MC/DC50%1/2
$self->{"_DEEPSEARCH"} == 3 and not $args{"want_arr"}
$self->{"_DEEPSEARCH"} == 3
not $args{"want_arr"}
Truth table66%2/3
A and not(B)
A= $self->{"_DEEPSEARCH"} == 3B= not $args{"want_arr"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 518 | 72 | ▶ | if (exists $ref->{$self->{_END}}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 519 | # The "data" argument tells us if we want to return the word | ||||||||||||||||||||||||||||||||||||||||||||
| 520 | # or the data associated with it. | ||||||||||||||||||||||||||||||||||||||||||||
| 521 | 10 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 522 | 5 | $lastwordref = $ref; | |||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 524 | elsif ($wantref) { | ||||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | push(@{$lastword}, @{$pref}); | |||||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 527 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 528 | 5 | $lastword .= $pref; | |||||||||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 530 | 10 | ▶ | $pref = $wantref ? [] : ""; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 532 | 72 | ▶ | unless ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 533 | 36 | ▶ | if ($wantref) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | push(@{$pref}, $letter); | |||||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 536 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 537 | 36 | $pref .= $letter; | |||||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 541 | # If, at any point, we find that we've run out of tree before we've run out | ||||||||||||||||||||||||||||||||||||||||||||
| 542 | # of word, then there is nothing in the trie that begins with the input | ||||||||||||||||||||||||||||||||||||||||||||
| 543 | # word, so we return appropriately. | ||||||||||||||||||||||||||||||||||||||||||||
| 544 | 130 | ▶ | unless (exists $ref->{$letter}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 545 | # Array case. | ||||||||||||||||||||||||||||||||||||||||||||
| 546 | 9 | ▶ | if ($args{want_arr}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | return (); | |||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 549 | # "count" case. | ||||||||||||||||||||||||||||||||||||||||||||
| 550 | elsif ($self->{_DEEPSEARCH} == COUNT) { | ||||||||||||||||||||||||||||||||||||||||||||
| 551 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 553 | # "prefix" case. | ||||||||||||||||||||||||||||||||||||||||||||
| 554 | elsif ($self->{_DEEPSEARCH} == PREFIX) { | ||||||||||||||||||||||||||||||||||||||||||||
| 555 | 4 | ▶ | if ($args{data} && $lastwordref) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"data"} and $lastwordref
MC/DC50%1/2
$args{"data"} and $lastwordref
$args{"data"}
$lastwordref
Truth table66%2/3
A and B
A= $args{"data"}B= $lastwordref
| |||||||||||||||||||||||||||||||||||||||||||||
| 556 | 2 | return $lastwordref->{$self->{_END}}; | |||||||||||||||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 558 | 0 | ▶ | if (($wantref && scalar @{$lastword}) || length $lastword) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$wantref and scalar @{$lastword;} or length $lastword
$wantref and scalar @{$lastword;}
MC/DC0%0/2
$wantref and scalar @{$lastword;} or length $lastword
$wantref
length $lastword
Truth table33%1/3
A or B
A= $wantrefB= length $lastword
| |||||||||||||||||||||||||||||||||||||||||||||
| 559 | 2 | return $lastword; | |||||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 563 | # All other deepsearch cases are the same. | ||||||||||||||||||||||||||||||||||||||||||||
| 564 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 565 | 3 | return undef; | |||||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 568 | # If the letter is there, we just walk one step down the trie. | ||||||||||||||||||||||||||||||||||||||||||||
| 569 | 121 | $ref = $ref->{$letter}; | |||||||||||||||||||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 571 | # Once we've walked all the way down the tree to the end of the word we were | ||||||||||||||||||||||||||||||||||||||||||||
| 572 | # given, there are a few things that can be done, depending on the context | ||||||||||||||||||||||||||||||||||||||||||||
| 573 | # that the method was called in. | ||||||||||||||||||||||||||||||||||||||||||||
| 574 | 39 | ▶ | if ($args{want_arr}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 575 | # If they want an array, then we use the walktree subroutine to collect all | ||||||||||||||||||||||||||||||||||||||||||||
| 576 | # of the words beneath our current location in the trie, and return them. | ||||||||||||||||||||||||||||||||||||||||||||
| 577 | @retarray = $self->_walktree( | ||||||||||||||||||||||||||||||||||||||||||||
| 578 | # When fetching suffixes, we don't want to give the word begnning. | ||||||||||||||||||||||||||||||||||||||||||||
| 579 | word => $args{suff_len} ? "" : $word, | ||||||||||||||||||||||||||||||||||||||||||||
| 580 | 'ref' => $ref, | ||||||||||||||||||||||||||||||||||||||||||||
| 581 | suf_len => $args{suff_len}, | ||||||||||||||||||||||||||||||||||||||||||||
| 582 | data => $args{data}, | ||||||||||||||||||||||||||||||||||||||||||||
| 583 | 9 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 584 | 9 | return @retarray; | |||||||||||||||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 586 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 587 | 30 | ▶ | if ($self->{_DEEPSEARCH} == BOOLEAN) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 588 | # Here, the user only wants to know if any words in the trie begin | ||||||||||||||||||||||||||||||||||||||||||||
| 589 | # with their word, so that's what we give them. | ||||||||||||||||||||||||||||||||||||||||||||
| 590 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 592 | elsif ($self->{_DEEPSEARCH} == EXACT) { | ||||||||||||||||||||||||||||||||||||||||||||
| 593 | # In this case, the user wants us to return something only if the | ||||||||||||||||||||||||||||||||||||||||||||
| 594 | # exact word exists in the trie, and undef otherwise. | ||||||||||||||||||||||||||||||||||||||||||||
| 595 | # This option only really makes sense with when looking up data, | ||||||||||||||||||||||||||||||||||||||||||||
| 596 | # as otherwise it's essentially the same as BOOLEAN, above, but it | ||||||||||||||||||||||||||||||||||||||||||||
| 597 | # doesn't hurt to allow it to work with normal lookup, either. | ||||||||||||||||||||||||||||||||||||||||||||
| 598 | # I'd initially left this out because I didn't see a use for it, but | ||||||||||||||||||||||||||||||||||||||||||||
| 599 | # thanks to Otmal Lendl for pointing out to me a situation in which | ||||||||||||||||||||||||||||||||||||||||||||
| 600 | # it would be helpful to have. | ||||||||||||||||||||||||||||||||||||||||||||
| 601 | 4 | ▶ | if (exists $ref->{$self->{_END}}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 602 | 2 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 603 | 1 | return $ref->{$self->{_END}}; | |||||||||||||||||||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 605 | 1 | return $word; | |||||||||||||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 607 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 609 | elsif ($self->{_DEEPSEARCH} == CHOOSE) { | ||||||||||||||||||||||||||||||||||||||||||||
| 610 | # If they want this, then we continue to walk down the trie, collecting | ||||||||||||||||||||||||||||||||||||||||||||
| 611 | # letters, until we find a leaf node, at which point we stop. Note that | ||||||||||||||||||||||||||||||||||||||||||||
| 612 | # this works properly if the exact word is in the trie. Yay. | ||||||||||||||||||||||||||||||||||||||||||||
| 613 | # Of course, making it work that way means that we tend to get shorter | ||||||||||||||||||||||||||||||||||||||||||||
| 614 | # words in choose... is this a bad thing? I dunno. | ||||||||||||||||||||||||||||||||||||||||||||
| 615 | 9 | ▶ | my($stub) = $wantref ? [] : ""; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 616 | 9 | ▶ | while (scalar keys %{$ref} && !exists $ref->{$self->{_END}}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar keys %{$ref;} and not exists $$ref{$self->{"_END"}}
MC/DC100%2/2
scalar keys %{$ref;} and not exists $$ref{$self->{"_END"}}
scalar keys %{$ref;}
not exists $$ref{$self->{"_END"}}
Truth table100%3/3
A and not(B)
A= scalar keys %{$ref;}B= not exists $$ref{$self->{"_END"}}
| |||||||||||||||||||||||||||||||||||||||||||||
| 617 | 9 | $nextletter = each(%{ $ref }); | |||||||||||||||||||||||||||||||||||||||||||
| 618 | # I need to call this to clear the each() call. Wish I didn't... | ||||||||||||||||||||||||||||||||||||||||||||
| 619 | 9 | keys(%{ $ref }); | |||||||||||||||||||||||||||||||||||||||||||
| 620 | 9 | ▶ | if ($wantref) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | push(@{$stub}, $nextletter); | |||||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 623 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 624 | 9 | $stub .= $nextletter; | |||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 626 | 9 | $ref = $ref->{$nextletter}; | |||||||||||||||||||||||||||||||||||||||||||
| 627 | # If we're doing suffixes, bail out early once it's the right length. | ||||||||||||||||||||||||||||||||||||||||||||
| 628 | 9 | ▶ | if ($args{suff_len}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | ▶ | my $cmpr = $wantref ? scalar @{$stub} : length $stub; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 630 | 7 | ▶ | last if $cmpr == $args{suff_len}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 633 | 9 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 634 | 4 | return $ref->{$self->{_END}}; | |||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 636 | # If they've specified a suffix length, then they don't want the | ||||||||||||||||||||||||||||||||||||||||||||
| 637 | # beginning part of the word. | ||||||||||||||||||||||||||||||||||||||||||||
| 638 | 5 | ▶ | if ($args{suff_len}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 639 | 3 | return $stub; | |||||||||||||||||||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 641 | # Otherwise, they do. | ||||||||||||||||||||||||||||||||||||||||||||
| 642 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | ▶ | return $wantref ? [@{$word}, @{$stub}] : $word . $stub; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 646 | elsif ($self->{_DEEPSEARCH} == COUNT) { | ||||||||||||||||||||||||||||||||||||||||||||
| 647 | # Here, the user simply wants a count of words in the trie that begin | ||||||||||||||||||||||||||||||||||||||||||||
| 648 | # with their word, so we get that by calling our walktree method in | ||||||||||||||||||||||||||||||||||||||||||||
| 649 | # scalar context. | ||||||||||||||||||||||||||||||||||||||||||||
| 650 | return scalar $self->_walktree( | ||||||||||||||||||||||||||||||||||||||||||||
| 651 | # When fetching suffixes, we don't want to give the word begnning. | ||||||||||||||||||||||||||||||||||||||||||||
| 652 | word => $args{suff_len} ? "" : $word, | ||||||||||||||||||||||||||||||||||||||||||||
| 653 | 'ref' => $ref, | ||||||||||||||||||||||||||||||||||||||||||||
| 654 | suf_len => $args{suff_len}, | ||||||||||||||||||||||||||||||||||||||||||||
| 655 | 10 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 657 | elsif ($self->{_DEEPSEARCH} == PREFIX) { | ||||||||||||||||||||||||||||||||||||||||||||
| 658 | # This is the "longest prefix found" case. | ||||||||||||||||||||||||||||||||||||||||||||
| 659 | 4 | ▶ | if (exists $ref->{$self->{_END}}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 660 | 2 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 661 | 1 | return $ref->{$self->{_END}}; | |||||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 663 | 1 | ▶ | if ($wantref) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | return [@{$lastword}, @{$pref}]; | |||||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 666 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 667 | 1 | return $lastword . $pref; | |||||||||||||||||||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 670 | 2 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 671 | 1 | return $lastwordref->{$self->{_END}}; | |||||||||||||||||||||||||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 673 | 1 | return $lastword; | |||||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 675 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||||
| 678 | # This is the method which does all of the heavy lifting for add and | ||||||||||||||||||||||||||||||||||||||||||||
| 679 | # add_data. Given a word and a datum, it walks down the trie until | ||||||||||||||||||||||||||||||||||||||||||||
| 680 | # it finds a branch that hasn't been created yet. It then makes the rest | ||||||||||||||||||||||||||||||||||||||||||||
| 681 | # of the branch, and slaps an end marker and the datum inside of it. | ||||||||||||||||||||||||||||||||||||||||||||
| 682 | 66 | ▶ | sub _add_internal { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_internal: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 683 | 66 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 684 | 66 | my $word = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 685 | 66 | my $datum = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 686 | 66 | my @letters; | |||||||||||||||||||||||||||||||||||||||||||
| 687 | # We don't NEED to split a string into letters; Any array of tokens | ||||||||||||||||||||||||||||||||||||||||||||
| 688 | # will do. | ||||||||||||||||||||||||||||||||||||||||||||
| 689 | 66 | ▶ | if (ref($word) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 690 | # Note: this is a copy | ||||||||||||||||||||||||||||||||||||||||||||
| 691 | 8 | @letters = (@{$word}); | |||||||||||||||||||||||||||||||||||||||||||
| 692 | # Because in this case, a "letter" can be more than on character | ||||||||||||||||||||||||||||||||||||||||||||
| 693 | # long, we have to make sure we don't collide with whatever we're | ||||||||||||||||||||||||||||||||||||||||||||
| 694 | # using as an end marker. | ||||||||||||||||||||||||||||||||||||||||||||
| 695 | # However, if the user is feeling all fanciful and told us not to | ||||||||||||||||||||||||||||||||||||||||||||
| 696 | # bother, we won't. | ||||||||||||||||||||||||||||||||||||||||||||
| 697 | 8 | ▶ | unless ($self->{_FREEZE_END}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 698 | 8 | for my $letter (@letters) { | |||||||||||||||||||||||||||||||||||||||||||
| 699 | 29 | ▶ | if ($letter eq $self->{_END}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 700 | # If we had a collision, then make a new end marker. | ||||||||||||||||||||||||||||||||||||||||||||
| 701 | 3 | $self->end_marker($self->_gen_new_marker( | |||||||||||||||||||||||||||||||||||||||||||
| 702 | bad => \@letters, | ||||||||||||||||||||||||||||||||||||||||||||
| 703 | )); | ||||||||||||||||||||||||||||||||||||||||||||
| 704 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 709 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 710 | 58 | @letters = split('',$word); | |||||||||||||||||||||||||||||||||||||||||||
| 711 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 712 | # Start at the top of the Trie... | ||||||||||||||||||||||||||||||||||||||||||||
| 713 | 66 | my $ref = $self->{_MAINHASHREF}; | |||||||||||||||||||||||||||||||||||||||||||
| 714 | # This will walk down the trie as far as it can, until it either runs | ||||||||||||||||||||||||||||||||||||||||||||
| 715 | # out of word or out of trie. | ||||||||||||||||||||||||||||||||||||||||||||
| 716 | 66 | ▶ | while ( | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @letters and exists $$ref{$letters[0]}
MC/DC100%2/2
scalar @letters and exists $$ref{$letters[0]}
scalar @letters
exists $$ref{$letters[0]}
Truth table100%3/3
A and B
A= scalar @lettersB= exists $$ref{$letters[0]}
| |||||||||||||||||||||||||||||||||||||||||||||
| 717 | (scalar @letters) && | ||||||||||||||||||||||||||||||||||||||||||||
| 718 | exists($ref->{$letters[0]}) | ||||||||||||||||||||||||||||||||||||||||||||
| 719 | ) { | ||||||||||||||||||||||||||||||||||||||||||||
| 720 | 73 | $ref = $ref->{shift(@letters)}; | |||||||||||||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 722 | # If it ran out of trie before it ran out of word then this will create | ||||||||||||||||||||||||||||||||||||||||||||
| 723 | # the rest of the trie structure. | ||||||||||||||||||||||||||||||||||||||||||||
| 724 | 66 | for my $letter (@letters) { | |||||||||||||||||||||||||||||||||||||||||||
| 725 | 238 | $ref = $ref->{$letter} = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 727 | # In either case, this will make the new end marker for the end of the | ||||||||||||||||||||||||||||||||||||||||||||
| 728 | # word (assuming it wasn't already there) and set the return value | ||||||||||||||||||||||||||||||||||||||||||||
| 729 | # appropriately. | ||||||||||||||||||||||||||||||||||||||||||||
| 730 | 66 | my $ret = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 731 | 66 | ▶ | if (exists $ref->{$self->{_END}}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 732 | 4 | $ret = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 734 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 735 | 62 | $ref->{$self->{_END}} = undef; | |||||||||||||||||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 737 | # This will set the data if it was provided. | ||||||||||||||||||||||||||||||||||||||||||||
| 738 | 66 | ▶ | if (defined $datum) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 739 | 13 | $ref->{$self->{_END}} = $datum; | |||||||||||||||||||||||||||||||||||||||||||
| 740 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 741 | 66 | return $ret; | |||||||||||||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||||
| 744 | # This uses a heuristic (that is, a crappy method) to generate a new | ||||||||||||||||||||||||||||||||||||||||||||
| 745 | # end marker for the trie. In addition to being sure that whatever is | ||||||||||||||||||||||||||||||||||||||||||||
| 746 | # generated is not in use as a letter in the trie, it also makes a bold | ||||||||||||||||||||||||||||||||||||||||||||
| 747 | # yet mostly vain attempt to try to make something that might not be | ||||||||||||||||||||||||||||||||||||||||||||
| 748 | # used in the future. | ||||||||||||||||||||||||||||||||||||||||||||
| 749 | # In general, I do not try to make this functionality good or fast or | ||||||||||||||||||||||||||||||||||||||||||||
| 750 | # perfect -- if it's being called often, the module is being mis-used. | ||||||||||||||||||||||||||||||||||||||||||||
| 751 | # If a user is using multi-character letters, then they ought to find | ||||||||||||||||||||||||||||||||||||||||||||
| 752 | # a string that will be safe and set it themselves. | ||||||||||||||||||||||||||||||||||||||||||||
| 753 | 4 | ▶ | sub _gen_new_marker { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gen_new_marker: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 754 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 755 | 4 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 756 | # This will keep track of all of the letters used in the trie already | ||||||||||||||||||||||||||||||||||||||||||||
| 757 | 4 | my %used = (); | |||||||||||||||||||||||||||||||||||||||||||
| 758 | # This will keep track of what lengths they are | ||||||||||||||||||||||||||||||||||||||||||||
| 759 | 4 | my %sizes = (); | |||||||||||||||||||||||||||||||||||||||||||
| 760 | # First we process the letters of the word which sparked this | ||||||||||||||||||||||||||||||||||||||||||||
| 761 | # re-evaluation. | ||||||||||||||||||||||||||||||||||||||||||||
| 762 | 4 | for my $letter (@{$args{bad}}) { | |||||||||||||||||||||||||||||||||||||||||||
| 763 | 11 | my $len = length($letter); | |||||||||||||||||||||||||||||||||||||||||||
| 764 | 11 | ▶ | if ($len != 1) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 765 | 8 | $used{$letter}++; | |||||||||||||||||||||||||||||||||||||||||||
| 766 | 8 | $sizes{$len}++; | |||||||||||||||||||||||||||||||||||||||||||
| 767 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 768 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 769 | # Then we walk the tree and get the info on all the other letters. | ||||||||||||||||||||||||||||||||||||||||||||
| 770 | 4 | my @refs = ($self->{_MAINHASHREF}); | |||||||||||||||||||||||||||||||||||||||||||
| 771 | 4 | ▶ | while (@refs) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 772 | 5 | my $ref = shift @refs; | |||||||||||||||||||||||||||||||||||||||||||
| 773 | 5 | for my $key (keys %{$ref}) { | |||||||||||||||||||||||||||||||||||||||||||
| 774 | # Note we don't even care about length 1 letters. | ||||||||||||||||||||||||||||||||||||||||||||
| 775 | 7 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $key != 1 and $key ne $self->{"_END"}
MC/DC50%1/2
length $key != 1 and $key ne $self->{"_END"}
length $key != 1
$key ne $self->{"_END"}
Truth table66%2/3
A and B
A= length $key != 1B= $key ne $self->{"_END"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 776 | (length($key) != 1) && | ||||||||||||||||||||||||||||||||||||||||||||
| 777 | ($key ne $self->{_END}) | ||||||||||||||||||||||||||||||||||||||||||||
| 778 | ) { | ||||||||||||||||||||||||||||||||||||||||||||
| 779 | 1 | $used{$key}++; | |||||||||||||||||||||||||||||||||||||||||||
| 780 | 1 | $sizes{length($key)}++; | |||||||||||||||||||||||||||||||||||||||||||
| 781 | 1 | push(@refs, $ref->{$key}); | |||||||||||||||||||||||||||||||||||||||||||
| 782 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 783 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 785 | # The idea here is that we want to make the end marker as small as possible, | ||||||||||||||||||||||||||||||||||||||||||||
| 786 | # as it's stuck all over the place. However, we don't want to spend forever | ||||||||||||||||||||||||||||||||||||||||||||
| 787 | # trying to find one that isn't in use. | ||||||||||||||||||||||||||||||||||||||||||||
| 788 | # So, we find the smallest length such that there are fewer than 1/4 of | ||||||||||||||||||||||||||||||||||||||||||||
| 789 | # the total number of possible letters in use of that length, and we make | ||||||||||||||||||||||||||||||||||||||||||||
| 790 | # a key of that length. | ||||||||||||||||||||||||||||||||||||||||||||
| 791 | 4 | my $newlen = 2; | |||||||||||||||||||||||||||||||||||||||||||
| 792 | 4 | for my $len (sort keys %sizes) { | |||||||||||||||||||||||||||||||||||||||||||
| 793 | # Yes, I know there are well more than 26 available compositors, but | ||||||||||||||||||||||||||||||||||||||||||||
| 794 | # this will only mean I'm being too careful. | ||||||||||||||||||||||||||||||||||||||||||||
| 795 | 6 | ▶ | if ($sizes{$len} < ((26 ** $len) / 4)) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 796 | 3 | $newlen = $len; | |||||||||||||||||||||||||||||||||||||||||||
| 797 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 799 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 800 | # This makes it so that if all existing lengths are too full ( !! ) | ||||||||||||||||||||||||||||||||||||||||||||
| 801 | # then we will just use a key that's one longer than the longest | ||||||||||||||||||||||||||||||||||||||||||||
| 802 | # one already there. | ||||||||||||||||||||||||||||||||||||||||||||
| 803 | 3 | $newlen = $len + 1; | |||||||||||||||||||||||||||||||||||||||||||
| 804 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 806 | # Now we just generate end markers until we find one that isn't in use. | ||||||||||||||||||||||||||||||||||||||||||||
| 807 | 4 | my $newend; | |||||||||||||||||||||||||||||||||||||||||||
| 808 | do { | ||||||||||||||||||||||||||||||||||||||||||||
| 809 | 4 | $newend = join '', map { chr(int(rand(128))) } (('') x $newlen); | |||||||||||||||||||||||||||||||||||||||||||
| 810 | 4 | ▶ | } while (exists($used{$newend})); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 811 | # And return it. | ||||||||||||||||||||||||||||||||||||||||||||
| 812 | 4 | return $newend; | |||||||||||||||||||||||||||||||||||||||||||
| 813 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||||||||||||
| 815 | # Strewth! | ||||||||||||||||||||||||||||||||||||||||||||
| 816 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||||
| 818 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||