{-# LANGUAGE BangPatterns #-}
module Util.Seed (
Seed(to_seed), (&)
) where
import qualified Data.Text as Text
import qualified Data.Text.Array as Text.Array
import qualified Data.Text.Internal as Text.Internal
import qualified Data.Word as Word
import qualified Foreign
import qualified System.IO.Unsafe as Unsafe
class Seed a where
to_seed :: Int -> a -> Int
(&) :: Seed a => Int -> a -> Int
& :: forall a. Seed a => Int -> a -> Int
(&) = forall a. Seed a => Int -> a -> Int
to_seed
infixl 1 &
combine :: Int -> Int -> Int
combine :: Int -> Int -> Int
combine = forall a. Num a => a -> a -> a
(+)
instance Seed Int where
to_seed :: Int -> Int -> Int
to_seed Int
salt Int
n = Int -> Int -> Int
combine Int
salt Int
n
instance Seed Word.Word64 where
to_seed :: Int -> Word64 -> Int
to_seed Int
salt Word64
n = Int
salt Int -> Int -> Int
`combine` forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n
instance Seed Word.Word32 where
to_seed :: Int -> Word32 -> Int
to_seed Int
salt Word32
n = Int
salt Int -> Int -> Int
`combine` forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
n
instance Seed Double where
to_seed :: Int -> Double -> Int
to_seed Int
salt Double
n = forall a. Seed a => Int -> a -> Int
to_seed Int
salt
((forall a. IO a -> a
Unsafe.unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
Foreign.with Double
n forall a b. (a -> b) -> a -> b
$
forall a. Storable a => Ptr a -> IO a
Foreign.peek forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Ptr a -> Ptr b
Foreign.castPtr) :: Word.Word64)
instance Seed Text.Text where
to_seed :: Int -> Text -> Int
to_seed = Int -> Text -> Int
simple_text
simple_text :: Int -> Text.Text -> Int
simple_text :: Int -> Text -> Int
simple_text = forall a. (a -> Char -> a) -> a -> Text -> a
Text.foldl' (\Int
n -> (Int
n+) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum)
_prim_text :: Int -> Text.Text -> Int
_prim_text :: Int -> Text -> Int
_prim_text Int
salt Text
t = Int
salt Int -> Int -> Int
`combine` Text -> Int
prim_text_sum Text
t
prim_text_sum :: Text.Text -> Int
prim_text_sum :: Text -> Int
prim_text_sum (Text.Internal.Text Array
array Int
offset Int
len) = forall {t}. Num t => t -> Int -> t
go Int
0 Int
offset
where
go :: t -> Int -> t
go !t
accum Int
offset
| Int
offset forall a. Ord a => a -> a -> Bool
< Int
end =
let !v :: t
v = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Array -> Int -> Word8
Text.Array.unsafeIndex Array
array Int
offset
in t -> Int -> t
go (t
accum forall a. Num a => a -> a -> a
+ t
v) (Int
offsetforall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = t
accum
end :: Int
end = Int
offset forall a. Num a => a -> a -> a
+ Int
len