{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
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
{-# INLINE length #-}
length = phony G.length
null :: VECTOR s ty a => MVector s ty a -> Bool
{-# INLINE null #-}
null = phony G.null
slice :: VECTOR s ty a => Int -> Int -> MVector s ty a -> MVector s ty a
{-# INLINE slice #-}
slice i j = phony (unW . G.slice i j)
take :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE take #-}
take n = phony (unW . G.take n)
drop :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE drop #-}
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)
{-# INLINE splitAt #-}
splitAt n = phony (G.splitAt n >>> unW *** unW)
init :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE init #-}
init = phony (unW . G.init)
tail :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE tail #-}
tail = phony (unW . G.tail)
unsafeSlice :: VECTOR s ty a
=> Int
-> Int
-> MVector s ty a
-> MVector s ty a
{-# INLINE unsafeSlice #-}
unsafeSlice i j = phony (unW . G.unsafeSlice i j)
unsafeTake :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE unsafeTake #-}
unsafeTake n = phony (unW . G.unsafeTake n)
unsafeDrop :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE unsafeDrop #-}
unsafeDrop n = phony (unW . G.unsafeDrop n)
unsafeInit :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE unsafeInit #-}
unsafeInit = phony (unW . G.unsafeInit)
unsafeTail :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE unsafeTail #-}
unsafeTail = phony (unW . G.unsafeTail)
overlaps :: VECTOR s ty a => MVector s ty a -> MVector s ty a -> Bool
{-# INLINE overlaps #-}
overlaps = phony2 G.overlaps
new :: forall m ty a.
(MonadR m, VECTOR (Region m) ty a)
=> Int
-> m (MVector (Region m) ty a)
{-# INLINE new #-}
new n = withAcquire $ proxyW $ G.new n
unsafeNew :: (MonadR m, VECTOR (Region m) ty a) => Int -> m (MVector (Region m) ty a)
{-# INLINE unsafeNew #-}
unsafeNew n = withAcquire $ proxyW $ G.unsafeNew n
replicate :: (MonadR m, VECTOR (Region m) ty a) => Int -> a -> m (MVector (Region m) ty a)
{-# INLINE replicate #-}
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)
{-# INLINE replicateM #-}
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)
{-# INLINE clone #-}
clone v = withAcquire $ proxyW $ G.clone (W v)
clear :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> m ()
{-# INLINE clear #-}
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
{-# INLINE read #-}
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 ()
{-# INLINE write #-}
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 ()
{-# INLINE swap #-}
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
{-# INLINE unsafeRead #-}
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 ()
{-# INLINE unsafeWrite #-}
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 ()
{-# INLINE unsafeSwap #-}
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 ()
{-# INLINE set #-}
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 ()
{-# INLINE copy #-}
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 ()
{-# INLINE unsafeCopy #-}
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 ()
{-# INLINE move #-}
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 ()
{-# INLINE unsafeMove #-}
unsafeMove v1 v2 = withAcquire $ \p -> G.unsafeMove (withW p v1) (withW p v2)