module Data.Vector.Storable.Mutable(
MVector(..), IOVector, STVector, Storable,
length, overlaps, slice, new, newWith, read, write, swap,
clear, set, copy, grow,
unsafeSlice, unsafeNew, unsafeNewWith, unsafeRead, unsafeWrite, unsafeSwap,
unsafeCopy, unsafeGrow,
unsafeFromForeignPtr, unsafeToForeignPtr, unsafeWith
) 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, read )
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
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
unsafeSlice :: Storable a => Int
-> Int
-> MVector s a
-> MVector s a
unsafeSlice = G.unsafeSlice
unsafeNew :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a)
unsafeNew = G.unsafeNew
unsafeNewWith :: (PrimMonad m, Storable a)
=> Int -> a -> m (MVector (PrimState m) a)
unsafeNewWith = G.unsafeNewWith
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
unsafeCopy :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
unsafeCopy = G.unsafeCopy
unsafeGrow :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
unsafeGrow = G.unsafeGrow
length :: Storable a => MVector s a -> Int
length = G.length
overlaps :: Storable a => MVector s a -> MVector s a -> Bool
overlaps = G.overlaps
slice :: Storable a => Int -> Int -> MVector s a -> MVector s a
slice = G.slice
new :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a)
new = G.new
newWith :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a)
newWith = G.newWith
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
clear :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> m ()
clear = G.clear
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
grow :: (PrimMonad m, Storable a)
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
grow = G.grow