-- |
-- Copyright: (C) 2013 Amgen, Inc.
--                2016 Tweag I/O Limited.
--
-- Vectors that can be passed to and from R with no copying at all. These
-- vectors are wrappers over SEXP vectors used by R. Memory for vectors is
-- allocated from the R heap, and in such way that they can be converted to
-- a 'SEXP' by simple pointer arithmetic (see 'toSEXP').
--
-- Like "Data.Vector.Storable.Mutable" vectors, the vector type in this module
-- adds one extra level of indirection and a small amount of storage overhead
-- for maintainging lengths and slice offsets. If you're keeping a very large
-- number of tiny vectors in memory, you're better off keeping them as 'SEXP's
-- and calling 'fromSEXP' on-the-fly.

{-# 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
  ( -- * Mutable slices of 'SEXP' vector types
    MVector
  , fromSEXP
  , toSEXP
  , release
  , unsafeRelease
    -- * Accessors
    -- ** Length information
  , length
  , null
    -- * Construction
    -- ** Initialisation
  , new
  , unsafeNew
  , replicate
  , replicateM
  , clone
    -- ** Extracting subvectors
  , slice
  , init
  , tail
  , take
  , drop
  , splitAt
  , unsafeSlice
  , unsafeInit
  , unsafeTail
  , unsafeTake
  , unsafeDrop
    -- ** Overlapping
  , overlaps
    -- ** Restricting memory usage
  , clear
    -- * Accessing individual elements
  , read
  , write
  , swap
  , unsafeRead
  , unsafeWrite
  , unsafeSwap
    -- * Modifying vectors
    -- ** Filling and copying
  , 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 )

-- Internal helpers
-- ----------------

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 :: (forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony f :: forall t. Reifies t (AcquireIO s) => W t ty s a -> b
f v :: MVector s ty a
v =
    AcquireIO s
-> (forall s. Reifies s (AcquireIO s) => Proxy s -> b) -> b
forall a r. a -> (forall s. Reifies s a => Proxy s -> r) -> r
reify ((forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
forall s.
(forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
AcquireIO forall a. a
forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty)
acquireIO) ((forall s. Reifies s (AcquireIO s) => Proxy s -> b) -> b)
-> (forall s. Reifies s (AcquireIO s) => Proxy s -> b) -> b
forall a b. (a -> b) -> a -> b
$ \(Proxy s
Proxy :: Proxy t) -> do
      W s ty s a -> b
forall t. Reifies t (AcquireIO s) => W t ty s a -> b
f (MVector s ty a -> W s ty s a
forall t (ty :: SEXPTYPE) s a. MVector s ty a -> W t ty s a
W MVector s ty a
v :: W t ty s a)
  where
    acquireIO :: a
acquireIO = String -> String -> a
forall a. String -> String -> a
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 :: (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 :: forall t. Reifies t (AcquireIO s) => W t ty s a -> W t ty s a -> b
f v1 :: MVector s ty a
v1 v2 :: MVector s ty a
v2 =
    AcquireIO s
-> (forall s. Reifies s (AcquireIO s) => Proxy s -> b) -> b
forall a r. a -> (forall s. Reifies s a => Proxy s -> r) -> r
reify ((forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
forall s.
(forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
AcquireIO forall a. a
forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty)
acquireIO) ((forall s. Reifies s (AcquireIO s) => Proxy s -> b) -> b)
-> (forall s. Reifies s (AcquireIO s) => Proxy s -> b) -> b
forall a b. (a -> b) -> a -> b
$ \(Proxy s
Proxy :: Proxy t) -> do
      W s ty s a -> W s ty s a -> b
forall t. Reifies t (AcquireIO s) => W t ty s a -> W t ty s a -> b
f (MVector s ty a -> W s ty s a
forall t (ty :: SEXPTYPE) s a. MVector s ty a -> W t ty s a
W (MVector s ty a -> W s ty s a) -> MVector s ty a -> W s ty s a
forall a b. (a -> b) -> a -> b
$ MVector s ty a
v1 :: W t ty s a)
        (MVector s ty a -> W s ty s a
forall t (ty :: SEXPTYPE) s a. MVector s ty a -> W t ty s a
W (MVector s ty a -> W s ty s a) -> MVector s ty a -> W s ty s a
forall a b. (a -> b) -> a -> b
$ MVector s ty a
v2 :: W t ty s a)
  where
    acquireIO :: a
acquireIO = String -> String -> a
forall a. String -> String -> a
violation "phony2" "phony acquire called."

-- Conversions
-- -----------

-- | /O(1)/ Create a vector from a 'SEXP'.
fromSEXP :: VECTOR s ty a => SEXP s ty -> MVector s ty a
fromSEXP :: SEXP s ty -> MVector s ty a
fromSEXP sx :: SEXP s ty
sx =
    SEXP s ty -> Int32 -> Int32 -> MVector s ty a
forall s (ty :: SEXPTYPE) a.
SEXP s ty -> Int32 -> Int32 -> MVector s ty a
MVector SEXP s ty
sx 0 (Int32 -> MVector s ty a) -> Int32 -> MVector s ty a
forall a b. (a -> b) -> a -> b
$ IO Int32 -> Int32
forall a. IO a -> a
unsafePerformIO (IO Int32 -> Int32) -> IO Int32 -> Int32
forall a b. (a -> b) -> a -> b
$ do
      Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int32) -> IO Int -> IO Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP s ty -> IO Int
forall (a :: SEXPTYPE) s. IsVector a => SEXP s a -> IO Int
R.length SEXP s ty
sx

-- | /O(1)/ in the common case, /O(n)/ for proper slices. Convert a mutable
-- vector to a 'SEXP'. This can be done efficiently, without copy, because
-- vectors in this module always include a 'SEXP' header immediately before the
-- vector data in memory.
toSEXP
  :: (MonadR m, VECTOR (Region m) ty a)
  => MVector (Region m) ty a
  -> m (SEXP (Region m) ty)
toSEXP :: MVector (Region m) ty a -> m (SEXP (Region m) ty)
toSEXP (MVector sx :: SEXP (Region m) ty
sx 0 len :: Int32
len)
  | Int32
len Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int32
sexplen = SEXP (Region m) ty -> m (SEXP (Region m) ty)
forall (m :: * -> *) a. Monad m => a -> m a
return SEXP (Region m) ty
sx
  where
    sexplen :: Int32
sexplen = IO Int32 -> Int32
forall a. IO a -> a
unsafePerformIO (IO Int32 -> Int32) -> IO Int32 -> Int32
forall a b. (a -> b) -> a -> b
$ do
      Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int32) -> IO Int -> IO Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP (Region m) ty -> IO Int
forall (a :: SEXPTYPE) s. IsVector a => SEXP s a -> IO Int
R.length SEXP (Region m) ty
sx
toSEXP v :: MVector (Region m) ty a
v = MVector (Region m) ty a -> m (SEXP (Region m) ty)
forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a) =>
MVector (Region m) ty a -> m (SEXP (Region m) ty)
toSEXP (MVector (Region m) ty a -> m (SEXP (Region m) ty))
-> m (MVector (Region m) ty a) -> m (SEXP (Region m) ty)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MVector (Region m) ty a -> m (MVector (Region m) ty a)
forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a) =>
MVector (Region m) ty a -> m (MVector (Region m) ty a)
clone MVector (Region m) ty a
v -- yield a zero based slice.

-- Length information
-- ------------------

-- | Length of the mutable vector.
length :: VECTOR s ty a => MVector s ty a -> Int
{-# INLINE length #-}
length :: MVector s ty a -> Int
length = (forall t. Reifies t (AcquireIO s) => W t ty s a -> Int)
-> MVector s ty a -> Int
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony forall t. Reifies t (AcquireIO s) => W t ty s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
G.length

-- | Check whether the vector is empty.
null :: VECTOR s ty a => MVector s ty a -> Bool
{-# INLINE null #-}
null :: MVector s ty a -> Bool
null = (forall t. Reifies t (AcquireIO s) => W t ty s a -> Bool)
-> MVector s ty a -> Bool
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony forall t. Reifies t (AcquireIO s) => W t ty s a -> Bool
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Bool
G.null

-- Extracting subvectors
-- ---------------------

-- | Yield a part of the mutable vector without copying it.
slice :: VECTOR s ty a => Int -> Int -> MVector s ty a -> MVector s ty a
{-# INLINE slice #-}
slice :: Int -> Int -> MVector s ty a -> MVector s ty a
slice i :: Int
i j :: Int
j = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
G.slice Int
i Int
j)

take :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE take #-}
take :: Int -> MVector s ty a -> MVector s ty a
take n :: Int
n = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
G.take Int
n)

drop :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE drop #-}
drop :: Int -> MVector s ty a -> MVector s ty a
drop n :: Int
n = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
G.drop Int
n)

splitAt :: VECTOR s ty a => Int -> MVector s ty a -> (MVector s ty a, MVector s ty a)
{-# INLINE splitAt #-}
splitAt :: Int -> MVector s ty a -> (MVector s ty a, MVector s ty a)
splitAt n :: Int
n = (forall t.
 Reifies t (AcquireIO s) =>
 W t ty s a -> (MVector s ty a, MVector s ty a))
-> MVector s ty a -> (MVector s ty a, MVector s ty a)
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (Int -> W t ty s a -> (W t ty s a, W t ty s a)
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> v s a -> (v s a, v s a)
G.splitAt Int
n (W t ty s a -> (W t ty s a, W t ty s a))
-> ((W t ty s a, W t ty s a) -> (MVector s ty a, MVector s ty a))
-> W t ty s a
-> (MVector s ty a, MVector s ty a)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> MVector s ty a)
-> (W t ty s a, W t ty s a)
-> (MVector s ty a, MVector s ty a)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW)

init :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE init #-}
init :: MVector s ty a -> MVector s ty a
init = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
G.init)

tail :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE tail #-}
tail :: MVector s ty a -> MVector s ty a
tail = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
G.tail)

-- | Yield a part of the mutable vector without copying it. No bounds checks
-- are performed.
unsafeSlice :: VECTOR s ty a
            => Int  -- ^ starting index
            -> Int  -- ^ length of the slice
            -> MVector s ty a
            -> MVector s ty a
{-# INLINE unsafeSlice #-}
unsafeSlice :: Int -> Int -> MVector s ty a -> MVector s ty a
unsafeSlice i :: Int
i j :: Int
j = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
G.unsafeSlice Int
i Int
j)

unsafeTake :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE unsafeTake #-}
unsafeTake :: Int -> MVector s ty a -> MVector s ty a
unsafeTake n :: Int
n = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
G.unsafeTake Int
n)

unsafeDrop :: VECTOR s ty a => Int -> MVector s ty a -> MVector s ty a
{-# INLINE unsafeDrop #-}
unsafeDrop :: Int -> MVector s ty a -> MVector s ty a
unsafeDrop n :: Int
n = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
G.unsafeDrop Int
n)

unsafeInit :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE unsafeInit #-}
unsafeInit :: MVector s ty a -> MVector s ty a
unsafeInit = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
G.unsafeInit)

unsafeTail :: VECTOR s ty a => MVector s ty a -> MVector s ty a
{-# INLINE unsafeTail #-}
unsafeTail :: MVector s ty a -> MVector s ty a
unsafeTail = (forall t. Reifies t (AcquireIO s) => W t ty s a -> MVector s ty a)
-> MVector s ty a -> MVector s ty a
forall s (ty :: SEXPTYPE) a b.
VECTOR s ty a =>
(forall t. Reifies t (AcquireIO s) => W t ty s a -> b)
-> MVector s ty a -> b
phony (W t ty s a -> MVector s ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
unW (W t ty s a -> MVector s ty a)
-> (W t ty s a -> W t ty s a) -> W t ty s a -> MVector s ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. W t ty s a -> W t ty s a
forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
G.unsafeTail)

-- Overlapping
-- -----------

-- | Check whether two vectors overlap.
overlaps :: VECTOR s ty a => MVector s ty a -> MVector s ty a -> Bool
{-# INLINE overlaps #-}
overlaps :: MVector s ty a -> MVector s ty a -> Bool
overlaps = (forall t.
 Reifies t (AcquireIO s) =>
 W t ty s a -> W t ty s a -> Bool)
-> MVector s ty a -> MVector s ty a -> Bool
forall s (ty :: SEXPTYPE) 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 forall t.
Reifies t (AcquireIO s) =>
W t ty s a -> W t ty s a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
G.overlaps

-- Initialisation
-- --------------

-- | Create a mutable vector of the given length.
new :: forall m ty a.
       (MonadR m, VECTOR (Region m) ty a)
    => Int
    -> m (MVector (Region m) ty a)
{-# INLINE new #-}
new :: Int -> m (MVector (Region m) ty a)
new n :: Int
n = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a)
forall (m :: * -> *) t (ty :: SEXPTYPE) s a (proxy :: * -> *).
Monad m =>
m (W t ty s a) -> proxy t -> m (MVector s ty a)
proxyW (m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a))
-> m (W s ty (Region m) a)
-> Proxy s
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ Int -> m (W s ty (Region m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
G.new Int
n

-- | Create a mutable vector of the given length. The length is not checked.
unsafeNew :: (MonadR m, VECTOR (Region m) ty a) => Int -> m (MVector (Region m) ty a)
{-# INLINE unsafeNew #-}
unsafeNew :: Int -> m (MVector (Region m) ty a)
unsafeNew n :: Int
n = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a)
forall (m :: * -> *) t (ty :: SEXPTYPE) s a (proxy :: * -> *).
Monad m =>
m (W t ty s a) -> proxy t -> m (MVector s ty a)
proxyW (m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a))
-> m (W s ty (Region m) a)
-> Proxy s
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ Int -> m (W s ty (Region m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
G.unsafeNew Int
n

-- | Create a mutable vector of the given length (0 if the length is negative)
-- and fill it with an initial value.
replicate :: (MonadR m, VECTOR (Region m) ty a) => Int -> a -> m (MVector (Region m) ty a)
{-# INLINE replicate #-}
replicate :: Int -> a -> m (MVector (Region m) ty a)
replicate n :: Int
n x :: a
x = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a)
forall (m :: * -> *) t (ty :: SEXPTYPE) s a (proxy :: * -> *).
Monad m =>
m (W t ty s a) -> proxy t -> m (MVector s ty a)
proxyW (m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a))
-> m (W s ty (Region m) a)
-> Proxy s
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ Int -> a -> m (W s ty (Region m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> a -> m (v (PrimState m) a)
G.replicate Int
n a
x

-- | Create a mutable vector of the given length (0 if the length is negative)
-- and fill it with values produced by repeatedly executing the monadic action.
replicateM :: (MonadR m, VECTOR (Region m) ty a) => Int -> m a -> m (MVector (Region m) ty a)
{-# INLINE replicateM #-}
replicateM :: Int -> m a -> m (MVector (Region m) ty a)
replicateM n :: Int
n m :: m a
m = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a)
forall (m :: * -> *) t (ty :: SEXPTYPE) s a (proxy :: * -> *).
Monad m =>
m (W t ty s a) -> proxy t -> m (MVector s ty a)
proxyW (m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a))
-> m (W s ty (Region m) a)
-> Proxy s
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ Int -> m a -> m (W s ty (Region m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m a -> m (v (PrimState m) a)
G.replicateM Int
n m a
m

-- | Create a copy of a mutable vector.
clone :: (MonadR m, VECTOR (Region m) ty a)
      => MVector (Region m) ty a
      -> m (MVector (Region m) ty a)
{-# INLINE clone #-}
clone :: MVector (Region m) ty a -> m (MVector (Region m) ty a)
clone v :: MVector (Region m) ty a
v = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a)
forall (m :: * -> *) t (ty :: SEXPTYPE) s a (proxy :: * -> *).
Monad m =>
m (W t ty s a) -> proxy t -> m (MVector s ty a)
proxyW (m (W s ty (Region m) a) -> Proxy s -> m (MVector (Region m) ty a))
-> m (W s ty (Region m) a)
-> Proxy s
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ W s ty (Region m) a -> m (W s ty (Region m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a)
G.clone (MVector (Region m) ty a -> W s ty (Region m) a
forall t (ty :: SEXPTYPE) s a. MVector s ty a -> W t ty s a
W MVector (Region m) ty a
v)

-- Restricting memory usage
-- ------------------------

-- | Reset all elements of the vector to some undefined value, clearing all
-- references to external objects. This is usually a noop for unboxed vectors.
clear :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> m ()
{-# INLINE clear #-}
clear :: MVector (Region m) ty a -> m ()
clear v :: MVector (Region m) ty a
v = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
G.clear (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v)

-- Accessing individual elements
-- -----------------------------

-- | Yield the element at the given position.
read :: (MonadR m, VECTOR (Region m) ty a)
     => MVector (Region m) ty a -> Int -> m a
{-# INLINE read #-}
read :: MVector (Region m) ty a -> Int -> m a
read v :: MVector (Region m) ty a
v i :: Int
i = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m a)
-> m a
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m a)
 -> m a)
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m a)
-> m a
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
G.read (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) Int
i

-- | Replace the element at the given position.
write :: (MonadR m, VECTOR (Region m) ty a)
      => MVector (Region m) ty a -> Int -> a -> m ()
{-# INLINE write #-}
write :: MVector (Region m) ty a -> Int -> a -> m ()
write v :: MVector (Region m) ty a
v i :: Int
i x :: a
x = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
G.write (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) Int
i a
x

-- | Swap the elements at the given positions.
swap :: (MonadR m, VECTOR (Region m) ty a)
     => MVector (Region m) ty a -> Int -> Int -> m ()
{-# INLINE swap #-}
swap :: MVector (Region m) ty a -> Int -> Int -> m ()
swap v :: MVector (Region m) ty a
v i :: Int
i j :: Int
j = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> Int -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
G.swap (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) Int
i Int
j

-- | Yield the element at the given position. No bounds checks are performed.
unsafeRead :: (MonadR m, VECTOR (Region m) ty a)
           => MVector (Region m) ty a -> Int -> m a
{-# INLINE unsafeRead #-}
unsafeRead :: MVector (Region m) ty a -> Int -> m a
unsafeRead v :: MVector (Region m) ty a
v i :: Int
i = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m a)
-> m a
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m a)
 -> m a)
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m a)
-> m a
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
G.unsafeRead (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) Int
i

-- | Replace the element at the given position. No bounds checks are performed.
unsafeWrite :: (MonadR m, VECTOR (Region m) ty a)
            => MVector (Region m) ty a -> Int -> a -> m ()
{-# INLINE unsafeWrite #-}
unsafeWrite :: MVector (Region m) ty a -> Int -> a -> m ()
unsafeWrite v :: MVector (Region m) ty a
v i :: Int
i x :: a
x = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
G.unsafeWrite (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) Int
i a
x

-- | Swap the elements at the given positions. No bounds checks are performed.
unsafeSwap :: (MonadR m, VECTOR (Region m) ty a)
           => MVector (Region m) ty a -> Int -> Int -> m ()
{-# INLINE unsafeSwap #-}
unsafeSwap :: MVector (Region m) ty a -> Int -> Int -> m ()
unsafeSwap v :: MVector (Region m) ty a
v i :: Int
i j :: Int
j = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> Int -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
G.unsafeSwap (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) Int
i Int
j

-- Filling and copying
-- -------------------

-- | Set all elements of the vector to the given value.
set :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> a -> m ()
{-# INLINE set #-}
set :: MVector (Region m) ty a -> a -> m ()
set v :: MVector (Region m) ty a
v x :: a
x = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> a -> m ()
G.set (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v) a
x

-- | Copy a vector. The two vectors must have the same length and may not
-- overlap.
copy :: (MonadR m, VECTOR (Region m) ty a)
     => MVector (Region m) ty a
     -> MVector (Region m) ty a
     -> m ()
{-# INLINE copy #-}
copy :: MVector (Region m) ty a -> MVector (Region m) ty a -> m ()
copy v1 :: MVector (Region m) ty a
v1 v2 :: MVector (Region m) ty a
v2 = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> W s ty (Region m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
G.copy (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v1) (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v2)

-- | Copy a vector. The two vectors must have the same length and may not
-- overlap. This is not checked.
unsafeCopy :: (MonadR m, VECTOR (Region m) ty a)
           => MVector (Region m) ty a   -- ^ target
           -> MVector (Region m) ty a   -- ^ source
           -> m ()
{-# INLINE unsafeCopy #-}
unsafeCopy :: MVector (Region m) ty a -> MVector (Region m) ty a -> m ()
unsafeCopy v1 :: MVector (Region m) ty a
v1 v2 :: MVector (Region m) ty a
v2 = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> W s ty (Region m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
G.unsafeCopy (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v1) (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v2)

-- | Move the contents of a vector. The two vectors must have the same
-- length.
--
-- If the vectors do not overlap, then this is equivalent to 'copy'.
-- Otherwise, the copying is performed as if the source vector were
-- copied to a temporary vector and then the temporary vector was copied
-- to the target vector.
move :: (MonadR m, VECTOR (Region m) ty a)
     => MVector (Region m) ty a
     -> MVector (Region m) ty a
     -> m ()
{-# INLINE move #-}
move :: MVector (Region m) ty a -> MVector (Region m) ty a -> m ()
move v1 :: MVector (Region m) ty a
v1 v2 :: MVector (Region m) ty a
v2 = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> W s ty (Region m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
G.move (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v1) (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v2)

-- | Move the contents of a vector. The two vectors must have the same
-- length, but this is not checked.
--
-- If the vectors do not overlap, then this is equivalent to 'unsafeCopy'.
-- Otherwise, the copying is performed as if the source vector were
-- copied to a temporary vector and then the temporary vector was copied
-- to the target vector.
unsafeMove :: (MonadR m, VECTOR (Region m) ty a)
           => MVector (Region m) ty a             -- ^ target
           -> MVector (Region m) ty a             -- ^ source
           -> m ()
{-# INLINE unsafeMove #-}
unsafeMove :: MVector (Region m) ty a -> MVector (Region m) ty a -> m ()
unsafeMove v1 :: MVector (Region m) ty a
v1 v2 :: MVector (Region m) ty a
v2 = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> W s ty (Region m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
G.unsafeMove (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v1) (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
withW Proxy s
p MVector (Region m) ty a
v2)