From 6f3f4e8fb85be5dd0874af3f956f6b6f3b47a402 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 28 Apr 2015 19:28:14 -0700 Subject: [PATCH] math.transforms.bwt: faster versions of Burrows-Wheeler Transform. --- extra/math/transforms/bwt/bwt-tests.factor | 4 ++-- extra/math/transforms/bwt/bwt.factor | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/extra/math/transforms/bwt/bwt-tests.factor b/extra/math/transforms/bwt/bwt-tests.factor index a5b9856bca..9491bf644e 100644 --- a/extra/math/transforms/bwt/bwt-tests.factor +++ b/extra/math/transforms/bwt/bwt-tests.factor @@ -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 diff --git a/extra/math/transforms/bwt/bwt.factor b/extra/math/transforms/bwt/bwt.factor index b7054c9d3e..35844580e5 100644 --- a/extra/math/transforms/bwt/bwt.factor +++ b/extra/math/transforms/bwt/bwt.factor @@ -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 [ { } ] keep ] keep - '[ _ [ prefix ] 2map natural-sort ] times - [ last 0 = ] find nip but-last ; +: ibwt ( i newseq -- seq ) + [ length ] + [ sort-values '[ _ nth first2 ] ] + [ replicate-as ] tri nip ;