#! perl -slw
use strict;
use Time::HiRes qw[ time ];
use Data::Dump qw[ pp ];
sub receive{
my $n = join '', map 1+int( rand 9 ), 1 .. 9;
chop $n if rand() < 0.1;
$n .= 1+int( rand 9 ) if rand() > 0.9;
return $n;
}
$|++;
our $SAVE //= 0;
our $LOAD //= 0;
our $SRAND //= 0; srand( $SRAND ) if $SRAND;
our $N //= 1e5;
my( $knowns, $deletions, $insertions, $transformations ) = ( chr(0)x125_000_000, chr(0)x12_500_000, chr(0)x1_250_000_000, chr(0)x125_000_000 );
my( $known, $new, $deletion, $insertion, $transformed ) = (0) x 5;
if( $LOAD ) {
open BIN, '<:raw', "$0.bin" or die $!;
{ local $/ = \1_250_000_000; $insertions = ; }
{ local $/ = \ 125_000_000; $knowns = ; }
{ local $/ = \ 125_000_000; $transformations = ; }
{ local $/ = \ 12_500_000; $deletions = ; }
close BIN;
$known = unpack '%32b*', $knowns;
}
my $start = time;
for ( 1 .. $N ) {
printf STDERR "\r%10d\t", $_ unless $_ % 1000;
my $received = receive();
if( length( $received ) == 9 ) {
if( vec( $knowns, $received, 1 ) ) {
print( "$received matched a known number" );
++$known;
next;
}
elsif( vec( $transformations, $received, 1 ) ) {
print "$received matches a known number with a substitution.";
++$transformed;
next;
}
} elsif( length( $received ) == 8 ) {
if( vec( $deletions, $received, 1 ) ) {
print "$received matches a known number with a deletion.";
++$deletion;
}
next;
} elsif( length( $received ) == 10 ) {
if( vec( $insertions, $received, 1 ) ) {
print "$received matches a known number with an insertion.";
++$insertion;
}
next;
}
++$new, vec( $knowns, $received, 1 ) = 1; ## new number
for my $pos ( 0 .. 8 ) {
my $copy;
vec( $deletions, substr( $copy = $received, $pos, 1, '' ), 1 ) = 1; ## add all possible 1-digit deletions to their index
vec( $insertions, substr( $copy = $received, $pos, 1, $_ ), 1 ) = 1 for 1 .. 9; ## add all possible 1-digit insertions to their index
my $digit = substr( $received, $pos, 1 );
vec( $transformations, substr( $copy = $received, $pos, 1, $_ ), 1 ) = 1 for 1 .. $digit-1, $digit+1 .. 9; ## all poss 1-digit substitutions
}
vec( $insertions, $received . $_, 1 ) = 1 for 1 .. 9; ## all possible insertions after last digit.
}
printf STDERR "From %d received there were: %d new; %d known; %d deletions; %d insertions; %d transformations.\n",
$N, $new, $known, $deletion, $insertion, $transformed;
printf STDERR "Numbers processed at a rate of %.f/second\n", $N / ( time() - $start );
if( $SAVE ) {
open BIN, '>:raw', "$0.bin" or die $!;
printf BIN "%s%s%s%s", $knowns, $deletions, $insertions, $transformations;
close BIN;
}
__END__
C:\test>\Perl22\bin\perl.exe 1172842.pl -N=1e7 -SAVE > null
10000000 From 10000000 received there were: 8116086 new; 86155 known; 0 deletions; 9459 insertions; 0 transformations.
Numbers processed at a rate of 4082/second
C:\test>\Perl22\bin\perl.exe 1172842.pl -N=1e7 -SAVE -LOAD > null
10000000 From 10000000 received there were: 7963211 new; 8364929 known; 0 deletions; 24187 insertions; 0 transformations.
Numbers processed at a rate of 4218/second