-- |
-- Copyright: (C) 2013 Amgen, Inc.
--
-- Vectors that can be passed to and from R with no copying at all. These
-- vectors are an instance of "Data.Vector.Storable", where the memory is
-- allocated from the R heap, in such a way that they can be converted to
-- a 'SEXP' through simple pointer arithmetic (see 'toSEXP') /in constant time/.
--
-- The main difference between "Data.Vector.SEXP" and "Data.Vector.Storable" is
-- that the former uses a header-prefixed data layout (the header immediately
-- precedes the payload of the vector). This means that no additional pointer
-- dereferencing is needed to reach the vector data. The trade-off is that most
-- slicing operations are O(N) instead of O(1).
--
-- If you make heavy use of slicing, then it's best to convert to
-- a "Data.Vector.Storable" vector first, using 'unsafeToStorable'.
--
-- Note that since 'unstream' relies on slicing operations, it will still be an
-- O(N) operation but it will copy vector data twice (instead of once).

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}

module Data.Vector.SEXP
  ( Vector(..)
  , Mutable.MVector(..)
  , ElemRep
  , VECTOR
  , SVECTOR
  , Data.Vector.SEXP.fromSEXP
  , unsafeFromSEXP
  , Data.Vector.SEXP.toSEXP
  , unsafeToSEXP
  -- * Accessors
  -- ** Length information
  , length
  , null
  -- ** Indexing
  , (!)
  , (!?)
  , head
  , last
  , unsafeIndex
  , unsafeHead
  , unsafeLast
  -- ** Monadic indexing
  , indexM
  , headM
  , lastM
  , unsafeIndexM
  , unsafeHeadM
  , unsafeLastM
  -- ** Extracting subvectors (slicing)
  , slice
  , init
  , take
  , drop
  , tail
  , splitAt
  , unsafeTail
  , unsafeSlice
  , unsafeDrop
  , unsafeTake
  , unsafeInit

  -- * Construction
  -- ** Initialisation
  , empty
  , singleton
  , replicate
  , generate
  , iterateN
  -- ** Monadic initialisation
  , replicateM
  , generateM
  , create
  -- ** Unfolding
  , unfoldr
  , unfoldrN
  , constructN
  , constructrN
  -- ** Enumeration
  , enumFromN
  , enumFromStepN
  , enumFromTo
  , enumFromThenTo
  -- ** Concatenation
  , cons
  , snoc
  , (++)
  , concat

  -- ** Restricting memory usage
  , force

  -- * Modifying vectors

  -- ** Bulk updates
  , (//)
  -- , update_
  , unsafeUpd
  -- , unsafeUpdate_

  -- ** Accumulations
  , accum
  -- , accumulate_
  , unsafeAccum
  -- , unsafeAccumulate_

  -- ** Permutations
  , reverse
  -- , backpermute
  -- , unsafeBackpermute

  -- ** Safe destructive updates
  -- , modify

  -- * Elementwise operations

  -- ** Mapping
  , map
  , imap
  , concatMap

  -- ** Monadic mapping
  , mapM
  , mapM_
  , forM
  , forM_

  -- ** Zipping
  , zipWith
  , zipWith3
  , zipWith4
  , zipWith5
  , zipWith6
  , izipWith
  , izipWith3
  , izipWith4
  , izipWith5
  , izipWith6

  -- ** Monadic zipping
  , zipWithM
  , zipWithM_

  -- * Working with predicates

  -- ** Filtering
  , filter
  , ifilter
  , filterM
  , takeWhile
  , dropWhile

  -- ** Partitioning
  , partition
  , unstablePartition
  , span
  , break

  -- ** Searching
  , elem
  , notElem
  , find
  , findIndex
  -- , findIndices
  , elemIndex
  -- , elemIndices

  -- * Folding
  , foldl
  , foldl1
  , foldl'
  , foldl1'
  , foldr
  , foldr1
  , foldr'
  , foldr1'
  , ifoldl
  , ifoldl'
  , ifoldr
  , ifoldr'

  -- ** Specialised folds
  , all
  , any
  -- , and
  -- , or
  , sum
  , product
  , maximum
  , maximumBy
  , minimum
  , minimumBy
  , minIndex
  , minIndexBy
  , maxIndex
  , maxIndexBy

  -- ** Monadic folds
  , foldM
  , foldM'
  , fold1M
  , fold1M'
  , foldM_
  , foldM'_
  , fold1M_
  , fold1M'_

  -- * Prefix sums (scans)
  , prescanl
  , prescanl'
  , postscanl
  , postscanl'
  , scanl
  , scanl'
  , scanl1
  , scanl1'
  , prescanr
  , prescanr'
  , postscanr
  , postscanr'
  , scanr
  , scanr'
  , scanr1
  , scanr1'

  -- * Conversions
  -- ** Lists
  , toList
  , fromList
  , fromListN
  -- ** Mutable vectors
  , freeze
  , thaw
  , copy
  , unsafeFreeze
  , unsafeThaw
  , unsafeCopy

  -- ** SEXP specific helpers.
  , toString
  , toByteString
  , unsafeWithByteString
  ) where

import Control.Exception (evaluate)
import Control.Monad.R.Class
import Control.Monad.R.Internal
import Control.Memory.Region
import Data.Vector.SEXP.Base
import Data.Vector.SEXP.Mutable (MVector)
import qualified Data.Vector.SEXP.Mutable as Mutable
import qualified Data.Vector.SEXP.Mutable.Internal as Mutable
import Foreign.R ( SEXP(..), SEXP0(..) )
import qualified Foreign.R as R
import Foreign.R.Type ( SEXPTYPE(Char) )

import Control.Monad.ST (ST, runST)
import Data.Int
import Data.Proxy (Proxy(..))
import Data.Reflection (Reifies(..), reify)
import qualified Data.Vector.Generic as G
import Data.Vector.Generic.New (run)
import Data.ByteString ( ByteString )
import qualified Data.ByteString as B
import qualified Data.ByteString.Unsafe as B

import Control.Applicative hiding (empty)
import Control.Exception (mask_)
#if MIN_VERSION_vector(0,11,0)
import qualified Data.Vector.Fusion.Bundle.Monadic as Bundle
import           Data.Vector.Fusion.Bundle.Monadic (sSize, sElems)
import           Data.Vector.Fusion.Bundle.Size (Size(Unknown), smaller)
import           Data.Vector.Fusion.Bundle (lift)
import qualified Data.Vector.Fusion.Stream.Monadic as Stream
import qualified Data.List as List
#else
import qualified Data.Vector.Fusion.Stream as Stream
import qualified Data.Vector.Fusion.Stream.Monadic as MStream
#endif

import Control.Monad.Primitive ( PrimMonad, unsafeInlineIO, unsafePrimToPrim )
import qualified Control.DeepSeq as DeepSeq
import Data.Word ( Word8 )
import Foreign ( Storable, Ptr, castPtr, peekElemOff )
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr)
import Foreign.Marshal.Array ( copyArray )
import qualified GHC.Foreign as GHC
import qualified GHC.ForeignPtr as GHC
import GHC.IO.Encoding.UTF8
import qualified GHC.Exts as Exts
import System.IO.Unsafe

import Prelude
  ( Eq(..)
  , Enum
  , Monad(..)
  , Num(..)
  , Ord(..)
  , Show(..)
  , Bool
  , IO
  , Maybe
  , Ordering
  , String
  , (.)
  , ($)
  , fromIntegral
  , seq
  , uncurry
  )
import qualified Prelude

newtype ForeignSEXP (ty::SEXPTYPE) = ForeignSEXP (ForeignPtr R.SEXPREC)

-- | Create a 'ForeignSEXP' from 'SEXP'.
foreignSEXP :: PrimMonad m => SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP :: forall (m :: * -> *) s (ty :: SEXPTYPE).
PrimMonad m =>
SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP sx :: SEXP s ty
sx@(SEXP (SEXP0 Ptr SEXPREC
ptr)) =
    forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim forall a b. (a -> b) -> a -> b
$ forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ do
      forall s (a :: SEXPTYPE). SEXP s a -> IO ()
R.preserveObject SEXP s ty
sx
      forall (ty :: SEXPTYPE). ForeignPtr SEXPREC -> ForeignSEXP ty
ForeignSEXP forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Ptr a -> IO () -> IO (ForeignPtr a)
GHC.newConcForeignPtr Ptr SEXPREC
ptr (forall s (a :: SEXPTYPE). SEXP s a -> IO ()
R.releaseObject SEXP s ty
sx)

withForeignSEXP
  :: ForeignSEXP ty
  -> (SEXP V ty -> IO r)
  -> IO r
withForeignSEXP :: forall (ty :: SEXPTYPE) r.
ForeignSEXP ty -> (SEXP V ty -> IO r) -> IO r
withForeignSEXP (ForeignSEXP ForeignPtr SEXPREC
fptr) SEXP V ty -> IO r
f =
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SEXPREC
fptr forall a b. (a -> b) -> a -> b
$ \Ptr SEXPREC
ptr -> SEXP V ty -> IO r
f (forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
SEXP (Ptr SEXPREC -> SEXP0
SEXP0 Ptr SEXPREC
ptr))

