module Data.Vector.Mutable ( Vector(..) )
where
import qualified Data.Vector.MVector as MVector
import Data.Vector.MVector ( MVector, MVectorPure )
import GHC.Prim ( MutableArray#,
newArray#, readArray#, writeArray#, sameMutableArray#, (+#) )
import GHC.ST ( ST(..) )
import GHC.Base ( Int(..) )
data Vector s a = Vector !Int
!Int
(MutableArray# s a)
instance 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#)
= sameMutableArray# arr1# arr2#
&& (between i j (j+n) || between j i (i+m))
where
between x y z = x >= y && x < z
instance MVector (Vector s) (ST s) a where
unsafeNew = unsafeNew
unsafeNewWith = unsafeNewWith
unsafeRead (Vector (I# i#) _ arr#) (I# j#) = ST (readArray# arr# (i# +# j#))
unsafeWrite (Vector (I# i#) _ arr#) (I# j#) x = ST (\s# ->
case writeArray# arr# (i# +# j#) x s# of s2# -> (# s2#, () #)
)
clear v = MVector.set v uninitialised
uninitialised :: a
uninitialised = error "Data.Vector.Mutable: uninitialised elemen t"
unsafeNew :: Int -> ST s (Vector s a)
unsafeNew n = unsafeNewWith n uninitialised
unsafeNewWith :: Int -> a -> ST s (Vector s a)
unsafeNewWith (I# n#) x = ST (\s# ->
case newArray# n# x s# of
(# s2#, arr# #) -> (# s2#, Vector 0 (I# n#) arr# #)
)