{-# LANGUAGE Trustworthy #-}{-# LANGUAGE CPP #-}#ifdef __GLASGOW_HASKELL__{-# LANGUAGE MagicHash, DeriveDataTypeable #-}#endif------------------------------------------------------------------------------- |-- Module : Data.Unique-- Copyright : (c) The University of Glasgow 2001-- License : BSD-style (see the file libraries/base/LICENSE)-- -- Maintainer : libraries@haskell.org-- Stability : experimental-- Portability : non-portable---- An abstract interface to a unique symbol generator.-------------------------------------------------------------------------------moduleData.Unique(-- * Unique objectsUnique,-- instance (Eq, Ord)newUnique,-- :: IO UniquehashUnique-- :: Unique -> Int)whereimportPreludeimportSystem.IO.Unsafe(unsafePerformIO)#ifdef __GLASGOW_HASKELL__importGHC.BaseimportGHC.NumimportData.TypeableimportData.IORef#endif-- | An abstract unique object. Objects of type 'Unique' may be-- compared for equality and ordering and hashed into 'Int'.newtypeUnique=UniqueIntegerderiving(Eq,Ord#ifdef __GLASGOW_HASKELL__,Typeable#endif)uniqSource::IORefIntegeruniqSource=unsafePerformIO(newIORef0){-# NOINLINE uniqSource #-}-- | Creates a new object of type 'Unique'. The value returned will-- not compare equal to any other value of type 'Unique' returned by-- previous calls to 'newUnique'. There is no limit on the number of-- times 'newUnique' may be called.newUnique::IOUniquenewUnique=dor<-atomicModifyIORefuniqSource$\x->letz=x+1in(z,z)r`seq`return(Uniquer)-- SDM (18/3/2010): changed from MVar to STM. This fixes-- 1. there was no async exception protection-- 2. there was a space leak (now new value is strict)-- 3. using atomicModifyIORef would be slightly quicker, but can-- suffer from adverse scheduling issues (see #3838)-- 4. also, the STM version is faster.-- SDM (30/4/2012): changed to IORef using atomicModifyIORef. Reasons:-- 1. STM version could not be used inside unsafePerformIO, if it-- happened to be poked inside an STM transaction.-- 2. IORef version can be used with unsafeIOToSTM inside STM,-- because if the transaction retries then we just get a new-- Unique.-- 3. IORef version is very slightly faster.-- | Hashes a 'Unique' into an 'Int'. Two 'Unique's may hash to the-- same value, although in practice this is unlikely. The 'Int'-- returned makes a good hash key.hashUnique::Unique->Int#if defined(__GLASGOW_HASKELL__)hashUnique(Uniquei)=I#(hashIntegeri)#elsehashUnique(Uniqueu)=fromInteger(u`mod`(toInteger(maxBound::Int)+1))#endif