-- | Immutable vectors. The second type paramater is a phantom parameter
-- reflecting at the type level the tag of the vector when viewed as a 'SEXP'.
-- The tag of the vector and the representation type are related via 'ElemRep'.
data Vector (ty :: SEXPTYPE) a = Vector
  { forall (ty :: SEXPTYPE) a. Vector ty a -> ForeignSEXP ty
vectorBase   :: {-# UNPACK #-} !(ForeignSEXP ty)
  , forall (ty :: SEXPTYPE) a. Vector ty a -> Int32
vectorOffset :: {-# UNPACK #-} !Int32
  , forall (ty :: SEXPTYPE) a. Vector ty a -> Int32
vectorLength :: {-# UNPACK #-} !Int32
  }

instance (Eq a, SVECTOR ty a) => Eq (Vector ty a) where
  Vector ty a
a == :: Vector ty a -> Vector ty a -> Bool
== Vector ty a
b = forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList Vector ty a
a forall a. Eq a => a -> a -> Bool
== forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList Vector ty a
b

instance (Show a, SVECTOR ty a)  => Show (Vector ty a) where
  show :: Vector ty a -> String
show Vector ty a
v = String
"fromList " forall a. [a] -> [a] -> [a]
Prelude.++ forall a. Show a => [a] -> ShowS
showList (forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList Vector ty a
v) String
""

-- | Internal wrapper type for reflection. First type parameter is the reified
-- type to reflect.
newtype W t ty a = W { forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW :: Vector ty a }

withW :: proxy t -> Vector ty a -> W t ty a
withW :: forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW proxy t
_ Vector ty a
v = forall t (ty :: SEXPTYPE) a. Vector ty a -> W t ty a
W Vector ty a
v

proxyFW :: (W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW :: forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> r
f Vector ty a
v p t
p = W t ty a -> r
f (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW p t
p Vector ty a
v)

proxyFW2 :: (W t tya a -> W t tyb b -> r) -> Vector tya a -> Vector tyb b -> p t -> r
proxyFW2 :: forall t (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b r (p :: * -> *).
(W t tya a -> W t tyb b -> r)
-> Vector tya a -> Vector tyb b -> p t -> r
proxyFW2 W t tya a -> W t tyb b -> r
f Vector tya a
v1 Vector tyb b
v2 p t
p = W t tya a -> W t tyb b -> r
f (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW p t
p Vector tya a
v1) (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW p t
p Vector tyb b
v2)

proxyW :: W t ty a -> p t -> Vector ty a
proxyW :: forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW W t ty a
v p t
_ = forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
v

type instance G.Mutable (W t ty) = Mutable.W t ty

instance (Reifies t (AcquireIO s), SVECTOR ty a) => G.Vector (W t ty) a where
  {-# INLINE basicUnsafeFreeze #-}
  basicUnsafeFreeze :: forall (m :: * -> *).
PrimMonad m =>
Mutable (W t ty) (PrimState m) a -> m (W t ty a)
basicUnsafeFreeze (forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
Mutable.unW -> Mutable.MVector SEXP (PrimState m) ty
sx Int32
off Int32
len) = do
      ForeignSEXP ty
fp <- forall (m :: * -> *) s (ty :: SEXPTYPE).
PrimMonad m =>
SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP SEXP (PrimState m) ty
sx
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. Vector ty a -> W t ty a
W forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) a.
ForeignSEXP ty -> Int32 -> Int32 -> Vector ty a
Vector ForeignSEXP ty
fp Int32
off Int32
len
  {-# INLINE basicUnsafeThaw #-}
  basicUnsafeThaw :: forall (m :: * -> *).
PrimMonad m =>
W t ty a -> m (Mutable (W t ty) (PrimState m) a)
basicUnsafeThaw (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW -> Vector ForeignSEXP ty
fp Int32
off Int32
len) = forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim forall a b. (a -> b) -> a -> b
$
      forall (ty :: SEXPTYPE) r.
ForeignSEXP ty -> (SEXP V ty -> IO r) -> IO r
withForeignSEXP ForeignSEXP ty
fp forall a b. (a -> b) -> a -> b
$ \SEXP V ty
ptr -> do
         SEXP s ty
sx' <- forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty)
acquireIO (forall t s (a :: SEXPTYPE). (t <= s) => SEXP s a -> SEXP t a
R.release SEXP V ty
ptr)
         forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy t
p forall a b. (a -> b) -> a -> b
$ forall s (ty :: SEXPTYPE) a.
SEXP s ty -> Int32 -> Int32 -> MVector s ty a
Mutable.MVector (forall s (a :: SEXPTYPE) r. SEXP s a -> SEXP r a
R.unsafeRelease SEXP s ty
sx') Int32
off Int32
len
    where
      AcquireIO forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty)
acquireIO = forall {k} (s :: k) a (proxy :: k -> *).
Reifies s a =>
proxy s -> a
reflect (forall {k} (t :: k). Proxy t
Proxy :: Proxy t)
      p :: Proxy t
p = forall {k} (t :: k). Proxy t
Proxy :: Proxy t
  basicLength :: W t ty a -> Int
basicLength (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW -> Vector ForeignSEXP ty
_ Int32
_ Int32
len) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
len
  {-# INLINE basicUnsafeSlice #-}
  basicUnsafeSlice :: Int -> Int -> W t ty a -> W t ty a
basicUnsafeSlice (forall a b. (Integral a, Num b) => a -> b
fromIntegral ->Int32
i)
     (forall a b. (Integral a, Num b) => a -> b
fromIntegral ->Int32
n) (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW -> Vector ForeignSEXP ty
fp Int32
off Int32
_len) = forall t (ty :: SEXPTYPE) a. Vector ty a -> W t ty a
W forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) a.
ForeignSEXP ty -> Int32 -> Int32 -> Vector ty a
Vector ForeignSEXP ty
fp (Int32
off forall a. Num a => a -> a -> a
+ Int32
i) Int32
n
  {-# INLINE basicUnsafeIndexM #-}
  basicUnsafeIndexM :: forall (m :: * -> *). Monad m => W t ty a -> Int -> m a
basicUnsafeIndexM W t ty a
v Int
i = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
v)) Int
i
  {-# INLINE basicUnsafeCopy #-}
  basicUnsafeCopy :: forall (m :: * -> *).
PrimMonad m =>
Mutable (W t ty) (PrimState m) a -> W t ty a -> m ()
basicUnsafeCopy Mutable (W t ty) (PrimState m) a
mv W t ty a
v =
      forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim forall a b. (a -> b) -> a -> b
$
        forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray (forall a s (ty :: SEXPTYPE). Storable a => MVector s ty a -> Ptr a
Mutable.unsafeToPtr (forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
Mutable.unW Mutable (W t ty) (PrimState m) a
mv))
                  (forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
v))
                  (forall (v :: * -> *) a. Vector v a => v a -> Int
G.basicLength W t ty a
v)
  {-# INLINE elemseq #-}
  elemseq :: forall b. W t ty a -> a -> b -> b
elemseq W t ty a
_ = seq :: forall a b. a -> b -> b
seq

instance SVECTOR ty a => Exts.IsList (Vector ty a) where
  type Item (Vector ty a) = a
  fromList :: [Item (Vector ty a)] -> Vector ty a
fromList = forall (ty :: SEXPTYPE) a. SVECTOR ty a => [a] -> Vector ty a
fromList
  fromListN :: Int -> [Item (Vector ty a)] -> Vector ty a
fromListN = forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> [a] -> Vector ty a
fromListN
  toList :: Vector ty a -> [Item (Vector ty a)]
toList = forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList

-- | Return Pointer of the first element of the vector storage.
unsafeToPtr :: Storable a => Vector ty a -> Ptr a
{-# INLINE unsafeToPtr #-}
unsafeToPtr :: forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr (Vector ForeignSEXP ty
fp Int32
off Int32
len) = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) r.
ForeignSEXP ty -> (SEXP V ty -> IO r) -> IO r
withForeignSEXP ForeignSEXP ty
fp forall a b. (a -> b) -> a -> b
$ \SEXP V ty
sx ->
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s (ty :: SEXPTYPE). Storable a => MVector s ty a -> Ptr a
Mutable.unsafeToPtr forall a b. (a -> b) -> a -> b
$ forall s (ty :: SEXPTYPE) a.
SEXP s ty -> Int32 -> Int32 -> MVector s ty a
Mutable.MVector SEXP V ty
sx Int32
off Int32
len

-- | /O(n)/ Create an immutable vector from a 'SEXP'. Because 'SEXP's are
-- mutable, this function yields an immutable /copy/ of the 'SEXP'.
fromSEXP :: (SVECTOR ty a) => SEXP s ty -> Vector ty a
fromSEXP :: forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
SEXP s ty -> Vector ty a
fromSEXP SEXP s ty
s = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
  W t ty s a
w <- forall (v :: * -> *) a s. New v a -> ST s (Mutable v s a)
run (forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> New v a
G.clone (forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
SEXP s ty -> Vector ty a
unsafeFromSEXP SEXP s ty
s) Proxy t
p)
  W t ty a
v <- forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.unsafeFreeze W t ty s a
w
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
v)

-- | /O(1)/ Unsafe convert a mutable 'SEXP' to an immutable vector without
-- copying. The mutable vector must not be used after this operation, lest one
-- runs the risk of breaking referential transparency.
unsafeFromSEXP :: SVECTOR ty a
               => SEXP s ty
               -> Vector ty a
unsafeFromSEXP :: forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
SEXP s ty -> Vector ty a
unsafeFromSEXP SEXP s ty
s = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
  ForeignSEXP ty
sxp <- forall (m :: * -> *) s (ty :: SEXPTYPE).
PrimMonad m =>
SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP SEXP s ty
s
  CInt
l <- forall (a :: SEXPTYPE) s. IsVector a => SEXP s a -> IO CInt
R.length SEXP s ty
s
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) a.
ForeignSEXP ty -> Int32 -> Int32 -> Vector ty a
Vector ForeignSEXP ty
sxp Int32
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
l)

-- | /O(n)/ Yield a (mutable) copy of the vector as a 'SEXP'.
toSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty
toSEXP :: forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
Vector ty a -> SEXP s ty
toSEXP Vector ty a
s = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
  W t ty s a
w <- forall (v :: * -> *) a s. New v a -> ST s (Mutable v s a)
run (forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> New v a
G.clone Vector ty a
s Proxy t
p)
  W t ty a
v <- forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.unsafeFreeze W t ty s a
w
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
Vector ty a -> SEXP s ty
unsafeToSEXP (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
v))

-- | /O(1)/ Unsafely convert an immutable vector to a (mutable) 'SEXP' without
-- copying. The immutable vector must not be used after this operation.
unsafeToSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty
unsafeToSEXP :: forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
Vector ty a -> SEXP s ty
unsafeToSEXP (Vector (ForeignSEXP ForeignPtr SEXPREC
fsx) Int32
_ Int32
_) = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ -- XXX
  forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SEXPREC
fsx forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
R.sexp forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr SEXPREC -> SEXP0
SEXP0

-- | /O(n)/ Convert a character vector into a 'String'.
toString :: Vector 'Char Word8 -> String
toString :: Vector 'Char Word8 -> String
toString Vector 'Char Word8
v = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$
  TextEncoding -> CStringLen -> IO String
GHC.peekCStringLen TextEncoding
utf8 ( forall a b. Ptr a -> Ptr b
castPtr forall a b. (a -> b) -> a -> b
$ forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr Vector 'Char Word8
v
                          , forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) a. Vector ty a -> Int32
vectorLength Vector 'Char Word8
v)

-- | /O(n)/ Convert a character vector into a strict 'ByteString'.
toByteString :: Vector 'Char Word8 -> ByteString
toByteString :: Vector 'Char Word8 -> ByteString
toByteString Vector 'Char Word8
v = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$
   CStringLen -> IO ByteString
B.packCStringLen ( forall a b. Ptr a -> Ptr b
castPtr forall a b. (a -> b) -> a -> b
$ forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr Vector 'Char Word8
v
                    , forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) a. Vector ty a -> Int32
vectorLength Vector 'Char Word8
v)

-- | This function is unsafe and ByteString should not be used
-- outside of the function. Any change to bytestring will be
-- reflected in the source vector, thus breaking referencial
-- transparancy.
unsafeWithByteString :: DeepSeq.NFData a => Vector 'Char Word8 -> (ByteString -> IO a) -> a
unsafeWithByteString :: forall a.
NFData a =>
Vector 'Char Word8 -> (ByteString -> IO a) -> a
unsafeWithByteString Vector 'Char Word8
v ByteString -> IO a
f = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
   ByteString
x <- CStringLen -> IO ByteString
B.unsafePackCStringLen (forall a b. Ptr a -> Ptr b
castPtr forall a b. (a -> b) -> a -> b
$ forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr Vector 'Char Word8
v
                               ,forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (ty :: SEXPTYPE) a. Vector ty a -> Int32
vectorLength Vector 'Char Word8
v)
   a
w <- forall a. NFData a => a -> a
DeepSeq.force forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> IO a
f ByteString
x
   forall a. a -> IO a
evaluate a
w 

------------------------------------------------------------------------
-- Vector API
--

------------------------------------------------------------------------
-- Length
------------------------------------------------------------------------

