| 1 | package FactorOracle; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.008002; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.01'; | |||||||||
| 8 | |||||||||||
| 9 | # Factor Oracle data structure is in the form of two contiguous | ||||||||||
| 10 | # strings of data (in memory or on disk) | ||||||||||
| 11 | # STATES: [suffix link(int)][initial via char][transitions link (int)] | ||||||||||
| 12 | # TRANSITIONS: [via char][state link (int)][next trans (int)] | ||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 1 | my $class = shift; | |||||||||
| 18 | 1 | my $self = { S => '', T => '' }; | |||||||||
| 19 | |||||||||||
| 20 | # initial state | ||||||||||
| 21 | 1 | $self->{S} .= pack("lal", -1, 'a', -1); | |||||||||
| 22 | 1 | return bless $self, $class; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 26 | 1 | my $self = shift; | |||||||||
| 27 | 1 | my $string = shift; | |||||||||
| 28 | 1 | for my $i (0..length($string)-1){ | |||||||||
| 29 | 7 | $self->add_char( substr($string, $i, 1) ); | |||||||||
| 30 | } | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | 7 | ▶ | sub add_char { | ||||||||
|
Subroutine
add_char: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 36 | 7 | my $self = shift; | |||||||||
| 37 | 7 | my $char = shift; | |||||||||
| 38 | 7 | my $Slen = length $self->{S}; | |||||||||
| 39 | 7 | ▶ | die "bad length" unless ($Slen % 9) == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 7 | my $m = $Slen/9 - 1; # index of final state | |||||||||
| 41 | 7 | my $final = $m*9; # string index position of final state | |||||||||
| 42 | 7 | my $sl = $self->sl($m); # suffix link of final state | |||||||||
| 43 | |||||||||||
| 44 | # set initial transition via $char | ||||||||||
| 45 | 7 | substr($self->{S}, $final+4, 1) = $char; | |||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 7 | ▶ | while($sl > -1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 8 | ▶ | if(my $state = $self->trans_exists($sl, $char)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 5 | $sl = $state; # [state pointed to by state $sl via $char] | |||||||||
| 51 | 5 | last; | |||||||||
| 52 | } | ||||||||||
| 53 | else { | ||||||||||
| 54 | # Create transition, follow back | ||||||||||
| 55 | 3 | $self->create_trans($sl, $char, $m+1); | |||||||||
| 56 | 3 | $sl = $self->sl($sl); | |||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | 7 | ▶ | $sl = ($sl < 0) ? 0 : $sl; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | # Add new state with just suffix link initialized. | ||||||||||
| 62 | 7 | $self->{S} .= pack("lal", $sl, 0, -1); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 11 | ▶ | sub trans_exists { | ||||||||
|
Subroutine
trans_exists: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||
| 66 | 11 | my $self = shift; | |||||||||
| 67 | 11 | my $from = shift; | |||||||||
| 68 | 11 | my $via = shift; | |||||||||
| 69 | |||||||||||
| 70 | 11 | my ($to, $char, $extra) = unpack("lal", substr($self->{S}, $from*9, 9)); | |||||||||
| 71 | 11 | ▶ | return $from+1 if $char eq $via; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | # search transition string for $via | ||||||||||
| 74 | 6 | ▶ | while($extra > -1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 3 | ($char, $to, $extra) = unpack("all", substr($self->{T}, $extra*9, 9)); | |||||||||
| 76 | 3 | ▶ | return $to if $char eq $via; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | ▶ | last unless $extra > -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | # no such transition exists | ||||||||||
| 80 | 3 | return undef; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | 3 | ▶ | sub create_trans { | ||||||||
|
Subroutine
create_trans: called CC=4 SCAR=16.6
Pod
undocumented
| |||||||||||
| 85 | 3 | my $self = shift; | |||||||||
| 86 | 3 | my $from = shift; | |||||||||
| 87 | 3 | my $via = shift; | |||||||||
| 88 | 3 | my $to = shift; | |||||||||
| 89 | |||||||||||
| 90 | 3 | my $ntrans = length($self->{T})/9; | |||||||||
| 91 | 3 | my(undef, undef, $extra) = unpack("lal", substr($self->{S}, $from*9, 9)); | |||||||||
| 92 | 3 | ▶ | if($extra == -1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 3 | substr($self->{S}, $from*9+5, 4) = pack("l", $ntrans); | |||||||||
| 94 | } | ||||||||||
| 95 | 3 | ▶ | while($extra > -1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | my $next = unpack("l", substr($self->{T}, $extra*9+5, 4)); | |||||||||
| 97 | 0 | ▶ | if($next == 0){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | # point last trans to new linked trans | ||||||||||
| 99 | 0 | substr($self->{T}, $extra*9+5, 4) = pack("l", $ntrans); | |||||||||
| 100 | 0 | last; | |||||||||
| 101 | } | ||||||||||
| 102 | 0 | $extra = $next; | |||||||||
| 103 | } | ||||||||||
| 104 | 3 | $self->{T} .= pack("all", $via, $to, -1); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | ▶ | sub states { | ||||||||
|
Subroutine
states: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 108 | 1 | my $self = shift; | |||||||||
| 109 | 1 | return length($self->{S})/9; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | sub transitions { | ||||||||
|
Subroutine
transitions: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 113 | 0 | my $self = shift; | |||||||||
| 114 | 0 | return length($self->{T})/9; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 18 | ▶ | sub sl { | ||||||||
|
Subroutine
sl: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 118 | 18 | my $self = shift; | |||||||||
| 119 | 18 | my $state = shift; | |||||||||
| 120 | |||||||||||
| 121 | 18 | return unpack("l", substr($self->{S}, $state*9, 4)); | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | 1 | 1; | |||||||||
| 126 | __END__ | ||||||||||