-- |
-- 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(..) )
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
#if __GLASGOW_HASKELL__ >= 708
import qualified GHC.Exts as Exts
#endif
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 ())

-- | Create a 'ForeignSEXP' from 'SEXP'.
foreignSEXP :: PrimMonad m => SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP :: SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP sx :: SEXP s ty
sx@(SEXP ptr :: Ptr (HExp s ty)
ptr) =
    IO (ForeignSEXP ty) -> m (ForeignSEXP ty)
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO (ForeignSEXP ty) -> m (ForeignSEXP ty))
-> IO (ForeignSEXP ty) -> m (ForeignSEXP ty)
forall a b. (a -> b) -> a -> b
$ IO (ForeignSEXP ty) -> IO (ForeignSEXP ty)
forall a. IO a -> IO a
mask_ (IO (ForeignSEXP ty) -> IO (ForeignSEXP ty))
-> IO (ForeignSEXP ty) -> IO (ForeignSEXP ty)
forall a b. (a -> b) -> a -> b
$ do
      SEXP s ty -> IO ()
forall s (a :: SEXPTYPE). SEXP s a -> IO ()
R.preserveObject SEXP s ty
sx
      ForeignPtr () -> ForeignSEXP ty
forall (ty :: SEXPTYPE). ForeignPtr () -> ForeignSEXP ty
ForeignSEXP (ForeignPtr () -> ForeignSEXP ty)
-> IO (ForeignPtr ()) -> IO (ForeignSEXP ty)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr () -> IO () -> IO (ForeignPtr ())
forall a. Ptr a -> IO () -> IO (ForeignPtr a)
GHC.newConcForeignPtr (Ptr (HExp s ty) -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr (HExp s ty)
ptr) (SEXP s ty -> IO ()
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 :: ForeignSEXP ty -> (SEXP V ty -> IO r) -> IO r
withForeignSEXP (ForeignSEXP fptr :: ForeignPtr ()
fptr) f :: SEXP V ty -> IO r
f =
    ForeignPtr () -> (Ptr () -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr ()
fptr ((Ptr () -> IO r) -> IO r) -> (Ptr () -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr ()
ptr -> SEXP V ty -> IO r
f (Ptr (HExp V ty) -> SEXP V ty
forall s (a :: SEXPTYPE). Ptr (HExp s a) -> SEXP s a
SEXP (Ptr () -> Ptr (HExp V ty)
forall a b. Ptr a -> Ptr b
castPtr Ptr ()
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
  { Vector ty a -> ForeignSEXP ty
vectorBase   :: {-# UNPACK #-} !(ForeignSEXP ty)
  , Vector ty a -> Int32
vectorOffset :: {-# UNPACK #-} !Int32
  , Vector ty a -> Int32
vectorLength :: {-# UNPACK #-} !Int32
  }

instance (Eq a, SVECTOR ty a) => Eq (Vector ty a) where
  a :: Vector ty a
a == :: Vector ty a -> Vector ty a -> Bool
== b :: Vector ty a
b = Vector ty a -> [a]
forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList Vector ty a
a [a] -> [a] -> Bool
forall a. Eq a => a -> a -> Bool
== Vector ty a -> [a]
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 v :: Vector ty a
v = "fromList " String -> ShowS
forall a. [a] -> [a] -> [a]
Prelude.++ [a] -> ShowS
forall a. Show a => [a] -> ShowS
showList (Vector ty a -> [a]
forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList Vector ty a
v) ""

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

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

#if __GLASGOW_HASKELL__ >= 708
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 = [Item (Vector ty a)] -> Vector ty a
forall (ty :: SEXPTYPE) a. SVECTOR ty a => [a] -> Vector ty a
fromList
  fromListN :: Int -> [Item (Vector ty a)] -> Vector ty a
fromListN = Int -> [Item (Vector ty a)] -> Vector ty a
forall (ty :: SEXPTYPE) a.
SVECTOR ty a =>
Int -> [a] -> Vector ty a
fromListN
  toList :: Vector ty a -> [Item (Vector ty a)]
toList = Vector ty a -> [Item (Vector ty a)]
forall (ty :: SEXPTYPE) a. SVECTOR ty a => Vector ty a -> [a]
toList
#endif

-- | Return Pointer of the first element of the vector storage.
unsafeToPtr :: Storable a => Vector ty a -> Ptr a
{-# INLINE unsafeToPtr #-}
unsafeToPtr :: Vector ty a -> Ptr a
unsafeToPtr (Vector fp :: ForeignSEXP ty
fp off :: Int32
off len :: Int32
len) = IO (Ptr a) -> Ptr a
forall a. IO a -> a
unsafeInlineIO (IO (Ptr a) -> Ptr a) -> IO (Ptr a) -> Ptr a
forall a b. (a -> b) -> a -> b
$ ForeignSEXP ty -> (SEXP V ty -> IO (Ptr a)) -> IO (Ptr a)
forall (ty :: SEXPTYPE) r.
ForeignSEXP ty -> (SEXP V ty -> IO r) -> IO r
withForeignSEXP ForeignSEXP ty
fp ((SEXP V ty -> IO (Ptr a)) -> IO (Ptr a))
-> (SEXP V ty -> IO (Ptr a)) -> IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ \sx :: SEXP V ty
sx ->
    Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr a -> IO (Ptr a)) -> Ptr a -> IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ MVector V ty a -> Ptr a
forall a s (ty :: SEXPTYPE). Storable a => MVector s ty a -> Ptr a
Mutable.unsafeToPtr (MVector V ty a -> Ptr a) -> MVector V ty a -> Ptr a
forall a b. (a -> b) -> a -> b
$ SEXP V ty -> Int32 -> Int32 -> MVector V ty a
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 :: SEXP s ty -> Vector ty a
fromSEXP s :: SEXP s ty
s = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> (forall s. ST s (Vector ty a)) -> Vector ty a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector ty a)) -> Vector ty a)
-> (forall s. ST s (Vector ty a)) -> Vector ty a
forall a b. (a -> b) -> a -> b
$ do
  W t ty s a
w <- New (W t ty) a -> ST s (Mutable (W t ty) s a)
forall (v :: * -> *) a s. New v a -> ST s (Mutable v s a)
run ((W t ty a -> New (W t ty) a)
-> Vector ty a -> Proxy t -> New (W t ty) a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> New (W t ty) a
forall (v :: * -> *) a. Vector v a => v a -> New v a
G.clone (SEXP s ty -> Vector ty a
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 <- Mutable (W t ty) (PrimState (ST s)) a -> ST s (W t ty a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.unsafeFreeze Mutable (W t ty) (PrimState (ST s)) a
W t ty s a
w
  Vector ty a -> ST s (Vector ty a)
forall (m :: * -> *) a. Monad m => a -> m a
return (W t ty a -> Vector ty a
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 :: SEXP s ty -> Vector ty a
unsafeFromSEXP s :: SEXP s ty
s = IO (Vector ty a) -> Vector ty a
forall a. IO a -> a
unsafeInlineIO (IO (Vector ty a) -> Vector ty a)
-> IO (Vector ty a) -> Vector ty a
forall a b. (a -> b) -> a -> b
$ do
  ForeignSEXP ty
sxp <- SEXP s ty -> IO (ForeignSEXP ty)
forall (m :: * -> *) s (ty :: SEXPTYPE).
PrimMonad m =>
SEXP s ty -> m (ForeignSEXP ty)
foreignSEXP SEXP s ty
s
  Int
l <- SEXP s ty -> IO Int
forall (a :: SEXPTYPE) s. IsVector a => SEXP s a -> IO Int
R.length SEXP s ty
s
  Vector ty a -> IO (Vector ty a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector ty a -> IO (Vector ty a))
-> Vector ty a -> IO (Vector ty a)
forall a b. (a -> b) -> a -> b
$ ForeignSEXP ty -> Int32 -> Int32 -> Vector ty a
forall (ty :: SEXPTYPE) a.
ForeignSEXP ty -> Int32 -> Int32 -> Vector ty a
Vector ForeignSEXP ty
sxp 0 (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l)

-- | /O(n)/ Yield a (mutable) copy of the vector as a 'SEXP'.
toSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty
toSEXP :: Vector ty a -> SEXP s ty
toSEXP s :: Vector ty a
s = (forall t. Reifies t (AcquireIO Any) => Proxy t -> SEXP s ty)
-> SEXP s ty
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> SEXP s ty)
 -> SEXP s ty)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> SEXP s ty)
-> SEXP s ty
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> (forall s. ST s (SEXP s ty)) -> SEXP s ty
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SEXP s ty)) -> SEXP s ty)
-> (forall s. ST s (SEXP s ty)) -> SEXP s ty
forall a b. (a -> b) -> a -> b
$ do
  W t ty s a
w <- New (W t ty) a -> ST s (Mutable (W t ty) s a)
forall (v :: * -> *) a s. New v a -> ST s (Mutable v s a)
run ((W t ty a -> New (W t ty) a)
-> Vector ty a -> Proxy t -> New (W t ty) a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> New (W t ty) a
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 <- Mutable (W t ty) (PrimState (ST s)) a -> ST s (W t ty a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.unsafeFreeze Mutable (W t ty) (PrimState (ST s)) a
W t ty s a
w
  SEXP s ty -> ST s (SEXP s ty)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector ty a -> SEXP s ty
forall (ty :: SEXPTYPE) a s.
SVECTOR ty a =>
Vector ty a -> SEXP s ty
unsafeToSEXP (W t ty a -> Vector ty a
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 :: Vector ty a -> SEXP s ty
unsafeToSEXP (Vector (ForeignSEXP fsx :: ForeignPtr ()
fsx) _ _) = IO (SEXP s ty) -> SEXP s ty
forall a. IO a -> a
unsafePerformIO (IO (SEXP s ty) -> SEXP s ty) -> IO (SEXP s ty) -> SEXP s ty
forall a b. (a -> b) -> a -> b
$ -- XXX
  ForeignPtr () -> (Ptr () -> IO (SEXP s ty)) -> IO (SEXP s ty)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr ()
fsx ((Ptr () -> IO (SEXP s ty)) -> IO (SEXP s ty))
-> (Ptr () -> IO (SEXP s ty)) -> IO (SEXP s ty)
forall a b. (a -> b) -> a -> b
$ SEXP s ty -> IO (SEXP s ty)
forall (m :: * -> *) a. Monad m => a -> m a
return (SEXP s ty -> IO (SEXP s ty))
-> (Ptr () -> SEXP s ty) -> Ptr () -> IO (SEXP s ty)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP0 -> SEXP s ty
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
R.sexp (SEXP0 -> SEXP s ty) -> (Ptr () -> SEXP0) -> Ptr () -> SEXP s ty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr () -> SEXP0
forall a b. Ptr a -> Ptr b
castPtr

-- | /O(n)/ Convert a character vector into a 'String'.
toString :: Vector 'Char Word8 -> String
toString :: Vector 'Char Word8 -> String
toString v :: Vector 'Char Word8
v = IO String -> String
forall a. IO a -> a
unsafeInlineIO (IO String -> String) -> IO String -> String
forall a b. (a -> b) -> a -> b
$
  TextEncoding -> CStringLen -> IO String
GHC.peekCStringLen TextEncoding
utf8 ( Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr CChar) -> Ptr Word8 -> Ptr CChar
forall a b. (a -> b) -> a -> b
$ Vector 'Char Word8 -> Ptr Word8
forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr Vector 'Char Word8
v
                          , Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Vector 'Char Word8 -> Int32
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 v :: Vector 'Char Word8
v = IO ByteString -> ByteString
forall a. IO a -> a
unsafeInlineIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$
   CStringLen -> IO ByteString
B.packCStringLen ( Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr CChar) -> Ptr Word8 -> Ptr CChar
forall a b. (a -> b) -> a -> b
$ Vector 'Char Word8 -> Ptr Word8
forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr Vector 'Char Word8
v
                    , Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Vector 'Char Word8 -> Int32
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 :: Vector 'Char Word8 -> (ByteString -> IO a) -> a
unsafeWithByteString v :: Vector 'Char Word8
v f :: ByteString -> IO a
f = IO a -> a
forall a. IO a -> a
unsafeInlineIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
   ByteString
x <- CStringLen -> IO ByteString
B.unsafePackCStringLen (Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr CChar) -> Ptr Word8 -> Ptr CChar
forall a b. (a -> b) -> a -> b
$ Vector 'Char Word8 -> Ptr Word8
forall a (ty :: SEXPTYPE). Storable a => Vector ty a -> Ptr a
unsafeToPtr Vector 'Char Word8
v
                               ,Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Vector 'Char Word8 -> Int32
forall (ty :: SEXPTYPE) a. Vector ty a -> Int32
vectorLength Vector 'Char Word8
v)
   a
w <- a -> a
forall a. NFData a => a -> a
DeepSeq.force (a -> a) -> IO a -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> IO a
f ByteString
x
   a -> IO a
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 :: Vector ty a -> Int
length v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Int) -> Vector ty a -> Proxy t -> Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> Int
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 :: Vector ty a -> Bool
null v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Bool) -> Vector ty a -> Proxy t -> Bool
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> Bool
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 (!) #-}
(!) v :: Vector ty a
v i :: Int
i = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> Int -> a
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 (!?) #-}
!? :: Vector ty a -> Int -> Maybe a
(!?) v :: Vector ty a
v i :: Int
i = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe a)
-> Maybe a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe a)
 -> Maybe a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe a)
