Created
April 1, 2016 12:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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