module Data.Vector.Mutable (
MVector(..), IOVector, STVector,
length, overlaps, slice, new, newWith, read, write, swap,
clear, set, copy, grow,
unsafeSlice, unsafeNew, unsafeNewWith, unsafeRead, unsafeWrite,
unsafeCopy, unsafeGrow
) where
import qualified Data.Vector.Generic.Mutable as G
import Data.Primitive.Array
import Control.Monad.Primitive
import Prelude hiding ( length, read )
import Data.Typeable ( Typeable )
#include "vector.h"
data MVector s a = MVector !Int
!Int
!(MutableArray s a)
deriving ( Typeable )
type IOVector = MVector RealWorld
type STVector s = MVector s
instance G.MVector MVector a where
basicLength (MVector _ n _) = n
basicUnsafeSlice j m (MVector i n arr) = MVector (i+j) m arr
basicOverlaps (MVector i m arr1) (MVector 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
basicUnsafeNew n
= do
arr <- newArray n uninitialised
return (MVector 0 n arr)
basicUnsafeNewWith n x
= do
arr <- newArray n x
return (MVector 0 n arr)
basicUnsafeRead (MVector i n arr) j = readArray arr (i+j)
basicUnsafeWrite (MVector i n arr) j x = writeArray arr (i+j) x
basicClear v = G.set v uninitialised
uninitialised :: a
uninitialised = error "Data.Vector.Mutable: uninitialised element"
unsafeSlice :: Int
-> Int
-> MVector s a
-> MVector s a
unsafeSlice = G.unsafeSlice
unsafeNew :: PrimMonad m => Int -> m (MVector (PrimState m) a)
unsafeNew = G.unsafeNew
unsafeNewWith :: PrimMonad m => Int -> a -> m (MVector (PrimState m) a)
unsafeNewWith = G.unsafeNewWith
unsafeRead :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
unsafeRead = G.unsafeRead
unsafeWrite :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
unsafeWrite = G.unsafeWrite
unsafeSwap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
unsafeSwap = G.unsafeSwap
unsafeCopy :: PrimMonad m => MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
unsafeCopy = G.unsafeCopy
unsafeGrow :: PrimMonad m
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
unsafeGrow = G.unsafeGrow
length :: MVector s a -> Int
length = G.length
overlaps :: MVector s a -> MVector s a -> Bool
overlaps = G.overlaps
slice :: Int -> Int -> MVector s a -> MVector s a
slice = G.slice
new :: PrimMonad m => Int -> m (MVector (PrimState m) a)
new = G.new
newWith :: PrimMonad m => Int -> a -> m (MVector (PrimState m) a)
newWith = G.newWith
read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
read = G.read
write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
write = G.write
swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
swap = G.swap
clear :: PrimMonad m => MVector (PrimState m) a -> m ()
clear = G.clear
set :: PrimMonad m => MVector (PrimState m) a -> a -> m ()
set = G.set
copy :: PrimMonad m
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
copy = G.copy
grow :: PrimMonad m
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
grow = G.grow