on master help sign in
merge Streamly.Fold.Internal with Streamly.Internal.Data.Fold
commit885a66f45a8d76a8a8e95a0347a176cce1d5732b authorHarendra Kumar <harendra.kumar@gmail.com> dateSun 22 Sep 2019, 22:07 (7 years ago) parent042b677
5 files changed+44−69
src/Streamly/Data/Fold.hs−1 src/Streamly/Fold/Internal.hs−66 src/Streamly/Internal.hs+1−1 src/Streamly/Internal/Data/Fold.hs+43 streamly.cabal−1
src/Streamly/Data/Fold.hs−1
@@ -244,4 +244,3 @@ import Prelude
244244 span, splitAt, break, mapM)
245245
246246import Streamly.Internal.Data.Fold
247−import Streamly.Internal.Data.Fold.Types
src/Streamly/Fold/Internal.hsdeleted−66
@@ -1,66 +0,0 @@
1−{-# LANGUAGE BangPatterns #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE ExistentialQuantification #-}
4−{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE RankNTypes #-}
6−{-# LANGUAGE RecordWildCards #-}
7−{-# LANGUAGE ScopedTypeVariables #-}
8−
9−-- |
10−-- Module : Streamly.Fold.Internal
11−-- Copyright : (c) 2019 Composewell Technologies
12−-- (c) 2013 Gabriel Gonzalez
13−-- License : BSD3
14−-- Maintainer : harendra.kumar@gmail.com
15−-- Stability : experimental
16−-- Portability : GHC
17−
18−module Streamly.Fold.Internal
19− (
20− rollingHash
21− , rollingHashWithSalt
22− -- , rollingHashFirstN
23− -- , rollingHashLastN
24− )
25−where
26−
27−import Streamly.Internal.Data.Fold.Types (Fold(..))
28−
29−-- | Compute an 'Int' sized polynomial rolling hash
30−--
31−-- > H = salt * k ^ n + c1 * k ^ (n - 1) + c2 * k ^ (n - 2) + ... + cn * k ^ 0
32−--
33−-- Where @c1@, @c2@, @cn@ are the elements in the input stream and @k@ is a
34−-- constant.
35−--
36−-- This hash is often used in Rabin-Karp string search algorithm.
37−--
38−-- See https://en.wikipedia.org/wiki/Rolling_hash
39−--
40−-- @since 0.7.0
41−{-# INLINABLE rollingHashWithSalt #-}
42−rollingHashWithSalt :: (Monad m, Enum a) => Int -> Fold m a Int
43−rollingHashWithSalt salt = Fold step initial extract
44− where
45− k = 2891336453
46− initial = return salt
47− step cksum a = return $ cksum * k + fromEnum a
48− extract = return
49−
50−-- | A default salt used in the implementation of 'rollingHash'.
51−{-# INLINE defaultSalt #-}
52−defaultSalt :: Int
53−#if WORD_SIZE_IN_BITS == 64
54−defaultSalt = 0xdc36d1615b7400a4
55−#else
56−defaultSalt = 0x087fc72c
57−#endif
58−
59−-- | Compute an 'Int' sized polynomial rolling hash of a stream.
60−--
61−-- > rollingHash = rollingHashWithSalt defaultSalt
62−--
63−-- @since 0.7.0
64−{-# INLINABLE rollingHash #-}
65−rollingHash :: (Monad m, Enum a) => Fold m a Int
66−rollingHash = rollingHashWithSalt defaultSalt
src/Streamly/Internal.hs+1−1
@@ -55,5 +55,5 @@ import Streamly.Streams.Combinators (inspectMode)
5555import Streamly.Unfold.Types (Unfold(..))
5656import Streamly.Unfold (fromList)
5757
58−import Streamly.Fold.Internal
58+import Streamly.Internal.Data.Fold
5959import Streamly.Internal.Data.Fold.Types
src/Streamly/Internal/Data/Fold.hs+43
@@ -50,6 +50,10 @@ module Streamly.Internal.Data.Fold
5050 , mean
5151 , variance
5252 , stdDev
53+ , rollingHash
54+ , rollingHashWithSalt
55+ -- , rollingHashFirstN
56+ -- , rollingHashLastN
5357
5458 -- ** Full Folds (Monoidal)
5559 , mconcat
@@ -486,6 +490,45 @@ variance = Fold step (return begin) (return . done)
486490stdDev :: (Monad m, Floating a) => Fold m a a
487491stdDev = sqrt variance
488492
493+-- | Compute an 'Int' sized polynomial rolling hash
494+--
495+-- > H = salt * k ^ n + c1 * k ^ (n - 1) + c2 * k ^ (n - 2) + ... + cn * k ^ 0
496+--
497+-- Where @c1@, @c2@, @cn@ are the elements in the input stream and @k@ is a
498+-- constant.
499+--
500+-- This hash is often used in Rabin-Karp string search algorithm.
501+--
502+-- See https://en.wikipedia.org/wiki/Rolling_hash
503+--
504+-- @since 0.7.0
505+{-# INLINABLE rollingHashWithSalt #-}
506+rollingHashWithSalt :: (Monad m, Enum a) => Int -> Fold m a Int
507+rollingHashWithSalt salt = Fold step initial extract
508+ where
509+ k = 2891336453
510+ initial = return salt
511+ step cksum a = return $ cksum * k + fromEnum a
512+ extract = return
513+
514+-- | A default salt used in the implementation of 'rollingHash'.
515+{-# INLINE defaultSalt #-}
516+defaultSalt :: Int
517+#if WORD_SIZE_IN_BITS == 64
518+defaultSalt = 0xdc36d1615b7400a4
519+#else
520+defaultSalt = 0x087fc72c
521+#endif
522+
523+-- | Compute an 'Int' sized polynomial rolling hash of a stream.
524+--
525+-- > rollingHash = rollingHashWithSalt defaultSalt
526+--
527+-- @since 0.7.0
528+{-# INLINABLE rollingHash #-}
529+rollingHash :: (Monad m, Enum a) => Fold m a Int
530+rollingHash = rollingHashWithSalt defaultSalt
531+
489532------------------------------------------------------------------------------
490533-- Monoidal left folds
491534------------------------------------------------------------------------------
streamly.cabal−1
@@ -288,7 +288,6 @@ library
288288 , Streamly.Unfold
289289
290290 -- Folds
291− , Streamly.Fold.Internal
292291 , Streamly.Sink.Types
293292 , Streamly.Sink
294293 , Streamly.Pipe.Types