fix various typos; cleanup and fully document boyer-moore
parent
19bb304391
commit
c5b38ebfd7
|
@ -514,7 +514,7 @@ PRIVATE>
|
||||||
: 4seq ( parser1 parser2 parser3 parser4 -- parser )
|
: 4seq ( parser1 parser2 parser3 parser4 -- parser )
|
||||||
4array seq ;
|
4array seq ;
|
||||||
|
|
||||||
: seq* ( quot -- paser )
|
: seq* ( quot -- parser )
|
||||||
{ } make seq ; inline
|
{ } make seq ; inline
|
||||||
|
|
||||||
: choice ( seq -- parser )
|
: choice ( seq -- parser )
|
||||||
|
@ -529,7 +529,7 @@ PRIVATE>
|
||||||
: 4choice ( parser1 parser2 parser3 parser4 -- parser )
|
: 4choice ( parser1 parser2 parser3 parser4 -- parser )
|
||||||
4array choice ;
|
4array choice ;
|
||||||
|
|
||||||
: choice* ( quot -- paser )
|
: choice* ( quot -- parser )
|
||||||
{ } make choice ; inline
|
{ } make choice ; inline
|
||||||
|
|
||||||
: repeat0 ( parser -- parser )
|
: repeat0 ( parser -- parser )
|
||||||
|
|
|
@ -5,13 +5,22 @@ IN: boyer-moore
|
||||||
|
|
||||||
HELP: <boyer-moore>
|
HELP: <boyer-moore>
|
||||||
{ $values
|
{ $values
|
||||||
{ "pat" sequence } { "bm" boyer-moore }
|
{ "pattern" sequence } { "boyer-moore" boyer-moore }
|
||||||
}
|
}
|
||||||
{ $description
|
{ $description
|
||||||
"Given a pattern performs pattern preprocessing and returns "
|
"Given a pattern performs pattern preprocessing and returns "
|
||||||
"results as an (opaque) object that is reusable across "
|
"results as an (opaque) object that is reusable across "
|
||||||
"searches in different sequences via " { $link search-from }
|
"searches in different sequences via " { $link search-from } "."
|
||||||
" generic word."
|
} { $examples
|
||||||
|
{ $example
|
||||||
|
"USING: boyer-moore prettyprint ;"
|
||||||
|
"\"abc\" <boyer-moore> ."
|
||||||
|
"T{ boyer-moore
|
||||||
|
{ pattern \"abc\" }
|
||||||
|
{ bad-char-table H{ { 97 0 } { 98 -1 } { 99 -2 } } }
|
||||||
|
{ good-suffix-table { 3 3 1 } }
|
||||||
|
}"
|
||||||
|
}
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
HELP: search-from
|
HELP: search-from
|
||||||
|
@ -21,12 +30,18 @@ HELP: search-from
|
||||||
{ "obj" object }
|
{ "obj" object }
|
||||||
{ "i/f" "the index of first match or " { $link f } }
|
{ "i/f" "the index of first match or " { $link f } }
|
||||||
}
|
}
|
||||||
{ $description "Performs an attempt to find the first "
|
{ $contract "Performs an attempt to find the first "
|
||||||
"occurrence of pattern in " { $snippet "seq" }
|
"occurrence of pattern in " { $snippet "seq" }
|
||||||
" starting from " { $snippet "from" } " using "
|
" starting from " { $snippet "from" } " using "
|
||||||
"Boyer-Moore search algorithm. Output is the index "
|
"Boyer-Moore search algorithm. Output is the index "
|
||||||
"if the attempt was succeessful and " { $link f }
|
"if the attempt was succeessful, or " { $link f }
|
||||||
" otherwise."
|
" otherwise."
|
||||||
|
} { $examples
|
||||||
|
{ $example
|
||||||
|
"USING: boyer-moore prettyprint ;"
|
||||||
|
"{ 1 2 7 10 20 2 7 10 } 3 { 2 7 10 } search-from ."
|
||||||
|
"5"
|
||||||
|
}
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
HELP: search
|
HELP: search
|
||||||
|
@ -37,21 +52,27 @@ HELP: search
|
||||||
}
|
}
|
||||||
{ $description "A simpler variant of " { $link search-from }
|
{ $description "A simpler variant of " { $link search-from }
|
||||||
" that starts searching from the beginning of the sequence."
|
" that starts searching from the beginning of the sequence."
|
||||||
|
} { $examples
|
||||||
|
{ $example
|
||||||
|
"USING: boyer-moore prettyprint ;"
|
||||||
|
"\"Source string\" \"ce st\" search ."
|
||||||
|
"4"
|
||||||
|
}
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
ARTICLE: "boyer-moore" "The Boyer-Moore algorithm"
|
ARTICLE: "boyer-moore" "The Boyer-Moore algorithm"
|
||||||
{ $heading "Summary" }
|
{ $heading "Summary" }
|
||||||
"The " { $vocab-link "boyer-moore" } " vocabulary "
|
"The " { $vocab-link "boyer-moore" } " vocabulary "
|
||||||
"implements a Boyer-Moore string search algorithm with "
|
"implements a Boyer-Moore string search algorithm with the "
|
||||||
"so-called 'strong good suffix shift rule'. Since algorithm is "
|
"so-called 'strong good suffix shift rule'. Since the algorithm is "
|
||||||
"alphabet-independent it is applicable to searching in any "
|
"alphabet-independent, it is applicable to searching in any "
|
||||||
"collection that implements " { $links "sequence-protocol" } "."
|
"collection that implements the " { $links "sequence-protocol" } "."
|
||||||
|
|
||||||
{ $heading "Complexity" }
|
{ $heading "Complexity" }
|
||||||
"Let " { $snippet "n" } " and " { $snippet "m" } " be lengths "
|
"Let " { $snippet "n" } " and " { $snippet "m" } " be the lengths "
|
||||||
"of the sequences being searched " { $emphasis "in" } " and "
|
"of the sequences being searched " { $emphasis "in" } " and "
|
||||||
{ $emphasis "for" } " respectively. Then searching runs in "
|
{ $emphasis "for" } " respectively. Then searching runs in "
|
||||||
{ $snippet "O(n)" } " time in its worst case using additional "
|
{ $snippet "O(n)" } " time worst-case, using additional "
|
||||||
{ $snippet "O(m)" } " space. The preprocessing phase runs in "
|
{ $snippet "O(m)" } " space. The preprocessing phase runs in "
|
||||||
{ $snippet "O(m)" } " time."
|
{ $snippet "O(m)" } " time."
|
||||||
;
|
;
|
||||||
|
|
|
@ -21,26 +21,26 @@ IN: boyer-moore
|
||||||
[ length dup ] [ <reversed> ] bi
|
[ length dup ] [ <reversed> ] bi
|
||||||
[ (partial-suffixes) ] map-index 2nip ; inline
|
[ (partial-suffixes) ] map-index 2nip ; inline
|
||||||
|
|
||||||
: <gs-table> ( seq -- table )
|
: <good-suffix-table> ( seq -- table )
|
||||||
z-values [ partial-suffixes ] [ normal-suffixes ] bi
|
z-values [ partial-suffixes ] [ normal-suffixes ] bi
|
||||||
[ [ nip ] when* ] 2map reverse! ; inline
|
[ [ nip ] when* ] 2map reverse! ; inline
|
||||||
|
|
||||||
: insert-bc-shift ( table elt len i -- table )
|
: insert-bad-char-shift ( table elt len i -- table )
|
||||||
1 + swap - swap pick 2dup key?
|
1 + swap - swap pick 2dup key?
|
||||||
[ 3drop ] [ set-at ] if ; inline
|
[ 3drop ] [ set-at ] if ; inline
|
||||||
|
|
||||||
: <bc-table> ( seq -- table )
|
: <bad-char-table> ( seq -- table )
|
||||||
H{ } clone swap [ length ] keep
|
H{ } clone swap [ length ] keep
|
||||||
[ insert-bc-shift ] with each-index ; inline
|
[ insert-bad-char-shift ] with each-index ; inline
|
||||||
|
|
||||||
TUPLE: boyer-moore pattern bc-table gs-table ;
|
TUPLE: boyer-moore pattern bad-char-table good-suffix-table ;
|
||||||
|
|
||||||
: gs-shift ( i c bm -- s ) nip gs-table>> nth-unsafe ; inline
|
: good-suffix-shift ( i c boyer-moore -- s ) nip good-suffix-table>> nth-unsafe ; inline
|
||||||
|
|
||||||
: bc-shift ( i c bm -- s ) bc-table>> at dup 1 ? + ; inline
|
: bad-char-shift ( i c boyer-moore -- s ) bad-char-table>> at dup 1 ? + ; inline
|
||||||
|
|
||||||
: do-shift ( pos i c bm -- newpos )
|
: do-shift ( pos i c boyer-moore -- newpos )
|
||||||
[ gs-shift ] [ bc-shift ] bi-curry 2bi max + ; inline
|
[ good-suffix-shift ] [ bad-char-shift ] bi-curry 2bi max + ; inline
|
||||||
|
|
||||||
: match? ( i1 s1 i2 s2 -- ? ) [ nth-unsafe ] 2bi@ = ; inline
|
: match? ( i1 s1 i2 s2 -- ? ) [ nth-unsafe ] 2bi@ = ; inline
|
||||||
|
|
||||||
|
@ -48,23 +48,23 @@ TUPLE: boyer-moore pattern bc-table gs-table ;
|
||||||
len 1 - [ [ pos + s1 ] keep s2 match? not ]
|
len 1 - [ [ pos + s1 ] keep s2 match? not ]
|
||||||
find-last-integer ; inline
|
find-last-integer ; inline
|
||||||
|
|
||||||
:: (search-from) ( seq from bm -- i/f )
|
:: (search-from) ( seq from boyer-moore -- i/f )
|
||||||
bm pattern>> :> pat
|
boyer-moore pattern>> :> pat
|
||||||
pat length :> plen
|
pat length :> plen
|
||||||
seq length plen - :> lim
|
seq length plen - :> lim
|
||||||
from
|
from
|
||||||
[
|
[
|
||||||
dup lim <=
|
dup lim <=
|
||||||
[
|
[
|
||||||
seq pat pick plen mismatch?
|
seq pat pick plen mismatch?
|
||||||
[ 2dup + seq nth-unsafe bm do-shift t ] [ f ] if*
|
[ 2dup + seq nth-unsafe boyer-moore do-shift t ] [ f ] if*
|
||||||
] [ drop f f ] if
|
] [ drop f f ] if
|
||||||
] loop ; inline
|
] loop ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: <boyer-moore> ( pat -- bm )
|
: <boyer-moore> ( pattern -- boyer-moore )
|
||||||
dup <reversed> [ <bc-table> ] [ <gs-table> ] bi
|
dup <reversed> [ <bad-char-table> ] [ <good-suffix-table> ] bi
|
||||||
boyer-moore boa ;
|
boyer-moore boa ;
|
||||||
|
|
||||||
GENERIC: search-from ( seq from obj -- i/f )
|
GENERIC: search-from ( seq from obj -- i/f )
|
||||||
|
|
Loading…
Reference in New Issue