~ubuntu-branches/ubuntu/precise/ghc/precise

« back to all changes in this revision

Viewing changes to utils/haddock/tests/golden-tests/tests/Hash.hs

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2011-01-17 12:49:24 UTC
  • Revision ID: james.westby@ubuntu.com-20110117124924-do1pym1jlf5o636m
Tags: upstream-7.0.1
ImportĀ upstreamĀ versionĀ 7.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{- |
 
2
  Implementation of fixed-size hash tables, with a type 
 
3
  class for constructing hash values for structured types.
 
4
-}
 
5
module Hash (
 
6
  -- * The @HashTable@ type
 
7
  HashTable,
 
8
 
 
9
  -- ** Operations on @HashTable@s
 
10
  new, insert, lookup,
 
11
 
 
12
  -- * The @Hash@ class
 
13
  Hash(..),
 
14
 ) where
 
15
 
 
16
import Array
 
17
import Prelude hiding (lookup)
 
18
 
 
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)])
 
22
 
 
23
-- | Builds a new hash table with a given size
 
24
new :: (Eq key, Hash key) => Int -> IO (HashTable key val)
 
25
new = undefined
 
26
 
 
27
-- | Inserts a new element into the hash table
 
28
insert :: (Eq key, Hash key) => key -> val -> IO ()
 
29
insert = undefined
 
30
 
 
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)
 
34
lookup = undefined
 
35
 
 
36
-- | A class of types which can be hashed.
 
37
class Hash a where
 
38
   -- | hashes the value of type @a@ into an 'Int'
 
39
   hash :: a -> Int
 
40
 
 
41
instance Hash Int where
 
42
   hash = id
 
43
 
 
44
instance Hash Float where
 
45
   hash = trunc
 
46
 
 
47
instance (Hash a, Hash b) => Hash (a,b) where
 
48
   hash (a,b) = hash a `xor` hash b
 
49
 
 
50
trunc = undefined
 
51
xor = undefined