-- | /O(1)/ Yield the length of the vector.
length :: SVECTOR ty a => Vector ty a -> Int
{-# INLINE length #-}
length :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> Int
length Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> Int
G.length Vector ty a
v

-- | /O(1)/ Test whether a vector if empty
null :: SVECTOR ty a => Vector ty a -> Bool
{-# INLINE null #-}
null :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> Bool
null Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> Bool
G.null Vector ty a
v

------------------------------------------------------------------------
-- Indexing
------------------------------------------------------------------------

-- | O(1) Indexing
(!) :: SVECTOR ty a => Vector ty a -> Int -> a
{-# INLINE (!) #-}
! :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> Int -> a
(!) Vector ty a
v Int
i = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => v a -> Int -> a
G.! Int
i) Vector ty a
v

-- | O(1) Safe indexing
(!?) :: SVECTOR ty a => Vector ty a -> Int -> Maybe a
{-# INLINE (!?) #-}
!? :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Int -> Maybe a
(!?) Vector ty a
v Int
i = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => v a -> Int -> Maybe a
G.!? Int
i) Vector ty a
v

-- | /O(1)/ First element
head :: SVECTOR ty a => Vector ty a -> a
{-# INLINE head #-}
head :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> a
head Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> a
G.head Vector ty a
v

-- | /O(1)/ Last element
last :: SVECTOR ty a => Vector ty a -> a
{-# INLINE last #-}
last :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> a
last Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> a
G.last Vector ty a
v

-- | /O(1)/ Unsafe indexing without bounds checking
unsafeIndex :: SVECTOR ty a => Vector ty a -> Int -> a
{-# INLINE unsafeIndex #-}
unsafeIndex :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> Int -> a
unsafeIndex Vector ty a
v Int
i = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => v a -> Int -> a
`G.unsafeIndex` Int
i) Vector ty a
v

-- | /O(1)/ First element without checking if the vector is empty
unsafeHead :: SVECTOR ty a => Vector ty a -> a
{-# INLINE unsafeHead #-}
unsafeHead :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> a
unsafeHead Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> a
G.unsafeHead Vector ty a
v

-- | /O(1)/ Last element without checking if the vector is empty
unsafeLast :: SVECTOR ty a => Vector ty a -> a
{-# INLINE unsafeLast #-}
unsafeLast :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> a
unsafeLast Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> a
G.unsafeLast Vector ty a
v

------------------------------------------------------------------------
-- Monadic indexing
------------------------------------------------------------------------

-- | /O(1)/ Indexing in a monad.
--
-- The monad allows operations to be strict in the vector when necessary.
-- Suppose vector copying is implemented like this:
--
-- > copy mv v = ... write mv i (v ! i) ...
--
-- For lazy vectors, @v ! i@ would not be evaluated which means that @mv@
-- would unnecessarily retain a reference to @v@ in each element written.
--
-- With 'indexM', copying can be implemented like this instead:
--
-- > copy mv v = ... do
-- >                   x <- indexM v i
-- >                   write mv i x
--
-- Here, no references to @v@ are retained because indexing (but /not/ the
-- elements) is evaluated eagerly.
--
indexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a
{-# INLINE indexM #-}
indexM :: forall (ty :: SEXPTYPE) a (m :: * -> *).
(SVECTOR ty a, Monad m) =>
Vector ty a -> Int -> m a
indexM Vector ty a
v Int
i = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
`G.indexM` Int
i) Vector ty a
v

-- | /O(1)/ First element of a vector in a monad. See 'indexM' for an
-- explanation of why this is useful.
headM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
{-# INLINE headM #-}
headM :: forall (ty :: SEXPTYPE) a (m :: * -> *).
(SVECTOR ty a, Monad m) =>
Vector ty a -> m a
headM Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> m a
G.headM Vector ty a
v

-- | /O(1)/ Last element of a vector in a monad. See 'indexM' for an
-- explanation of why this is useful.
lastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
{-# INLINE lastM #-}
lastM :: forall (ty :: SEXPTYPE) a (m :: * -> *).
(SVECTOR ty a, Monad m) =>
Vector ty a -> m a
lastM Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> m a
G.lastM Vector ty a
v

-- | /O(1)/ Indexing in a monad without bounds checks. See 'indexM' for an
-- explanation of why this is useful.
unsafeIndexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a
{-# INLINE unsafeIndexM #-}
unsafeIndexM :: forall (ty :: SEXPTYPE) a (m :: * -> *).
(SVECTOR ty a, Monad m) =>
Vector ty a -> Int -> m a
unsafeIndexM Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.unsafeIndexM Vector ty a
v

-- | /O(1)/ First element in a monad without checking for empty vectors.
-- See 'indexM' for an explanation of why this is useful.
unsafeHeadM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
{-# INLINE unsafeHeadM #-}
unsafeHeadM :: forall (ty :: SEXPTYPE) a (m :: * -> *).
(SVECTOR ty a, Monad m) =>
Vector ty a -> m a
unsafeHeadM Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> m a
G.unsafeHeadM Vector ty a
v

-- | /O(1)/ Last element in a monad without checking for empty vectors.
-- See 'indexM' for an explanation of why this is useful.
unsafeLastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
{-# INLINE unsafeLastM #-}
unsafeLastM :: forall (ty :: SEXPTYPE) a (m :: * -> *).
(SVECTOR ty a, Monad m) =>
Vector ty a -> m a
unsafeLastM Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> m a
G.unsafeLastM Vector ty a
v

------------------------------------------------------------------------
-- Extracting subvectors (slicing)
------------------------------------------------------------------------

-- | /O(N)/ Yield a slice of the vector with copying it. The vector must
-- contain at least @i+n@ elements.
slice :: SVECTOR ty a
      => Int   -- ^ @i@ starting index
      -> Int   -- ^ @n@ length
      -> Vector ty a
      -> Vector ty a
{-# INLINE slice #-}
slice :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Int -> Vector ty a -> Vector ty a
slice Int
i Int
n Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
G.slice Int
i Int
n) Vector ty a
v

-- | /O(N)/ Yield all but the last element, this operation will copy an array.
-- The vector may not be empty.
init :: SVECTOR ty a => Vector ty a -> Vector ty a
{-# INLINE init #-}
init :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a
init Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> v a
G.init Vector ty a
v

-- | /O(N)/ Copy all but the first element. The vector may not be empty.
tail :: SVECTOR ty a => Vector ty a -> Vector ty a
{-# INLINE tail #-}
tail :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a
tail Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> v a
G.tail Vector ty a
v

-- | /O(N)/ Yield at the first @n@ elements with copying. The vector may
-- contain less than @n@ elements in which case it is returned unchanged.
take :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
{-# INLINE take #-}
take :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Vector ty a -> Vector ty a
take Int
i Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
G.take Int
i) Vector ty a
v

-- | /O(N)/ Yield all but the first @n@ elements with copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
drop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
{-# INLINE drop #-}
drop :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Vector ty a -> Vector ty a
drop Int
i Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
G.drop Int
i) Vector ty a
v

-- | /O(N)/ Yield the first @n@ elements paired with the remainder with copying.
--
-- Note that @'splitAt' n v@ is equivalent to @('take' n v, 'drop' n v)@
-- but slightly more efficient.
{-# INLINE splitAt #-}
splitAt :: SVECTOR ty a => Int -> Vector ty a -> (Vector ty a, Vector ty a)
splitAt :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Vector ty a -> (Vector ty a, Vector ty a)
splitAt Int
i Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ (\(W t ty a
a,W t ty a
b) -> (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> v a -> (v a, v a)
G.splitAt Int
i) Vector ty a
v

-- | /O(N)/ Yield a slice of the vector with copying. The vector must
-- contain at least @i+n@ elements but this is not checked.
unsafeSlice :: SVECTOR ty a => Int   -- ^ @i@ starting index
                       -> Int   -- ^ @n@ length
                       -> Vector ty a
                       -> Vector ty a
{-# INLINE unsafeSlice #-}
unsafeSlice :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Int -> Vector ty a -> Vector ty a
unsafeSlice Int
i Int
j Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
G.unsafeSlice Int
i Int
j) Vector ty a
v

-- | /O(N)/ Yield all but the last element with copying. The vector may not
-- be empty but this is not checked.
unsafeInit :: SVECTOR ty a => Vector ty a -> Vector ty a
{-# INLINE unsafeInit #-}
unsafeInit :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a
unsafeInit Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> v a
G.unsafeInit Vector ty a
v

-- | /O(N)/ Yield all but the first element with copying. The vector may not
-- be empty but this is not checked.
unsafeTail :: SVECTOR ty a => Vector ty a -> Vector ty a
{-# INLINE unsafeTail #-}
unsafeTail :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a
unsafeTail Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> v a
G.unsafeTail Vector ty a
v

-- | /O(N)/ Yield the first @n@ elements with copying. The vector must
-- contain at least @n@ elements but this is not checked.
unsafeTake :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
{-# INLINE unsafeTake #-}
unsafeTake :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Vector ty a -> Vector ty a
unsafeTake Int
i Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
G.unsafeTake Int
i) Vector ty a
v

-- | /O(N)/ Yield all but the first @n@ elements with copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDrop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
{-# INLINE unsafeDrop #-}
unsafeDrop :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> Vector ty a -> Vector ty a
unsafeDrop Int
i Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
G.unsafeDrop Int
i) Vector ty a
v

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

-- | /O(1)/ Empty vector
empty :: SVECTOR ty a => Vector ty a
{-# INLINE empty #-}
empty :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a
empty = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW forall (v :: * -> *) a. Vector v a => v a
G.empty

-- | /O(1)/ Vector with exactly one element
singleton :: SVECTOR ty a => a -> Vector ty a
{-# INLINE singleton #-}
singleton :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => a -> Vector ty a
singleton a
a = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => a -> v a
G.singleton a
a)

-- | /O(n)/ Vector of the given length with the same value in each position
replicate :: SVECTOR ty a => Int -> a -> Vector ty a
{-# INLINE replicate #-}
replicate :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Int -> a -> Vector ty a
replicate Int
i a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> a -> v a
G.replicate Int
i a
v)

-- | /O(n)/ Construct a vector of the given length by applying the function to
-- each index
generate :: SVECTOR ty a => Int -> (Int -> a) -> Vector ty a
{-# INLINE generate #-}
generate :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> (Int -> a) -> Vector ty a
generate Int
i Int -> a
f = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> (Int -> a) -> v a
G.generate Int
i Int -> a
f)

-- | /O(n)/ Apply function n times to value. Zeroth element is original value.
iterateN :: SVECTOR ty a => Int -> (a -> a) -> a -> Vector ty a
{-# INLINE iterateN #-}
iterateN :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> (a -> a) -> a -> Vector ty a
iterateN Int
i a -> a
f a
a = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> (a -> a) -> a -> v a
G.iterateN Int
i a -> a
f a
a)

-- Unfolding
-- ---------
-- | /O(n)/ Construct a Vector ty by repeatedly applying the generator function
-- to a seed. The generator function yields 'Just' the next element and the
-- new seed or 'Nothing' if there are no more elements.
--
-- > unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
-- >  = <10,9,8,7,6,5,4,3,2,1>
unfoldr :: SVECTOR ty a => (b -> Maybe (a, b)) -> b -> Vector ty a
{-# INLINE unfoldr #-}
unfoldr :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(b -> Maybe (a, b)) -> b -> Vector ty a
unfoldr b -> Maybe (a, b)
g b
a = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a b.
Vector v a =>
(b -> Maybe (a, b)) -> b -> v a
G.unfoldr b -> Maybe (a, b)
g b
a)

-- | /O(n)/ Construct a vector with at most @n@ by repeatedly applying the
-- generator function to the a seed. The generator function yields 'Just' the
-- next element and the new seed or 'Nothing' if there are no more elements.
--
-- > unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8>
unfoldrN :: SVECTOR ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector ty a
{-# INLINE unfoldrN #-}
unfoldrN :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
Int -> (b -> Maybe (a, b)) -> b -> Vector ty a
unfoldrN Int
n b -> Maybe (a, b)
g b
a = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a b.
Vector v a =>
Int -> (b -> Maybe (a, b)) -> b -> v a
G.unfoldrN Int
n b -> Maybe (a, b)
g b
a)

-- | /O(n)/ Construct a vector with @n@ elements by repeatedly applying the
-- generator function to the already constructed part of the vector.
--
-- > constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in f <a,b,c>
--
constructN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a
{-# INLINE constructN #-}
constructN :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> (Vector ty a -> a) -> Vector ty a
constructN Int
n Vector ty a -> a
g = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> (v a -> a) -> v a
G.constructN Int
n (Vector ty a -> a
gforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW))

-- | /O(n)/ Construct a vector with @n@ elements from right to left by
-- repeatedly applying the generator function to the already constructed part
-- of the vector.
--
-- > constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in f <c,b,a>
--
constructrN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a
{-# INLINE constructrN #-}
constructrN :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> (Vector ty a -> a) -> Vector ty a
constructrN Int
n Vector ty a -> a
g = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> (v a -> a) -> v a
G.constructrN Int
n (Vector ty a -> a
gforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW))

-- Enumeration
-- -----------

-- | /O(n)/ Yield a vector of the given length containing the values @x@, @x+1@
-- etc. This operation is usually more efficient than 'enumFromTo'.
--
-- > enumFromN 5 3 = <5,6,7>
enumFromN :: (SVECTOR ty a, Num a) => a -> Int -> Vector ty a
{-# INLINE enumFromN #-}
enumFromN :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Num a) =>
a -> Int -> Vector ty a
enumFromN a
a Int
i = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. (Vector v a, Num a) => a -> Int -> v a
G.enumFromN a
a Int
i)

-- | /O(n)/ Yield a vector of the given length containing the values @x@, @x+y@,
-- @x+y+y@ etc. This operations is usually more efficient than 'enumFromThenTo'.
--
-- > enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4>
enumFromStepN :: (SVECTOR ty a, Num a) => a -> a -> Int -> Vector ty a
{-# INLINE enumFromStepN #-}
enumFromStepN :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Num a) =>
a -> a -> Int -> Vector ty a
enumFromStepN a
f a
t Int
s = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. (Vector v a, Num a) => a -> a -> Int -> v a
G.enumFromStepN a
f a
t Int
s)

-- | /O(n)/ Enumerate values from @x@ to @y@.
--
-- /WARNING:/ This operation can be very inefficient. If at all possible, use
-- 'enumFromN' instead.
enumFromTo :: (SVECTOR ty a, Enum a) => a -> a -> Vector ty a
{-# INLINE enumFromTo #-}
enumFromTo :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Enum a) =>
a -> a -> Vector ty a
enumFromTo a
f a
t = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. (Vector v a, Enum a) => a -> a -> v a
G.enumFromTo a
f a
t)

-- | /O(n)/ Enumerate values from @x@ to @y@ with a specific step @z@.
--
-- /WARNING:/ This operation can be very inefficient. If at all possible, use
-- 'enumFromStepN' instead.
enumFromThenTo :: (SVECTOR ty a, Enum a) => a -> a -> a -> Vector ty a
{-# INLINE enumFromThenTo #-}
enumFromThenTo :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Enum a) =>
a -> a -> a -> Vector ty a
enumFromThenTo a
f a
t a
s = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. (Vector v a, Enum a) => a -> a -> a -> v a
G.enumFromThenTo a
f a
t a
s)

-- Concatenation
-- -------------

-- | /O(n)/ Prepend an element
cons :: SVECTOR ty a => a -> Vector ty a -> Vector ty a
{-# INLINE cons #-}
cons :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
a -> Vector ty a -> Vector ty a
cons a
a Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => a -> v a -> v a
G.cons a
a) Vector ty a
v

-- | /O(n)/ Append an element
snoc :: SVECTOR ty a => Vector ty a -> a -> Vector ty a
{-# INLINE snoc #-}
snoc :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> a -> Vector ty a
snoc Vector ty a
v a
a = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => v a -> a -> v a
`G.snoc` a
a) Vector ty a
v

infixr 5 ++
-- | /O(m+n)/ Concatenate two vectors
(++) :: SVECTOR ty a => Vector ty a -> Vector ty a -> Vector ty a
{-# INLINE (++) #-}
Vector ty a
v1 ++ :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a -> Vector ty a
++ Vector ty a
v2 = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b r (p :: * -> *).
(W t tya a -> W t tyb b -> r)
-> Vector tya a -> Vector tyb b -> p t -> r
proxyFW2 forall (v :: * -> *) a. Vector v a => v a -> v a -> v a
(G.++) Vector ty a
v1 Vector ty a
v2

-- | /O(n)/ Concatenate all vectors in the list
concat :: SVECTOR ty a => [Vector ty a] -> Vector ty a
{-# INLINE concat #-}
concat :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
[Vector ty a] -> Vector ty a
concat [Vector ty a]
vs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a. Vector v a => [v a] -> v a
G.concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
Prelude.map (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p) [Vector ty a]
vs

-- Monadic initialisation
-- ----------------------

-- | /O(n)/ Execute the monadic action the given number of times and store the
-- results in a vector.
replicateM :: (Monad m, SVECTOR ty a) => Int -> m a -> m (Vector ty a)
{-# INLINE replicateM #-}
replicateM :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
Int -> m a -> m (Vector ty a)
replicateM Int
n m a
f = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> (\W t ty a
v -> forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW W t ty a
v Proxy t
p) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
Int -> m a -> m (v a)
G.replicateM Int
n m a
f

-- | /O(n)/ Construct a vector of the given length by applying the monadic
-- action to each index
generateM :: (Monad m, SVECTOR ty a) => Int -> (Int -> m a) -> m (Vector ty a)
{-# INLINE generateM #-}
generateM :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
Int -> (Int -> m a) -> m (Vector ty a)
generateM Int
n Int -> m a
f = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> (\W t ty a
v -> forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW W t ty a
v Proxy t
p) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
Int -> (Int -> m a) -> m (v a)
G.generateM Int
n Int -> m a
f

-- | Execute the monadic action and freeze the resulting vector.
--
-- @
-- create (do { v \<- new 2; write v 0 \'a\'; write v 1 \'b\'; return v }) = \<'a','b'\>
-- @
create :: SVECTOR ty a => (forall r. ST r (MVector r ty a)) -> Vector ty a
{-# INLINE create #-}
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
create :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(forall r. ST r (MVector r ty a)) -> Vector ty a
create forall r. ST r (MVector r ty a)
f = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
G.create (forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy t
p forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r. ST r (MVector r ty a)
f)

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

-- | /O(n)/ Yield the argument but force it not to retain any extra memory,
-- possibly by copying it.
--
-- This is especially useful when dealing with slices. For example:
--
-- > force (slice 0 2 <huge vector>)
--
-- Here, the slice retains a reference to the huge vector. Forcing it creates
-- a copy of just the elements that belong to the slice and allows the huge
-- vector to be garbage collected.
force :: SVECTOR ty a => Vector ty a -> Vector ty a
{-# INLINE force #-}
force :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a
force Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> v a
G.force Vector ty a
v

-- Bulk updates
-- ------------

-- | /O(m+n)/ For each pair @(i,a)@ from the list, replace the vector
-- element at position @i@ by @a@.
--
-- > <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7>
--
(//) :: SVECTOR ty a
     => Vector ty a   -- ^ initial vector (of length @m@)
     -> [(Int, a)]      -- ^ list of index/value pairs (of length @n@)
     -> Vector ty a
{-# INLINE (//) #-}
// :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> [(Int, a)] -> Vector ty a
(//) Vector ty a
v [(Int, a)]
l = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => v a -> [(Int, a)] -> v a
G.// [(Int, a)]
l) Vector ty a
v

{-
-- | /O(m+min(n1,n2))/ For each index @i@ from the index Vector ty and the
-- corresponding value @a@ from the value vector, replace the element of the
-- initial Vector ty at position @i@ by @a@.
--
-- > update_ <5,9,2,7>  <2,0,2> <1,3,8> = <3,9,8,7>
--
update_ :: VECTOR s ty a
        => Vector ty a   -- ^ initial vector (of length @m@)
        -> Vector Int -- ^ index vector (of length @n1@)
        -> Vector ty a   -- ^ value vector (of length @n2@)
        -> Vector ty a
{-# INLINE update_ #-}
update_ = G.update_
-}

-- | Same as ('//') but without bounds checking.
unsafeUpd :: SVECTOR ty a => Vector ty a -> [(Int, a)] -> Vector ty a
{-# INLINE unsafeUpd #-}
unsafeUpd :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> [(Int, a)] -> Vector ty a
unsafeUpd Vector ty a
v [(Int, a)]
l = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => v a -> [(Int, a)] -> v a
`G.unsafeUpd` [(Int, a)]
l) Vector ty a
v

{-
-- | Same as 'update_' but without bounds checking.
unsafeUpdate_ :: VECTOR s ty a => Vector ty a -> Vector Int -> Vector ty a -> Vector ty a
{-# INLINE unsafeUpdate_ #-}
unsafeUpdate_ = G.unsafeUpdate_
-}

-- Accumulations
-- -------------

-- | /O(m+n)/ For each pair @(i,b)@ from the list, replace the vector element
-- @a@ at position @i@ by @f a b@.
--
-- > accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>
accum :: SVECTOR ty a
      => (a -> b -> a) -- ^ accumulating function @f@
      -> Vector ty a      -- ^ initial vector (of length @m@)
      -> [(Int,b)]     -- ^ list of index/value pairs (of length @n@)
      -> Vector ty a
{-# INLINE accum #-}
accum :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a
accum a -> b -> a
f Vector ty a
v [(Int, b)]
l = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (\W t ty a
w -> forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> a) -> v a -> [(Int, b)] -> v a
G.accum a -> b -> a
f W t ty a
w [(Int, b)]
l) Vector ty a
v

{-
-- | /O(m+min(n1,n2))/ For each index @i@ from the index Vector ty and the
-- corresponding value @b@ from the the value vector,
-- replace the element of the initial Vector ty at
-- position @i@ by @f a b@.
--
-- > accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4>
--
accumulate_ :: (VECTOR s ty a, VECTOR s ty b)
            => (a -> b -> a) -- ^ accumulating function @f@
            -> Vector ty a      -- ^ initial vector (of length @m@)
            -> Vector Int    -- ^ index vector (of length @n1@)
            -> Vector ty b      -- ^ value vector (of length @n2@)
            -> Vector ty a
{-# INLINE accumulate_ #-}
accumulate_ = G.accumulate_
-}

-- | Same as 'accum' but without bounds checking.
unsafeAccum :: SVECTOR ty a => (a -> b -> a) -> Vector ty a -> [(Int,b)] -> Vector ty a
{-# INLINE unsafeAccum #-}
unsafeAccum :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a
unsafeAccum a -> b -> a
f Vector ty a
v [(Int, b)]
l = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (\W t ty a
w -> forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> a) -> v a -> [(Int, b)] -> v a
G.unsafeAccum a -> b -> a
f W t ty a
w [(Int, b)]
l) Vector ty a
v

{-
-- | Same as 'accumulate_' but without bounds checking.
unsafeAccumulate_ :: (VECTOR s ty a, VECTOR s ty b) =>
               (a -> b -> a) -> Vector ty a -> Vector Int -> Vector ty b -> Vector ty a
{-# INLINE unsafeAccumulate_ #-}
unsafeAccumulate_ = G.unsafeAccumulate_
-}

-- Permutations
-- ------------

-- | /O(n)/ Reverse a vector
reverse :: SVECTOR ty a => Vector ty a -> Vector ty a
{-# INLINE reverse #-}
reverse :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Vector ty a -> Vector ty a
reverse Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> v a
G.reverse Vector ty a
v

{-
-- | /O(n)/ Yield the vector obtained by replacing each element @i@ of the
-- index Vector s ty by @xs'!'i@. This is equivalent to @'map' (xs'!') is@ but is
-- often much more efficient.
--
-- > backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a>
backpermute :: VECTOR s ty a => Vector s ty a -> Vector Int -> Vector s ty a
{-# INLINE backpermute #-}
backpermute = G.backpermute
-}

{-
-- | Same as 'backpermute' but without bounds checking.
unsafeBackpermute :: VECTOR s ty a => Vector s ty a -> Vector Int -> Vector s ty a
{-# INLINE unsafeBackpermute #-}
unsafeBackpermute = G.unsafeBackpermute
-}

-- Safe destructive updates
-- ------------------------

{-
-- | Apply a destructive operation to a vector. The operation will be
-- performed in place if it is safe to do so and will modify a copy of the
-- vector otherwise.
--
-- @
-- modify (\\v -> write v 0 \'x\') ('replicate' 3 \'a\') = \<\'x\',\'a\',\'a\'\>
-- @
modify :: VECTOR s ty a => (forall s. MVector a -> ST s ()) -> Vector ty a -> Vector ty a
{-# INLINE modify #-}
modify p = G.modify p
-}

-- Mapping
-- -------

-- | /O(n)/ Map a function over a vector
map :: (SVECTOR ty a, SVECTOR ty b) => (a -> b) -> Vector ty a -> Vector ty b
{-# INLINE map #-}
map :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b) -> Vector ty a -> Vector ty b
map a -> b
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map a -> b
f) Vector ty a
v

-- | /O(n)/ Apply a function to every element of a Vector ty and its index
imap :: (SVECTOR ty a, SVECTOR ty b) => (Int -> a -> b) -> Vector ty a -> Vector ty b
{-# INLINE imap #-}
imap :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(Int -> a -> b) -> Vector ty a -> Vector ty b
imap Int -> a -> b
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(Int -> a -> b) -> v a -> v b
G.imap Int -> a -> b
f) Vector ty a
v


-- | Map a function over a Vector ty and concatenate the results.
concatMap :: (SVECTOR tya a, SVECTOR tyb b)
          => (a -> Vector tyb b)
          -> Vector tya a
          -> Vector tyb b
{-# INLINE concatMap #-}
#if MIN_VERSION_vector(0,11,0)
concatMap :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b.
(SVECTOR tya a, SVECTOR tyb b) =>
(a -> Vector tyb b) -> Vector tya a -> Vector tyb b
concatMap a -> Vector tyb b
f Vector tya a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let v' :: Bundle (W t tya) a
v' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
v)
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
Stream.concatMap (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Vector tyb b
f) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
v')) Size
Unknown) Proxy t
p
#else
concatMap f v =
    phony $ \p ->
    (`proxyW` p) $
    G.unstream $
    Stream.concatMap (G.stream . withW p . f) $
    G.stream $
    withW p v
#endif

-- Monadic mapping
-- ---------------

-- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
-- vector of results
mapM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => (a -> m b) -> Vector ty a -> m (Vector ty b)
{-# INLINE mapM #-}
mapM :: forall (m :: * -> *) (ty :: SEXPTYPE) a b.
(Monad m, SVECTOR ty a, SVECTOR ty b) =>
(a -> m b) -> Vector ty a -> m (Vector ty b)
mapM a -> m b
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a, Vector v b) =>
(a -> m b) -> v a -> m (v b)
G.mapM a -> m b
f) Vector ty a
v Proxy t
p

-- | /O(n)/ Apply the monadic action to all elements of a Vector ty and ignore the
-- results
mapM_ :: (Monad m, SVECTOR ty a) => (a -> m b) -> Vector ty a -> m ()
{-# INLINE mapM_ #-}
mapM_ :: forall (m :: * -> *) (ty :: SEXPTYPE) a b.
(Monad m, SVECTOR ty a) =>
(a -> m b) -> Vector ty a -> m ()
mapM_ a -> m b
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a) =>
(a -> m b) -> v a -> m ()
G.mapM_ a -> m b
f) Vector ty a
v

-- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
-- vector of results. Equvalent to @flip 'mapM'@.
forM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => Vector ty a -> (a -> m b) -> m (Vector ty b)
{-# INLINE forM #-}
forM :: forall (m :: * -> *) (ty :: SEXPTYPE) a b.
(Monad m, SVECTOR ty a, SVECTOR ty b) =>
Vector ty a -> (a -> m b) -> m (Vector ty b)
forM Vector ty a
v a -> m b
f = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a, Vector v b) =>
v a -> (a -> m b) -> m (v b)
`G.forM` a -> m b
f) Vector ty a
v Proxy t
p

-- | /O(n)/ Apply the monadic action to all elements of a Vector ty and ignore the
-- results. Equivalent to @flip 'mapM_'@.
forM_ :: (Monad m, SVECTOR ty a) => Vector ty a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ :: forall (m :: * -> *) (ty :: SEXPTYPE) a b.
(Monad m, SVECTOR ty a) =>
Vector ty a -> (a -> m b) -> m ()
forM_ Vector ty a
v a -> m b
f = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a) =>
v a -> (a -> m b) -> m ()
`G.forM_` a -> m b
f) Vector ty a
v

-- Zipping
-- -------
#if MIN_VERSION_vector(0,11,0)
smallest :: [Size] -> Size
smallest :: [Size] -> Size
smallest = forall a. (a -> a -> a) -> [a] -> a
List.foldl1' Size -> Size -> Size
smaller
#endif

-- | /O(min(m,n))/ Zip two vectors with the given function.
zipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c)
        => (a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c
{-# INLINE zipWith #-}
#if MIN_VERSION_vector(0,11,0)
zipWith :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) =>
(a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c
zipWith a -> b -> c
f Vector tya a
xs Vector tyb b
ys = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let xs' :: Bundle (W t tya) a
xs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
xs)
        ys' :: Bundle (W t tyb) b
ys' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
ys)
        sz :: Size
sz  = Size -> Size -> Size
smaller (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
xs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
ys')
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
Stream.zipWith a -> b -> c
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
xs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
ys')) Size
sz) Proxy t
p
#else
zipWith f xs ys = phony $ \p ->
   proxyW (G.unstream (Stream.zipWith f (G.stream (withW p xs)) (G.stream (withW p ys)))) p
#endif

-- | Zip three vectors with the given function.
zipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d)
         => (a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
{-# INLINE zipWith3 #-}
#if MIN_VERSION_vector(0,11,0)
zipWith3 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) =>
(a -> b -> c -> d)
-> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
zipWith3 a -> b -> c -> d
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        sz :: Size
sz  = [Size] -> Size
smallest [forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
Stream.zipWith3 a -> b -> c -> d
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs')) Size
sz) Proxy t
p
#else
zipWith3 f as bs cs = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith3 f (G.stream (withW p as)) (G.stream (withW p bs)) (G.stream (withW p cs)))) p
#endif

zipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e)
         => (a -> b -> c -> d -> e)
         -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
{-# INLINE zipWith4 #-}
#if MIN_VERSION_vector(0,11,0)
zipWith4 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d (tye :: SEXPTYPE) e.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d,
 SVECTOR tye e) =>
(a -> b -> c -> d -> e)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
zipWith4 a -> b -> c -> d -> e
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs Vector tyd d
ds = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        ds' :: Bundle (W t tyd) d
ds' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyd d
ds)
        sz :: Size
sz  = [Size] -> Size
smallest [forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
Stream.zipWith4 a -> b -> c -> d -> e
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds')) Size
sz) Proxy t
p
#else
zipWith4 f as bs cs ds = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith4 f (G.stream (withW p as)) (G.stream (withW p bs)) (G.stream (withW p cs)) (G.stream (withW p ds)))) p
#endif

zipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e,
             SVECTOR tyf f)
         => (a -> b -> c -> d -> e -> f)
         -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
         -> Vector tyf f
{-# INLINE zipWith5 #-}
#if MIN_VERSION_vector(0,11,0)
zipWith5 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d (tye :: SEXPTYPE) e (tyf :: SEXPTYPE) f.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d,
 SVECTOR tye e, SVECTOR tyf f) =>
(a -> b -> c -> d -> e -> f)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
-> Vector tyf f
zipWith5 a -> b -> c -> d -> e -> f
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs Vector tyd d
ds Vector tye e
es = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        ds' :: Bundle (W t tyd) d
ds' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyd d
ds)
        es' :: Bundle (W t tye) e
es' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tye e
es)
        sz :: Size
sz  = [Size] -> Size
smallest [forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
Stream.zipWith5 a -> b -> c -> d -> e -> f
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tye) e
es')) Size
sz) Proxy t
p
#else
zipWith5 f as bs cs ds es = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith5 f (G.stream (withW p as)) (G.stream (withW p bs)) (G.stream (withW p cs)) (G.stream (withW p ds)) (G.stream (withW p es)))) p
#endif

zipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e,
             SVECTOR tyf f, SVECTOR tyg g)
         => (a -> b -> c -> d -> e -> f -> g)
         -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
         -> Vector tyf f -> Vector tyg g
{-# INLINE zipWith6 #-}
#if MIN_VERSION_vector(0,11,0)
zipWith6 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d (tye :: SEXPTYPE) e (tyf :: SEXPTYPE) f
       (tyg :: SEXPTYPE) g.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d,
 SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) =>
(a -> b -> c -> d -> e -> f -> g)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
-> Vector tyf f
-> Vector tyg g
zipWith6 a -> b -> c -> d -> e -> f -> g
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs Vector tyd d
ds Vector tye e
es Vector tyf f
fs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        ds' :: Bundle (W t tyd) d
ds' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyd d
ds)
        es' :: Bundle (W t tye) e
es' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tye e
es)
        fs' :: Bundle (W t tyf) f
fs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyf f
fs)
        sz :: Size
sz  = [Size] -> Size
smallest [forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyf) f
fs']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
Stream.zipWith6 a -> b -> c -> d -> e -> f -> g
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tye) e
es') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyf) f
fs')) Size
sz) Proxy t
p
#else
zipWith6 f as bs cs ds es fs = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith6 f (G.stream (withW p as)) (G.stream (withW p bs)) (G.stream (withW p cs)) (G.stream (withW p ds)) (G.stream (withW p es)) (G.stream (withW p fs)))) p
#endif

-- | /O(min(m,n))/ Zip two vectors with a function that also takes the
-- elements' indices.
izipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c)
         => (Int -> a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c
{-# INLINE izipWith #-}
#if MIN_VERSION_vector(0,11,0)
izipWith :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) =>
(Int -> a -> b -> c)
-> Vector tya a -> Vector tyb b -> Vector tyc c
izipWith Int -> a -> b -> c
f Vector tya a
as Vector tyb b
bs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        sz :: Size
sz  = Size -> Size -> Size
smaller (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs')
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
Stream.zipWith (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c
f) (forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs')) Size
sz) Proxy t
p
#else
izipWith f as bs = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith (uncurry f) (Stream.indexed (G.stream (withW p as))) (G.stream (withW p bs)))) p
#endif

-- | Zip three vectors and their indices with the given function.
izipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d)
          => (Int -> a -> b -> c -> d)
          -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
{-# INLINE izipWith3 #-}
#if MIN_VERSION_vector(0,11,0)
izipWith3 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) =>
(Int -> a -> b -> c -> d)
-> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
izipWith3 Int -> a -> b -> c -> d
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        sz :: Size
sz  = [Size] -> Size
smallest [forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
Stream.zipWith3 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d
f) (forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs')) Size
sz) Proxy t
p
#else
izipWith3 f as bs cs = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith3 (uncurry f) (Stream.indexed (G.stream (withW p as))) (G.stream (withW p bs)) (G.stream (withW p cs)))) p
#endif

izipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e)
          => (Int -> a -> b -> c -> d -> e)
          -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
{-# INLINE izipWith4 #-}
#if MIN_VERSION_vector(0,11,0)
izipWith4 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d (tye :: SEXPTYPE) e.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d,
 SVECTOR tye e) =>
