gpt4 book ai didi

haskell - Data.ByteString 中的 findSubstrings 和 breakSubstring

转载 作者:行者123 更新时间:2023-12-04 15:26:35 24 4
gpt4 key购买 nike

来源Data/ByteString.hs它说函数findSubstrings已弃用,取而代之的是 breakSubstring .但是我认为 findSubstrings使用 KMP 算法实现的算法比 breakSubstring 中使用的算法高效得多。这是一个天真的。有人知道为什么会这样做吗?

这是旧的实现:

{-# DEPRECATED findSubstrings "findSubstrings is deprecated in favour of breakSubstring." #-}

{-
{- This function uses the Knuth-Morris-Pratt string matching algorithm. -}

findSubstrings pat@(PS _ _ m) str@(PS _ _ n) = search 0 0
where
patc x = pat `unsafeIndex` x
strc x = str `unsafeIndex` x

-- maybe we should make kmpNext a UArray before using it in search?
kmpNext = listArray (0,m) (-1:kmpNextL pat (-1))
kmpNextL p _ | null p = []
kmpNextL p j = let j' = next (unsafeHead p) j + 1
ps = unsafeTail p
x = if not (null ps) && unsafeHead ps == patc j'
then kmpNext Array.! j' else j'
in x:kmpNextL ps j'
search i j = match ++ rest -- i: position in string, j: position in pattern
where match = if j == m then [(i - j)] else []
rest = if i == n then [] else search (i+1) (next (strc i) j + 1)
next c j | j >= 0 && (j == m || c /= patc j) = next c (kmpNext Array.! j)
| otherwise = j
-}

这是新的天真的:
findSubstrings :: ByteString -- ^ String to search for.
-> ByteString -- ^ String to seach in.
-> [Int]
findSubstrings pat str
| null pat = [0 .. length str]
| otherwise = search 0 str
where
STRICT2(search)
search n s
| null s = []
| pat `isPrefixOf` s = n : search (n+1) (unsafeTail s)
| otherwise = search (n+1) (unsafeTail s)

最佳答案

改变的原因是 KMP 的实现实际上比着眼于性能的 naive 版本效率更低。

关于haskell - Data.ByteString 中的 findSubstrings 和 breakSubstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6331269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com