Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
The rolling hash algorithm lets you create ( \(O(N)\) or \(O(N \log N)\) ) fastly ( \(O(1)\) ) comparable, concatanatable string slices.
Overview
Rolling hash of a string is calculated as a B-adic number. As an example, slice [2, 4] of a string "abcde" is given as this:
s := a b c d e s4 = b^4 a + b^3 b + b^2 c + b^1 d + b^0 e s2 = b^1 a + b^0 b s4 - s2 * b^3 = b^2 c + b^1 d + b^0 e
Cumulative sum-based implementation
Unless extreamly fast calculaton is required, RH
is better.
RollingHash
is a cumulative sum-based implementation. It needs to calculate
\(b^{n_1} / b^{n_2}\) for the inverse operation. Thus it holds a cache of \(\{b^{n}\}_{n}\) and
not so flexible.
Monoid-based implmentation
RH
is the monoid-based rolling hash implementation. Convert a string into a segment tree of
RH
and you win.
Synopsis
- data RH b p = RH {
- nextDigitRH :: !Int
- hashRH :: !Int
- rh1 :: forall b p. KnownNat b => Int -> RH b p
- type RHRepr = A2 Int
- data RollingHash b p = RollingHash {
- sourceLength :: !Int
- dimensions :: !(Vector Int)
- hashSum :: !(Vector Int)
- type HashInt = 100 :: Nat
- newRH :: forall p. KnownNat p => String -> RollingHash HashInt p
- lengthRH :: RollingHash b p -> Int
- data HashSlice p = HashSlice {
- hashValue :: !Int
- hashLength :: !Int
- sliceRH :: forall b p. KnownNat p => RollingHash b p -> Int -> Int -> HashSlice p
- consHS :: forall b p. KnownNat p => RollingHash b p -> HashSlice p -> HashSlice p -> HashSlice p
- emptyHS :: HashSlice p
- concatHS :: forall b p t. (KnownNat p, Foldable t) => RollingHash b p -> t (HashSlice p) -> HashSlice p
Documentation
Rolling hash monoid. Essensially a (Dual
) Affine2d ModInt
.
Typical prolbems
RH | |
|
Instances
data RollingHash b p #
Rolling hash of a string.
Internals
Slice (2, 4) of "abcdef" is given as this:
s := a b c d e s4 = b^4 a + b^3 b + b^2 c + b^1 d + b^0 e s2 = b^1 a + b^0 b s4 - s2 * b^3 = b^2 c + b^1 d + b^0 e
RollingHash | |
|
Instances
Show (RollingHash b p) # | |
Defined in Data.RollingHash showsPrec :: Int -> RollingHash b p -> ShowS # show :: RollingHash b p -> String # showList :: [RollingHash b p] -> ShowS # | |
Eq (RollingHash b p) # | |
Defined in Data.RollingHash (==) :: RollingHash b p -> RollingHash b p -> Bool # (/=) :: RollingHash b p -> RollingHash b p -> Bool # |
Type level integer that represents the B-adic number used for the rolling hash algorithm.
newRH :: forall p. KnownNat p => String -> RollingHash HashInt p #
\(O(N)\) Creates a rolling hash of given string.
lengthRH :: RollingHash b p -> Int #
\(O(1)\) Retrieves the original length of the RollingHash
string.
HashSlice value length
. See also the example of RollingHash
.
HashSlice | |
|
Instances
sliceRH :: forall b p. KnownNat p => RollingHash b p -> Int -> Int -> HashSlice p #
\(O(1)\) Slices a rolling hash string.