--
-- Maintainer : libraries@haskell.org
-- Stability : experimental
--- Portability : non-portable
+-- Portability : non-portable (MPTCs, uses Control.Monad.ST)
--
-- Basis for IArray and MArray. Not intended for external consumption;
-- use IArray or MArray instead.
import Prelude
+import Control.Monad.ST.Lazy ( strictToLazyST )
+import qualified Control.Monad.ST.Lazy as Lazy (ST)
import Data.Ix ( Ix, range, index, rangeSize )
import Data.Int
import Data.Word
{-# INLINE unsafeAccumArray #-}
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
-#ifdef __GLASGOW_HASKELL__
instance IArray UArray Word where
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
+#ifdef __GLASGOW_HASKELL__
{-# INLINE unsafeAt #-}
unsafeAt (UArray _ _ arr#) (I# i#) = W# (indexWordArray# arr# i#)
+#endif
+#ifdef __HUGS__
+ unsafeAt = unsafeAtBArray
+#endif
{-# INLINE unsafeReplace #-}
unsafeReplace arr ies = runST (unsafeReplaceUArray arr ies)
{-# INLINE unsafeAccum #-}
unsafeAccum f arr ies = runST (unsafeAccumUArray f arr ies)
{-# INLINE unsafeAccumArray #-}
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
-#endif
instance IArray UArray (Ptr a) where
{-# INLINE unsafeArray #-}
instance Ix ix => Eq (UArray ix Int) where
(==) = eqUArray
-#ifdef __GLASGOW_HASKELL__
instance Ix ix => Eq (UArray ix Word) where
(==) = eqUArray
-#endif
instance Ix ix => Eq (UArray ix (Ptr a)) where
(==) = eqUArray
instance Ix ix => Ord (UArray ix Int) where
compare = cmpUArray
-#ifdef __GLASGOW_HASKELL__
instance Ix ix => Ord (UArray ix Word) where
compare = cmpUArray
-#endif
instance Ix ix => Ord (UArray ix (Ptr a)) where
compare = cmpUArray
instance (Ix ix, Show ix) => Show (UArray ix Int) where
showsPrec = showsIArray
-#ifdef __GLASGOW_HASKELL__
instance (Ix ix, Show ix) => Show (UArray ix Word) where
showsPrec = showsIArray
-#endif
instance (Ix ix, Show ix) => Show (UArray ix Float) where
showsPrec = showsIArray
unsafeRead :: Ix i => a i e -> Int -> m e
unsafeWrite :: Ix i => a i e -> Int -> e -> m ()
+ {-# INLINE newArray #-}
+ -- The INLINE is crucial, because until we know at least which monad
+ -- we are in, the code below allocates like crazy. So inline it,
+ -- in the hope that the context will know the monad.
newArray (l,u) init = do
marr <- newArray_ (l,u)
sequence_ [unsafeWrite marr i init | i <- [0 .. rangeSize (l,u) - 1]]
{-# INLINE unsafeWrite #-}
unsafeWrite = ArrST.unsafeWriteSTArray
------------------------------------------------------------------------------
--- Typeable instance for STArray
+instance MArray (STArray s) e (Lazy.ST s) where
+ {-# INLINE newArray #-}
+ newArray (l,u) e = strictToLazyST (ArrST.newSTArray (l,u) e)
+ {-# INLINE unsafeRead #-}
+ unsafeRead arr i = strictToLazyST (ArrST.unsafeReadSTArray arr i)
+ {-# INLINE unsafeWrite #-}
+ unsafeWrite arr i e = strictToLazyST (ArrST.unsafeWriteSTArray arr i e)
+#ifdef __HUGS__
INSTANCE_TYPEABLE3(STArray,sTArrayTc,"STArray")
+#endif
-----------------------------------------------------------------------------
-- Flat unboxed mutable arrays (ST monad)
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
+instance MArray (STUArray s) Word (ST s) where
+ newArray_ = newMBArray_
+ unsafeRead = unsafeReadMBArray
+ unsafeWrite = unsafeWriteMBArray
+
instance MArray (STUArray s) (Ptr a) (ST s) where
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray