module Data.Vector.SEXP.Mutable
(
MVector
, fromSEXP
, toSEXP
, release
, unsafeRelease
, length
, null
, new
, unsafeNew
, replicate
, replicateM
, clone
, slice
, init
, tail
, take
, drop
, splitAt
, unsafeSlice
, unsafeInit
, unsafeTail
, unsafeTake
, unsafeDrop
, overlaps
, clear
, read
, write
, swap
, unsafeRead
, unsafeWrite
, unsafeSwap
, set
, copy
, move
, unsafeCopy
, unsafeMove
) where
import Control.Monad.R.Class
import Control.Monad.R.Internal
import Data.Vector.SEXP.Base
import Data.Vector.SEXP.Mutable.Internal
import qualified Foreign.R as R
import Foreign.R (SEXP)
import Internal.Error
import qualified Data.Vector.Generic.Mutable as G
import Control.Applicative
import Control.Arrow ((>>>), (***))
import Data.Proxy (Proxy(..))
import Data.Reflection (Reifies(..), reify)
import System.IO.Unsafe (unsafePerformIO)
import Prelude hiding
( length, drop, init, null, read, replicate, splitAt, tail, take )
phony
:: forall s ty a b.
(VECTOR s ty a)
=> (forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a
-> b
phony f v =
reify (AcquireIO acquireIO) $ \(Proxy :: Proxy t) -> do
f (W v :: W t ty s a)
where
acquireIO = violation "phony" "phony acquire called."
phony2
:: forall s ty a b.
(VECTOR s ty a)
=> (forall t. Reifies t (AcquireIO s) => W t ty s a -> W t ty s a -> b)
-> MVector s ty a
-> MVector s ty a
-> b
phony2 f v1 v2 =
reify (AcquireIO acquireIO) $ \(Proxy :: Proxy t) -> do
f (W $ v1 :: W t ty s a)
(W $ v2 :: W t ty s a)
where
acquireIO = violation "phony2" "phony acquire called."
fromSEXP :: VECTOR s ty a => SEXP s ty -> MVector s ty a
fromSEXP sx =
MVector sx 0 $ unsafePerformIO $ do
fromIntegral <$> R.length sx
toSEXP
:: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a
-> m (SEXP (Region m) ty)
toSEXP (MVector sx 0 len)
| len == sexplen = return sx
where
sexplen = unsafePerformIO $ do
fromIntegral <$> R.length sx
toSEXP v = toSEXP =<< clone v
length :: VECTOR s ty a => MVector s ty a -> Int
length = phony G.length
null :: VECTOR s ty a => MVector s ty a -> Bool
null = phony G.null
slice :: VECTOR s ty a => Int -> Int -> MVector s ty a -> MVector s ty a
slice i j = phony (unW . G.slice i j)
take :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
take n = phony (unW . G.take n)
drop :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
drop n = phony (unW . G.drop n)
splitAt :: VECTOR s ty a => Int -> MVector s ty a -> (MVector s ty a, MVector s ty a)
splitAt n = phony (G.splitAt n >>> unW *** unW)
init :: VECTOR s ty a => MVector s ty a -> MVector s ty a
init = phony (unW . G.init)
tail :: VECTOR s ty a => MVector s ty a -> MVector s ty a
tail = phony (unW . G.tail)
unsafeSlice :: VECTOR s ty a
=> Int
-> Int
-> MVector s ty a
-> MVector s ty a
unsafeSlice i j = phony (unW . G.unsafeSlice i j)
unsafeTake :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
unsafeTake n = phony (unW . G.unsafeTake n)
unsafeDrop :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
unsafeDrop n = phony (unW . G.unsafeDrop n)
unsafeInit :: VECTOR s ty a => MVector s ty a -> MVector s ty a
unsafeInit = phony (unW . G.unsafeInit)
unsafeTail :: VECTOR s ty a => MVector s ty a -> MVector s ty a
unsafeTail = phony (unW . G.unsafeTail)
overlaps :: VECTOR s ty a => MVector s ty a -> MVector s ty a -> Bool
overlaps = phony2 G.overlaps
new :: forall m ty a.
(MonadR m, VECTOR (Region m) ty a)
=> Int
-> m (MVector (Region m) ty a)
new n = withAcquire $ proxyW $ G.new n
unsafeNew :: (MonadR m, VECTOR (Region m) ty a) => Int -> m (MVector (Region m) ty a)
unsafeNew n = withAcquire $ proxyW $ G.unsafeNew n
replicate :: (MonadR m, VECTOR (Region m) ty a) => Int -> a -> m (MVector (Region m) ty a)
replicate n x = withAcquire $ proxyW $ G.replicate n x
replicateM :: (MonadR m, VECTOR (Region m) ty a) => Int -> m a -> m (MVector (Region m) ty a)
replicateM n m = withAcquire $ proxyW $ G.replicateM n m
clone :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a
-> m (MVector (Region m) ty a)
clone v = withAcquire $ proxyW $ G.clone (W v)
clear :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> m ()
clear v = withAcquire $ \p -> G.clear (withW p v)
read :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a -> Int -> m a
read v i = withAcquire $ \p -> G.read (withW p v) i
write :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a -> Int -> a -> m ()
write v i x = withAcquire $ \p -> G.write (withW p v) i x
swap :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a -> Int -> Int -> m ()
swap v i j = withAcquire $ \p -> G.swap (withW p v) i j
unsafeRead :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a -> Int -> m a
unsafeRead v i = withAcquire $ \p -> G.unsafeRead (withW p v) i
unsafeWrite :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a -> Int -> a -> m ()
unsafeWrite v i x = withAcquire $ \p -> G.unsafeWrite (withW p v) i x
unsafeSwap :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a -> Int -> Int -> m ()
unsafeSwap v i j = withAcquire $ \p -> G.unsafeSwap (withW p v) i j
set :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> a -> m ()
set v x = withAcquire $ \p -> G.set (withW p v) x
copy :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a
-> MVector (Region m) ty a
-> m ()
copy v1 v2 = withAcquire $ \p -> G.copy (withW p v1) (withW p v2)
unsafeCopy :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a
-> MVector (Region m) ty a
-> m ()
unsafeCopy v1 v2 = withAcquire $ \p -> G.unsafeCopy (withW p v1) (withW p v2)
move :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a
-> MVector (Region m) ty a
-> m ()
move v1 v2 = withAcquire $ \p -> G.move (withW p v1) (withW p v2)
unsafeMove :: (MonadR m, VECTOR (Region m) ty a)
=> MVector (Region m) ty a
-> MVector (Region m) ty a
-> m ()
unsafeMove v1 v2 = withAcquire $ \p -> G.unsafeMove (withW p v1) (withW p v2)