|
| 1 | +{-# LANGUAGE ForeignFunctionInterface #-} |
| 2 | + |
| 3 | +----------------------------------------------------------------------------- |
| 4 | +-- | |
| 5 | +-- Module : Data.Hash |
| 6 | +-- Copyright : (c) Milan Straka 2010 |
| 7 | +-- License : BSD-style |
| 8 | + |
| 9 | +-- Stability : provisional |
| 10 | +-- Portability : portable |
| 11 | +-- |
| 12 | +-- 'Hashable' class for hashable types, with instances for basic types. The only |
| 13 | +-- function of this class is |
| 14 | +-- |
| 15 | +-- @ |
| 16 | +-- 'hash' :: Hashable h => h -> Int |
| 17 | +-- @ |
| 18 | +-- |
| 19 | +-- The 'hash' function should be as collision-free as possible, the probability |
| 20 | +-- of @'hash' a == 'hash' b@ should ideally be 1 over the number of representable |
| 21 | +-- values in an 'Int'. |
| 22 | +-- |
| 23 | +-- Returning an 'Int' is a result of the 'Data.IntMap.IntMap' using 'Int' as |
| 24 | +-- a key, as inserting the hash values to the 'Data.IntMap.IntMap' was the |
| 25 | +-- purpose of creating this class. |
| 26 | +----------------------------------------------------------------------------- |
| 27 | + |
| 28 | +module Data.Hashable ( Hashable(..) |
| 29 | + , combine |
| 30 | + ) where |
| 31 | + |
| 32 | +import Data.Bits |
| 33 | +import Data.Int |
| 34 | +import Data.Word |
| 35 | +import Data.List (foldl') |
| 36 | +import qualified Data.ByteString as B |
| 37 | +import qualified Data.ByteString.Internal as BInt |
| 38 | +import qualified Data.ByteString.Unsafe as BInt |
| 39 | +import qualified Data.ByteString.Lazy as BL |
| 40 | +import qualified Data.ByteString.Lazy.Internal as BLInt |
| 41 | +import Foreign.C |
| 42 | + |
| 43 | +-- | The class containing a function 'hash' which computes the hash values of |
| 44 | +-- given value. |
| 45 | +class Hashable a where |
| 46 | + -- | The computed 'hash' value should be as collision-free as possible, the |
| 47 | + -- probability of @'hash' a == 'hash' b@ should ideally be 1 over the |
| 48 | + -- number of representable values in an 'Int'. |
| 49 | + hash :: a -> Int |
| 50 | + |
| 51 | +-- | Combines two given hash values. |
| 52 | +combine :: Int -> Int -> Int |
| 53 | +combine h1 h2 = (h1 + h1 `shiftL` 5) `xor` h2 |
| 54 | + |
| 55 | +hashAndCombine :: Hashable h => Int -> h -> Int |
| 56 | +hashAndCombine acc h = acc `combine` hash h |
| 57 | + |
| 58 | +instance Hashable () where hash _ = 0 |
| 59 | + |
| 60 | +instance Hashable Bool where hash x = case x of { True -> 1; False -> 0 } |
| 61 | + |
| 62 | +instance Hashable Int where hash = id |
| 63 | +instance Hashable Int8 where hash = fromIntegral |
| 64 | +instance Hashable Int16 where hash = fromIntegral |
| 65 | +instance Hashable Int32 where hash = fromIntegral |
| 66 | +instance Hashable Int64 where hash = fromIntegral |
| 67 | + |
| 68 | +instance Hashable Word where hash = fromIntegral |
| 69 | +instance Hashable Word8 where hash = fromIntegral |
| 70 | +instance Hashable Word16 where hash = fromIntegral |
| 71 | +instance Hashable Word32 where hash = fromIntegral |
| 72 | +instance Hashable Word64 where hash = fromIntegral |
| 73 | + |
| 74 | +instance Hashable Char where hash = fromEnum |
| 75 | + |
| 76 | +instance Hashable a => Hashable (Maybe a) where |
| 77 | + hash Nothing = 0 |
| 78 | + hash (Just a) = 42 `combine` hash a |
| 79 | + |
| 80 | +instance (Hashable a1, Hashable a2) => Hashable (a1, a2) where |
| 81 | + hash (a1, a2) = hash a1 `combine` hash a2 |
| 82 | + |
| 83 | +instance (Hashable a1, Hashable a2, Hashable a3) => Hashable (a1, a2, a3) where |
| 84 | + hash (a1, a2, a3) = hash a1 `combine` hash a2 `combine` hash a3 |
| 85 | + |
| 86 | +instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4) => Hashable (a1, a2, a3, a4) where |
| 87 | + hash (a1, a2, a3, a4) = hash a1 `combine` hash a2 `combine` hash a3 `combine` hash a4 |
| 88 | + |
| 89 | +instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5) |
| 90 | + => Hashable (a1, a2, a3, a4, a5) where |
| 91 | + hash (a1, a2, a3, a4, a5) = |
| 92 | + hash a1 `combine` hash a2 `combine` hash a3 `combine` hash a4 `combine` hash a5 |
| 93 | + |
| 94 | +instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5, Hashable a6) |
| 95 | + => Hashable (a1, a2, a3, a4, a5, a6) where |
| 96 | + hash (a1, a2, a3, a4, a5, a6) = |
| 97 | + hash a1 `combine` hash a2 `combine` hash a3 `combine` hash a4 `combine` hash a5 `combine` hash a6 |
| 98 | + |
| 99 | +instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5, Hashable a6, Hashable a7) |
| 100 | + => Hashable (a1, a2, a3, a4, a5, a6, a7) where |
| 101 | + hash (a1, a2, a3, a4, a5, a6, a7) = |
| 102 | + hash a1 `combine` hash a2 `combine` hash a3 `combine` hash a4 `combine` hash a5 `combine` hash a6 `combine` hash a7 |
| 103 | + |
| 104 | +instance Hashable a => Hashable [a] where |
| 105 | + {-# SPECIALIZE instance Hashable [Char] #-} |
| 106 | + hash = foldl' hashAndCombine 0 |
| 107 | + |
| 108 | +foreign import ccall unsafe hashByteString :: CString -> CInt -> IO CInt |
| 109 | +instance Hashable B.ByteString where |
| 110 | + hash bstr = fromIntegral $ BInt.inlinePerformIO $ BInt.unsafeUseAsCStringLen bstr $ |
| 111 | + \(str, len) -> hashByteString str (fromIntegral len) |
| 112 | + |
| 113 | +instance Hashable BL.ByteString where hash = BLInt.foldlChunks hashAndCombine 0 |
0 commit comments