packages feed

MicroHs-0.17.4.0 / lib / Data / Hashable.hs

-- Simplified Hashable, just to have somethingmodule Data.Hashable(  Hashable(..),  hashInt,  defaultSalt,  distinguisher,  ) where--import Data.Bitsimport qualified Data.ByteString as Bimport Data.Compleximport Data.Eqimport Data.Fixedimport Data.Intimport Data.Integer(_integerToIntList)import Data.List(foldl')import Data.Ratioimport qualified Data.Text as Timport Data.Wordtype Salt = Intinfixl 0 `hashWithSalt`class Eq a => Hashable a where  hashWithSalt :: Salt -> a -> Int  hash :: a -> Int  hash = hashWithSalt defaultSaltdefaultSalt :: SaltdefaultSalt = 0x087fc72chashUsing :: (Hashable b) => (a -> b) -> Salt -> a -> InthashUsing f salt x = hashWithSalt salt (f x)defaultHashWithSalt :: Hashable a => Int -> a -> IntdefaultHashWithSalt salt x = salt `hashInt` hash x-----------------instance Hashable Int where    hash = id    hashWithSalt = hashIntinstance Hashable Int8 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Int16 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Int32 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Int64 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Word where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Word8 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Word16 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Word32 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable Word64 where    hash = fromIntegral    hashWithSalt = defaultHashWithSaltinstance Hashable () where    hash _ = 0    hashWithSalt = defaultHashWithSaltinstance Hashable Bool where    hash = fromEnum    hashWithSalt = defaultHashWithSaltinstance Hashable Ordering where    hash = fromEnum    hashWithSalt = defaultHashWithSaltinstance Hashable Char where    hash = fromEnum    hashWithSalt = defaultHashWithSaltinstance Hashable Float where  hash = hash . show  hashWithSalt = defaultHashWithSaltinstance Hashable Double where  hash = hash . show  hashWithSalt = defaultHashWithSaltinstance Hashable Integer where  hashWithSalt s = hashWithSalt s . _integerToIntListinstance Hashable B.ByteString where  hashWithSalt s = hashWithSalt s . B.unpackinstance Hashable T.Text where  hashWithSalt s = hashWithSalt s . T.unpackdistinguisher :: Intdistinguisher = fromIntegral $ (maxBound :: Word) `quot` 3instance Hashable a => Hashable (Maybe a) where    hash Nothing = 0    hash (Just a) = distinguisher `hashWithSalt` a    hashWithSalt = defaultHashWithSaltinstance Hashable a => Hashable [a] where    hashWithSalt = foldl' hashWithSaltinstance (Hashable a, Hashable b) => Hashable (Either a b) where    hash (Left a)  = 0 `hashWithSalt` a    hash (Right b) = distinguisher `hashWithSalt` b    hashWithSalt = defaultHashWithSaltinstance Hashable a => Hashable (Complex a) where  hashWithSalt s (r :+ i) = s `hashWithSalt` r `hashWithSalt` iinstance Hashable a => Hashable (Ratio a) where  hashWithSalt s r = s `hashWithSalt` numerator r `hashWithSalt` denominator rinstance HasResolution a => Hashable (Fixed a) where  hashWithSalt s = hashWithSalt s . toRationalinstance (Hashable a1, Hashable a2) => Hashable (a1, a2) where  hashWithSalt s (a1, a2) = s `hashWithSalt` a1 `hashWithSalt` a2instance (Hashable a1, Hashable a2, Hashable a3) => Hashable (a1, a2, a3) where  hashWithSalt s (a1, a2, a3) = s `hashWithSalt` a1 `hashWithSalt` a2 `hashWithSalt` a3instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4) => Hashable (a1, a2, a3, a4) where  hashWithSalt s (a1, a2, a3, a4) = s `hashWithSalt` a1 `hashWithSalt` a2 `hashWithSalt` a3 `hashWithSalt` a4-------------- XXX This needs workhashInt :: Salt -> Int -> SalthashInt s x = s * 33 + x{--- This is what we'd like, but it uses primitives byteSwap# and timesWord2#hashInt :: Salt -> Int -> SalthashInt s x = fromIntegral (mixHash (fromIntegral s) (fromIntegral x))mulFold :: Word -> Word -> WordmulFold (W# x) (W# y) = case timesWord2# x y of    (# hi, lo #) -> W# (xor# hi lo)byteSwap :: Word -> WordbyteSwap (W# w) = W# (byteSwap# w)avalanche :: Word -> Wordavalanche z0 =#if WORD_SIZE_IN_BITS == 64   -- MurmurHash3Mixer    let z1 = shiftXorMultiply 33 0xff51afd7ed558ccd z0        z2 = shiftXorMultiply 33 0xc4ceb9fe1a85ec53 z1        z3 = shiftXor 33 z2    in z3#else   -- MurmurHash3Mixer 32bit    let z1 = shiftXorMultiply 16 0x85ebca6b z0        z2 = shiftXorMultiply 13 0xc2b2ae35 z1        z3 = shiftXor 16 z2    in z3#endifshiftXor :: Int -> Word -> WordshiftXor n w = w `xor` (w `unsafeShiftR` n)shiftXorMultiply :: Int -> Word -> Word -> WordshiftXorMultiply n k w = shiftXor n w * k-- | Mix hash is inspired by how xxh3 works on small (<=16byte) inputs.mixHash :: Word -> Word -> WordmixHash hi lo = avalanche (byteSwap lo + hi + mulFold hi lo)-}