-> Maybe a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Maybe a) -> Vector ty a -> Proxy t -> Maybe a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> Int -> Maybe a
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 :: Vector ty a -> a
head v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: Vector ty a -> a
last v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: Vector ty a -> Int -> a
unsafeIndex v :: Vector ty a
v i :: Int
i = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> Int -> a
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 :: Vector ty a -> a
unsafeHead v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: Vector ty a -> a
unsafeLast v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: Vector ty a -> Int -> m a
indexM v :: Vector ty a
v i :: Int
i = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> Int -> m a
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 :: Vector ty a -> m a
headM v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> m a
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 :: Vector ty a -> m a
lastM v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> m a
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 :: Vector ty a -> Int -> m a
unsafeIndexM v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int -> m a)
-> Int -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Int -> m a)
 -> Int -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int -> m a)
-> Int
-> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Int -> m a) -> Vector ty a -> Proxy t -> Int -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> Int -> m a
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 :: Vector ty a -> m a
unsafeHeadM v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> m a
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 :: Vector ty a -> m a
unsafeLastM v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> m a
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 :: Int -> Int -> Vector ty a -> Vector ty a
slice i :: Int
i n :: Int
n v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> Int -> W t ty a -> W t ty a
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 :: Vector ty a -> Vector ty a
init v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> W t ty a
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 :: Vector ty a -> Vector ty a
tail v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> W t ty a
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 :: Int -> Vector ty a -> Vector ty a
take i :: Int
i v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> W t ty a -> W t ty a
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 :: Int -> Vector ty a -> Vector ty a
drop i :: Int
i v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> W t ty a -> W t ty a
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 :: Int -> Vector ty a -> (Vector ty a, Vector ty a)
splitAt i :: Int
i v :: Vector ty a
v = (forall t.
 Reifies t (AcquireIO Any) =>
 Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> (Vector ty a, Vector ty a))
 -> (Vector ty a, Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall a b. (a -> b) -> a -> b
$ (\(a :: W t ty a
a,b :: W t ty a
b) -> (W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) ((W t ty a, W t ty a) -> (Vector ty a, Vector ty a))
-> (Proxy t -> (W t ty a, W t ty a))
-> Proxy t
-> (Vector ty a, Vector ty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> (W t ty a, W t ty a))
-> Vector ty a -> Proxy t -> (W t ty a, W t ty a)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> W t ty a -> (W t ty a, W t ty a)
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 :: Int -> Int -> Vector ty a -> Vector ty a
unsafeSlice i :: Int
i j :: Int
j v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> Int -> W t ty a -> W t ty a
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 :: Vector ty a -> Vector ty a
unsafeInit v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> W t ty a
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 :: Vector ty a -> Vector ty a
unsafeTail v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> W t ty a
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 :: Int -> Vector ty a -> Vector ty a
unsafeTake i :: Int
i v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> W t ty a -> W t ty a
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 :: Int -> Vector ty a -> Vector ty a
unsafeDrop i :: Int
i v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (Int -> W t ty a -> W t ty a
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 :: Vector ty a
empty = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW W t ty a
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 :: a -> Vector ty a
singleton a :: a
a = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (a -> W t ty a
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 :: Int -> a -> Vector ty a
replicate i :: Int
i v :: a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> a -> W t ty a
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 :: Int -> (Int -> a) -> Vector ty a
generate i :: Int
i f :: Int -> a
f = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> (Int -> a) -> W t ty a
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 :: Int -> (a -> a) -> a -> Vector ty a
iterateN i :: Int
i f :: a -> a
f a :: a
a = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> (a -> a) -> a -> W t ty a
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 :: (b -> Maybe (a, b)) -> b -> Vector ty a
unfoldr g :: b -> Maybe (a, b)
g a :: b
a = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW ((b -> Maybe (a, b)) -> b -> W t ty a
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 :: Int -> (b -> Maybe (a, b)) -> b -> Vector ty a
unfoldrN n :: Int
n g :: b -> Maybe (a, b)
g a :: b
a = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> (b -> Maybe (a, b)) -> b -> W t ty a
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 :: Int -> (Vector ty a -> a) -> Vector ty a
constructN n :: Int
n g :: Vector ty a -> a
g = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> (W t ty a -> a) -> W t ty a
forall (v :: * -> *) a. Vector v a => Int -> (v a -> a) -> v a
G.constructN Int
n (Vector ty a -> a
g(Vector ty a -> a) -> (W t ty a -> Vector ty a) -> W t ty a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.W t ty a -> Vector ty a
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 :: Int -> (Vector ty a -> a) -> Vector ty a
constructrN n :: Int
n g :: Vector ty a -> a
g = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> (W t ty a -> a) -> W t ty a
forall (v :: * -> *) a. Vector v a => Int -> (v a -> a) -> v a
G.constructrN Int
n (Vector ty a -> a
g(Vector ty a -> a) -> (W t ty a -> Vector ty a) -> W t ty a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.W t ty a -> Vector ty a
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 :: a -> Int -> Vector ty a
enumFromN a :: a
a i :: Int
i = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (a -> Int -> W t ty a
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 :: a -> a -> Int -> Vector ty a
enumFromStepN f :: a
f t :: a
t s :: Int
s = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (a -> a -> Int -> W t ty a
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 :: a -> a -> Vector ty a
enumFromTo f :: a
f t :: a
t = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (a -> a -> W t ty a
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 :: a -> a -> a -> Vector ty a
enumFromThenTo f :: a
f t :: a
t s :: a
s = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (a -> a -> a -> W t ty a
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 :: a -> Vector ty a -> Vector ty a
cons a :: a
a v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (a -> W t ty a -> W t ty a
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 :: Vector ty a -> a -> Vector ty a
snoc v :: Vector ty a
v a :: a
a = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> a -> W t ty a
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 (++) #-}
v1 :: Vector ty a
v1 ++ :: Vector ty a -> Vector ty a -> Vector ty a
++ v2 :: Vector ty a
v2 = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a -> W t ty a)
-> Vector ty a -> Vector ty a -> Proxy t -> W t ty a
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 ty a -> W t ty a -> W t ty a
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 :: [Vector ty a] -> Vector ty a
concat vs :: [Vector ty a]
vs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a) -> W t ty a -> Vector ty a
forall a b. (a -> b) -> a -> b
$ [W t ty a] -> W t ty a
forall (v :: * -> *) a. Vector v a => [v a] -> v a
G.concat ([W t ty a] -> W t ty a) -> [W t ty a] -> W t ty a
forall a b. (a -> b) -> a -> b
$ (Vector ty a -> W t ty a) -> [Vector ty a] -> [W t ty a]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map (Proxy t -> Vector ty a -> W t ty a
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 :: Int -> m a -> m (Vector ty a)
replicateM n :: Int
n f :: m a
f = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m (Vector ty a))
-> m (Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> m (Vector ty a))
 -> m (Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> m (Vector ty a))
-> m (Vector ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> (\v :: W t ty a
v -> W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW W t ty a
v Proxy t
p) (W t ty a -> Vector ty a) -> m (W t ty a) -> m (Vector ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m a -> m (W t ty a)
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 :: Int -> (Int -> m a) -> m (Vector ty a)
generateM n :: Int
n f :: Int -> m a
f = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m (Vector ty a))
-> m (Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> m (Vector ty a))
 -> m (Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> m (Vector ty a))
-> m (Vector ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> (\v :: W t ty a
v -> W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW W t ty a
v Proxy t
p) (W t ty a -> Vector ty a) -> m (W t ty a) -> m (Vector ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (Int -> m a) -> m (W t ty a)
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 r. ST r (MVector r ty a)) -> Vector ty a
create f :: forall r. ST r (MVector r ty a)
f = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a) -> W t ty a -> Vector ty a
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (Mutable (W t ty) s a)) -> W t ty a
forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
G.create (Proxy t -> MVector s ty a -> W t ty s a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy t
p (MVector s ty a -> W t ty s a)
-> ST s (MVector s ty a) -> ST s (W t ty s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ST s (MVector s ty a)
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 :: Vector ty a -> Vector ty a
force v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> W t ty a
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 (//) #-}
// :: Vector ty a -> [(Int, a)] -> Vector ty a
(//) v :: Vector ty a
v l :: [(Int, a)]
l = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> [(Int, a)] -> W t ty a
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 :: Vector ty a -> [(Int, a)] -> Vector ty a
unsafeUpd v :: Vector ty a
v l :: [(Int, a)]
l = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> [(Int, a)] -> W t ty a
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 :: (a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a
accum f :: a -> b -> a
f v :: Vector ty a
v l :: [(Int, b)]
l = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (\w :: W t ty a
w -> (a -> b -> a) -> W t ty a -> [(Int, b)] -> W t ty a
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 :: (a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a
unsafeAccum f :: a -> b -> a
f v :: Vector ty a
v l :: [(Int, b)]
l = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (\w :: W t ty a
w -> (a -> b -> a) -> W t ty a -> [(Int, b)] -> W t ty a
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 :: Vector ty a -> Vector ty a
reverse v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> W t ty a
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 :: (a -> b) -> Vector ty a -> Vector ty b
map f :: a -> b
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b) -> W t ty a -> W t ty b
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 :: (Int -> a -> b) -> Vector ty a -> Vector ty b
imap f :: Int -> a -> b
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((Int -> a -> b) -> W t ty a -> W t ty b
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 :: (a -> Vector tyb b) -> Vector tya a -> Vector tyb b
concatMap f :: a -> Vector tyb b
f v :: Vector tya a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyb b)
-> Vector tyb b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyb b)
 -> Vector tyb b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyb b)
-> Vector tyb b
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let v' :: Bundle (W t tya) a
v' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tya a
v)
    in W t tyb b -> Proxy t -> Vector tyb b
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyb) b -> W t tyb b
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyb) b -> W t tyb b)
-> Bundle (W t tyb) b -> W t tyb b
forall a b. (a -> b) -> a -> b
$ Stream Id b -> Size -> Bundle (W t tyb) b
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream ((a -> Stream Id b) -> Stream Id a -> Stream Id b
forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
Stream.concatMap (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems (Bundle (W t tyb) b -> Stream Id b)
-> (a -> Bundle (W t tyb) b) -> a -> Stream Id b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (W t tyb b -> Bundle (W t tyb) b)
-> (a -> W t tyb b) -> a -> Bundle (W t tyb) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy t -> Vector tyb b -> W t tyb b
forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p (Vector tyb b -> W t tyb b)
-> (a -> Vector tyb b) -> a -> W t tyb b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Vector tyb b
f) (Bundle (W t tya) a -> Stream Id a
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 :: (a -> m b) -> Vector ty a -> m (Vector ty b)
mapM f :: a -> m b
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m (Vector ty b))
-> m (Vector ty b)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> m (Vector ty b))
 -> m (Vector ty b))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> m (Vector ty b))
-> m (Vector ty b)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b) -> m (W t ty b) -> m (Vector ty b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (W t ty a -> m (W t ty b))
-> Vector ty a -> Proxy t -> m (W t ty b)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> m b) -> W t ty a -> m (W t ty b)
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_ :: (a -> m b) -> Vector ty a -> m ()
mapM_ f :: a -> m b
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m ()) -> Vector ty a -> Proxy t -> m ()
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> m b) -> W t ty a -> m ()
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 :: Vector ty a -> (a -> m b) -> m (Vector ty b)
forM v :: Vector ty a
v f :: a -> m b
f = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m (Vector ty b))
-> m (Vector ty b)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> m (Vector ty b))
 -> m (Vector ty b))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> m (Vector ty b))
-> m (Vector ty b)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b) -> m (W t ty b) -> m (Vector ty b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (W t ty a -> m (W t ty b))
-> Vector ty a -> Proxy t -> m (W t ty b)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> (a -> m b) -> m (W t ty b)
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_ :: Vector ty a -> (a -> m b) -> m ()
forM_ v :: Vector ty a
v f :: a -> m b
f = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m ()) -> Vector ty a -> Proxy t -> m ()
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (W t ty a -> (a -> m b) -> m ()
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 = (Size -> Size -> Size) -> [Size] -> Size
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 :: (a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c
zipWith f :: a -> b -> c
f xs :: Vector tya a
xs ys :: Vector tyb b
ys = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyc c)
-> Vector tyc c
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyc c)
 -> Vector tyc c)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyc c)