(Int -> a -> b -> c -> d -> e)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
izipWith4 Int -> a -> b -> c -> d -> e
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs Vector tyd d
ds = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        ds' :: Bundle (W t tyd) d
ds' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyd d
ds)
        sz :: Size
sz  = [Size] -> Size
smallest [ forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
Stream.zipWith4 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d -> e
f) (forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds')) Size
sz) Proxy t
p
#else
izipWith4 f as bs cs ds = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith4 (uncurry f) (Stream.indexed (G.stream (withW p as))) (G.stream (withW p bs)) (G.stream (withW p cs)) (G.stream (withW p ds)))) p
#endif

izipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e,
              SVECTOR tyf f)
          => (Int -> a -> b -> c -> d -> e -> f)
          -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
          -> Vector tyf f
{-# INLINE izipWith5 #-}
#if MIN_VERSION_vector(0,11,0)
izipWith5 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d (tye :: SEXPTYPE) e (tyf :: SEXPTYPE) f.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d,
 SVECTOR tye e, SVECTOR tyf f) =>
(Int -> a -> b -> c -> d -> e -> f)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
-> Vector tyf f
izipWith5 Int -> a -> b -> c -> d -> e -> f
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs Vector tyd d
ds Vector tye e
es = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        ds' :: Bundle (W t tyd) d
ds' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyd d
ds)
        es' :: Bundle (W t tye) e
es' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tye e
es)
        sz :: Size
sz  = [Size] -> Size
smallest [ forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
Stream.zipWith5 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d -> e -> f
f) (forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tye) e
es')) Size
sz) Proxy t
p
#else
izipWith5 f as bs cs ds es = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith5 (uncurry f) (Stream.indexed (G.stream (withW p as))) (G.stream (withW p bs)) (G.stream (withW p cs)) (G.stream (withW p ds)) (G.stream (withW p es)))) p
#endif

izipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e,
              SVECTOR tyf f, SVECTOR tyg g)
          => (Int -> a -> b -> c -> d -> e -> f -> g)
          -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
          -> Vector tyf f -> Vector tyg g
{-# INLINE izipWith6 #-}
#if MIN_VERSION_vector(0,11,0)
izipWith6 :: forall (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b (tyc :: SEXPTYPE) c
       (tyd :: SEXPTYPE) d (tye :: SEXPTYPE) e (tyf :: SEXPTYPE) f
       (tyg :: SEXPTYPE) g.
(SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d,
 SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) =>
(Int -> a -> b -> c -> d -> e -> f -> g)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
-> Vector tyf f
-> Vector tyg g
izipWith6 Int -> a -> b -> c -> d -> e -> f -> g
f Vector tya a
as Vector tyb b
bs Vector tyc c
cs Vector tyd d
ds Vector tye e
es Vector tyf f
fs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
as)
        bs' :: Bundle (W t tyb) b
bs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
bs)
        cs' :: Bundle (W t tyc) c
