X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FUnique.hs;h=6f8c24f0e3dc3da0f641904d3307f68c7903e1ad;hb=88c71497ea34789b25d87486548a88539af2ecde;hp=3e093d31e5a579a1856d7c05b412870fef7e1c87;hpb=746ef6a7fd71bb1e9ebe3cd107c5f9f79f3b7a68;p=ghc-base.git diff --git a/Data/Unique.hs b/Data/Unique.hs index 3e093d3..6f8c24f 100644 --- a/Data/Unique.hs +++ b/Data/Unique.hs @@ -2,38 +2,45 @@ -- | -- Module : Data.Unique -- Copyright : (c) The University of Glasgow 2001 --- License : BSD-style (see the file libraries/core/LICENSE) +-- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : experimental -- Portability : non-portable -- --- An infinite supply of unique objects, supporting ordering and equality. +-- An abstract interface to a unique symbol generator. -- ----------------------------------------------------------------------------- module Data.Unique ( - Unique, -- instance (Eq, Ord) - newUnique, -- :: IO Unique - hashUnique -- :: Unique -> Int + -- * Unique objects + Unique, -- instance (Eq, Ord) + newUnique, -- :: IO Unique + hashUnique -- :: Unique -> Int ) where import Prelude -import Control.Concurrent +import Control.Concurrent.MVar import System.IO.Unsafe (unsafePerformIO) #ifdef __GLASGOW_HASKELL__ import GHC.Base -import GHC.Num ( Integer(..) ) +import GHC.Num #endif +-- | An abstract unique object. Objects of type 'Unique' may be +-- compared for equality and ordering and hashed into 'Int'. newtype Unique = Unique Integer deriving (Eq,Ord) uniqSource :: MVar Integer uniqSource = unsafePerformIO (newMVar 0) {-# 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 :: IO Unique newUnique = do val <- takeMVar uniqSource @@ -41,11 +48,12 @@ newUnique = do putMVar uniqSource next return (Unique next) +-- | 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 -#ifdef __GLASGOW_HASKELL__ -hashUnique (Unique (S# i)) = I# i -hashUnique (Unique (J# s d)) | s ==# 0# = 0 - | otherwise = I# (indexIntArray# d 0#) +#if defined(__GLASGOW_HASKELL__) +hashUnique (Unique i) = I# (hashInteger i) #else -hashUnique (Unique u) = u `mod` (fromIntegral (maxBound :: Int) + 1) +hashUnique (Unique u) = fromInteger (u `mod` (toInteger (maxBound :: Int) + 1)) #endif