Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function for testing insert/lookup times of cuckoo hash table with pathological data.
import Data.Hashable
import Control.Monad.ST
import qualified Data.HashTable.ST.Cuckoo as HT
data HashCollider = HashCollider String Int
deriving (Eq, Show)
instance Hashable HashCollider where
hashWithSalt salt (HashCollider name value) = salt + value
test :: Int -> Int -> [Maybe String]
test insertCount lookupCount = runST $ do
table <- HT.new
let keys = take insertCount (generateKeys 1)
values = take insertCount (generateValues 1)
mapM (uncurry $ HT.insert table) (zip keys values)
sequence (map (HT.lookup table) (take lookupCount $ cycle keys))
generateKeys :: Int -> [HashCollider]
generateKeys n = (HashCollider ("key" ++ show n) 1234) : generateKeys (n+1)
generateValues :: Int -> [String]
generateValues n = ("value" ++ show n) : generateValues (n+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment