{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

-- |
-- Module: Data.Hash.Class.Mutable
-- Copyright: Copyright © 2021-2024 Lars Kuhtz <lakuhtz@gmail.com>
-- License: MIT
-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com>
-- Stability: experimental
--
-- Class of Salted Pure Hashes
--
module Data.Hash.Class.Mutable
( Hash(..)
, IncrementalHash(..)

-- * Hash functions
, hashPtr
, hashStorable
, hashByteString
, hashByteStringLazy
, hashShortByteString
, hashByteArray

-- ** Pure variants of hash functions
--
-- The following pure variants of the hash functions are implemented with
-- 'unsafePerformIO'. This is generally less efficient than running them
-- directly in 'IO'. Often the performance difference does not matter. However,
-- when many hashes are computed one should prefer the variants that run in
-- 'IO'. When a 'ResetableHash' instance is available it provides the most
-- efficient way to compute many hashes in a tight loop.

, hashPtr_
, hashStorable_
, hashByteString_
, hashByteStringLazy_
, hashShortByteString_
, hashByteArray_

-- * Incremental Hashing
, updateByteString
, updateByteStringLazy
, updateShortByteString
, updateStorable
, updateByteArray

-- * Resetable Hashes
, ResetableHash(..)
) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Short as BS
import Data.Word

import Foreign.Ptr
import Foreign.Storable

import GHC.Exts

import System.IO.Unsafe

-- internal modules

import Data.Hash.Class.Mutable.Internal

-- -------------------------------------------------------------------------- --
-- Class of Mutable Hashes

class IncrementalHash a => Hash a where
    initialize :: IO (Context a)

-- -------------------------------------------------------------------------- --
-- Hash Functions

hashPtr :: forall a . Hash a => Ptr Word8 -> Int -> IO a
hashPtr :: forall a. Hash a => Ptr Word8 -> Int -> IO a
hashPtr Ptr Word8
p Int
n = do
    Context a
ctx <- forall a. Hash a => IO (Context a)
initialize @a
    forall a.
IncrementalHash a =>
Context a -> Ptr Word8 -> Int -> IO ()
update @a Context a
ctx Ptr Word8
p Int
n
    Context a -> IO a
forall a. IncrementalHash a => Context a -> IO a
finalize Context a
ctx
{-# INLINE hashPtr #-}

hashByteString :: forall a . Hash a => B.ByteString -> IO a
hashByteString :: forall a. Hash a => ByteString -> IO a
hashByteString ByteString
b = do
    Context a
ctx <- forall a. Hash a => IO (Context a)
initialize @a
    forall a. IncrementalHash a => Context a -> ByteString -> IO ()
updateByteString @a Context a
ctx ByteString
b
    Context a -> IO a
forall a. IncrementalHash a => Context a -> IO a
finalize Context a
ctx
{-# INLINE hashByteString #-}

hashByteStringLazy :: forall a . Hash a => BL.ByteString -> IO a
hashByteStringLazy :: forall a. Hash a => ByteString -> IO a
hashByteStringLazy ByteString
b = do
    Context a
ctx <- forall a. Hash a => IO (Context a)
initialize @a
    forall a. IncrementalHash a => Context a -> ByteString -> IO ()
updateByteStringLazy @a Context a
ctx ByteString
b
    Context a -> IO a
forall a. IncrementalHash a => Context a -> IO a
finalize Context a
ctx
{-# INLINE hashByteStringLazy #-}

hashShortByteString :: forall a . Hash a => BS.ShortByteString -> IO a
hashShortByteString :: forall a. Hash a => ShortByteString -> IO a
hashShortByteString ShortByteString
b = do
    Context a
ctx <- forall a. Hash a => IO (Context a)
initialize @a
    forall a.
IncrementalHash a =>
Context a -> ShortByteString -> IO ()
updateShortByteString @a Context a
ctx ShortByteString
b
    Context a -> IO a
forall a. IncrementalHash a => Context a -> IO a
finalize Context a
ctx
{-# INLINE hashShortByteString #-}

hashStorable :: forall a b . Hash a => Storable b => b -> IO a
hashStorable :: forall a b. (Hash a, Storable b) => b -> IO a
hashStorable b
b = do
    Context a
ctx <- forall a. Hash a => IO (Context a)
initialize @a
    forall a b.
(IncrementalHash a, Storable b) =>
Context a -> b -> IO ()
updateStorable @a Context a
ctx b
b
    Context a -> IO a
forall a. IncrementalHash a => Context a -> IO a
finalize Context a
ctx
{-# INLINE hashStorable #-}

hashByteArray :: forall a . Hash a => ByteArray# -> IO a
hashByteArray :: forall a. Hash a => ByteArray# -> IO a
hashByteArray ByteArray#
b = do
    Context a
ctx <- forall a. Hash a => IO (Context a)
initialize @a
    forall a. IncrementalHash a => Context a -> ByteArray# -> IO ()
updateByteArray @a Context a
ctx ByteArray#
b
    Context a -> IO a
forall a. IncrementalHash a => Context a -> IO a
finalize Context a
ctx
{-# INLINE hashByteArray #-}

-- --------------------------------------------------------------------------
-- Pure variants of hashes

hashPtr_ :: forall a . Hash a => Ptr Word8 -> Int -> a
hashPtr_ :: forall a. Hash a => Ptr Word8 -> Int -> a
hashPtr_ Ptr Word8
a = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> (Int -> IO a) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Word8 -> Int -> IO a
forall a. Hash a => Ptr Word8 -> Int -> IO a
hashPtr Ptr Word8
a
{-# INLINE hashPtr_ #-}

hashByteString_ :: forall a . Hash a => B.ByteString -> a
hashByteString_ :: forall a. Hash a => ByteString -> a
hashByteString_ = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> (ByteString -> IO a) -> ByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> IO a
forall a. Hash a => ByteString -> IO a
hashByteString
{-# INLINE hashByteString_ #-}

hashByteStringLazy_ :: forall a . Hash a => BL.ByteString -> a
hashByteStringLazy_ :: forall a. Hash a => ByteString -> a
hashByteStringLazy_ = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> (ByteString -> IO a) -> ByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> IO a
forall a. Hash a => ByteString -> IO a
hashByteStringLazy
{-# INLINE hashByteStringLazy_ #-}

hashShortByteString_ :: forall a . Hash a => BS.ShortByteString -> a
hashShortByteString_ :: forall a. Hash a => ShortByteString -> a
hashShortByteString_ = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> (ShortByteString -> IO a) -> ShortByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> IO a
forall a. Hash a => ShortByteString -> IO a
hashShortByteString
{-# INLINE hashShortByteString_ #-}

hashStorable_ :: forall a b . Hash a => Storable b => b -> a
hashStorable_ :: forall a b. (Hash a, Storable b) => b -> a
hashStorable_ = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> (b -> IO a) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> IO a
forall a b. (Hash a, Storable b) => b -> IO a
hashStorable
{-# INLINE hashStorable_ #-}

hashByteArray_ :: forall a . Hash a => ByteArray# -> a
hashByteArray_ :: forall a. Hash a => ByteArray# -> a
hashByteArray_ ByteArray#
a = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ ByteArray# -> IO a
forall a. Hash a => ByteArray# -> IO a
hashByteArray ByteArray#
a
{-# INLINE hashByteArray_ #-}