1 -----------------------------------------------------------------------------
3 -- Module : Data.Unique
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- An infinite supply of unique objects, supporting ordering and equality.
13 -----------------------------------------------------------------------------
16 Unique, -- instance (Eq, Ord)
17 newUnique, -- :: IO Unique
18 hashUnique -- :: Unique -> Int
23 import Control.Concurrent
24 import System.IO.Unsafe (unsafePerformIO)
26 #ifdef __GLASGOW_HASKELL__
28 import GHC.Num ( Integer(..) )
31 newtype Unique = Unique Integer deriving (Eq,Ord)
33 uniqSource :: MVar Integer
34 uniqSource = unsafePerformIO (newMVar 0)
35 {-# NOINLINE uniqSource #-}
37 newUnique :: IO Unique
39 val <- takeMVar uniqSource
41 putMVar uniqSource next
44 hashUnique :: Unique -> Int
45 #ifdef __GLASGOW_HASKELL__
46 hashUnique (Unique (S# i)) = I# i
47 hashUnique (Unique (J# s d)) | s ==# 0# = 0
48 | otherwise = I# (indexIntArray# d 0#)
50 hashUnique (Unique u) = u `mod` (fromIntegral (maxBound :: Int) + 1)