-> Vector tyc c
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let xs' :: Bundle (W t tya) a
xs' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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 (Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
xs') (Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
ys')
    in W t tyc c -> Proxy t -> Vector tyc c
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyc) c -> W t tyc c
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyc) c -> W t tyc c)
-> Bundle (W t tyc) c -> W t tyc c
forall a b. (a -> b) -> a -> b
$ Stream Id c -> Size -> Bundle (W t tyc) c
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream ((a -> b -> c) -> Stream Id a -> Stream Id b -> Stream Id c
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 (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
xs') (Bundle (W t tyb) b -> Stream Id b
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 :: (a -> b -> c -> d)
-> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
zipWith3 f :: a -> b -> c -> d
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyd d)
-> Vector tyd d
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyd d)
 -> Vector tyd d)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyd d)
-> Vector tyd d
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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 [Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs']
    in W t tyd d -> Proxy t -> Vector tyd d
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyd) d -> W t tyd d
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyd) d -> W t tyd d)
-> Bundle (W t tyd) d -> W t tyd d
forall a b. (a -> b) -> a -> b
$ Stream Id d -> Size -> Bundle (W t tyd) d
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream ((a -> b -> c -> d)
-> Stream Id a -> Stream Id b -> Stream Id c -> Stream Id d
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 (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
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 :: (a -> b -> c -> d -> e)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
zipWith4 f :: a -> b -> c -> d -> e
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs ds :: Vector tyd d
ds = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tye e)
-> Vector tye e
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tye e)
 -> Vector tye e)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tye e)
