module Data.Vector.Primitive.Mutable (
MVector(..), IOVector, STVector, Prim,
length, null,
slice, init, tail, take, drop, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
overlaps,
new, unsafeNew, replicate, replicateM, clone,
grow, unsafeGrow,
clear,
read, write, swap,
unsafeRead, unsafeWrite, unsafeSwap,
set, copy, move, unsafeCopy, unsafeMove
) where
import qualified Data.Vector.Generic.Mutable as G
import Data.Primitive.ByteArray
import Data.Primitive ( Prim, sizeOf )
import Control.Monad.Primitive
import Control.Monad ( liftM )
import Prelude hiding ( length, null, replicate, reverse, map, read,
take, drop, splitAt, init, tail )
import Data.Typeable ( Typeable )
#include "vector.h"
data MVector s a = MVector !Int
!Int
!(MutableByteArray s)
deriving ( Typeable )
type IOVector = MVector RealWorld
type STVector s = MVector s
instance Prim a => 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)
= sameMutableByteArray arr1 arr2
&& (between i j (j+n) || between j i (i+m))
where
between x y z = x >= y && x < z
basicUnsafeNew n = MVector 0 n
`liftM` newByteArray (n * sizeOf (undefined :: a))
basicUnsafeRead (MVector i n arr) j = readByteArray arr (i+j)
basicUnsafeWrite (MVector i n arr) j x = writeByteArray arr (i+j) x
basicUnsafeCopy (MVector i n dst) (MVector j _ src)
= copyMutableByteArray dst (i*sz) src (j*sz) (n*sz)
where
sz = sizeOf (undefined :: a)
basicUnsafeMove (MVector i n dst) (MVector j _ src)
= moveByteArray dst (i*sz) src (j*sz) (n * sz)
where
sz = sizeOf (undefined :: a)
length :: Prim a => MVector s a -> Int
length = G.length
null :: Prim a => MVector s a -> Bool
null = G.null
slice :: Prim a => Int -> Int -> MVector s a -> MVector s a
slice = G.slice
take :: Prim a => Int -> MVector s a -> MVector s a
take = G.take
drop :: Prim a => Int -> MVector s a -> MVector s a
drop = G.drop
splitAt :: Prim a => Int -> MVector s a -> (MVector s a, MVector s a)
splitAt = G.splitAt
init :: Prim a => MVector s a -> MVector s a
init = G.init
tail :: Prim a => MVector s a -> MVector s a
tail = G.tail
unsafeSlice :: Prim a
=> Int
-> Int
-> MVector s a
-> MVector s a
unsafeSlice = G.unsafeSlice
unsafeTake :: Prim a => Int -> MVector s a -> MVector s a
unsafeTake = G.unsafeTake
unsafeDrop :: Prim a => Int -> MVector s a -> MVector s a
unsafeDrop = G.unsafeDrop
unsafeInit :: Prim a => MVector s a -> MVector s a
unsafeInit = G.unsafeInit
unsafeTail :: Prim a => MVector s a -> MVector s a
unsafeTail = G.unsafeTail
overlaps :: Prim a => MVector s a -> MVector s a -> Bool
overlaps = G.overlaps
new :: (PrimMonad m, Prim a) => Int -> m (MVector (PrimState m) a)
new = G.new
unsafeNew :: (PrimMonad m, Prim a) => Int -> m (MVector (PrimState m) a)
unsafeNew = G.unsafeNew
replicate :: (PrimMonad m, Prim a) => Int -> a -> m (MVector (PrimState m) a)
replicate = G.replicate
replicateM :: (PrimMonad m, Prim a) => Int -> m a -> m (MVector (PrimState m) a)
replicateM = G.replicateM
clone :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a -> m (MVector (PrimState m) a)
clone = G.clone
grow :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
grow = G.grow
unsafeGrow :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
unsafeGrow = G.unsafeGrow
clear :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> m ()
clear = G.clear
read :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m a
read = G.read
write :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m ()
write = G.write
swap :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> Int -> m ()
swap = G.swap
unsafeRead :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m a
unsafeRead = G.unsafeRead
unsafeWrite
:: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m ()
unsafeWrite = G.unsafeWrite
unsafeSwap
:: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> Int -> m ()
unsafeSwap = G.unsafeSwap
set :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> a -> m ()
set = G.set
copy :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
copy = G.copy
unsafeCopy :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
unsafeCopy = G.unsafeCopy
move :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
move = G.move
unsafeMove :: (PrimMonad m, Prim a)
=> MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
unsafeMove = G.unsafeMove