Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions tests/Util/Key.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TypeApplications #-}

module Util.Key (Key(..), keyToInt, incKey, collisionAtHash) where

import Data.Bits (bit, (.&.))
import Data.Hashable (Hashable (hashWithSalt))
import Data.Word (Word16)
import GHC.Exts (Int (..), bitReverse#, int2Word#, word2Int#)
import GHC.Generics (Generic)
import Test.QuickCheck (Arbitrary (..), CoArbitrary (..), Function, Gen, Large)

Expand Down Expand Up @@ -51,8 +53,8 @@ arbitraryHash = do
, (1, QC.elements [-1, 0xFF, 0xFFF])
]
i <- QC.frequency gens
moreCollisions' <- QC.elements [moreCollisions, id]
pure (moreCollisions' i)
transform <- QC.elements [id, moreCollisions, bitReverse]
pure (transform i)

-- | Mask out most bits to produce more collisions
moreCollisions :: Int -> Int
Expand All @@ -62,6 +64,11 @@ moreCollisions w = fromIntegral (w .&. moreCollisionsMask)
moreCollisionsMask :: Int
moreCollisionsMask = sum [bit n | n <- [0, 3, 8, 14, 61]]

-- | Reverse order of bits, in order to generate variation in the
-- high bits, resulting in HashMap trees of greater height.
bitReverse :: Int -> Int
bitReverse (I# i) = I# (word2Int# (bitReverse# (int2Word# i)))

keyToInt :: Key -> Int
keyToInt (K h x) = h * fromEnum x

Expand Down