Paste: word-suffix-array
Author: | prunedtree |
Mode: | factor |
Date: | Wed, 1 Oct 2008 09:57:29 |
Plain Text |
USING: kernel arrays math accessors sequences math.vectors math.order sorting
binary-search sets assocs ;
IN: suffix-array
: load first2 [ name>> ] dip tail-slice ;
: suffix<=> [ load ] bi@ <=> ;
: suffixes [ name>> length ] keep [ swap 2array ] curry map ;
: true-reduce
over length 0 = not
[ [ unclip swap ] dip each ]
[ 2drop f ] if ;
: new-sa
[ suffixes ] map [ append ] true-reduce
[ suffix<=> ] sort ;
: find-one
[ load 2dup swap head? [ 2drop +eq+ ] [ <=> ] if ] with search drop ;
: (fetch-more)
[ pick swap nth load swap head? ] dip swap slip ;
: fetch-more
swap [ [ (fetch-more) ] 3curry ] dip
swap [ dup ] [ ] produce nip ;
: 3with with with with ; inline
: query-sa
[ 2dup find-one
[ [ 1 - ] [ fetch-more ] 3with [ 1 + ] swap bi@ ] keep
1array 3array [ append ] true-reduce
] keep [ nth first ] curry map unique values ;
New Annotation