-> Vector tye e
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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' = W t tyd d -> Bundle (W t tyd) d
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyd d -> W t tyd d
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 [Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', Bundle (W t tyd) d -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds']
    in W t tye e -> Proxy t -> Vector tye e
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tye) e -> W t tye e
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tye) e -> W t tye e)
-> Bundle (W t tye) e -> W t tye e
forall a b. (a -> b) -> a -> b
$ Stream Id e -> Size -> Bundle (W t tye) e
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream ((a -> b -> c -> d -> e)
-> Stream Id a
-> Stream Id b
-> Stream Id c
-> Stream Id d
-> Stream Id e
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 (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (Bundle (W t tyd) d -> Stream Id d
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 :: (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 f :: a -> b -> c -> d -> e -> f
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs ds :: Vector tyd d
ds es :: Vector tye e
es = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyf f)
-> Vector tyf f
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyf f)
 -> Vector tyf f)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyf f)
-> Vector tyf f
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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' = W t tyd d -> Bundle (W t tyd) d
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyd d -> W t tyd d
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' = W t tye e -> Bundle (W t tye) e
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tye e -> W t tye e
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 [Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', Bundle (W t tyd) d -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', Bundle (W t tye) e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es']
    in W t tyf f -> Proxy t -> Vector tyf f
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyf) f -> W t tyf f
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyf) f -> W t tyf f)
-> Bundle (W t tyf) f -> W t tyf f
forall a b. (a -> b) -> a -> b
$ Stream Id f -> Size -> Bundle (W t tyf) f
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream ((a -> b -> c -> d -> e -> f)
-> Stream Id a
-> Stream Id b
-> Stream Id c
-> Stream Id d
-> Stream Id e
-> Stream Id f
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 (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (Bundle (W t tyd) d -> Stream Id d
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (Bundle (W t tye) e -> Stream Id e
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 :: (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 f :: a -> b -> c -> d -> e -> f -> g
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs ds :: Vector tyd d
ds es :: Vector tye e
es fs :: Vector tyf f
fs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyg g)
-> Vector tyg g
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyg g)
 -> Vector tyg g)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyg g)
-> Vector tyg g
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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' = W t tyd d -> Bundle (W t tyd) d
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyd d -> W t tyd d
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' = W t tye e -> Bundle (W t tye) e
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tye e -> W t tye e
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' = W t tyf f -> Bundle (W t tyf) f
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyf f -> W t tyf f
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 [Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', Bundle (W t tyd) d -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', Bundle (W t tye) e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es', Bundle (W t tyf) f -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyf) f
fs']
    in W t tyg g -> Proxy t -> Vector tyg g
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyg) g -> W t tyg g
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyg) g -> W t tyg g)
-> Bundle (W t tyg) g -> W t tyg g
forall a b. (a -> b) -> a -> b
$ Stream Id g -> Size -> Bundle (W t tyg) g
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream ((a -> b -> c -> d -> e -> f -> g)
-> Stream Id a
-> Stream Id b
-> Stream Id c
-> Stream Id d
-> Stream Id e
-> Stream Id f
-> Stream Id g
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 (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as') (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (Bundle (W t tyd) d -> Stream Id d
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (Bundle (W t tye) e -> Stream Id e
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tye) e
es') (Bundle (W t tyf) f -> Stream Id f
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 :: (Int -> a -> b -> c)
-> Vector tya a -> Vector tyb b -> Vector tyc c
izipWith f :: Int -> a -> b -> c
f as :: Vector tya a
as bs :: Vector tyb b
bs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyc c)
-> Vector tyc c
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyc c)
 -> Vector tyc c)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyc c)
