Paste: word-suffix-array 2
Author: | prunedtree |
Mode: | factor |
Date: | Wed, 1 Oct 2008 10:39:17 |
Plain Text |
USING: kernel arrays math accessors sequences math.vectors math.order sorting
binary-search sets assocs fry ;
IN: suffix-array
: load first2 [ name>> ] dip tail-slice ;
: suffix<=> [ load ] bi@ <=> ;
: suffixes dup name>> length [ 2array ] with map ;
: new-sa
[ suffixes ] map concat [ suffix<=> ] sort ;
: find-one
[ [ load ] dip 2dup head? [ 2drop +eq+ ] [ swap <=> ] if ] curry search drop ;
: (fetch-more)
[ [ dupd nth load ] dip head? ] dip swap slip ;
: fetch-more
swap [ [ (fetch-more) ] 3curry ] dip
swap [ dup ] [ ] produce nip ;
: query-sa
[ swap 2dup find-one dup
'[ [ 1 - ] [ 1 + ] [ [ _ _ _ ] dip fetch-more ] bi@ _ ] call
1array 3array concat
] keep [ nth first ] curry map unique values ;
New Annotation