cs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyc c
cs)
        ds' :: Bundle (W t tyd) d
ds' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyd d
ds)
        es' :: Bundle (W t tye) e
es' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tye e
es)
        fs' :: Bundle (W t tyf) f
fs' = forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyf f
fs)
        sz :: Size
sz  = [Size] -> Size
smallest [ forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es', forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyf) f
fs']
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
Stream.zipWith6 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d -> e -> f -> g
f) (forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tye) e
es') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyf) f
fs')) Size
sz) Proxy t
p
#else
izipWith6 f as bs cs ds es fs = phony $ \p ->
  proxyW (G.unstream (Stream.zipWith6 (uncurry f) (Stream.indexed (G.stream (withW p as))) (G.stream (withW p bs)) (G.stream (withW p cs)) (G.stream (withW p ds)) (G.stream (withW p es)) (G.stream (withW p fs)))) p
#endif

-- Monadic zipping
-- ---------------


-- | /O(min(m,n))/ Zip the two vectors with the monadic action and yield a
-- vector of results
zipWithM :: (MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b, VECTOR (Region m) tyc c, ElemRep V tya ~ a, ElemRep V tyb ~ b, ElemRep V tyc ~ c)
         => (a -> b -> m c)
         -> Vector tya a
         -> Vector tyb b
         -> m (Vector tyc c)
{-# INLINE zipWithM #-}
#if MIN_VERSION_vector(0,11,0)
zipWithM :: forall (m :: * -> *) (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b
       (tyc :: SEXPTYPE) c.
(MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b,
 VECTOR (Region m) tyc c, ElemRep V tya ~ a, ElemRep V tyb ~ b,
 ElemRep V tyc ~ c) =>
(a -> b -> m c) -> Vector tya a -> Vector tyb b -> m (Vector tyc c)
zipWithM a -> b -> m c
f Vector tya a
xs Vector tyb b
ys = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let xs' :: Bundle m (W t tya) a
xs' = forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
xs)
        ys' :: Bundle m (W t tyb) b
ys' = forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
ys)
        sz :: Size
sz  = Size -> Size -> Size
smaller (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m (W t tya) a
xs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m (W t tyb) b
ys')
    in forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.fmap forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Size -> [a] -> Bundle m v a
Bundle.unsafeFromList Size
sz forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
Stream.toList (forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
Stream.zipWithM a -> b -> m c
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tya) a
xs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tyb) b
ys')))
              forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Proxy t
p
#else
zipWithM f xs ys = phony $ \p ->
    proxyW <$>
    unstreamM (Stream.zipWithM f (G.stream (withW p xs)) (G.stream (withW p ys))) <*>
    return p
  where
    -- Inlined from vector-0.10, which doesn't export unstreamM.
    unstreamM s = do
        zs <- MStream.toList s
        return $ G.unstream $ Stream.unsafeFromList (MStream.size s) zs
#endif


-- | /O(min(m,n))/ Zip the two vectors with the monadic action and ignore the
-- results
zipWithM_ :: (Monad m, SVECTOR tya a, SVECTOR tyb b)
          => (a -> b -> m c)
          -> Vector tya a
          -> Vector tyb b
          -> m ()
{-# INLINE zipWithM_ #-}
#if MIN_VERSION_vector(0,11,0)
zipWithM_ :: forall (m :: * -> *) (tya :: SEXPTYPE) a (tyb :: SEXPTYPE) b c.
(Monad m, SVECTOR tya a, SVECTOR tyb b) =>
(a -> b -> m c) -> Vector tya a -> Vector tyb b -> m ()
zipWithM_ a -> b -> m c
f Vector tya a
xs Vector tyb b
ys = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p ->
    let xs' :: Bundle m (W t tya) a
xs' = forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
xs)
        ys' :: Bundle m (W t tyb) b
ys' = forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
ys)
    in forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> m ()
Stream.zipWithM_ a -> b -> m c
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tya) a
xs') (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tyb) b
ys')
#else
zipWithM_ f xs ys = phony $ \p ->
    Stream.zipWithM_ f (G.stream (withW p xs)) (G.stream (withW p ys))
#endif

-- Filtering
-- ---------

-- | /O(n)/ Drop elements that do not satisfy the predicate
filter :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a
{-# INLINE filter #-}
filter :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Vector ty a
filter a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
G.filter a -> Bool
f) Vector ty a
v

-- | /O(n)/ Drop elements that do not satisfy the predicate which is applied to
-- values and their indices
ifilter :: SVECTOR ty a => (Int -> a -> Bool) -> Vector ty a -> Vector ty a
{-# INLINE ifilter #-}
ifilter :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(Int -> a -> Bool) -> Vector ty a -> Vector ty a
ifilter Int -> a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(Int -> a -> Bool) -> v a -> v a
G.ifilter Int -> a -> Bool
f) Vector ty a
v

-- | /O(n)/ Drop elements that do not satisfy the monadic predicate
filterM :: (Monad m, SVECTOR ty a) => (a -> m Bool) -> Vector ty a -> m (Vector ty a)
{-# INLINE filterM #-}
filterM :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
(a -> m Bool) -> Vector ty a -> m (Vector ty a)
filterM a -> m Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
(a -> m Bool) -> v a -> m (v a)
G.filterM a -> m Bool
f) Vector ty a
v Proxy t
p

-- | /O(n)/ Yield the longest prefix of elements satisfying the predicate
-- with copying.
takeWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a
{-# INLINE takeWhile #-}
takeWhile :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Vector ty a
takeWhile a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
G.takeWhile a -> Bool
f) Vector ty a
v

-- | /O(n)/ Drop the longest prefix of elements that satisfy the predicate
-- with copying.
dropWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a
{-# INLINE dropWhile #-}
dropWhile :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Vector ty a
dropWhile a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
G.dropWhile a -> Bool
f) Vector ty a
v

-- Parititioning
-- -------------

