module Data.Vector.Unboxed.Mutable ( Vector(..) )
where
import qualified Data.Vector.MVector as MVector
import Data.Vector.MVector ( MVector, MVectorPure )
import Data.Vector.Unboxed.Unbox
import GHC.Prim ( MutableByteArray#,
newByteArray#, sameMutableByteArray#, (+#) )
import GHC.ST ( ST(..) )
import GHC.Base ( Int(..) )
data Vector s a = Vector !Int
!Int
(MutableByteArray# s)
instance Unbox a => MVectorPure (Vector s) a where
length (Vector _ n _) = n
unsafeSlice (Vector i _ arr#) j m = Vector (i+j) m arr#
overlaps (Vector i m arr1#) (Vector j n arr2#)
= sameMutableByteArray# arr1# arr2#
&& (between i j (j+n) || between j i (i+m))
where
between x y z = x >= y && x < z
instance Unbox a => MVector (Vector s) (ST s) a where
unsafeNew (I# n#) = ST (\s# ->
case newByteArray# (size# (undefined :: a) n#) s# of
(# s2#, arr# #) -> (# s2#, Vector 0 (I# n#) arr# #)
)
unsafeRead (Vector (I# i#) _ arr#) (I# j#) = ST (read# arr# (i# +# j#))
unsafeWrite (Vector (I# i#) _ arr#) (I# j#) x = ST (\s# ->
case write# arr# (i# +# j#) x s# of s2# -> (# s2#, () #)
)
clear _ = return ()