Copyright | (C) 2015 mniip |
---|---|
License | BSD3 |
Maintainer | mniip <mniip@mniip.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell2010 |
Implementation of the prefix-function on cons-cells.
prefix-function has a simple implementation using arrays, the challenge,
however was to implement it on haskell lists (which are cons cells) without
losing any complexity. The following code uses a tying-the-knot data passing
structure, however no complicated laziness mechanisms are used: a KmpState
only depends on previous KmpState
s and therefore this algorithm can be
safely implemented in a strict language using pointers.
- data KmpState a = KmpState {}
- data GenericKmpState i a = GenericKmpState {
- gKmpTail :: [a]
- gKmpLength :: Maybe i
- gKmpPrev :: [GenericKmpState i a]
- kmpTraverse :: Eq a => [a] -> [KmpState a]
- kmpTraverseBy :: (a -> a -> Bool) -> [a] -> [KmpState a]
- gKmpTraverse :: (Num i, Eq a) => [a] -> [GenericKmpState i a]
- gKmpTraverseBy :: Num i => (a -> a -> Bool) -> [a] -> [GenericKmpState i a]
Documentation
data GenericKmpState i a Source
KmpState
generalized over the number type. Nothing
represents zero and
is used to avoid the Eq
constraint
GenericKmpState | |
|
kmpTraverse :: Eq a => [a] -> [KmpState a] Source
O(N). Compute the list of prefix-function states for a given input.
kmpTraverseBy :: (a -> a -> Bool) -> [a] -> [KmpState a] Source
O(N) and O(N) calls to the predicate. Compute the list of prefix-function
states using a given equality predicate. See prefixFunBy
for a detailed explanation of what predicates are allowed.
gKmpTraverse :: (Num i, Eq a) => [a] -> [GenericKmpState i a] Source
O(N) and O(N) plus-1's. Compute the list of prefix-function states using a given number type.
gKmpTraverseBy :: Num i => (a -> a -> Bool) -> [a] -> [GenericKmpState i a] Source
O(N), O(N) calls to the predicate, and O(N) plus-1's. Compute the list of prefix-function states using a given number type and a given equality predicate.