Skip to content

Instantly share code, notes, and snippets.

@jh3141
Created April 1, 2016 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jh3141/168fbbed42e24a224f3252d98c88d304 to your computer and use it in GitHub Desktop.
Save jh3141/168fbbed42e24a224f3252d98c88d304 to your computer and use it in GitHub Desktop.
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