X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FArray%2FST.hs;h=828ae63ffa6d4b3e2a955e1098746a38ab0adbb1;hb=74bc2d04fdbae494bcf4839c4ec5e6ec1d0bf600;hp=cd81d8309c82739d2c2b4b506cf708dbb9ab3094;hpb=c54cf4e02e743cc50dd403812bd7a31ff5f583bc;p=haskell-directory.git diff --git a/Data/Array/ST.hs b/Data/Array/ST.hs index cd81d83..828ae63 100644 --- a/Data/Array/ST.hs +++ b/Data/Array/ST.hs @@ -6,9 +6,9 @@ -- -- Maintainer : libraries@haskell.org -- Stability : experimental --- Portability : non-portable +-- Portability : non-portable (uses Data.Array.MArray) -- --- Mutable boxed and unboxed arrays in the 'ST' monad. +-- Mutable boxed and unboxed arrays in the 'Control.Monad.ST.ST' monad. -- ----------------------------------------------------------------------------- @@ -16,12 +16,12 @@ module Data.Array.ST ( -- * Boxed arrays STArray, -- instance of: Eq, MArray + runSTArray, -#ifdef __GLASGOW_HASKELL__ -- * Unboxed arrays STUArray, -- instance of: Eq, MArray + runSTUArray, castSTUArray, -- :: STUArray s i a -> ST s (STUArray s i b) -#endif -- * Overloaded mutable array interface module Data.Array.MArray, @@ -30,19 +30,48 @@ module Data.Array.ST ( import Prelude import Data.Array.MArray +import Data.Array.Base ( STUArray, castSTUArray, UArray, unsafeFreezeSTUArray ) +import Control.Monad.ST ( ST, runST ) + #ifdef __HUGS__ -import Hugs.ST -#else -import Data.Array.Base hiding (MArray(..)) +import Hugs.Array ( Array ) +import Hugs.ST ( STArray, unsafeFreezeSTArray ) #endif #ifdef __GLASGOW_HASKELL__ -import GHC.Arr -import GHC.ST - --- | Casts an 'STUArray' with one element type into one with a --- different element type. All the elements of the resulting array --- are undefined (unless you know what you\'re doing...). -castSTUArray :: STUArray s ix a -> ST s (STUArray s ix b) -castSTUArray (STUArray l u marr#) = return (STUArray l u marr#) +import GHC.Arr ( STArray, Array, unsafeFreezeSTArray ) #endif + +-- | A safe way to create and work with a mutable array before returning an +-- immutable array for later perusal. This function avoids copying +-- the array before returning it - it uses 'unsafeFreeze' internally, but +-- this wrapper is a safe interface to that function. +-- +runSTArray :: (Ix i) + => (forall s . ST s (STArray s i e)) + -> Array i e +runSTArray st = runST (st >>= unsafeFreezeSTArray) + +-- | A safe way to create and work with an unboxed mutable array before +-- returning an immutable array for later perusal. This function +-- avoids copying the array before returning it - it uses +-- 'unsafeFreeze' internally, but this wrapper is a safe interface to +-- that function. +-- +runSTUArray :: (Ix i) + => (forall s . ST s (STUArray s i e)) + -> UArray i e +runSTUArray st = runST (st >>= unsafeFreezeSTUArray) + + +-- INTERESTING... this is the type we'd like to give to runSTUArray: +-- +-- runSTUArray :: (Ix i, IArray UArray e, +-- forall s. MArray (STUArray s) e (ST s)) +-- => (forall s . ST s (STUArray s i e)) +-- -> UArray i e +-- +-- Note the quantified constraint. We dodged the problem by using +-- unsafeFreezeSTUArray directly in the defn of runSTUArray above, but +-- this essentially constrains us to a single unsafeFreeze for all STUArrays +-- (in theory we might have a different one for certain element types).