From: Ian Lynagh Date: Tue, 21 Aug 2007 14:07:06 +0000 (+0000) Subject: Fix hashInt X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=0c685143cbdfcfc3964cf945f8df1b2f3cb0cef8;p=ghc-base.git Fix hashInt As pointed out in http://www.haskell.org/pipermail/glasgow-haskell-bugs/2007-August/009545.html the old behaviour was Prelude Data.HashTable> map hashInt [0..10] [0,-1,-1,-2,-2,-2,-3,-3,-4,-4,-4] Fixed according to the "Fibonacci Hashing" algorithm described in http://www.brpreiss.com/books/opus4/html/page213.html http://www.brpreiss.com/books/opus4/html/page214.html --- diff --git a/Data/HashTable.hs b/Data/HashTable.hs index 0cee737..391876f 100644 --- a/Data/HashTable.hs +++ b/Data/HashTable.hs @@ -189,13 +189,13 @@ golden :: Int32 golden = -1640531527 -- | A sample (and useful) hash function for Int and Int32, --- implemented by extracting the uppermost 32 bits of the 64-bit +-- implemented by extracting the lowermost 32 bits of the -- result of multiplying by a 32-bit constant. The constant is from -- Knuth, derived from the golden ratio: -- -- > golden = round ((sqrt 5 - 1) * 2^31) :: Int hashInt :: Int -> Int32 -hashInt x = mulHi (fromIntegral x) golden +hashInt x = fromIntegral x * golden -- hi 32 bits of a x-bit * 32 bit -> 64-bit multiply mulHi :: Int32 -> Int32 -> Int32 @@ -208,7 +208,7 @@ mulHi a b = fromIntegral (r `shiftR` 32) -- -- > hashString = foldl' f 0 -- > where f m c = fromIntegral (fromEnum c + 1) * golden + mulHi m golden --- +-- -- Note that this has not been extensively tested for reasonability, -- but Knuth argues that repeated multiplication by the golden ratio -- will minimize gaps in the hash space.