1 -----------------------------------------------------------------------------
3 -- Module : Data.Unique
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- $Id: Unique.hs,v 1.1 2001/07/04 10:48:39 simonmar Exp $
13 -- An infinite supply of unique objects, supporting ordering and equality.
15 -----------------------------------------------------------------------------
18 Unique, -- instance (Eq, Ord)
19 newUnique, -- :: IO Unique
20 hashUnique -- :: Unique -> Int
25 import Control.Concurrent
26 import System.IO.Unsafe (unsafePerformIO)
28 #ifdef __GLASGOW_HASKELL__
30 import GHC.Num ( Integer(..) )
33 newtype Unique = Unique Integer deriving (Eq,Ord)
35 uniqSource :: MVar Integer
36 uniqSource = unsafePerformIO (newMVar 0)
37 {-# NOINLINE uniqSource #-}
39 newUnique :: IO Unique
41 val <- takeMVar uniqSource
43 putMVar uniqSource next
46 hashUnique :: Unique -> Int
47 #ifdef __GLASGOW_HASKELL__
48 hashUnique (Unique (S# i)) = I# i
49 hashUnique (Unique (J# s d)) | s ==# 0# = 0
50 | otherwise = I# (indexIntArray# d 0#)
52 hashUnique (Unique u) = u `mod` (fromIntegral (maxBound :: Int) + 1)