Skip to content

Commit

Permalink
Write a safer Hashable instance for Integer.
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed May 11, 2011
1 parent 3fa360e commit 9ae3602
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Data/Hashable.hs
Expand Up @@ -71,6 +71,8 @@ import Control.Concurrent (ThreadId)
import System.Mem.StableName
#endif

#include "MachDeps.h"

infixl 0 `combine`, `hashWithSalt`

------------------------------------------------------------------------
Expand Down Expand Up @@ -143,12 +145,18 @@ instance Hashable Word64 where
| bitSize (undefined :: Int) == 64 = fromIntegral n
| otherwise = fromIntegral (n `xor` (n `shiftR` 32))

instance Hashable Integer where hash = fromIntegral
instance Hashable Integer where
hash = foldl' hashWithSalt 0 . go
where
go n | inBounds n = [fromIntegral n :: Int]
| otherwise = fromIntegral n : go (n `shiftR` WORD_SIZE_IN_BITS)
maxInt = fromIntegral (maxBound :: Int)
inBounds x = x >= fromIntegral (minBound :: Int) && x <= maxInt

instance (Integral a, Hashable a) => Hashable (Ratio a) where
{-# SPECIALIZE instance Hashable (Ratio Integer) #-}
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
Expand Down

0 comments on commit 9ae3602

Please sign in to comment.