module Data.Vector.Storable.Mutable(
MVector(..), IOVector, STVector, Storable,
length, null,
slice, init, tail, take, drop,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
overlaps,
new, unsafeNew, replicate, clone,
grow, unsafeGrow,
clear,
read, write, swap,
unsafeRead, unsafeWrite, unsafeSwap,
set, copy, unsafeCopy,
unsafeFromForeignPtr, unsafeToForeignPtr, unsafeWith,
newWith, unsafeNewWith
) where
import qualified Data.Vector.Generic.Mutable as G
import Data.Vector.Storable.Internal
import Foreign.Storable
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Marshal.Array ( advancePtr, copyArray )
import Foreign.C.Types ( CInt )
import Control.Monad.Primitive
import Prelude hiding ( length, null, replicate, reverse, map, read,
take, drop, init, tail )
import Data.Typeable ( Typeable )
#include "vector.h"
data MVector s a = MVector !(Ptr a)
!Int
!(ForeignPtr a)
deriving ( Typeable )
type IOVector = MVector RealWorld
type STVector s = MVector s
instance Storable a => G.MVector MVector a where
basicLength (MVector _ n _) = n
basicUnsafeSlice j m (MVector p n fp) = MVector (p `advancePtr` j) m fp
basicOverlaps (MVector p m _) (MVector q n _)
= between p q (q `advancePtr` n) || between q p (p `advancePtr` m)
where
between x y z = x >= y && x < z
basicUnsafeNew n
= unsafePrimToPrim
$ do
fp <- mallocForeignPtrArray n
withForeignPtr fp $ \p -> return $ MVector p n fp
basicUnsafeRead (MVector p _ fp) i
= unsafePrimToPrim
$ withForeignPtr fp $ \_ -> peekElemOff p i
basicUnsafeWrite (MVector p n fp) i x
= unsafePrimToPrim
$ withForeignPtr fp $ \_ -> pokeElemOff p i x
basicUnsafeCopy (MVector p n fp) (MVector q _ fq)
= unsafePrimToPrim
$ withForeignPtr fp $ \_ ->
withForeignPtr fq $ \_ ->
copyArray p q n
length :: Storable a => MVector s a -> Int
length = G.length
null :: Storable a => MVector s a -> Bool
null = G.null
slice :: Storable a => Int -> Int -> MVector s a -> MVector s a
slice = G.slice
take :: Storable a => Int -> MVector s a -> MVector s a
take = G.take
drop :: Storable a => Int -> MVector s a -> MVector s a
drop = G.drop
init :: Storable a => MVector s a -> MVector s a
init = G.init
tail :: Storable a => MVector s a -> MVector s a
tail = G.tail
unsafeSlice :: Storable a
=> Int
-> Int
-> MVector s a
-> MVector s a
unsafeSlice = G.unsafeSlice
unsafeTake :: Storable a => Int -> MVector s a -> MVector s a
unsafeTake = G.unsafeTake
unsafeDrop :: Storable a => Int -> MVector s a -> MVector s a
unsafeDrop = G.unsafeDrop
unsafeInit :: Storable a => MVector s a -> MVector s a
unsafeInit = G.unsafeInit
unsafeTail :: Storable a => MVector s a -> MVector s a
unsafeTail = G.unsafeTail
overlaps :: Storable a => MVector s a -> MVector s a -> Bool
overlaps = G.overlaps
new :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a)
new = G.new
unsafeNew :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a)
unsafeNew = G.unsafeNew
replicate :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a)
replicate = G.replicate
clone :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a -> m (MVector (PrimState m) a)
clone = G.clone
grow :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
grow = G.grow
unsafeGrow :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
unsafeGrow = G.unsafeGrow
clear :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> m ()
clear = G.clear
read :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a
read = G.read
write
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m ()
write = G.write
swap
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> Int -> m ()
swap = G.swap
unsafeRead :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a
unsafeRead = G.unsafeRead
unsafeWrite
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m ()
unsafeWrite = G.unsafeWrite
unsafeSwap
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> Int -> m ()
unsafeSwap = G.unsafeSwap
set :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> a -> m ()
set = G.set
copy :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
copy = G.copy
unsafeCopy :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
unsafeCopy = G.unsafeCopy
unsafeFromForeignPtr :: Storable a
=> ForeignPtr a
-> Int
-> Int
-> MVector s a
unsafeFromForeignPtr fp i n = MVector (offsetToPtr fp i) n fp
unsafeToForeignPtr :: Storable a => MVector s a -> (ForeignPtr a, Int, Int)
unsafeToForeignPtr (MVector p n fp) = (fp, ptrToOffset fp p, n)
unsafeWith :: Storable a => IOVector a -> (Ptr a -> IO b) -> IO b
unsafeWith (MVector p n fp) m = withForeignPtr fp $ \_ -> m p
newWith :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a)
newWith = G.replicate
unsafeNewWith :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a)
unsafeNewWith = G.replicate