Skip to content

Commit

Permalink
Add instances for Integer and Ratio a
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed May 10, 2011
1 parent 7fb69a9 commit 3fa360e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Data/Hashable.hs
Expand Up @@ -42,6 +42,7 @@ import Data.Bits (bitSize, shiftL, shiftR, xor)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Data.List (foldl')
import Data.Ratio (Ratio, denominator, numerator)
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import qualified Data.ByteString.Unsafe as B
Expand Down Expand Up @@ -142,6 +143,13 @@ instance Hashable Word64 where
| bitSize (undefined :: Int) == 64 = fromIntegral n
| otherwise = fromIntegral (n `xor` (n `shiftR` 32))

instance Hashable Integer where hash = fromIntegral

This comment has been minimized.

Copy link
@tibbe

tibbe May 11, 2011

Does this really lead to good mixing of all bits in the Integer for Integers larger than maxBound :: Int? The fromIntegral call eventually calls toInt#

toInt# :: Integer -> Int#
toInt# (S# i)   = i
toInt# (J# s d) = integer2Int# s d

which calls integer2Int#

integer2Int# :: Int# -> ByteArray# -> Int#
integer2Int# s d = if s ==# 0#
                       then 0#
                       else let !v = indexIntArray# d 0# in
                            if s <# 0#
                               then negateInt# v
                               else v
instance (Integral a, Hashable a) => Hashable (Ratio a) where
hash a = hash (numerator a) `hashWithSalt` denominator a
{-# SPECIALIZE hash :: Ratio Integer -> Int #-}
hashWithSalt s a = s `hashWithSalt` numerator a `hashWithSalt` denominator a
{-# SPECIALIZE hashWithSalt :: Int -> Ratio Integer -> Int #-}

instance Hashable Float where
hash x
| isIEEE x =
Expand Down

0 comments on commit 3fa360e

Please sign in to comment.