math.transforms.bwt: faster versions of Burrows-Wheeler Transform.

db4
John Benediktsson 2015-04-28 19:28:14 -07:00
parent 16213f8779
commit 6f3f4e8fb8
2 changed files with 12 additions and 11 deletions

View File

@ -1,6 +1,6 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: strings tools.test ;
USING: tools.test ;
IN: math.transforms.bwt
{ "asdf" } [ "asdf" bwt ibwt >string ] unit-test
{ "asdf" } [ "asdf" bwt ibwt ] unit-test

View File

@ -1,15 +1,16 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: arrays fry kernel math sequences sequences.extras
sorting ;
USING: assocs fry kernel sequences sequences.rotated sorting ;
IN: math.transforms.bwt
! Inefficient versions of Burrows-Wheeler Transform
! Semi-efficient versions of Burrows-Wheeler Transform
: bwt ( seq -- newseq )
0 suffix all-rotations natural-sort [ last ] map ;
: bwt ( seq -- i newseq )
dup all-rotations natural-sort
[ [ sequence= ] with find drop ]
[ [ last ] rot map-as ] 2bi ;
: ibwt ( newseq -- seq )
[ length [ { } <array> ] keep ] keep
'[ _ [ prefix ] 2map natural-sort ] times
[ last 0 = ] find nip but-last ;
: ibwt ( i newseq -- seq )
[ length ]
[ <enum> sort-values '[ _ nth first2 ] ]
[ replicate-as ] tri nip ;