-> Vector tyc c
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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 (Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as') (Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs')
    in W t tyc c -> Proxy t -> Vector tyc c
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyc) c -> W t tyc c
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyc) c -> W t tyc c)
-> Bundle (W t tyc) c -> W t tyc c
forall a b. (a -> b) -> a -> b
$ Stream Id c -> Size -> Bundle (W t tyc) c
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (((Int, a) -> b -> c)
-> Stream Id (Int, a) -> Stream Id b -> Stream Id c
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
Stream.zipWith ((Int -> a -> b -> c) -> (Int, a) -> b -> c
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c
f) (Stream Id a -> Stream Id (Int, a)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (Bundle (W t tyb) b -> Stream Id b
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 :: (Int -> a -> b -> c -> d)
-> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
izipWith3 f :: Int -> a -> b -> c -> d
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyd d)
-> Vector tyd d
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyd d)
 -> Vector tyd d)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyd d)
-> Vector tyd d
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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 [Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs']
    in W t tyd d -> Proxy t -> Vector tyd d
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyd) d -> W t tyd d
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyd) d -> W t tyd d)
-> Bundle (W t tyd) d -> W t tyd d
forall a b. (a -> b) -> a -> b
$ Stream Id d -> Size -> Bundle (W t tyd) d
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (((Int, a) -> b -> c -> d)
-> Stream Id (Int, a) -> Stream Id b -> Stream Id c -> Stream Id d
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 ((Int -> a -> b -> c -> d) -> (Int, a) -> b -> c -> d
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d
f) (Stream Id a -> Stream Id (Int, a)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
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 :: (Int -> a -> b -> c -> d -> e)
-> Vector tya a
-> Vector tyb b
-> Vector tyc c
-> Vector tyd d
-> Vector tye e
izipWith4 f :: Int -> a -> b -> c -> d -> e
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs ds :: Vector tyd d
ds = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tye e)
-> Vector tye e
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tye e)
 -> Vector tye e)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tye e)
-> Vector tye e
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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' = W t tyd d -> Bundle (W t tyd) d
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyd d -> W t tyd d
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 [ Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', Bundle (W t tyd) d -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds']
    in W t tye e -> Proxy t -> Vector tye e
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tye) e -> W t tye e
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tye) e -> W t tye e)
-> Bundle (W t tye) e -> W t tye e
forall a b. (a -> b) -> a -> b
$ Stream Id e -> Size -> Bundle (W t tye) e
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (((Int, a) -> b -> c -> d -> e)
-> Stream Id (Int, a)
-> Stream Id b
-> Stream Id c
-> Stream Id d
-> Stream Id e
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 ((Int -> a -> b -> c -> d -> e) -> (Int, a) -> b -> c -> d -> e
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d -> e
f) (Stream Id a -> Stream Id (Int, a)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (Bundle (W t tyd) d -> Stream Id d
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 :: (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 f :: Int -> a -> b -> c -> d -> e -> f
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs ds :: Vector tyd d
ds es :: Vector tye e
es = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyf f)
-> Vector tyf f
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyf f)
 -> Vector tyf f)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyf f)
-> Vector tyf f
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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' = W t tyd d -> Bundle (W t tyd) d
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyd d -> W t tyd d
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' = W t tye e -> Bundle (W t tye) e
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tye e -> W t tye e
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 [ Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', Bundle (W t tyd) d -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', Bundle (W t tye) e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es']
    in W t tyf f -> Proxy t -> Vector tyf f
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyf) f -> W t tyf f
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyf) f -> W t tyf f)
-> Bundle (W t tyf) f -> W t tyf f
forall a b. (a -> b) -> a -> b
$ Stream Id f -> Size -> Bundle (W t tyf) f
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (((Int, a) -> b -> c -> d -> e -> f)
-> Stream Id (Int, a)
-> Stream Id b
-> Stream Id c
-> Stream Id d
-> Stream Id e
-> Stream Id f
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 ((Int -> a -> b -> c -> d -> e -> f)
-> (Int, a) -> b -> c -> d -> e -> f
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d -> e -> f
f) (Stream Id a -> Stream Id (Int, a)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (Bundle (W t tyd) d -> Stream Id d
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (Bundle (W t tye) e -> Stream Id e
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 :: (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 f :: Int -> a -> b -> c -> d -> e -> f -> g
f as :: Vector tya a
as bs :: Vector tyb b
bs cs :: Vector tyc c
cs ds :: Vector tyd d
ds es :: Vector tye e
es fs :: Vector tyf f
fs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyg g)
-> Vector tyg g
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyg g)
 -> Vector tyg g)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector tyg g)
-> Vector tyg g
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let as' :: Bundle (W t tya) a
as' = W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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' = W t tyc c -> Bundle (W t tyc) c
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyc c -> W t tyc c
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' = W t tyd d -> Bundle (W t tyd) d
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyd d -> W t tyd d
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' = W t tye e -> Bundle (W t tye) e
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tye e -> W t tye e
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' = W t tyf f -> Bundle (W t tyf) f
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyf f -> W t tyf f
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 [ Bundle (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tya) a
as', Bundle (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyb) b
bs', Bundle (W t tyc) c -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyc) c
cs', Bundle (W t tyd) d -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyd) d
ds', Bundle (W t tye) e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tye) e
es', Bundle (W t tyf) f -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle (W t tyf) f
fs']
    in W t tyg g -> Proxy t -> Vector tyg g
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Bundle (W t tyg) g -> W t tyg g
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Bundle (W t tyg) g -> W t tyg g)
-> Bundle (W t tyg) g -> W t tyg g
forall a b. (a -> b) -> a -> b
$ Stream Id g -> Size -> Bundle (W t tyg) g
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
Bundle.fromStream (((Int, a) -> b -> c -> d -> e -> f -> g)
-> Stream Id (Int, a)
-> Stream Id b
-> Stream Id c
-> Stream Id d
-> Stream Id e
-> Stream Id f
-> Stream Id g
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 ((Int -> a -> b -> c -> d -> e -> f -> g)
-> (Int, a) -> b -> c -> d -> e -> f -> g
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> a -> b -> c -> d -> e -> f -> g
f) (Stream Id a -> Stream Id (Int, a)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
Stream.indexed (Bundle (W t tya) a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tya) a
as')) (Bundle (W t tyb) b -> Stream Id b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyb) b
bs') (Bundle (W t tyc) c -> Stream Id c
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyc) c
cs') (Bundle (W t tyd) d -> Stream Id d
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tyd) d
ds') (Bundle (W t tye) e -> Stream Id e
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle (W t tye) e
es') (Bundle (W t tyf) f -> Stream Id f
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 :: (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m (Vector tyc c)
zipWithM f :: a -> b -> m c
f xs :: Vector tya a
xs ys :: Vector tyb b
ys = (forall t.
 Reifies t (AcquireIO Any) =>
 Proxy t -> m (Vector tyc c))
-> m (Vector tyc c)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> m (Vector tyc c))
 -> m (Vector tyc c))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> m (Vector tyc c))