-- | /O(n)/ Split the vector in two parts, the first one containing those
-- elements that satisfy the predicate and the second one those that don't. The
-- relative order of the elements is preserved at the cost of a sometimes
-- reduced performance compared to 'unstablePartition'.
partition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
{-# INLINE partition #-}
partition :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
partition a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ (\(W t ty a
a,W t ty a
b) -> (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> (v a, v a)
G.partition a -> Bool
f) Vector ty a
v

-- | /O(n)/ Split the vector in two parts, the first one containing those
-- elements that satisfy the predicate and the second one those that don't.
-- The order of the elements is not preserved but the operation is often
-- faster than 'partition'.
unstablePartition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
{-# INLINE unstablePartition #-}
unstablePartition :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
unstablePartition a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ (\(W t ty a
a,W t ty a
b) -> (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> (v a, v a)
G.unstablePartition a -> Bool
f) Vector ty a
v

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest with copying.
span :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
{-# INLINE span #-}
span :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
span a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ (\(W t ty a
a,W t ty a
b) -> (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> (v a, v a)
G.span a -> Bool
f) Vector ty a
v

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest with copying.
break :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
{-# INLINE break #-}
break :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
break a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ (\(W t ty a
a,W t ty a
b) -> (forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> (v a, v a)
G.break a -> Bool
f) Vector ty a
v

-- Searching
-- ---------

infix 4 `elem`
-- | /O(n)/ Check if the vector contains an element
elem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool
{-# INLINE elem #-}
elem :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Eq a) =>
a -> Vector ty a -> Bool
elem a
a Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. (Vector v a, Eq a) => a -> v a -> Bool
G.elem a
a) Vector ty a
v

infix 4 `notElem`
-- | /O(n)/ Check if the vector does not contain an element (inverse of 'elem')
notElem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool
{-# INLINE notElem #-}
notElem :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Eq a) =>
a -> Vector ty a -> Bool
notElem a
a Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. (Vector v a, Eq a) => a -> v a -> Bool
G.notElem a
a) Vector ty a
v

-- | /O(n)/ Yield 'Just' the first element matching the predicate or 'Nothing'
-- if no such element exists.
find :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe a
{-# INLINE find #-}
find :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Maybe a
find a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Maybe a
G.find a -> Bool
f) Vector ty a
v

-- | /O(n)/ Yield 'Just' the index of the first element matching the predicate
-- or 'Nothing' if no such element exists.
findIndex :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe Int
{-# INLINE findIndex #-}
findIndex :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Maybe Int
findIndex a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> Maybe Int
G.findIndex a -> Bool
f) Vector ty a
v

{-
-- | /O(n)/ Yield the indices of elements satisfying the predicate in ascending
-- order.
findIndices :: VECTOR s ty a => (a -> Bool) -> Vector ty a -> Vector Int
{-# INLINE findIndices #-}
findIndices f v = phony $ proxyFW (G.findIndices f) v
-}

-- | /O(n)/ Yield 'Just' the index of the first occurence of the given element or
-- 'Nothing' if the vector does not contain the element. This is a specialised
-- version of 'findIndex'.
elemIndex :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Maybe Int
{-# INLINE elemIndex #-}
elemIndex :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Eq a) =>
a -> Vector ty a -> Maybe Int
elemIndex a
a Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. (Vector v a, Eq a) => a -> v a -> Maybe Int
G.elemIndex a
a) Vector ty a
v

{-
-- | /O(n)/ Yield the indices of all occurences of the given element in
-- ascending order. This is a specialised version of 'findIndices'.
elemIndices :: (VECTOR s ty a, Eq a) => a -> Vector ty a -> Vector 'R.Int Int32
{-# INLINE elemIndices #-}
elemIndices s v = phony $ unW . proxyFW (G.elemIndices s) v
-}

-- Folding
-- -------

-- | /O(n)/ Left fold
foldl :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a
{-# INLINE foldl #-}
foldl :: forall (ty :: SEXPTYPE) b a.
SVECTOR ty b =>
(a -> b -> a) -> a -> Vector ty b -> a
foldl a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Left fold on non-empty vectors
foldl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
{-# INLINE foldl1 #-}
foldl1 :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> a
foldl1 a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> a
G.foldl1 a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Left fold with strict accumulator
foldl' :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a
{-# INLINE foldl' #-}
foldl' :: forall (ty :: SEXPTYPE) b a.
SVECTOR ty b =>
(a -> b -> a) -> a -> Vector ty b -> a
foldl' a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Left fold on non-empty vectors with strict accumulator
foldl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
{-# INLINE foldl1' #-}
foldl1' :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> a
foldl1' a -> a -> a
f Vector ty a
v  = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> a
G.foldl1' a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Right fold
foldr :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b
{-# INLINE foldr #-}
foldr :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(a -> b -> b) -> b -> Vector ty a -> b
foldr a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
G.foldr a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right fold on non-empty vectors
foldr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
{-# INLINE foldr1 #-}
foldr1 :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> a
foldr1 a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> a
G.foldr1 a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Right fold with a strict accumulator
foldr' :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b
{-# INLINE foldr' #-}
foldr' :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(a -> b -> b) -> b -> Vector ty a -> b
foldr' a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
G.foldr' a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right fold on non-empty vectors with strict accumulator
foldr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
{-# INLINE foldr1' #-}
foldr1' :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> a
foldr1' a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> a
G.foldr1' a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Left fold (function applied to each element and its index)
ifoldl :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a
{-# INLINE ifoldl #-}
ifoldl :: forall (ty :: SEXPTYPE) b a.
SVECTOR ty b =>
(a -> Int -> b -> a) -> a -> Vector ty b -> a
ifoldl a -> Int -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) b a.
Vector v b =>
(a -> Int -> b -> a) -> a -> v b -> a
G.ifoldl a -> Int -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Left fold with strict accumulator (function applied to each element
-- and its index)
ifoldl' :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a
{-# INLINE ifoldl' #-}
ifoldl' :: forall (ty :: SEXPTYPE) b a.
SVECTOR ty b =>
(a -> Int -> b -> a) -> a -> Vector ty b -> a
ifoldl' a -> Int -> b -> a
f a
s  Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) b a.
Vector v b =>
(a -> Int -> b -> a) -> a -> v b -> a
G.ifoldl' a -> Int -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Right fold (function applied to each element and its index)
ifoldr :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b
{-# INLINE ifoldr #-}
ifoldr :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(Int -> a -> b -> b) -> b -> Vector ty a -> b
ifoldr Int -> a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
Vector v a =>
(Int -> a -> b -> b) -> b -> v a -> b
G.ifoldr Int -> a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right fold with strict accumulator (function applied to each
-- element and its index)
ifoldr' :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b
{-# INLINE ifoldr' #-}
ifoldr' :: forall (ty :: SEXPTYPE) a b.
SVECTOR ty a =>
(Int -> a -> b -> b) -> b -> Vector ty a -> b
ifoldr' Int -> a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
Vector v a =>
(Int -> a -> b -> b) -> b -> v a -> b
G.ifoldr' Int -> a -> b -> b
f b
s) Vector ty a
v

-- Specialised folds
-- -----------------


-- | /O(n)/ Check if all elements satisfy the predicate.
all :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool
{-# INLINE all #-}
all :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Bool
all a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Bool
G.all a -> Bool
f (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector ty a
v)

-- | /O(n)/ Check if any element satisfies the predicate.
any :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool
{-# INLINE any #-}
any :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> Bool) -> Vector ty a -> Bool
any a -> Bool
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ \Proxy t
p -> forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Bool
G.any a -> Bool
f (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector ty a
v)

-- -- | /O(n)/ Check if all elements are 'True'
-- and :: Vector 'Logical Bool -> Bool
-- {-# INLINE and #-}
-- and v = phony $ \p -> G.and (withW p v)
--
-- -- | /O(n)/ Check if any element is 'True'
-- or :: Vector 'Logical Bool -> Bool
-- {-# INLINE or #-}
-- or v = phony $ \p -> G.or (withW p v)

-- | /O(n)/ Compute the sum of the elements
sum :: (SVECTOR ty a, Num a) => Vector ty a -> a
{-# INLINE sum #-}
sum :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Num a) =>
Vector ty a -> a
sum Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. (Vector v a, Num a) => v a -> a
G.sum Vector ty a
v

-- | /O(n)/ Compute the produce of the elements
product :: (SVECTOR ty a, Num a) => Vector ty a -> a
{-# INLINE product #-}
product :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Num a) =>
Vector ty a -> a
product Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. (Vector v a, Num a) => v a -> a
G.product Vector ty a
v

-- | /O(n)/ Yield the maximum element of the vector. The vector may not be
-- empty.
maximum :: (SVECTOR ty a, Ord a) => Vector ty a -> a
{-# INLINE maximum #-}
maximum :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Ord a) =>
Vector ty a -> a
maximum Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> a
G.maximum Vector ty a
v

-- | /O(n)/ Yield the maximum element of the Vector ty according to the given
-- comparison function. The vector may not be empty.
maximumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a
{-# INLINE maximumBy #-}
maximumBy :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> Ordering) -> Vector ty a -> a
maximumBy a -> a -> Ordering
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> a
G.maximumBy a -> a -> Ordering
f) Vector ty a
v

-- | /O(n)/ Yield the minimum element of the vector. The vector may not be
-- empty.
minimum :: (SVECTOR ty a, Ord a) => Vector ty a -> a
{-# INLINE minimum #-}
minimum :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Ord a) =>
Vector ty a -> a
minimum Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> a
G.minimum Vector ty a
v

-- | /O(n)/ Yield the minimum element of the Vector ty according to the given
-- comparison function. The vector may not be empty.
minimumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a
{-# INLINE minimumBy #-}
minimumBy :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> Ordering) -> Vector ty a -> a
minimumBy a -> a -> Ordering
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> a
G.minimumBy a -> a -> Ordering
f) Vector ty a
v

-- | /O(n)/ Yield the index of the maximum element of the vector. The vector
-- may not be empty.
maxIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int
{-# INLINE maxIndex #-}
maxIndex :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Ord a) =>
Vector ty a -> Int
maxIndex Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> Int
G.maxIndex Vector ty a
v

-- | /O(n)/ Yield the index of the maximum element of the Vector ty according to
-- the given comparison function. The vector may not be empty.
maxIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int
{-# INLINE maxIndexBy #-}
maxIndexBy :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> Ordering) -> Vector ty a -> Int
maxIndexBy a -> a -> Ordering
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> Int
G.maxIndexBy a -> a -> Ordering
f) Vector ty a
v

-- | /O(n)/ Yield the index of the minimum element of the vector. The vector
-- may not be empty.
minIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int
{-# INLINE minIndex #-}
minIndex :: forall (ty :: SEXPTYPE) a.
(SVECTOR ty a, Ord a) =>
Vector ty a -> Int
minIndex Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> Int
G.minIndex Vector ty a
v

-- | /O(n)/ Yield the index of the minimum element of the Vector ty according to
-- the given comparison function. The vector may not be empty.
minIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int
{-# INLINE minIndexBy #-}
minIndexBy :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> Ordering) -> Vector ty a -> Int
minIndexBy a -> a -> Ordering
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> Int
G.minIndexBy a -> a -> Ordering
f) Vector ty a
v

-- Monadic folds
-- -------------

-- | /O(n)/ Monadic fold
foldM :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a
{-# INLINE foldM #-}
foldM :: forall (m :: * -> *) (ty :: SEXPTYPE) b a.
(Monad m, SVECTOR ty b) =>
(a -> b -> m a) -> a -> Vector ty b -> m a
foldM a -> b -> m a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m a
G.foldM a -> b -> m a
f a
s) Vector ty b
v

-- | /O(n)/ Monadic fold over non-empty vectors
fold1M :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a
{-# INLINE fold1M #-}
fold1M :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
(a -> a -> m a) -> Vector ty a -> m a
fold1M a -> a -> m a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m a
G.fold1M a -> a -> m a
f) Vector ty a
v

-- | /O(n)/ Monadic fold with strict accumulator
foldM' :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a
{-# INLINE foldM' #-}
foldM' :: forall (m :: * -> *) (ty :: SEXPTYPE) b a.
(Monad m, SVECTOR ty b) =>
(a -> b -> m a) -> a -> Vector ty b -> m a
foldM' a -> b -> m a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m a
G.foldM' a -> b -> m a
f a
s) Vector ty b
v

-- | /O(n)/ Monadic fold over non-empty vectors with strict accumulator
fold1M' :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a
{-# INLINE fold1M' #-}
fold1M' :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
(a -> a -> m a) -> Vector ty a -> m a
fold1M' a -> a -> m a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m a
G.fold1M' a -> a -> m a
f) Vector ty a
v

-- | /O(n)/ Monadic fold that discards the result
foldM_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m ()
{-# INLINE foldM_ #-}
foldM_ :: forall (m :: * -> *) (ty :: SEXPTYPE) b a.
(Monad m, SVECTOR ty b) =>
(a -> b -> m a) -> a -> Vector ty b -> m ()
foldM_ a -> b -> m a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m ()
G.foldM_ a -> b -> m a
f a
s) Vector ty b
v

-- | /O(n)/ Monadic fold over non-empty vectors that discards the result
fold1M_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m ()
{-# INLINE fold1M_ #-}
fold1M_ :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
(a -> a -> m a) -> Vector ty a -> m ()
fold1M_ a -> a -> m a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m ()
G.fold1M_ a -> a -> m a
f) Vector ty a
v

-- | /O(n)/ Monadic fold with strict accumulator that discards the result
foldM'_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m ()
{-# INLINE foldM'_ #-}
foldM'_ :: forall (m :: * -> *) (ty :: SEXPTYPE) b a.
(Monad m, SVECTOR ty b) =>
(a -> b -> m a) -> a -> Vector ty b -> m ()
foldM'_ a -> b -> m a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m ()
G.foldM'_ a -> b -> m a
f a
s) Vector ty b
v

-- | /O(n)/ Monadic fold over non-empty vectors with strict accumulator
-- that discards the result
fold1M'_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m ()
{-# INLINE fold1M'_ #-}
fold1M'_ :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(Monad m, SVECTOR ty a) =>
(a -> a -> m a) -> Vector ty a -> m ()
fold1M'_ a -> a -> m a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m ()
G.fold1M'_ a -> a -> m a
f) Vector ty a
v

-- Prefix sums (scans)
-- -------------------

-- | /O(n)/ Prescan
--
-- @
-- prescanl f z = 'init' . 'scanl' f z
-- @
--
-- Example: @prescanl (+) 0 \<1,2,3,4\> = \<0,1,3,6\>@
--
prescanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
{-# INLINE prescanl #-}
prescanl :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> a) -> a -> Vector ty b -> Vector ty a
prescanl a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
G.prescanl a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Prescan with strict accumulator
prescanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
{-# INLINE prescanl' #-}
prescanl' :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> a) -> a -> Vector ty b -> Vector ty a
prescanl' a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
G.prescanl' a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Scan
--
-- @
-- postscanl f z = 'tail' . 'scanl' f z
-- @
--
-- Example: @postscanl (+) 0 \<1,2,3,4\> = \<1,3,6,10\>@
--
postscanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
{-# INLINE postscanl #-}
postscanl :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> a) -> a -> Vector ty b -> Vector ty a
postscanl a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
G.postscanl a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Scan with strict accumulator
postscanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
{-# INLINE postscanl' #-}
postscanl' :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> a) -> a -> Vector ty b -> Vector ty a
postscanl' a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
G.postscanl' a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Haskell-style scan
--
-- > scanl f z <x1,...,xn> = <y1,...,y(n+1)>
-- >   where y1 = z
-- >         yi = f y(i-1) x(i-1)
--
-- Example: @scanl (+) 0 \<1,2,3,4\> = \<0,1,3,6,10\>@
--
scanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
{-# INLINE scanl #-}
scanl :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> a) -> a -> Vector ty b -> Vector ty a
scanl a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
G.scanl a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Haskell-style scan with strict accumulator
scanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
{-# INLINE scanl' #-}
scanl' :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> a) -> a -> Vector ty b -> Vector ty a
scanl' a -> b -> a
f a
s Vector ty b
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
G.scanl' a -> b -> a
f a
s) Vector ty b
v

-- | /O(n)/ Scan over a non-empty vector
--
-- > scanl f <x1,...,xn> = <y1,...,yn>
-- >   where y1 = x1
-- >         yi = f y(i-1) xi
--
scanl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
{-# INLINE scanl1 #-}
scanl1 :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> Vector ty a
scanl1 a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> v a
G.scanl1 a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Scan over a non-empty vector with a strict accumulator
scanl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
{-# INLINE scanl1' #-}
scanl1' :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> Vector ty a
scanl1' a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> v a
G.scanl1' a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Right-to-left prescan
--
-- @
-- prescanr f z = 'reverse' . 'prescanl' (flip f) z . 'reverse'
-- @
--
prescanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
{-# INLINE prescanr #-}
prescanr :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> b) -> b -> Vector ty a -> Vector ty b
prescanr a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
G.prescanr a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right-to-left prescan with strict accumulator
prescanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
{-# INLINE prescanr' #-}
prescanr' :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> b) -> b -> Vector ty a -> Vector ty b
prescanr' a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
G.prescanr' a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right-to-left scan
postscanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
{-# INLINE postscanr #-}
postscanr :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> b) -> b -> Vector ty a -> Vector ty b
postscanr a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
G.postscanr a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right-to-left scan with strict accumulator
postscanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
{-# INLINE postscanr' #-}
postscanr' :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> b) -> b -> Vector ty a -> Vector ty b
postscanr' a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
G.postscanr' a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right-to-left Haskell-style scan
scanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
{-# INLINE scanr #-}
scanr :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> b) -> b -> Vector ty a -> Vector ty b
scanr a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
G.scanr a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right-to-left Haskell-style scan with strict accumulator
scanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
{-# INLINE scanr' #-}
scanr' :: forall (ty :: SEXPTYPE) a b.
(SVECTOR ty a, SVECTOR ty b) =>
(a -> b -> b) -> b -> Vector ty a -> Vector ty b
scanr' a -> b -> b
f b
s Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
G.scanr' a -> b -> b
f b
s) Vector ty a
v

-- | /O(n)/ Right-to-left scan over a non-empty vector
scanr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
{-# INLINE scanr1 #-}
scanr1 :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> Vector ty a
scanr1 a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> v a
G.scanr1 a -> a -> a
f) Vector ty a
v

-- | /O(n)/ Right-to-left scan over a non-empty vector with a strict
-- accumulator
scanr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
{-# INLINE scanr1' #-}
scanr1' :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
(a -> a -> a) -> Vector ty a -> Vector ty a
scanr1' a -> a -> a
f Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> v a
G.scanr1' a -> a -> a
f) Vector ty a
v

-- Conversions - Lists
-- ------------------------

-- | /O(n)/ Convert a vector to a list
toList :: SVECTOR ty a => Vector ty a -> [a]
{-# INLINE toList #-}
toList :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList Vector ty a
v = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW forall (v :: * -> *) a. Vector v a => v a -> [a]
G.toList Vector ty a
v

-- | /O(n)/ Convert a list to a vector
fromList :: forall ty a . SVECTOR ty a => [a] -> Vector ty a
{-# INLINE fromList #-}
fromList :: forall (ty :: SEXPTYPE) a. SVECTOR ty a => [a] -> Vector ty a
fromList [a]
xs = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
G.fromListN (forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
xs) [a]
xs)

-- | /O(n)/ Convert the first @n@ elements of a list to a vector
--
-- @
-- fromListN n xs = 'fromList' ('take' n xs)
-- @
fromListN :: forall ty a . SVECTOR ty a => Int -> [a] -> Vector ty a
{-# INLINE fromListN #-}
fromListN :: forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> [a] -> Vector ty a
fromListN Int
i [a]
l = forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall a b. (a -> b) -> a -> b
$ forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
G.fromListN Int
i [a]
l)

-- Conversions - Unsafe casts
-- --------------------------

-- Conversions - Mutable vectors
-- -----------------------------

-- | /O(1)/ Unsafe convert a mutable vector to an immutable one with
-- copying. The mutable vector may not be used after this operation.
unsafeFreeze :: (VECTOR (Region m) ty a, MonadR m, ElemRep V ty ~ a)
             => MVector (Region m) ty a -> m (Vector ty a)
{-# INLINE unsafeFreeze #-}
unsafeFreeze :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(VECTOR (Region m) ty a, MonadR m, ElemRep V ty ~ a) =>
MVector (Region m) ty a -> m (Vector ty a)
unsafeFreeze MVector (Region m) ty a
m = forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire forall a b. (a -> b) -> a -> b
$ \Proxy s
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.unsafeFreeze (forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy s
p MVector (Region m) ty a
m)

-- | /O(1)/ Unsafely convert an immutable vector to a mutable one with
-- copying. The immutable vector may not be used after this operation.
unsafeThaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a)
           => Vector ty a -> m (MVector (Region m) ty a)
{-# INLINE unsafeThaw #-}
unsafeThaw :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) =>
Vector ty a -> m (MVector (Region m) ty a)
unsafeThaw Vector ty a
v = forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire forall a b. (a -> b) -> a -> b
$ \Proxy s
p -> forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
Mutable.unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
G.unsafeThaw (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy s
p Vector ty a
v)

-- | /O(n)/ Yield a mutable copy of the immutable vector.
thaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a)
     => Vector ty a -> m (MVector (Region m) ty a)
{-# INLINE thaw #-}
thaw :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) =>
Vector ty a -> m (MVector (Region m) ty a)
thaw Vector ty a
v1 = forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire forall a b. (a -> b) -> a -> b
$ \Proxy s
p -> forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
Mutable.unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
G.thaw (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy s
p Vector ty a
v1)

-- | /O(n)/ Yield an immutable copy of the mutable vector.
freeze :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a)
       => MVector (Region m) ty a -> m (Vector ty a)
{-# INLINE freeze #-}
freeze :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) =>
MVector (Region m) ty a -> m (Vector ty a)
freeze MVector (Region m) ty a
m1 = forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire forall a b. (a -> b) -> a -> b
$ \Proxy s
p -> forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.freeze (forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy s
p MVector (Region m) ty a
m1)

-- | /O(n)/ Copy an immutable vector into a mutable one. The two vectors must
-- have the same length. This is not checked.
unsafeCopy
  :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a)
  => MVector (Region m) ty a -> Vector ty a -> m ()
{-# INLINE unsafeCopy #-}
unsafeCopy :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) =>
MVector (Region m) ty a -> Vector ty a -> m ()
unsafeCopy MVector (Region m) ty a
m1 Vector ty a
v2 = forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire forall a b. (a -> b) -> a -> b
$ \Proxy s
p -> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
G.unsafeCopy (forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy s
p MVector (Region m) ty a
m1) (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy s
p Vector ty a
v2)

-- | /O(n)/ Copy an immutable vector into a mutable one. The two vectors must
-- have the same length.
copy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a)
     => MVector (Region m) ty a -> Vector ty a -> m ()
{-# INLINE copy #-}
copy :: forall (m :: * -> *) (ty :: SEXPTYPE) a.
(MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) =>
MVector (Region m) ty a -> Vector ty a -> m ()
copy MVector (Region m) ty a
m1 Vector ty a
v2 = forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire forall a b. (a -> b) -> a -> b
$ \Proxy s
p -> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
G.copy (forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy s
p MVector (Region m) ty a
m1) (forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy s
p Vector ty a
v2)

phony :: (forall t . Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony :: forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony forall t. Reifies t (AcquireIO s) => Proxy t -> r
f = forall a r. a -> (forall s. Reifies s a => Proxy s -> r) -> r
reify (forall s.
(forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
AcquireIO forall (ty :: SEXPTYPE) g. SEXP V ty -> IO (SEXP g ty)
acquireIO) forall a b. (a -> b) -> a -> b
$ \Proxy s
p ->  forall t. Reifies t (AcquireIO s) => Proxy t -> r
f Proxy s
p
  where
    acquireIO :: SEXP V ty -> IO (SEXP g ty)
    acquireIO :: forall (ty :: SEXPTYPE) g. SEXP V ty -> IO (SEXP g ty)
acquireIO SEXP V ty
x = forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ do
      forall s (a :: SEXPTYPE). SEXP s a -> IO ()
R.preserveObject SEXP V ty
x
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s (a :: SEXPTYPE) r. SEXP s a -> SEXP r a
R.unsafeRelease SEXP V ty
x