| 1 | package Lingua::Anagrams; | ||||||||||||||||||||||||||||||
| 2 | 2 | $Lingua::Anagrams::VERSION = '0.017'; | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: pure Perl anagram finder | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | # don't cache anagrams for bigger character counts than this | ||||||||||||||||||||||||||||||
| 12 | 2 | our $LIMIT = 20; | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | # some global variables to be localized | ||||||||||||||||||||||||||||||
| 15 | # used to limit time spent copying values | ||||||||||||||||||||||||||||||
| 16 | 2 | our ( $limit, $known, $trie, %cache, $cleaner, @jumps, $word_cache, @indices ); | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||||
| 21 | 7 | my $wl = shift; | |||||||||||||||||||||||||||||
| 22 | 7 | ▶ | die 'first parameter expected to be an array reference' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 23 | unless ref $wl eq 'ARRAY'; | ||||||||||||||||||||||||||||||
| 24 | 7 | my %params = _make_opts(@_); | |||||||||||||||||||||||||||||
| 25 | 7 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||
| 26 | 7 | ▶ | local $cleaner = $params{clean} // \&_clean; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$params{"clean"} // \&Lingua::Anagrams::_clean
MC/DC0%0/1
$params{"clean"} // \&Lingua::Anagrams::_clean
$params{"clean"}
Truth table50%1/2
A
A= $params{"clean"}
| |||||||||||||||||||||||||||||||
| 27 | 7 | my @word_lists; | |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | if ( ref $wl->[0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 29 | 1 | @word_lists = @$wl; | |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||||
| 32 | 6 | @word_lists = ($wl); | |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||
| 34 | 7 | my ( @tries, @all_words ); | |||||||||||||||||||||||||||||
| 35 | 7 | for my $words (@word_lists) { | |||||||||||||||||||||||||||||
| 36 | 8 | ▶ | next unless @$words; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 37 | 8 | ▶ | die 'items in lists expected to be words' if ref $words->[0]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 38 | 8 | $cleaner->($_) for @$words; | |||||||||||||||||||||||||||||
| 39 | 8 | my $s1 = @all_words; | |||||||||||||||||||||||||||||
| 40 | 8 | push @all_words, @$words; | |||||||||||||||||||||||||||||
| 41 | 8 | @all_words = uniq @all_words; | |||||||||||||||||||||||||||||
| 42 | 8 | ▶ | next unless @all_words > $s1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 43 | 8 | my ( $trie, $known ) = _trieify( \@all_words ); | |||||||||||||||||||||||||||||
| 44 | 8 | push @tries, [ $trie, $known ]; | |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | die 'no words' unless @tries; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 47 | return bless { | ||||||||||||||||||||||||||||||
| 48 | limit => $params{limit} // $LIMIT, | ||||||||||||||||||||||||||||||
| 49 | sorted => $params{sorted} // 0, | ||||||||||||||||||||||||||||||
| 50 | min => $params{min}, | ||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | clean => $cleaner, | ||||||||||||||||||||||||||||
|
Condition80%4/5
$params{"limit"} // $Lingua::Anagrams::LIMIT
$params{"sorted"} // 0
MC/DC0%0/2
$params{"limit"} // $Lingua::Anagrams::LIMIT
$params{"limit"}
$Lingua::Anagrams::LIMIT
MC/DC100%1/1
$params{"sorted"} // 0
$params{"sorted"}
Truth table66%2/3
A // B
A= $params{"limit"}B= $Lingua::Anagrams::LIMIT
Truth table100%2/2
A
A= $params{"sorted"}
| |||||||||||||||||||||||||||||||
| 52 | tries => \@tries, | ||||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||||
| 54 | $class; | ||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | sub _trieify { | ||||||||||||||||||||||||||||||
| 58 | 8 | ▶ | my $words = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_trieify: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||
| 59 | 8 | my $base = []; | |||||||||||||||||||||||||||||
| 60 | 8 | my @known; | |||||||||||||||||||||||||||||
| 61 | 8 | my $terminal = []; | |||||||||||||||||||||||||||||
| 62 | 8 | for my $word (@$words) { | |||||||||||||||||||||||||||||
| 63 | 32 | ▶ | next unless length( $word // '' ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$word // ""
MC/DC0%0/1
$word // ""
$word
Truth table50%1/2
A
A= $word
| |||||||||||||||||||||||||||||||
| 64 | 32 | my @chars = map ord, split //, $word; | |||||||||||||||||||||||||||||
| 65 | 32 | _learn( \@known, \@chars ); | |||||||||||||||||||||||||||||
| 66 | 32 | _add( $base, \@chars, $terminal ); | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | 8 | return $base, \@known; | |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | sub _learn { | ||||||||||||||||||||||||||||||
| 72 | 32 | ▶ | my ( $known, $new ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_learn: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 73 | 32 | for my $i (@$new) { | |||||||||||||||||||||||||||||
| 74 | 47 | ▶ | $known->[$i] ||= 1; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$known->[$i] ||= 1
MC/DC100%1/1
$known->[$i] ||= 1
$known->[$i]
Truth table100%2/2
A
A= $known->[$i]
| |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | sub _add { | ||||||||||||||||||||||||||||||
| 79 | 79 | ▶ | my ( $base, $chars, $terminal ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_add: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||
| 80 | 79 | my $i = shift @$chars; | |||||||||||||||||||||||||||||
| 81 | 79 | ▶ | if ($i) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | 47 | ▶ | my $next = $base->[$i] //= []; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$base->[$i] //= []
MC/DC100%1/1
$base->[$i] //= []
$base->[$i]
Truth table100%2/2
A
A= $base->[$i]
| |||||||||||||||||||||||||||||||
| 83 | 47 | _add( $next, $chars, $terminal ); | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||
| 86 | 32 | ▶ | $base->[0] //= $terminal; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$base->[0] //= $terminal
MC/DC0%0/2
$base->[0] //= $terminal
$base->[0]
$terminal
Truth table33%1/3
A //= B
A= $base->[0]B= $terminal
| |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | # walk the trie looking for words you can make out of the current character count | ||||||||||||||||||||||||||||||
| 91 | sub _words_in { | ||||||||||||||||||||||||||||||
| 92 | 71 | ▶ | my ( $counts, $total ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_words_in: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||
| 93 | 71 | my @words; | |||||||||||||||||||||||||||||
| 94 | 71 | my @stack = ( [ 0, $trie ] ); | |||||||||||||||||||||||||||||
| 95 | 71 | while (1) { | |||||||||||||||||||||||||||||
| 96 | 698 | my ( $c, $level ) = @{ $stack[-1] }; | |||||||||||||||||||||||||||||
| 97 | 698 | ▶ | if ( $c == -1 || $c >= @$level ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c == -1 or $c >= @$level
MC/DC100%2/2
$c == -1 or $c >= @$level
$c == -1
$c >= @$level
Truth table100%3/3
A or B
A= $c == -1B= $c >= @$level
| |||||||||||||||||||||||||||||||
| 98 | 160 | ▶ | last if @stack == 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | 89 | pop @stack; | |||||||||||||||||||||||||||||
| 100 | 89 | ++$total; | |||||||||||||||||||||||||||||
| 101 | 89 | $c = \( $stack[-1][0] ); | |||||||||||||||||||||||||||||
| 102 | 89 | ++$counts->[$$c]; | |||||||||||||||||||||||||||||
| 103 | 89 | $$c = $jumps[$$c]; | |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||||
| 106 | 538 | my $l = $level->[$c]; | |||||||||||||||||||||||||||||
| 107 | 538 | ▶ | if ($l) { # trie holds corresponding node | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 388 | ▶ | if ($c) { # character | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 109 | 245 | ▶ | if ( $counts->[$c] ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 110 | 149 | push @stack, [ 0, $l ]; | |||||||||||||||||||||||||||||
| 111 | 149 | --$counts->[$c]; | |||||||||||||||||||||||||||||
| 112 | 149 | --$total; | |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||
| 115 | 96 | $stack[-1][0] = $jumps[$c]; | |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | else { # terminal | ||||||||||||||||||||||||||||||
| 119 | my $w = join '', | ||||||||||||||||||||||||||||||
| 120 | 143 | map { chr( $_->[0] ) } @stack[ 0 .. $#stack - 1 ]; | |||||||||||||||||||||||||||||
| 121 | 143 | ▶ | $w = $word_cache->{$w} //= scalar keys %$word_cache; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$Lingua::Anagrams::word_cache->{$w} //= scalar keys %$Lingua::Anagrams::word_cache
MC/DC100%1/1
$Lingua::Anagrams::word_cache->{$w} //= scalar keys %$Lingua::Anagrams::word_cache
$Lingua::Anagrams::word_cache->{$w}
Truth table100%2/2
A
A= $Lingua::Anagrams::word_cache->{$w}
| |||||||||||||||||||||||||||||||
| 122 | 143 | push @words, [ $w, [@$counts] ]; | |||||||||||||||||||||||||||||
| 123 | 143 | ▶ | if ($total) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 124 | 83 | $stack[-1][0] = $jumps[$c]; | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||||
| 127 | 60 | pop @stack; | |||||||||||||||||||||||||||||
| 128 | 60 | ++$total; | |||||||||||||||||||||||||||||
| 129 | 60 | $c = \( $stack[-1][0] ); | |||||||||||||||||||||||||||||
| 130 | 60 | ++$counts->[$$c]; | |||||||||||||||||||||||||||||
| 131 | 60 | $$c = $jumps[$$c]; | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||||
| 136 | 150 | $stack[-1][0] = $jumps[$c]; | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||
| 140 | 71 | \@words; | |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | sub anagrams { | ||||||||||||||||||||||||||||||
| 145 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
anagrams: called CC=18 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 146 | 8 | my $phrase = shift; | |||||||||||||||||||||||||||||
| 147 | 8 | my %opts = _make_opts(@_); | |||||||||||||||||||||||||||||
| 148 | 8 | local ( $limit, $cleaner ) = @$self{qw(limit clean)}; | |||||||||||||||||||||||||||||
| 149 | 8 | $cleaner->($phrase); | |||||||||||||||||||||||||||||
| 150 | 8 | ▶ | return () unless length $phrase; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 151 | 8 | my ( $sort, $min ); | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | 8 | ▶ | if ( exists $opts{sorted} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 154 | 3 | $sort = $opts{sorted}; | |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||||
| 157 | 5 | $sort = $self->{sorted}; | |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||
| 159 | 8 | ▶ | if ( exists $opts{min} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 160 | 2 | $min = $opts{min}; | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||||
| 163 | 6 | $min = $self->{min}; | |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||
| 165 | 8 | ▶ | my $i = $opts{start_list} // 0; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"start_list"} // 0
MC/DC100%1/1
$opts{"start_list"} // 0
$opts{"start_list"}
Truth table100%2/2
A
A= $opts{"start_list"}
| |||||||||||||||||||||||||||||||
| 166 | 8 | my @pairs = @{ $self->{tries} }; | |||||||||||||||||||||||||||||
| 167 | 8 | ▶ | if ($i) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 168 | 1 | ▶ | die "impossible index for start list: $i" unless defined $pairs[$i]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 169 | 1 | ▶ | $i = @pairs + $i if $i < 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 170 | 1 | @pairs = @pairs[ $i .. $#pairs ]; | |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||
| 172 | 8 | my $counts = _counts($phrase); | |||||||||||||||||||||||||||||
| 173 | 8 | local @jumps = _jumps($counts); | |||||||||||||||||||||||||||||
| 174 | 8 | local @indices = _indices($counts); | |||||||||||||||||||||||||||||
| 175 | 8 | my @anagrams; | |||||||||||||||||||||||||||||
| 176 | 8 | local $word_cache = {}; | |||||||||||||||||||||||||||||
| 177 | 8 | for my $pair (@pairs) { | |||||||||||||||||||||||||||||
| 178 | 8 | local ( $trie, $known ) = @$pair; | |||||||||||||||||||||||||||||
| 179 | 8 | ▶ | next unless _all_known($counts); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 180 | 8 | local %cache = (); | |||||||||||||||||||||||||||||
| 181 | 8 | %$word_cache = (); | |||||||||||||||||||||||||||||
| 182 | 8 | @anagrams = _anagramize($counts); | |||||||||||||||||||||||||||||
| 183 | 8 | ▶ | next unless @anagrams; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 184 | 8 | ▶ | next if $min and @anagrams < $min; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$min and @anagrams < $min
MC/DC0%0/2
$min and @anagrams < $min
$min
@anagrams < $min
Truth table66%2/3
A and B
A= $minB= @anagrams < $min
| |||||||||||||||||||||||||||||||
| 185 | 8 | last; | |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||
| 187 | 8 | my %r = reverse %$word_cache; | |||||||||||||||||||||||||||||
| 188 | @anagrams = map { | ||||||||||||||||||||||||||||||
| 189 | 8 | [ map { $r{$_} } @$_ ] | |||||||||||||||||||||||||||||
| 190 | } @anagrams; | ||||||||||||||||||||||||||||||
| 191 | 8 | ▶ | if ($sort) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 192 | @anagrams = sort { | ||||||||||||||||||||||||||||||
| 193 | 24 | ▶ | my $ordered = @$a <= @$b ? 1 : -1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 194 | 24 | ▶ | my ( $d, $e ) = $ordered == 1 ? ( $a, $b ) : ( $b, $a ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 195 | 24 | for ( 0 .. $#$d ) { | |||||||||||||||||||||||||||||
| 196 | 27 | my $c = $d->[$_] cmp $e->[$_]; | |||||||||||||||||||||||||||||
| 197 | 27 | ▶ | return $ordered * $c if $c; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||
| 199 | 0 | -$ordered; | |||||||||||||||||||||||||||||
| 200 | 4 | } map { [ sort @$_ ] } @anagrams; | |||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||
| 202 | 8 | return @anagrams; | |||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||
| 205 | sub _make_opts { | ||||||||||||||||||||||||||||||
| 206 | 22 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||||||||||||
|
Subroutine
_make_opts: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 207 | 1 | my $r = shift; | |||||||||||||||||||||||||||||
| 208 | 1 | ▶ | die 'options expected to be key value pairs or a hash ref' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 209 | unless 'HASH' eq ref $r; | ||||||||||||||||||||||||||||||
| 210 | 1 | return %$r; | |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||
| 212 | else { | ||||||||||||||||||||||||||||||
| 213 | 21 | return @_; | |||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||
| 218 | 2 | ▶ | our $null = sub { }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||
| 220 | sub iterator { | ||||||||||||||||||||||||||||||
| 221 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||
| 222 | 7 | my $phrase = shift; | |||||||||||||||||||||||||||||
| 223 | 7 | my %opts = _make_opts(@_); | |||||||||||||||||||||||||||||
| 224 | 7 | ▶ | $opts{sorted} //= $self->{sorted}; | ||||||||||||||||||||||||||||
|
Condition66%2/3
$opts{"sorted"} //= $self->{"sorted"}
MC/DC0%0/2
$opts{"sorted"} //= $self->{"sorted"}
$opts{"sorted"}
$self->{"sorted"}
Truth table66%2/3
A //= B
A= $opts{"sorted"}B= $self->{"sorted"}
| |||||||||||||||||||||||||||||||
| 225 | 7 | $self->{clean}->($phrase); | |||||||||||||||||||||||||||||
| 226 | 7 | ▶ | my $i = $opts{start_list} // 0; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"start_list"} // 0
MC/DC100%1/1
$opts{"start_list"} // 0
$opts{"start_list"}
Truth table100%2/2
A
A= $opts{"start_list"}
| |||||||||||||||||||||||||||||||
| 227 | 7 | my @pairs = @{ $self->{tries} }; | |||||||||||||||||||||||||||||
| 228 | 7 | ▶ | if ($i) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 229 | 1 | ▶ | die "impossible index for start list: $i" unless defined $pairs[$i]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | $i = @pairs + $i if $i < 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 231 | 1 | @pairs = @pairs[ $i .. $#pairs ]; | |||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||
| 233 | 7 | ▶ | return $null unless length $phrase; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 234 | 7 | return _super_iterator( \@pairs, $phrase, \%opts ); | |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||
| 237 | # iterator that converts word indices back to words | ||||||||||||||||||||||||||||||
| 238 | sub _super_iterator { | ||||||||||||||||||||||||||||||
| 239 | 7 | ▶ | my ( $tries, $phrase, $opts ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_super_iterator: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 240 | 7 | my $counts = _counts($phrase); | |||||||||||||||||||||||||||||
| 241 | 7 | my @j = _jumps($counts); | |||||||||||||||||||||||||||||
| 242 | 7 | my @ix = _indices($counts); | |||||||||||||||||||||||||||||
| 243 | 7 | my $wc = {}; | |||||||||||||||||||||||||||||
| 244 | 7 | my $i = _iterator( $tries, $counts, $opts ); | |||||||||||||||||||||||||||||
| 245 | 7 | my ( %reverse_cache, %c ); | |||||||||||||||||||||||||||||
| 246 | return sub { | ||||||||||||||||||||||||||||||
| 247 | 28 | ▶ | my $rv; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 248 | 28 | local @jumps = @j; | |||||||||||||||||||||||||||||
| 249 | 28 | local @indices = @ix; | |||||||||||||||||||||||||||||
| 250 | 28 | local $word_cache = $wc; | |||||||||||||||||||||||||||||
| 251 | { | ||||||||||||||||||||||||||||||
| 252 | 28 | $rv = $i->(); | |||||||||||||||||||||||||||||
| 253 | 54 | ▶ | return unless $rv; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 254 | 47 | my $key = join ',', sort { $a <=> $b } @$rv; | |||||||||||||||||||||||||||||
| 255 | 47 | ▶ | redo if $c{$key}++; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||
| 257 | 21 | for my $j (@$rv) { | |||||||||||||||||||||||||||||
| 258 | 31 | ▶ | if ( !$reverse_cache{$j} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 259 | 7 | %reverse_cache = reverse %$word_cache; | |||||||||||||||||||||||||||||
| 260 | 7 | last; | |||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||
| 263 | 21 | $rv = [ map { $reverse_cache{$_} } @$rv ]; | |||||||||||||||||||||||||||||
| 264 | 21 | ▶ | if ( $opts->{sorted} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 265 | 5 | $rv = [ sort @$rv ]; | |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||
| 267 | 21 | $rv; | |||||||||||||||||||||||||||||
| 268 | 7 | }; | |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||
| 271 | # iterator that manages the trie list | ||||||||||||||||||||||||||||||
| 272 | sub _iterator { | ||||||||||||||||||||||||||||||
| 273 | 49 | ▶ | my ( $tries, $counts, $opts ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_iterator: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 274 | 49 | my $total = 0; | |||||||||||||||||||||||||||||
| 275 | 49 | $total += $_ for @$counts[@indices]; | |||||||||||||||||||||||||||||
| 276 | 49 | my @t = @$tries; | |||||||||||||||||||||||||||||
| 277 | 49 | my $i; | |||||||||||||||||||||||||||||
| 278 | my $s = sub { | ||||||||||||||||||||||||||||||
| 279 | 154 | ▶ | my $rv; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 280 | { | ||||||||||||||||||||||||||||||
| 281 | 154 | ▶ | unless ($i) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 282 | 96 | ▶ | if (@t) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 283 | 49 | my $pair = shift @t; | |||||||||||||||||||||||||||||
| 284 | 49 | local ( $trie, $known ) = @$pair; | |||||||||||||||||||||||||||||
| 285 | 49 | ▶ | redo unless _all_known($counts); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 286 | 49 | my $words = _words_in( $counts, $total ); | |||||||||||||||||||||||||||||
| 287 | 49 | ▶ | redo unless _worth_pursuing( $counts, $words ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 288 | 49 | $i = _sub_iterator( $tries, $words, $opts ); | |||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||||
| 291 | 47 | return $rv; | |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||
| 294 | 152 | $rv = $i->(); | |||||||||||||||||||||||||||||
| 295 | 152 | ▶ | unless ($rv) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 296 | 45 | undef $i; | |||||||||||||||||||||||||||||
| 297 | 45 | redo; | |||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||
| 300 | 107 | $rv; | |||||||||||||||||||||||||||||
| 301 | 49 | }; | |||||||||||||||||||||||||||||
| 302 | 49 | $s; | |||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||
| 305 | # iterator that actually walks tries looking for anagrams | ||||||||||||||||||||||||||||||
| 306 | sub _sub_iterator { | ||||||||||||||||||||||||||||||
| 307 | 49 | ▶ | my ( $tries, $words, $opts ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_sub_iterator: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 308 | 49 | my @pairs = @$words; | |||||||||||||||||||||||||||||
| 309 | return sub { | ||||||||||||||||||||||||||||||
| 310 | { | ||||||||||||||||||||||||||||||
| 311 | 152 | ▶ | return unless @pairs; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 312 | 192 | ▶ | if ( $opts->{random} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 313 | 112 | my $i = int rand scalar @pairs; | |||||||||||||||||||||||||||||
| 314 | 112 | ▶ | if ($i) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 315 | 44 | my $p = $pairs[0]; | |||||||||||||||||||||||||||||
| 316 | 44 | $pairs[0] = $pairs[$i]; | |||||||||||||||||||||||||||||
| 317 | 44 | $pairs[$i] = $p; | |||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||
| 320 | 192 | my ( $w, $s ) = @{ $pairs[0] }; | |||||||||||||||||||||||||||||
| 321 | 192 | ▶ | unless ( ref $s eq 'CODE' ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 322 | 89 | ▶ | if ( _any($s) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 323 | 42 | $s = _iterator( $tries, $s, $opts ); | |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||
| 325 | else { | ||||||||||||||||||||||||||||||
| 326 | 47 | my $next = []; | |||||||||||||||||||||||||||||
| 327 | $s = sub { | ||||||||||||||||||||||||||||||
| 328 | 92 | ▶ | my $rv = $next; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 329 | 92 | undef $next; | |||||||||||||||||||||||||||||
| 330 | 92 | $rv; | |||||||||||||||||||||||||||||
| 331 | 47 | }; | |||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||
| 333 | 89 | $pairs[0][1] = $s; | |||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||
| 335 | 192 | my $remainder = $s->(); | |||||||||||||||||||||||||||||
| 336 | 192 | ▶ | unless ($remainder) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 337 | 85 | shift @pairs; | |||||||||||||||||||||||||||||
| 338 | 85 | redo; | |||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||
| 340 | 107 | return [ $w, @$remainder ]; | |||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||
| 342 | 49 | }; | |||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||
| 345 | # all character counts decremented | ||||||||||||||||||||||||||||||
| 346 | sub _worth_pursuing { | ||||||||||||||||||||||||||||||
| 347 | 49 | ▶ | my ( $counts, $words ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_worth_pursuing: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||
| 349 | 49 | my $c; | |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||
| 351 | # if any letter count didn't change, there's no hope | ||||||||||||||||||||||||||||||
| 352 | 49 | OUTER: for my $i (@indices) { | |||||||||||||||||||||||||||||
| 353 | 137 | ▶ | next unless $c = $counts->[$i]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 354 | 68 | for (@$words) { | |||||||||||||||||||||||||||||
| 355 | 89 | ▶ | next OUTER if $_->[1][$i] < $c; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||
| 357 | 0 | return; | |||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||
| 359 | 49 | return 1; | |||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||
| 362 | sub _indices { | ||||||||||||||||||||||||||||||
| 363 | 15 | ▶ | my $counts = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_indices: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 364 | 15 | my @indices; | |||||||||||||||||||||||||||||
| 365 | 15 | for my $i ( 0 .. $#$counts ) { | |||||||||||||||||||||||||||||
| 366 | 1545 | ▶ | push @indices, $i if $counts->[$i]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||
| 368 | 15 | return @indices; | |||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||
| 371 | sub _jumps { | ||||||||||||||||||||||||||||||
| 372 | 15 | ▶ | my $counts = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_jumps: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 373 | 15 | my @jumps = (0) x @$counts; | |||||||||||||||||||||||||||||
| 374 | 15 | my $j = 0; | |||||||||||||||||||||||||||||
| 375 | 15 | ▶ | while ( my $n = _next_jump( $counts, $j ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 376 | 36 | $jumps[$j] = $n; | |||||||||||||||||||||||||||||
| 377 | 36 | $j = $n; | |||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||
| 379 | 15 | $jumps[-1] = -1; | |||||||||||||||||||||||||||||
| 380 | 15 | return @jumps; | |||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||
| 383 | sub _next_jump { | ||||||||||||||||||||||||||||||
| 384 | 51 | ▶ | my ( $counts, $j ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_next_jump: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 385 | 51 | for my $i ( $j + 1 .. $#$counts ) { | |||||||||||||||||||||||||||||
| 386 | 1530 | ▶ | return $i if $counts->[$i]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||
| 388 | 15 | return; | |||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||
| 391 | sub _clean { | ||||||||||||||||||||||||||||||
| 392 | 52 | ▶ | $_[0] =~ s/\W+//g; | ||||||||||||||||||||||||||||
|
Subroutine
_clean: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 393 | 52 | $_[0] = lc $_[0]; | |||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||
| 396 | sub _all_known { | ||||||||||||||||||||||||||||||
| 397 | 57 | ▶ | my $counts = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_all_known: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||||
| 398 | 57 | ▶ | return if @$counts > @$known; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 399 | 57 | for my $i ( 0 .. $#$counts ) { | |||||||||||||||||||||||||||||
| 400 | 5767 | ▶ | return if $counts->[$i] && !$known->[$i]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$counts->[$i] and not $Lingua::Anagrams::known->[$i]
MC/DC0%0/2
$counts->[$i] and not $Lingua::Anagrams::known->[$i]
$counts->[$i]
not $Lingua::Anagrams::known->[$i]
Truth table66%2/3
A and not(B)
A= $counts->[$i]B= not $Lingua::Anagrams::known->[$i]
| |||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||
| 402 | 57 | return 1; | |||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||
| 406 | sub key { | ||||||||||||||||||||||||||||||
| 407 | 6 | ▶ | my ( $self, $phrase ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
key: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 408 | 6 | $self->{clean}->($phrase); | |||||||||||||||||||||||||||||
| 409 | 6 | my ( @counts, $lowest ); | |||||||||||||||||||||||||||||
| 410 | 6 | for my $c ( map ord, split //, $phrase ) { | |||||||||||||||||||||||||||||
| 411 | 18 | ▶ | if ( defined $lowest ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 412 | 12 | ▶ | $lowest = $c if $c < $lowest; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||
| 414 | else { | ||||||||||||||||||||||||||||||
| 415 | 6 | $lowest = $c; | |||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||
| 417 | 18 | $counts[$c]++; | |||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||
| 419 | 6 | @counts = @counts[ $lowest .. $#counts ]; | |||||||||||||||||||||||||||||
| 420 | 6 | ▶ | $_ //= '' for @counts; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$_ //= ""
MC/DC100%1/1
$_ //= ""
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||||
| 421 | 6 | my $suffix = join '.', @counts; | |||||||||||||||||||||||||||||
| 422 | 6 | $suffix =~ s/\.(\.+)\./'('.length($1).')'/ge; | |||||||||||||||||||||||||||||
| 423 | 6 | return "$lowest:$suffix"; | |||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||
| 427 | sub lists { | ||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
lists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 429 | 1 | return scalar @{ $self->{tries} }; | |||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||
| 432 | sub _counts { | ||||||||||||||||||||||||||||||
| 433 | 15 | ▶ | my $phrase = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_counts: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||
| 434 | 15 | my @counts; | |||||||||||||||||||||||||||||
| 435 | 15 | for my $c ( map ord, split //, $phrase ) { | |||||||||||||||||||||||||||||
| 436 | 42 | $counts[$c]++; | |||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||
| 438 | 15 | ▶ | $_ //= 0 for @counts; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$_ //= 0
MC/DC100%1/1
$_ //= 0
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||||
| 439 | 15 | \@counts; | |||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||
| 442 | sub _any { | ||||||||||||||||||||||||||||||
| 443 | 125 | ▶ | for ( @{ $_[0] } ) { | ||||||||||||||||||||||||||||
|
Subroutine
_any: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 444 | 12548 | ▶ | return 1 if $_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||
| 446 | 66 | ''; | |||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||
| 449 | sub _anagramize { | ||||||||||||||||||||||||||||||
| 450 | 25 | ▶ | my $counts = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_anagramize: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||||
| 451 | 25 | my $total = 0; | |||||||||||||||||||||||||||||
| 452 | 25 | $total += $_ for @$counts[@indices]; | |||||||||||||||||||||||||||||
| 453 | 25 | my $key; | |||||||||||||||||||||||||||||
| 454 | 25 | ▶ | if ( $total <= $limit ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 455 | 25 | $key = join ',', @$counts[@indices]; | |||||||||||||||||||||||||||||
| 456 | 25 | my $cached = $cache{$key}; | |||||||||||||||||||||||||||||
| 457 | 25 | ▶ | return @$cached if $cached; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||
| 459 | 22 | my @anagrams; | |||||||||||||||||||||||||||||
| 460 | 22 | my $words = _words_in( $counts, $total ); | |||||||||||||||||||||||||||||
| 461 | 22 | ▶ | if ( _all_touched( $counts, $words ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 462 | 22 | for (@$words) { | |||||||||||||||||||||||||||||
| 463 | 36 | my ( $word, $c ) = @$_; | |||||||||||||||||||||||||||||
| 464 | 36 | ▶ | if ( _any($c) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 465 | 17 | push @anagrams, [ $word, @$_ ] for _anagramize($c); | |||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||
| 467 | else { | ||||||||||||||||||||||||||||||
| 468 | 19 | push @anagrams, [$word]; | |||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||
| 471 | 22 | my %seen; | |||||||||||||||||||||||||||||
| 472 | @anagrams = map { | ||||||||||||||||||||||||||||||
| 473 | 22 | ▶ | $seen{ join ' ', sort { $a <=> $b } @$_ }++ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 474 | ? () | ||||||||||||||||||||||||||||||
| 475 | : $_ | ||||||||||||||||||||||||||||||
| 476 | } @anagrams; | ||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||
| 478 | 22 | ▶ | $cache{$key} = \@anagrams if $key; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 479 | 22 | @anagrams; | |||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||
| 482 | sub _all_touched { | ||||||||||||||||||||||||||||||
| 483 | 22 | ▶ | my ( $counts, $words ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_all_touched: called CC=14 SCAR=26.5
| |||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||
| 485 | 22 | my $c; | |||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||
| 487 | 22 | my ( @tallies, @good_indices ); | |||||||||||||||||||||||||||||
| 488 | 22 | for (@$words) { | |||||||||||||||||||||||||||||
| 489 | 52 | my $wc = $_->[1]; | |||||||||||||||||||||||||||||
| 490 | 52 | for (@indices) { | |||||||||||||||||||||||||||||
| 491 | 140 | ▶ | next unless $c = $counts->[$_]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 492 | 114 | ▶ | $good_indices[$_] //= $_; | ||||||||||||||||||||||||||||
|
Condition66%2/3
$good_indices[$_] //= $_
MC/DC0%0/2
$good_indices[$_] //= $_
$good_indices[$_]
$_
Truth table66%2/3
A //= B
A= $good_indices[$_]B= $_
| |||||||||||||||||||||||||||||||
| 493 | 114 | ▶ | $tallies[$_]++ if $wc->[$_] < $c; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||
| 497 | # if any letter count didn't change, there's no hope | ||||||||||||||||||||||||||||||
| 498 | 22 | ▶ | return unless @good_indices; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 499 | 22 | for (@good_indices) { | |||||||||||||||||||||||||||||
| 500 | 2241 | ▶ | next unless $_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 501 | 36 | ▶ | return unless $tallies[$_]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||
| 504 | # find the letter with the fewest possibilities | ||||||||||||||||||||||||||||||
| 505 | 22 | my ( $best, $min, $n ); | |||||||||||||||||||||||||||||
| 506 | 22 | for (@good_indices) { | |||||||||||||||||||||||||||||
| 507 | 2241 | ▶ | next unless $_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 508 | 36 | $n = $tallies[$_]; | |||||||||||||||||||||||||||||
| 509 | 36 | ▶ | if ( !$best || $n < $min ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $best or $n < $min
MC/DC50%1/2
not $best or $n < $min
not $best
$n < $min
Truth table66%2/3
not(A) or B
A= not $bestB= $n < $min
| |||||||||||||||||||||||||||||||
| 510 | 22 | $best = $_; | |||||||||||||||||||||||||||||
| 511 | 22 | $min = $n; | |||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||
| 515 | # we only need consider all the branches which affected a | ||||||||||||||||||||||||||||||
| 516 | # particular letter; we will find all possibilities in their | ||||||||||||||||||||||||||||||
| 517 | # ramifications | ||||||||||||||||||||||||||||||
| 518 | 22 | $c = $counts->[$best]; | |||||||||||||||||||||||||||||
| 519 | 22 | @$words = grep { $_->[1][$best] < $c } @$words; | |||||||||||||||||||||||||||||
| 520 | 22 | return 1; | |||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||
| 523 | 2 | 1; | |||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||
| 525 | __END__ | ||||||||||||||||||||||||||||||