2
Implementation of fixed-size hash tables, with a type
3
class for constructing hash values for structured types.
6
-- * The @HashTable@ type
9
-- ** Operations on @HashTable@s
17
import Prelude hiding (lookup)
19
-- | A hash table with keys of type @key@ and values of type @val@.
20
-- The type @key@ should be an instance of 'Eq'.
21
data HashTable key val = HashTable Int (Array Int [(key,val)])
23
-- | Builds a new hash table with a given size
24
new :: (Eq key, Hash key) => Int -> IO (HashTable key val)
27
-- | Inserts a new element into the hash table
28
insert :: (Eq key, Hash key) => key -> val -> IO ()
31
-- | Looks up a key in the hash table, returns @'Just' val@ if the key
32
-- was found, or 'Nothing' otherwise.
33
lookup :: Hash key => key -> IO (Maybe val)
36
-- | A class of types which can be hashed.
38
-- | hashes the value of type @a@ into an 'Int'
41
instance Hash Int where
44
instance Hash Float where
47
instance (Hash a, Hash b) => Hash (a,b) where
48
hash (a,b) = hash a `xor` hash b