module Data.Primitive.Array (
Array(..), MutableArray(..),
newArray, readArray, writeArray, indexArray, indexArrayM,
unsafeFreezeArray, unsafeThawArray, sameMutableArray
) where
import Control.Monad.Primitive
import GHC.Base ( Int(..) )
import GHC.Prim
import Data.Typeable ( Typeable )
import Data.Data ( Data(..), mkNorepType )
data Array a = Array (Array# a) deriving ( Typeable )
data MutableArray s a = MutableArray (MutableArray# s a)
deriving ( Typeable )
newArray :: PrimMonad m => Int -> a -> m (MutableArray (PrimState m) a)
newArray (I# n#) x = primitive
(\s# -> case newArray# n# x s# of
(# s'#, arr# #) -> (# s'#, MutableArray arr# #))
readArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> m a
readArray (MutableArray arr#) (I# i#) = primitive (readArray# arr# i#)
writeArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray (MutableArray arr#) (I# i#) x = primitive_ (writeArray# arr# i# x)
indexArray :: Array a -> Int -> a
indexArray (Array arr#) (I# i#) = case indexArray# arr# i# of (# x #) -> x
indexArrayM :: Monad m => Array a -> Int -> m a
indexArrayM (Array arr#) (I# i#)
= case indexArray# arr# i# of (# x #) -> return x
unsafeFreezeArray :: PrimMonad m => MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray (MutableArray arr#)
= primitive (\s# -> case unsafeFreezeArray# arr# s# of
(# s'#, arr'# #) -> (# s'#, Array arr'# #))
unsafeThawArray :: PrimMonad m => Array a -> m (MutableArray (PrimState m) a)
unsafeThawArray (Array arr#)
= primitive (\s# -> case unsafeThawArray# arr# s# of
(# s'#, arr'# #) -> (# s'#, MutableArray arr'# #))
sameMutableArray :: MutableArray s a -> MutableArray s a -> Bool
sameMutableArray (MutableArray arr#) (MutableArray brr#)
= sameMutableArray# arr# brr#
instance Typeable a => Data (Array a) where
toConstr _ = error "toConstr"
gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "Data.Primitive.Array.Array"
instance (Typeable s, Typeable a) => Data (MutableArray s a) where
toConstr _ = error "toConstr"
gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "Data.Primitive.Array.MutableArray"