-> m (Vector tyc c)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let xs' :: Bundle m (W t tya) a
xs' = Bundle (W t tya) a -> Bundle m (W t tya) a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
lift (Bundle (W t tya) a -> Bundle m (W t tya) a)
-> Bundle (W t tya) a -> Bundle m (W t tya) a
forall a b. (a -> b) -> a -> b
$ W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = Bundle (W t tyb) b -> Bundle m (W t tyb) b
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
lift (Bundle (W t tyb) b -> Bundle m (W t tyb) b)
-> Bundle (W t tyb) b -> Bundle m (W t tyb) b
forall a b. (a -> b) -> a -> b
$ W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
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 (Bundle m (W t tya) a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m (W t tya) a
xs') (Bundle m (W t tyb) b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m (W t tyb) b
ys')
    in W t tyc c -> Proxy t -> Vector tyc c
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (W t tyc c -> Proxy t -> Vector tyc c)
-> m (W t tyc c) -> m (Proxy t -> Vector tyc c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Bundle (W t tyc) c -> W t tyc c)
-> m (Bundle (W t tyc) c) -> m (W t tyc c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.fmap Bundle (W t tyc) c -> W t tyc c
forall (v :: * -> *) a. Vector v a => Bundle v a -> v a
G.unstream (Size -> [c] -> Bundle (W t tyc) c
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Size -> [a] -> Bundle m v a
Bundle.unsafeFromList Size
sz ([c] -> Bundle (W t tyc) c) -> m [c] -> m (Bundle (W t tyc) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream m c -> m [c]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
Stream.toList ((a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
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 (Bundle m (W t tya) a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tya) a
xs') (Bundle m (W t tyb) b -> Stream m b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tyb) b
ys')))
              m (Proxy t -> Vector tyc c) -> m (Proxy t) -> m (Vector tyc c)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Proxy t -> m (Proxy t)
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_ :: (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m ()
zipWithM_ f :: a -> b -> m c
f xs :: Vector tya a
xs ys :: Vector tyb b
ys = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p ->
    let xs' :: Bundle m (W t tya) a
xs' = Bundle (W t tya) a -> Bundle m (W t tya) a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
lift (Bundle (W t tya) a -> Bundle m (W t tya) a)
-> Bundle (W t tya) a -> Bundle m (W t tya) a
forall a b. (a -> b) -> a -> b
$ W t tya a -> Bundle (W t tya) a
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tya a -> W t tya a
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' = Bundle (W t tyb) b -> Bundle m (W t tyb) b
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
lift (Bundle (W t tyb) b -> Bundle m (W t tyb) b)
-> Bundle (W t tyb) b -> Bundle m (W t tyb) b
forall a b. (a -> b) -> a -> b
$ W t tyb b -> Bundle (W t tyb) b
forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
G.stream (Proxy t -> Vector tyb b -> W t tyb b
forall (proxy :: * -> *) t (ty :: SEXPTYPE) a.
proxy t -> Vector ty a -> W t ty a
withW Proxy t
p Vector tyb b
ys)
    in (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
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 (Bundle m (W t tya) a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m (W t tya) a
xs') (Bundle m (W t tyb) b -> Stream m b
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 :: (a -> Bool) -> Vector ty a -> Vector ty a
filter f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> W t ty a
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 :: (Int -> a -> Bool) -> Vector ty a -> Vector ty a
ifilter f :: Int -> a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((Int -> a -> Bool) -> W t ty a -> W t ty a
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 :: (a -> m Bool) -> Vector ty a -> m (Vector ty a)
filterM f :: a -> m Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m (Vector ty a))
-> m (Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> m (Vector ty a))
 -> m (Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> m (Vector ty a))
-> m (Vector ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a) -> m (W t ty a) -> m (Vector ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (W t ty a -> m (W t ty a))
-> Vector ty a -> Proxy t -> m (W t ty a)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> m Bool) -> W t ty a -> m (W t ty a)
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 :: (a -> Bool) -> Vector ty a -> Vector ty a
takeWhile f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> W t ty a
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 :: (a -> Bool) -> Vector ty a -> Vector ty a
dropWhile f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> W t ty a
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 :: (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
partition f :: a -> Bool
f v :: Vector ty a
v = (forall t.
 Reifies t (AcquireIO Any) =>
 Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> (Vector ty a, Vector ty a))
 -> (Vector ty a, Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall a b. (a -> b) -> a -> b
$ (\(a :: W t ty a
a,b :: W t ty a
b) -> (W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) ((W t ty a, W t ty a) -> (Vector ty a, Vector ty a))
-> (Proxy t -> (W t ty a, W t ty a))
-> Proxy t
-> (Vector ty a, Vector ty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> (W t ty a, W t ty a))
-> Vector ty a -> Proxy t -> (W t ty a, W t ty a)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> (W t ty a, W t ty a)
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 :: (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
unstablePartition f :: a -> Bool
f v :: Vector ty a
v = (forall t.
 Reifies t (AcquireIO Any) =>
 Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> (Vector ty a, Vector ty a))
 -> (Vector ty a, Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall a b. (a -> b) -> a -> b
$ (\(a :: W t ty a
a,b :: W t ty a
b) -> (W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) ((W t ty a, W t ty a) -> (Vector ty a, Vector ty a))
-> (Proxy t -> (W t ty a, W t ty a))
-> Proxy t
-> (Vector ty a, Vector ty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> (W t ty a, W t ty a))
-> Vector ty a -> Proxy t -> (W t ty a, W t ty a)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> (W t ty a, W t ty a)
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 :: (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
span f :: a -> Bool
f v :: Vector ty a
v = (forall t.
 Reifies t (AcquireIO Any) =>
 Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> (Vector ty a, Vector ty a))
 -> (Vector ty a, Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall a b. (a -> b) -> a -> b
$ (\(a :: W t ty a
a,b :: W t ty a
b) -> (W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) ((W t ty a, W t ty a) -> (Vector ty a, Vector ty a))
-> (Proxy t -> (W t ty a, W t ty a))
-> Proxy t
-> (Vector ty a, Vector ty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> (W t ty a, W t ty a))
-> Vector ty a -> Proxy t -> (W t ty a, W t ty a)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> (W t ty a, W t ty a)
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 :: (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
break f :: a -> Bool
f v :: Vector ty a
v = (forall t.
 Reifies t (AcquireIO Any) =>
 Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t.
  Reifies t (AcquireIO Any) =>
  Proxy t -> (Vector ty a, Vector ty a))
 -> (Vector ty a, Vector ty a))
-> (forall t.
    Reifies t (AcquireIO Any) =>
    Proxy t -> (Vector ty a, Vector ty a))
-> (Vector ty a, Vector ty a)
forall a b. (a -> b) -> a -> b
$ (\(a :: W t ty a
a,b :: W t ty a
b) -> (W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
a, W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW W t ty a
b)) ((W t ty a, W t ty a) -> (Vector ty a, Vector ty a))
-> (Proxy t -> (W t ty a, W t ty a))
-> Proxy t
-> (Vector ty a, Vector ty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> (W t ty a, W t ty a))
-> Vector ty a -> Proxy t -> (W t ty a, W t ty a)
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> (W t ty a, W t ty a)
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 :: a -> Vector ty a -> Bool
elem a :: a
a v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Bool) -> Vector ty a -> Proxy t -> Bool
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (a -> W t ty a -> Bool
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 :: a -> Vector ty a -> Bool
notElem a :: a
a v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Bool) -> Vector ty a -> Proxy t -> Bool
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (a -> W t ty a -> Bool
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 :: (a -> Bool) -> Vector ty a -> Maybe a
find f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe a)
-> Maybe a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe a)
 -> Maybe a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe a)
-> Maybe a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Maybe a) -> Vector ty a -> Proxy t -> Maybe a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> Maybe a
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 :: (a -> Bool) -> Vector ty a -> Maybe Int
findIndex f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe Int)
-> Maybe Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe Int)
 -> Maybe Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe Int)
-> Maybe Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Maybe Int) -> Vector ty a -> Proxy t -> Maybe Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Bool) -> W t ty a -> Maybe Int
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 :: a -> Vector ty a -> Maybe Int
elemIndex a :: a
a v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe Int)
-> Maybe Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe Int)
 -> Maybe Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Maybe Int)
-> Maybe Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Maybe Int) -> Vector ty a -> Proxy t -> Maybe Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW (a -> W t ty a -> Maybe Int
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 :: (a -> b -> a) -> a -> Vector ty b -> a
foldl f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty b -> a) -> Vector ty b -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> a
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 :: (a -> a -> a) -> Vector ty a -> a
foldl1 f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> a
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' :: (a -> b -> a) -> a -> Vector ty b -> a
foldl' f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty b -> a) -> Vector ty b -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> a
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' :: (a -> a -> a) -> Vector ty a -> a
foldl1' f :: a -> a -> a
f v :: Vector ty a
v  = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> a
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 :: (a -> b -> b) -> b -> Vector ty a -> b
foldr f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall a b. (a -> b) -> a -> b
$ (W t ty a -> b) -> Vector ty a -> Proxy t -> b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> b
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 :: (a -> a -> a) -> Vector ty a -> a
foldr1 f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> a
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' :: (a -> b -> b) -> b -> Vector ty a -> b
foldr' f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall a b. (a -> b) -> a -> b
$ (W t ty a -> b) -> Vector ty a -> Proxy t -> b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> b
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' :: (a -> a -> a) -> Vector ty a -> a
foldr1' f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> a
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 :: (a -> Int -> b -> a) -> a -> Vector ty b -> a
ifoldl f :: a -> Int -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty b -> a) -> Vector ty b -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Int -> b -> a) -> a -> W t ty b -> a
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' :: (a -> Int -> b -> a) -> a -> Vector ty b -> a
ifoldl' f :: a -> Int -> b -> a
f s :: a
s  v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty b -> a) -> Vector ty b -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> Int -> b -> a) -> a -> W t ty b -> a
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 :: (Int -> a -> b -> b) -> b -> Vector ty a -> b
ifoldr f :: Int -> a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall a b. (a -> b) -> a -> b
$ (W t ty a -> b) -> Vector ty a -> Proxy t -> b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((Int -> a -> b -> b) -> b -> W t ty a -> b
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' :: (Int -> a -> b -> b) -> b -> Vector ty a -> b
ifoldr' f :: Int -> a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> b) -> b
forall a b. (a -> b) -> a -> b
$ (W t ty a -> b) -> Vector ty a -> Proxy t -> b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((Int -> a -> b -> b) -> b -> W t ty a -> b
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 :: (a -> Bool) -> Vector ty a -> Bool
all f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> (a -> Bool) -> W t ty a -> Bool
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Bool
G.all a -> Bool
f (Proxy t -> Vector ty a -> W t ty a
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 :: (a -> Bool) -> Vector ty a -> Bool
any f :: a -> Bool
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ \p :: Proxy t
p -> (a -> Bool) -> W t ty a -> Bool
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Bool
G.any a -> Bool
f (Proxy t -> Vector ty a -> W t ty a
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 :: Vector ty a -> a
sum v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: Vector ty a -> a
product v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: Vector ty a -> a
maximum v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: (a -> a -> Ordering) -> Vector ty a -> a
maximumBy f :: a -> a -> Ordering
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> Ordering) -> W t ty a -> a
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 :: Vector ty a -> a
minimum v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> a
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 :: (a -> a -> Ordering) -> Vector ty a -> a
minimumBy f :: a -> a -> Ordering
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> a) -> a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> a) -> Vector ty a -> Proxy t -> a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> Ordering) -> W t ty a -> a
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 :: Vector ty a -> Int
maxIndex v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Int) -> Vector ty a -> Proxy t -> Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> Int
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 :: (a -> a -> Ordering) -> Vector ty a -> Int
maxIndexBy f :: a -> a -> Ordering
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Int) -> Vector ty a -> Proxy t -> Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> Ordering) -> W t ty a -> Int
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 :: Vector ty a -> Int
minIndex v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Int) -> Vector ty a -> Proxy t -> Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> Int
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 :: (a -> a -> Ordering) -> Vector ty a -> Int
minIndexBy f :: a -> a -> Ordering
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Int) -> Int
forall a b. (a -> b) -> a -> b
$ (W t ty a -> Int) -> Vector ty a -> Proxy t -> Int
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> Ordering) -> W t ty a -> Int
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 :: (a -> b -> m a) -> a -> Vector ty b -> m a
foldM f :: a -> b -> m a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty b -> m a) -> Vector ty b -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> m a) -> a -> W t ty b -> m a
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 :: (a -> a -> m a) -> Vector ty a -> m a
fold1M f :: a -> a -> m a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> m a) -> W t ty a -> m a
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' :: (a -> b -> m a) -> a -> Vector ty b -> m a
foldM' f :: a -> b -> m a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty b -> m a) -> Vector ty b -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> m a) -> a -> W t ty b -> m a
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' :: (a -> a -> m a) -> Vector ty a -> m a
fold1M' f :: a -> a -> m a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m a) -> Vector ty a -> Proxy t -> m a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> m a) -> W t ty a -> m a
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_ :: (a -> b -> m a) -> a -> Vector ty b -> m ()
foldM_ f :: a -> b -> m a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ (W t ty b -> m ()) -> Vector ty b -> Proxy t -> m ()
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> m a) -> a -> W t ty b -> m ()
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_ :: (a -> a -> m a) -> Vector ty a -> m ()
fold1M_ f :: a -> a -> m a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m ()) -> Vector ty a -> Proxy t -> m ()
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> m a) -> W t ty a -> m ()
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'_ :: (a -> b -> m a) -> a -> Vector ty b -> m ()
foldM'_ f :: a -> b -> m a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ (W t ty b -> m ()) -> Vector ty b -> Proxy t -> m ()
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> m a) -> a -> W t ty b -> m ()
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'_ :: (a -> a -> m a) -> Vector ty a -> m ()
fold1M'_ f :: a -> a -> m a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ())
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ (W t ty a -> m ()) -> Vector ty a -> Proxy t -> m ()
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> m a) -> W t ty a -> m ()
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 :: (a -> b -> a) -> a -> Vector ty b -> Vector ty a
prescanl f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty b -> W t ty a) -> Vector ty b -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> W t ty a
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' :: (a -> b -> a) -> a -> Vector ty b -> Vector ty a
prescanl' f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty b -> W t ty a) -> Vector ty b -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> W t ty a
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 :: (a -> b -> a) -> a -> Vector ty b -> Vector ty a
postscanl f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty b -> W t ty a) -> Vector ty b -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> W t ty a
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' :: (a -> b -> a) -> a -> Vector ty b -> Vector ty a
postscanl' f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty b -> W t ty a) -> Vector ty b -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> W t ty a
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 :: (a -> b -> a) -> a -> Vector ty b -> Vector ty a
scanl f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty b -> W t ty a) -> Vector ty b -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> W t ty a
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' :: (a -> b -> a) -> a -> Vector ty b -> Vector ty a
scanl' f :: a -> b -> a
f s :: a
s v :: Vector ty b
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty b -> W t ty a) -> Vector ty b -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> a) -> a -> W t ty b -> W t ty a
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 :: (a -> a -> a) -> Vector ty a -> Vector ty a
scanl1 f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> W t ty a
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' :: (a -> a -> a) -> Vector ty a -> Vector ty a
scanl1' f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> W t ty a
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 :: (a -> b -> b) -> b -> Vector ty a -> Vector ty b
prescanr f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> W t ty b
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' :: (a -> b -> b) -> b -> Vector ty a -> Vector ty b
prescanr' f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> W t ty b
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 :: (a -> b -> b) -> b -> Vector ty a -> Vector ty b
postscanr f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> W t ty b
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' :: (a -> b -> b) -> b -> Vector ty a -> Vector ty b
postscanr' f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> W t ty b
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 :: (a -> b -> b) -> b -> Vector ty a -> Vector ty b
scanr f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> W t ty b
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' :: (a -> b -> b) -> b -> Vector ty a -> Vector ty b
scanr' f :: a -> b -> b
f s :: b
s v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
 -> Vector ty b)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty b)
