[KarpRabin] minor changes

parent 965fbb30
...@@ -35,6 +35,7 @@ import Data.String as S ...@@ -35,6 +35,7 @@ import Data.String as S
import Data.String (CodePoint) import Data.String (CodePoint)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.UInt (UInt, shl, fromInt) import Data.UInt (UInt, shl, fromInt)
import Partial.Unsafe (unsafePartial)
import Prelude import Prelude
...@@ -89,16 +90,15 @@ fromCodePoint c = fromInt (fromEnum c) ...@@ -89,16 +90,15 @@ fromCodePoint c = fromInt (fromEnum c)
-- | @'indicesOfAny'@ finds all occurrences of any of several non-empty patterns -- | @'indicesOfAny'@ finds all occurrences of any of several non-empty patterns
-- in a strict target string. If no non-empty patterns are given, -- in a strict target string. If no non-empty patterns are given,
-- the result is an empty array. Otherwise the result list contains -- the result is an empty array. Otherwise the result array contains
-- the pairs of all indices where any of the (non-empty) patterns start -- the pairs of all indices where any of the (non-empty) patterns start
-- and the array of all patterns starting at that index, the patterns being -- and the array of all patterns starting at that index, the patterns being
-- represented by their (zero-based) position in the pattern array. -- represented by their (zero-based) position in the pattern array.
-- Empty patterns are filtered out before processing begins. -- Empty patterns are filtered out before processing begins.
indicesOfAny :: Partial indicesOfAny :: Array String -- ^ Array of non-empty patterns
=> Array String -- ^ Array of non-empty patterns
-> String -- ^ String to search -> String -- ^ String to search
-> List (Tuple Int (Array Int)) -- ^ List of matches -> Array (Tuple Int (Array Int)) -- ^ Array of matches
indicesOfAny pats = if A.null nepats then const L.Nil indicesOfAny pats = if A.null nepats then const []
else strictMatcher nepats else strictMatcher nepats
where where
nepats = A.filter (not <<< S.null) pats nepats = A.filter (not <<< S.null) pats
...@@ -118,8 +118,8 @@ minimum1 a fa = ...@@ -118,8 +118,8 @@ minimum1 a fa =
Nothing -> a Nothing -> a
Just b -> min a b Just b -> min a b
strictMatcher :: Partial => Array String -> String -> List (Tuple Int (Array Int)) strictMatcher :: Array String -> String -> Array (Tuple Int (Array Int))
strictMatcher pats = search strictMatcher pats = unsafePartial search
where where
hLen = minimum1 32 (S.length <$> pats) hLen = minimum1 32 (S.length <$> pats)
hLen' = fromInt hLen hLen' = fromInt hLen
...@@ -141,8 +141,9 @@ strictMatcher pats = search ...@@ -141,8 +141,9 @@ strictMatcher pats = search
hashMap = hashMap =
M.fromFoldableWith (flip (<>)) M.fromFoldableWith (flip (<>))
(A.mapWithIndex (\i a -> Tuple (hash a) [i]) pats) (A.mapWithIndex (\i a -> Tuple (hash a) [i]) pats)
search str = if strLen < hLen then L.Nil search :: Partial => String -> Array (Tuple Int (Array Int))
else go 0 shash search str = if strLen < hLen then []
else A.fromFoldable (go 0 shash)
where where
strLen = S.length str strLen = S.length str
maxIdx = strLen - hLen maxIdx = strLen - hLen
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment