]> gitweb.factorcode.org Git - factor.git/commitdiff
suffix-arrays: simplify a bit.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 15 Apr 2016 20:20:11 +0000 (13:20 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 15 Apr 2016 20:20:11 +0000 (13:20 -0700)
basis/suffix-arrays/suffix-arrays.factor

index d4695554e98ed6256624c91529315450c2d81758..92c3fa2921c91d918ede47e5c4895d2e059177a5 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2008 Marc Fauconneau.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors binary-search fry kernel math math.order
-parser sequences sets sorting ;
+USING: accessors binary-search fry kernel math math.order parser
+sequences sets sorting ;
 IN: suffix-arrays
 
 <PRIVATE
@@ -15,26 +15,24 @@ IN: suffix-arrays
 : find-index ( begin suffix-array -- index/f )
     [ prefix<=> ] with search drop ;
 
-: from-to ( index begin suffix-array -- from/f to/f )
-    swap '[ _ head? not ]
-    [ find-last-from drop dup [ 1 + ] when ]
-    [ find-from drop ] 3bi ;
+: query-from ( index begin suffix-array -- from )
+    swap '[ _ head? not ] find-last-from drop [ 1 + ] [ 0 ] if* ;
 
-: <funky-slice> ( from/f to/f seq -- slice )
-    [
-        [ drop 0 or ] [ length or ] bi-curry bi*
-        [ min ] keep
-    ] keep <slice> ; inline
+: query-to ( index begin suffix-array -- to )
+    [ swap '[ _ head? not ] find-from drop ] [ length or ] bi ;
+
+: query-range ( index begin suffix-array -- from to )
+    [ query-from ] [ query-to ] 3bi [ min ] keep ;
+
+: (query) ( index begin suffix-array -- matches )
+    [ query-range ] keep <slice> [ seq>> ] map members ;
 
 PRIVATE>
 
-: >suffix-array ( seq -- array )
-    members
-    [ suffixes ] map concat natural-sort ;
+: >suffix-array ( seq -- suffix-array )
+    members [ suffixes ] map concat natural-sort ;
 
 SYNTAX: SA{ \ } [ >suffix-array ] parse-literal ;
 
 : query ( begin suffix-array -- matches )
-    2dup find-index dup
-    [ -rot [ from-to ] keep <funky-slice> [ seq>> ] map members ]
-    [ 3drop { } ] if ;
+    [ find-index ] 2keep '[ _ _ (query) ] [ { } ] if* ;