-> Vector ty b
forall a b. (a -> b) -> a -> b
$ W t ty b -> Vector ty b
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty b -> Vector ty b)
-> (Proxy t -> W t ty b) -> Proxy t -> Vector ty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty b) -> Vector ty a -> Proxy t -> W t ty b
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> b -> b) -> b -> W t ty a -> W t ty b
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 :: (a -> a -> a) -> Vector ty a -> Vector ty a
scanr1 f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> W t ty a
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' :: (a -> a -> a) -> Vector ty a -> Vector ty a
scanr1' f :: a -> a -> a
f v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W t ty a -> Vector ty a)
-> (Proxy t -> W t ty a) -> Proxy t -> Vector ty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (W t ty a -> W t ty a) -> Vector ty a -> Proxy t -> W t ty a
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW ((a -> a -> a) -> W t ty a -> W t ty a
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 :: Vector ty a -> [a]
toList v :: Vector ty a
v = (forall t. Reifies t (AcquireIO Any) => Proxy t -> [a]) -> [a]
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> [a]) -> [a])
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> [a]) -> [a]
forall a b. (a -> b) -> a -> b
$ (W t ty a -> [a]) -> Vector ty a -> Proxy t -> [a]
forall t (ty :: SEXPTYPE) a r (p :: * -> *).
(W t ty a -> r) -> Vector ty a -> p t -> r
proxyFW W t ty a -> [a]
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 :: [a] -> Vector ty a
fromList xs :: [a]
xs = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> [a] -> W t ty a
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
G.fromListN ([a] -> Int
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 :: Int -> [a] -> Vector ty a
fromListN i :: Int
i l :: [a]
l = (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall s r.
(forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony ((forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
 -> Vector ty a)
-> (forall t. Reifies t (AcquireIO Any) => Proxy t -> Vector ty a)
-> Vector ty a
forall a b. (a -> b) -> a -> b
$ W t ty a -> Proxy t -> Vector ty a
forall t (ty :: SEXPTYPE) a (p :: * -> *).
W t ty a -> p t -> Vector ty a
proxyW (Int -> [a] -> W t ty a
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 :: MVector (Region m) ty a -> m (Vector ty a)
unsafeFreeze m :: MVector (Region m) ty a
m = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (Vector ty a))
-> m (Vector ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (Vector ty a))
 -> m (Vector ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (Vector ty a))
-> m (Vector ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W s ty a -> Vector ty a) -> m (W s ty a) -> m (Vector ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable (W s ty) (Region m) a -> m (W s ty a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.unsafeFreeze (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
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 :: Vector ty a -> m (MVector (Region m) ty a)
unsafeThaw v :: Vector ty a
v = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> MVector (Region m) ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
Mutable.unW (W s ty (Region m) a -> MVector (Region m) ty a)
-> m (W s ty (Region m) a) -> m (MVector (Region m) ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> W s ty a -> m (Mutable (W s ty) (Region m) a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
G.unsafeThaw (Proxy s -> Vector ty a -> W s ty a
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 :: Vector ty a -> m (MVector (Region m) ty a)
thaw v1 :: Vector ty a
v1 = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (MVector (Region m) ty a))
 -> m (MVector (Region m) ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (MVector (Region m) ty a))
-> m (MVector (Region m) ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty (Region m) a -> MVector (Region m) ty a
forall t (ty :: SEXPTYPE) s a. W t ty s a -> MVector s ty a
Mutable.unW (W s ty (Region m) a -> MVector (Region m) ty a)
-> m (W s ty (Region m) a) -> m (MVector (Region m) ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> W s ty a -> m (Mutable (W s ty) (Region m) a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
G.thaw (Proxy s -> Vector ty a -> W s ty a
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 :: MVector (Region m) ty a -> m (Vector ty a)
freeze m1 :: MVector (Region m) ty a
m1 = (forall s.
 Reifies s (AcquireIO (Region m)) =>
 Proxy s -> m (Vector ty a))
-> m (Vector ty a)
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s.
  Reifies s (AcquireIO (Region m)) =>
  Proxy s -> m (Vector ty a))
 -> m (Vector ty a))
-> (forall s.
    Reifies s (AcquireIO (Region m)) =>
    Proxy s -> m (Vector ty a))
-> m (Vector ty a)
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> W s ty a -> Vector ty a
forall t (ty :: SEXPTYPE) a. W t ty a -> Vector ty a
unW (W s ty a -> Vector ty a) -> m (W s ty a) -> m (Vector ty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable (W s ty) (Region m) a -> m (W s ty a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
G.freeze (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
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 :: MVector (Region m) ty a -> Vector ty a -> m ()
unsafeCopy m1 :: MVector (Region m) ty a
m1 v2 :: Vector ty a
v2 = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> Mutable (W s ty) (Region m) a -> W s ty a -> m ()
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
G.unsafeCopy (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy s
p MVector (Region m) ty a
m1) (Proxy s -> Vector ty a -> W s ty a
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 :: MVector (Region m) ty a -> Vector ty a -> m ()
copy m1 :: MVector (Region m) ty a
m1 v2 :: Vector ty a
v2 = (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall (m :: * -> *) r.
MonadR m =>
(forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire ((forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
 -> m ())
-> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p -> Mutable (W s ty) (Region m) a -> W s ty a -> m ()
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
G.copy (Proxy s -> MVector (Region m) ty a -> W s ty (Region m) a
forall (proxy :: * -> *) t s (ty :: SEXPTYPE) a.
proxy t -> MVector s ty a -> W t ty s a
Mutable.withW Proxy s
p MVector (Region m) ty a
m1) (Proxy s -> Vector ty a -> W s ty a
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 t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
phony f :: forall t. Reifies t (AcquireIO s) => Proxy t -> r
f = AcquireIO s
-> (forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
forall a r. a -> (forall s. Reifies s a => Proxy s -> r) -> r
reify ((forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
forall s.
(forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty))
-> AcquireIO s
AcquireIO forall (ty :: SEXPTYPE). SEXP V ty -> IO (SEXP s ty)
forall (ty :: SEXPTYPE) g. SEXP V ty -> IO (SEXP g ty)
acquireIO) ((forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r)
-> (forall t. Reifies t (AcquireIO s) => Proxy t -> r) -> r
forall a b. (a -> b) -> a -> b
$ \p :: Proxy s
p ->  Proxy s -> r
forall t. Reifies t (AcquireIO s) => Proxy t -> r
f Proxy s
p
  where
    acquireIO :: SEXP V ty -> IO (SEXP g ty)
    acquireIO :: SEXP V ty -> IO (SEXP g ty)
acquireIO x :: SEXP V ty
x = IO (SEXP g ty) -> IO (SEXP g ty)
forall a. IO a -> IO a
mask_ (IO (SEXP g ty) -> IO (SEXP g ty))
-> IO (SEXP g ty) -> IO (SEXP g ty)
forall a b. (a -> b) -> a -> b
$ do
      SEXP V ty -> IO ()
forall s (a :: SEXPTYPE). SEXP s a -> IO ()
R.preserveObject SEXP V ty
x
      SEXP g ty -> IO (SEXP g ty)
forall (m :: * -> *) a. Monad m => a -> m a
return (SEXP g ty -> IO (SEXP g ty)) -> SEXP g ty -> IO (SEXP g ty)
forall a b. (a -> b) -> a -> b
$ SEXP V ty -> SEXP g ty
forall s (a :: SEXPTYPE) r. SEXP s a -> SEXP r a
R.unsafeRelease SEXP V ty
x