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, modify, swap,
unsafeRead, unsafeWrite, unsafeModify, 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 Data.Word ( Word8 )
import Control.Monad.Primitive
import Control.Monad ( liftM )
import Control.DeepSeq ( NFData(rnf) )
import Prelude hiding ( length, null, replicate, reverse, map, read,
take, drop, splitAt, init, tail )
import Data.Typeable ( Typeable )
#define NOT_VECTOR_MODULE
#include "vector.h"
data MVector s a = MVector !Int
!Int
!(MutableByteArray s)
deriving ( Typeable )
type IOVector = MVector RealWorld
type STVector s = MVector s
instance NFData (MVector s a) where
rnf (MVector _ _ _) = ()
instance Prim a => G.MVector MVector a where
basicLength (MVector _ n _) = n
basicUnsafeSlice j m (MVector i _ 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
| n < 0 = error $ "Primitive.basicUnsafeNew: negative length: " ++ show n
| n > mx = error $ "Primitive.basicUnsafeNew: length to large: " ++ show n
| otherwise = MVector 0 n `liftM` newByteArray (n * size)
where
size = sizeOf (undefined :: a)
mx = maxBound `div` size :: Int
basicInitialize (MVector off n v) =
setByteArray v (off * size) (n * size) (0 :: Word8)
where
size = sizeOf (undefined :: a)
basicUnsafeRead (MVector i _ arr) j = readByteArray arr (i+j)
basicUnsafeWrite (MVector i _ 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)
basicSet (MVector i n arr) x = setByteArray arr i n x
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
modify :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
modify = G.modify
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
unsafeModify :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify = G.unsafeModify
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