{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-duplicate-exports #-}
module Data.Massiv.Vector
( Vector
, MVector
, slength
, maxSize
, size
, snull
, (!?)
, (!)
, index
, index'
, head'
, shead'
, last'
, indexM
, headM
, sheadM
, lastM
, unconsM
, unsnocM
, slice
, slice'
, sliceM
, sslice
, sliceAt
, sliceAt'
, sliceAtM
, init
, init'
, initM
, tail
, tail'
, tailM
, take
, take'
, takeM
, stake
, drop
, drop'
, dropM
, sdrop
, empty
, sempty
, singleton
, ssingleton
, cons
, snoc
, A.replicate
, sreplicate
, generate
, sgenerate
, siterate
, siterateN
, sreplicateM
, sgenerateM
, siterateNM
, sunfoldr
, sunfoldrM
, sunfoldrN
, sunfoldrNM
, sunfoldrExactN
, sunfoldrExactNM
, (...)
, (..:)
, enumFromN
, senumFromN
, enumFromStepN
, senumFromStepN
, sappend
, sconcat
, smap
, simap
, straverse
, sitraverse
, smapM
, smapM_
, simapM
, simapM_
, sforM
, sforM_
, siforM
, siforM_
, szip
, szip3
, szip4
, szip5
, szip6
, szipWith
, szipWith3
, szipWith4
, szipWith5
, szipWith6
, sizipWith
, sizipWith3
, sizipWith4
, sizipWith5
, sizipWith6
, szipWithM
, szipWith3M
, szipWith4M
, szipWith5M
, szipWith6M
, sizipWithM
, sizipWith3M
, sizipWith4M
, sizipWith5M
, sizipWith6M
, szipWithM_
, szipWith3M_
, szipWith4M_
, szipWith5M_
, szipWith6M_
, sizipWithM_
, sizipWith3M_
, sizipWith4M_
, sizipWith5M_
, sizipWith6M_
, sfilter
, sifilter
, sfilterM
, sifilterM
, smapMaybe
, smapMaybeM
, scatMaybes
, simapMaybe
, simapMaybeM
, sfoldl
, sfoldlM
, sfoldlM_
, sifoldl
, sifoldlM
, sifoldlM_
, sfoldl1'
, sfoldl1M
, sfoldl1M_
, sor
, sand
, sall
, sany
, ssum
, sproduct
, smaximum'
, smaximumM
, sminimum'
, sminimumM
, stoList
, fromList
, sfromList
, sfromListN
, compute
, computeS
, computeIO
, computePrimM
, computeAs
, computeProxy
, computeSource
, computeWithStride
, computeWithStrideAs
, clone
, convert
, convertAs
, convertProxy
, takeS
, dropS
, unfoldr
, unfoldrN
, filterS
, ifilterS
, filterM
, ifilterM
, mapMaybeS
, imapMaybeS
, mapMaybeM
, imapMaybeM
, catMaybesS
, traverseS
, module Data.Massiv.Core
, module Data.Massiv.Array.Delayed
, module Data.Massiv.Array.Manifest
, module Data.Massiv.Array.Mutable
) where
import Control.Monad hiding (filterM, replicateM)
import Data.Coerce
import Data.Massiv.Array.Delayed
import Data.Massiv.Array.Delayed.Pull
import Data.Massiv.Array.Delayed.Push
import Data.Massiv.Array.Delayed.Stream
import Data.Massiv.Array.Manifest
import Data.Massiv.Array.Manifest.Internal
import Data.Massiv.Array.Manifest.List (fromList)
import Data.Massiv.Array.Mutable
import Data.Massiv.Array.Ops.Construct
import qualified Data.Massiv.Array.Ops.Construct as A (makeArrayR, replicate)
import Data.Massiv.Core
import Data.Massiv.Core.Common
import qualified Data.Massiv.Vector.Stream as S
import Data.Massiv.Vector.Unsafe
import Prelude hiding (drop, init, length, null, replicate, splitAt, tail, take)
slength :: Stream r ix e => Array r ix e -> Maybe Sz1
slength v =
case stepsSize (toStream v) of
Exact sz -> Just (SafeSz sz)
_ -> Nothing
{-# INLINE slength #-}
snull :: Load r ix e => Array r ix e -> Bool
snull = isEmpty
{-# INLINE snull #-}
head' :: Source r Ix1 e => Vector r e -> e
head' = either throw id . headM
{-# INLINE head' #-}
headM :: (Source r Ix1 e, MonadThrow m) => Vector r e -> m e
headM v
| isEmpty v = throwM $ SizeEmptyException (size v)
| otherwise = pure $ unsafeLinearIndex v 0
{-# INLINE headM #-}
shead' :: Stream r Ix1 e => Vector r e -> e
shead' = either throw id . sheadM
{-# INLINE shead' #-}
sheadM :: (Stream r Ix1 e, MonadThrow m) => Vector r e -> m e
sheadM v =
case S.unId (S.headMaybe (toStream v)) of
Nothing -> throwM $ SizeEmptyException (size v)
Just e -> pure e
{-# INLINE sheadM #-}
unconsM :: (MonadThrow m, Source r Ix1 e) => Vector r e -> m (e, Vector D e)
unconsM arr
| 0 == totalElem sz = throwM $ SizeEmptyException sz
| otherwise =
pure
( unsafeLinearIndex arr 0
, makeArray (getComp arr) (SafeSz (unSz sz - 1)) (\ !i -> unsafeLinearIndex arr (i + 1)))
where
!sz = size arr
{-# INLINE unconsM #-}
unsnocM :: (MonadThrow m, Source r Ix1 e) => Vector r e -> m (Vector D e, e)
unsnocM arr
| k < 0 = throwM $ SizeEmptyException sz
| otherwise =
pure (makeArray (getComp arr) (SafeSz k) (unsafeLinearIndex arr), unsafeLinearIndex arr k)
where
!sz = size arr
!k = unSz sz - 1
{-# INLINE unsnocM #-}
last' :: Source r Ix1 e => Vector r e -> e
last' = either throw id . lastM
{-# INLINE last' #-}
lastM :: (Source r Ix1 e, MonadThrow m) => Vector r e -> m e
lastM v
| k == 0 = throwM $ SizeEmptyException (size v)
| otherwise = pure $ unsafeLinearIndex v (k - 1)
where k = unSz (size v)
{-# INLINE lastM #-}
slice :: Source r Ix1 e => Ix1 -> Sz1 -> Vector r e -> Vector r e
slice !i (Sz k) v = unsafeLinearSlice i' newSz v
where
!i' = min n (max 0 i)
!newSz = SafeSz (min (n - i') k)
Sz n = size v
{-# INLINE slice #-}
slice' :: Source r Ix1 e => Ix1 -> Sz1 -> Vector r e -> Vector r e
slice' i k = either throw id . sliceM i k
{-# INLINE slice' #-}
sliceM :: (Source r Ix1 e, MonadThrow m) => Ix1 -> Sz1 -> Vector r e -> m (Vector r e)
sliceM i newSz@(Sz k) v
| i >= 0 && k <= n - i = pure $ unsafeLinearSlice i newSz v
| otherwise = throwM $ SizeSubregionException sz i newSz
where
sz@(Sz n) = size v
{-# INLINE sliceM #-}
sslice :: Stream r Ix1 e => Ix1 -> Sz1 -> Vector r e -> Vector DS e
sslice !i (Sz k) = fromSteps . S.slice i k . S.toStream
{-# INLINE sslice #-}
init :: Source r Ix1 e => Vector r e -> Vector r e
init v = unsafeLinearSlice 0 (Sz (coerce (size v) - 1)) v
{-# INLINE init #-}
init' :: Source r Ix1 e => Vector r e -> Vector r e
init' = either throw id . initM
{-# INLINE init' #-}
initM :: (Source r Ix1 e, MonadThrow m) => Vector r e -> m (Vector r e)
initM v = do
when (isEmpty v) $ throwM $ SizeEmptyException $ size v
pure $ unsafeInit v
{-# INLINE initM #-}
tail :: Source r Ix1 e => Vector r e -> Vector r e
tail = drop 1
{-# INLINE tail #-}
tail' :: Source r Ix1 e => Vector r e -> Vector r e
tail' = either throw id . tailM
{-# INLINE tail' #-}
tailM :: (Source r Ix1 e, MonadThrow m) => Vector r e -> m (Vector r e)
tailM v = do
when (isEmpty v) $ throwM $ SizeEmptyException $ size v
pure $ unsafeTail v
{-# INLINE tailM #-}
take :: Source r Ix1 e => Sz1 -> Vector r e -> Vector r e
take k = fst . sliceAt k
{-# INLINE take #-}
take' :: Source r Ix1 e => Sz1 -> Vector r e -> Vector r e
take' k = either throw id . takeM k
{-# INLINE take' #-}
takeM :: (Source r Ix1 e, MonadThrow m) => Sz1 -> Vector r e -> m (Vector r e)
takeM k v = do
let sz = size v
when (k > sz) $ throwM $ SizeSubregionException sz 0 k
pure $ unsafeTake k v
{-# INLINE takeM #-}
stake :: Stream r Ix1 e => Sz1 -> Vector r e -> Vector DS e
stake n = fromSteps . S.take (unSz n) . S.toStream
{-# INLINE stake #-}
drop :: Source r Ix1 e => Sz1 -> Vector r e -> Vector r e
drop k = snd . sliceAt k
{-# INLINE drop #-}
sdrop :: Stream r Ix1 e => Sz1 -> Vector r e -> Vector DS e
sdrop n = fromSteps . S.drop (unSz n) . S.toStream
{-# INLINE sdrop #-}
drop' :: Source r Ix1 e => Sz1 -> Vector r e -> Vector r e
drop' k = either throw id . dropM k
{-# INLINE drop' #-}
dropM :: (Source r Ix1 e, MonadThrow m) => Sz1 -> Vector r e -> m (Vector r e)
dropM k@(Sz d) v = do
let sz@(Sz n) = size v
when (k > sz) $ throwM $ SizeSubregionException sz d (sz - k)
pure $ unsafeLinearSlice d (SafeSz (n - d)) v
{-# INLINE dropM #-}
sliceAt :: Source r Ix1 e => Sz1 -> Vector r e -> (Vector r e, Vector r e)
sliceAt (Sz k) v = (unsafeTake d v, unsafeDrop d v)
where
!n = coerce (size v)
!d = SafeSz (min k n)
{-# INLINE sliceAt #-}
sliceAt' :: Source r Ix1 e => Sz1 -> Vector r e -> (Vector r e, Vector r e)
sliceAt' k = either throw id . sliceAtM k
{-# INLINE sliceAt' #-}
sliceAtM :: (Source r Ix1 e, MonadThrow m) => Sz1 -> Vector r e -> m (Vector r e, Vector r e)
sliceAtM k v = do
l <- takeM k v
pure (l, unsafeDrop k v)
{-# INLINE sliceAtM #-}
sempty :: Vector DS e
sempty = DSArray S.empty
{-# INLINE sempty #-}
ssingleton :: e -> Vector DS e
ssingleton = DSArray . S.singleton
{-# INLINE ssingleton #-}
cons :: e -> Vector DL e -> Vector DL e
cons e arr =
arr
{ dlSize = SafeSz (1 + unSz (dlSize arr))
, dlLoad =
\scheduler startAt uWrite ->
uWrite startAt e >> dlLoad arr scheduler (startAt + 1) uWrite
}
{-# INLINE cons #-}
snoc :: Vector DL e -> e -> Vector DL e
snoc arr e =
arr
{ dlSize = SafeSz (1 + k)
, dlLoad =
\scheduler startAt uWrite -> dlLoad arr scheduler startAt uWrite >> uWrite (k + startAt) e
}
where
!k = unSz (size arr)
{-# INLINE snoc #-}
sreplicate :: Sz1 -> e -> Vector DS e
sreplicate (Sz n) = DSArray . S.replicate n
{-# INLINE sreplicate #-}
generate :: Comp -> Sz1 -> (Ix1 -> e) -> Vector D e
generate = makeArrayLinear
{-# INLINE generate #-}
sgenerate :: Sz1 -> (Ix1 -> e) -> Vector DS e
sgenerate (Sz n) = DSArray . S.generate n
{-# INLINE sgenerate #-}
siterate :: (e -> e) -> e -> Vector DS e
siterate f = fromSteps . S.unfoldr (\a -> Just (a, f a))
{-# INLINE siterate #-}
siterateN :: Sz1 -> (e -> e) -> e -> Vector DS e
siterateN n f a = fromSteps $ S.iterateN (unSz n) f a
{-# INLINE siterateN #-}
sreplicateM :: Monad m => Sz1 -> m e -> m (Vector DS e)
sreplicateM n f = fromStepsM $ S.replicateM (unSz n) f
{-# INLINE sreplicateM #-}
sgenerateM :: Monad m => Sz1 -> (Ix1 -> m e) -> m (Vector DS e)
sgenerateM n f = fromStepsM $ S.generateM (unSz n) f
{-# INLINE sgenerateM #-}
siterateNM :: Monad m => Sz1 -> (e -> m e) -> e -> m (Vector DS e)
siterateNM n f a = fromStepsM $ S.iterateNM (unSz n) f a
{-# INLINE siterateNM #-}
sunfoldr :: (s -> Maybe (e, s)) -> s -> Vector DS e
sunfoldr f = DSArray . S.unfoldr f
{-# INLINE sunfoldr #-}
sunfoldrN ::
Sz1
-> (s -> Maybe (e, s))
-> s
-> Vector DS e
sunfoldrN (Sz n) f = DSArray . S.unfoldrN n f
{-# INLINE sunfoldrN #-}
sunfoldrM :: Monad m => (s -> m (Maybe (e, s))) -> s -> m (Vector DS e)
sunfoldrM f = fromStepsM . S.unfoldrM f
{-# INLINE sunfoldrM #-}
sunfoldrNM :: Monad m => Sz1 -> (s -> m (Maybe (e, s))) -> s -> m (Vector DS e)
sunfoldrNM (Sz n) f = fromStepsM . S.unfoldrNM n f
{-# INLINE sunfoldrNM #-}
sunfoldrExactN :: Sz1 -> (s -> (e, s)) -> s -> Vector DS e
sunfoldrExactN (Sz n) f = fromSteps . S.unfoldrExactN n f
{-# INLINE sunfoldrExactN #-}
sunfoldrExactNM :: Monad m => Sz1 -> (s -> m (e, s)) -> s -> m (Vector DS e)
sunfoldrExactNM (Sz n) f = fromStepsM . S.unfoldrExactNM n f
{-# INLINE sunfoldrExactNM #-}
senumFromN ::
Num e
=> e
-> Sz1
-> Vector DS e
senumFromN x (Sz n) = DSArray $ S.enumFromStepN x 1 n
{-# INLINE senumFromN #-}
senumFromStepN ::
Num e
=> e
-> e
-> Sz1
-> Vector DS e
senumFromStepN x step (Sz n) = DSArray $ S.enumFromStepN x step n
{-# INLINE senumFromStepN #-}
sappend :: (Stream r1 Ix1 e, Stream r2 Ix1 e) => Vector r1 e -> Vector r2 e -> Vector DS e
sappend a1 a2 = fromSteps (toStream a1 `S.append` toStream a2)
{-# INLINE sappend #-}
sconcat :: Stream r Ix1 e => [Vector r e] -> Vector DS e
sconcat = DSArray . foldMap toStream
{-# INLINE sconcat #-}
sfromList :: [e] -> Vector DS e
sfromList = fromSteps . S.fromList
{-# INLINE sfromList #-}
sfromListN :: Sz1 -> [e] -> Vector DS e
sfromListN (Sz n) = fromSteps . S.fromListN n
{-# INLINE sfromListN #-}
stoList :: Stream r ix e => Array r ix e -> [e]
stoList = S.toList . toStream
{-# INLINE stoList #-}
sfilter :: S.Stream r ix e => (e -> Bool) -> Array r ix e -> Vector DS e
sfilter f = DSArray . S.filter f . S.toStream
{-# INLINE sfilter #-}
sifilter :: Stream r ix a => (ix -> a -> Bool) -> Array r ix a -> Vector DS a
sifilter f =
simapMaybe $ \ix e ->
if f ix e
then Just e
else Nothing
{-# INLINE sifilter #-}
sfilterM :: (S.Stream r ix e, Applicative f) => (e -> f Bool) -> Array r ix e -> f (Vector DS e)
sfilterM f arr = DSArray <$> S.filterA f (S.toStream arr)
{-# INLINE sfilterM #-}
sifilterM ::
(Stream r ix a, Applicative f) => (ix -> a -> f Bool) -> Array r ix a -> f (Vector DS a)
sifilterM f =
simapMaybeM $ \ix e ->
(\p ->
if p
then Just e
else Nothing) <$>
f ix e
{-# INLINE sifilterM #-}
smapMaybe :: S.Stream r ix a => (a -> Maybe b) -> Array r ix a -> Vector DS b
smapMaybe f = DSArray . S.mapMaybe f . S.toStream
{-# INLINE smapMaybe #-}
simapMaybe :: Stream r ix a => (ix -> a -> Maybe b) -> Array r ix a -> Vector DS b
simapMaybe f = DSArray . S.mapMaybe (uncurry f) . toStreamIx
{-# INLINE simapMaybe #-}
simapMaybeM ::
(Stream r ix a, Applicative f) => (ix -> a -> f (Maybe b)) -> Array r ix a -> f (Vector DS b)
simapMaybeM f = fmap DSArray . S.mapMaybeA (uncurry f) . toStreamIx
{-# INLINE simapMaybeM #-}
scatMaybes :: S.Stream r ix (Maybe a) => Array r ix (Maybe a) -> Vector DS a
scatMaybes = smapMaybe id
{-# INLINE scatMaybes #-}
smapMaybeM ::
(S.Stream r ix a, Applicative f) => (a -> f (Maybe b)) -> Array r ix a -> f (Vector DS b)
smapMaybeM f = fmap DSArray . S.mapMaybeA f . S.toStream
{-# INLINE smapMaybeM #-}
smap :: S.Stream r ix a => (a -> b) -> Array r ix a -> Vector DS b
smap f = fromSteps . S.map f . S.toStream
{-# INLINE smap #-}
simap :: S.Stream r ix a => (ix -> a -> b) -> Array r ix a -> Vector DS b
simap f = fromSteps . S.map (uncurry f) . S.toStreamIx
{-# INLINE simap #-}
straverse :: (S.Stream r ix a, Applicative f) => (a -> f b) -> Array r ix a -> f (Vector DS b)
straverse f = fmap fromSteps . S.traverse f . S.toStream
{-# INLINE straverse #-}
sitraverse :: (S.Stream r ix a, Applicative f) => (ix -> a -> f b) -> Array r ix a -> f (Vector DS b)
sitraverse f = fmap fromSteps . S.traverse (uncurry f) . S.toStreamIx
{-# INLINE sitraverse #-}
smapM :: (S.Stream r ix a, Monad m) => (a -> m b) -> Array r ix a -> m (Vector DS b)
smapM f = fromStepsM . S.mapM f . S.transStepsId . S.toStream
{-# INLINE smapM #-}
simapM :: (S.Stream r ix a, Monad m) => (ix -> a -> m b) -> Array r ix a -> m (Vector DS b)
simapM f = fromStepsM . S.mapM (uncurry f) . S.transStepsId . S.toStreamIx
{-# INLINE simapM #-}
smapM_ :: (S.Stream r ix a, Monad m) => (a -> m b) -> Array r ix a -> m ()
smapM_ f = S.mapM_ f . S.transStepsId . S.toStream
{-# INLINE smapM_ #-}
simapM_ :: (S.Stream r ix a, Monad m) => (ix -> a -> m b) -> Array r ix a -> m ()
simapM_ f = S.mapM_ (uncurry f) . S.transStepsId . S.toStreamIx
{-# INLINE simapM_ #-}
sforM :: (S.Stream r ix a, Monad m) => Array r ix a -> (a -> m b) -> m (Vector DS b)
sforM = flip smapM
{-# INLINE sforM #-}
siforM :: (S.Stream r ix a, Monad m) => Array r ix a -> (ix -> a -> m b) -> m (Vector DS b)
siforM = flip simapM
{-# INLINE siforM #-}
sforM_ :: (S.Stream r ix a, Monad m) => Array r ix a -> (a -> m b) -> m ()
sforM_ = flip smapM_
{-# INLINE sforM_ #-}
siforM_ :: (S.Stream r ix a, Monad m) => Array r ix a -> (ix -> a -> m b) -> m ()
siforM_ = flip simapM_
{-# INLINE siforM_ #-}
szip ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b) => Vector ra a -> Vector rb b -> Vector DS (a, b)
szip = szipWith (,)
{-# INLINE szip #-}
szip3 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c)
=> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector DS (a, b, c)
szip3 = szipWith3 (,,)
{-# INLINE szip3 #-}
szip4 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d)
=> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector DS (a, b, c, d)
szip4 = szipWith4 (,,,)
{-# INLINE szip4 #-}
szip5 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, S.Stream re Ix1 e)
=> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector DS (a, b, c, d, e)
szip5 = szipWith5 (,,,,)
{-# INLINE szip5 #-}
szip6 ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
)
=> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> Vector DS (a, b, c, d, e, f)
szip6 = szipWith6 (,,,,,)
{-# INLINE szip6 #-}
szipWith ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b)
=> (a -> b -> c)
-> Vector ra a
-> Vector rb b
-> Vector DS c
szipWith f v1 v2 = fromSteps $ S.zipWith f (S.toStream v1) (S.toStream v2)
{-# INLINE szipWith #-}
szipWith3 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c)
=> (a -> b -> c -> d)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector DS d
szipWith3 f v1 v2 v3 = fromSteps $ S.zipWith3 f (S.toStream v1) (S.toStream v2) (S.toStream v3)
{-# INLINE szipWith3 #-}
szipWith4 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d)
=> (a -> b -> c -> d -> e)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector DS e
szipWith4 f v1 v2 v3 v4 =
fromSteps $ S.zipWith4 f (S.toStream v1) (S.toStream v2) (S.toStream v3) (S.toStream v4)
{-# INLINE szipWith4 #-}
szipWith5 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, S.Stream re Ix1 e)
=> (a -> b -> c -> d -> e -> f)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector DS f
szipWith5 f v1 v2 v3 v4 v5 =
fromSteps $
S.zipWith5 f (S.toStream v1) (S.toStream v2) (S.toStream v3) (S.toStream v4) (S.toStream v5)
{-# INLINE szipWith5 #-}
szipWith6 ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
)
=> (a -> b -> c -> d -> e -> f -> g)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> Vector DS g
szipWith6 f v1 v2 v3 v4 v5 v6 =
fromSteps $
S.zipWith6
f
(S.toStream v1)
(S.toStream v2)
(S.toStream v3)
(S.toStream v4)
(S.toStream v5)
(S.toStream v6)
{-# INLINE szipWith6 #-}
sizipWith ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b)
=> (Ix1 -> a -> b -> c)
-> Vector ra a
-> Vector rb b
-> Vector DS c
sizipWith f v1 v2 = fromSteps $ S.zipWith (uncurry f) (S.toStreamIx v1) (S.toStream v2)
{-# INLINE sizipWith #-}
sizipWith3 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c)
=> (Ix1 -> a -> b -> c -> d)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector DS d
sizipWith3 f v1 v2 v3 =
fromSteps $ S.zipWith3 (uncurry f) (S.toStreamIx v1) (S.toStream v2) (S.toStream v3)
{-# INLINE sizipWith3 #-}
sizipWith4 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d)
=> (Ix1 -> a -> b -> c -> d -> e)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector DS e
sizipWith4 f v1 v2 v3 v4 =
fromSteps $
S.zipWith4 (uncurry f) (S.toStreamIx v1) (S.toStream v2) (S.toStream v3) (S.toStream v4)
{-# INLINE sizipWith4 #-}
sizipWith5 ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, S.Stream re Ix1 e)
=> (Ix1 -> a -> b -> c -> d -> e -> f)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector DS f
sizipWith5 f v1 v2 v3 v4 v5 =
fromSteps $
S.zipWith5
(uncurry f)
(S.toStreamIx v1)
(S.toStream v2)
(S.toStream v3)
(S.toStream v4)
(S.toStream v5)
{-# INLINE sizipWith5 #-}
sizipWith6 ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
)
=> (Ix1 -> a -> b -> c -> d -> e -> f -> g)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> Vector DS g
sizipWith6 f v1 v2 v3 v4 v5 v6 =
fromSteps $
S.zipWith6
(uncurry f)
(S.toStreamIx v1)
(S.toStream v2)
(S.toStream v3)
(S.toStream v4)
(S.toStream v5)
(S.toStream v6)
{-# INLINE sizipWith6 #-}
szipWithM ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, Monad m)
=> (a -> b -> m c)
-> Vector ra a
-> Vector rb b
-> m (Vector DS c)
szipWithM f v1 v2 = fromStepsM $ S.zipWithM f (toStreamM v1) (toStreamM v2)
{-# INLINE szipWithM #-}
szipWith3M ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, Monad m)
=> (a -> b -> c -> m d)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> m (Vector DS d)
szipWith3M f v1 v2 v3 = fromStepsM $ S.zipWith3M f (toStreamM v1) (toStreamM v2) (toStreamM v3)
{-# INLINE szipWith3M #-}
szipWith4M ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, Monad m)
=> (a -> b -> c -> d -> m e)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> m (Vector DS e)
szipWith4M f v1 v2 v3 v4 =
fromStepsM $ S.zipWith4M f (toStreamM v1) (toStreamM v2) (toStreamM v3) (toStreamM v4)
{-# INLINE szipWith4M #-}
szipWith5M ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, Monad m
)
=> (a -> b -> c -> d -> e -> m f)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> m (Vector DS f)
szipWith5M f v1 v2 v3 v4 v5 =
fromStepsM $
S.zipWith5M f (toStreamM v1) (toStreamM v2) (toStreamM v3) (toStreamM v4) (toStreamM v5)
{-# INLINE szipWith5M #-}
szipWith6M ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
, Monad m
)
=> (a -> b -> c -> d -> e -> f -> m g)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> m (Vector DS g)
szipWith6M f v1 v2 v3 v4 v5 v6 =
fromStepsM $
S.zipWith6M
f
(toStreamM v1)
(toStreamM v2)
(toStreamM v3)
(toStreamM v4)
(toStreamM v5)
(toStreamM v6)
{-# INLINE szipWith6M #-}
sizipWithM ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, Monad m)
=> (Ix1 -> a -> b -> m c)
-> Vector ra a
-> Vector rb b
-> m (Vector DS c)
sizipWithM f v1 v2 = fromStepsM $ S.zipWithM (uncurry f) (toStreamIxM v1) (toStreamM v2)
{-# INLINE sizipWithM #-}
sizipWith3M ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, Monad m)
=> (Ix1 -> a -> b -> c -> m d)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> m (Vector DS d)
sizipWith3M f v1 v2 v3 =
fromStepsM $ S.zipWith3M (uncurry f) (toStreamIxM v1) (toStreamM v2) (toStreamM v3)
{-# INLINE sizipWith3M #-}
sizipWith4M ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, Monad m)
=> (Ix1 -> a -> b -> c -> d -> m e)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> m (Vector DS e)
sizipWith4M f v1 v2 v3 v4 =
fromStepsM $
S.zipWith4M (uncurry f) (toStreamIxM v1) (toStreamM v2) (toStreamM v3) (toStreamM v4)
{-# INLINE sizipWith4M #-}
sizipWith5M ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, Monad m
)
=> (Ix1 -> a -> b -> c -> d -> e -> m f)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> m (Vector DS f)
sizipWith5M f v1 v2 v3 v4 v5 =
fromStepsM $
S.zipWith5M
(uncurry f)
(toStreamIxM v1)
(toStreamM v2)
(toStreamM v3)
(toStreamM v4)
(toStreamM v5)
{-# INLINE sizipWith5M #-}
sizipWith6M ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
, Monad m
)
=> (Ix1 -> a -> b -> c -> d -> e -> f -> m g)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> m (Vector DS g)
sizipWith6M f v1 v2 v3 v4 v5 v6 =
fromStepsM $
S.zipWith6M
(uncurry f)
(toStreamIxM v1)
(toStreamM v2)
(toStreamM v3)
(toStreamM v4)
(toStreamM v5)
(toStreamM v6)
{-# INLINE sizipWith6M #-}
szipWithM_ ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, Monad m)
=> (a -> b -> m c)
-> Vector ra a
-> Vector rb b
-> m ()
szipWithM_ f v1 v2 = S.zipWithM_ f (toStreamM v1) (toStreamM v2)
{-# INLINE szipWithM_ #-}
szipWith3M_ ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, Monad m)
=> (a -> b -> c -> m d)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> m ()
szipWith3M_ f v1 v2 v3 = S.zipWith3M_ f (toStreamM v1) (toStreamM v2) (toStreamM v3)
{-# INLINE szipWith3M_ #-}
szipWith4M_ ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, Monad m)
=> (a -> b -> c -> d -> m e)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> m ()
szipWith4M_ f v1 v2 v3 v4 =
S.zipWith4M_ f (toStreamM v1) (toStreamM v2) (toStreamM v3) (toStreamM v4)
{-# INLINE szipWith4M_ #-}
szipWith5M_ ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, Monad m
)
=> (a -> b -> c -> d -> e -> m f)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> m ()
szipWith5M_ f v1 v2 v3 v4 v5 =
S.zipWith5M_ f (toStreamM v1) (toStreamM v2) (toStreamM v3) (toStreamM v4) (toStreamM v5)
{-# INLINE szipWith5M_ #-}
szipWith6M_ ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
, Monad m
)
=> (a -> b -> c -> d -> e -> f -> m g)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> m ()
szipWith6M_ f v1 v2 v3 v4 v5 v6 =
S.zipWith6M_
f
(toStreamM v1)
(toStreamM v2)
(toStreamM v3)
(toStreamM v4)
(toStreamM v5)
(toStreamM v6)
{-# INLINE szipWith6M_ #-}
sizipWithM_ ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, Monad m)
=> (Ix1 -> a -> b -> m c)
-> Vector ra a
-> Vector rb b
-> m ()
sizipWithM_ f v1 v2 = S.zipWithM_ (uncurry f) (toStreamIxM v1) (toStreamM v2)
{-# INLINE sizipWithM_ #-}
sizipWith3M_ ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, Monad m)
=> (Ix1 -> a -> b -> c -> m d)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> m ()
sizipWith3M_ f v1 v2 v3 = S.zipWith3M_ (uncurry f) (toStreamIxM v1) (toStreamM v2) (toStreamM v3)
{-# INLINE sizipWith3M_ #-}
sizipWith4M_ ::
(S.Stream ra Ix1 a, S.Stream rb Ix1 b, S.Stream rc Ix1 c, S.Stream rd Ix1 d, Monad m)
=> (Ix1 -> a -> b -> c -> d -> m e)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> m ()
sizipWith4M_ f v1 v2 v3 v4 =
S.zipWith4M_ (uncurry f) (toStreamIxM v1) (toStreamM v2) (toStreamM v3) (toStreamM v4)
{-# INLINE sizipWith4M_ #-}
sizipWith5M_ ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, Monad m
)
=> (Ix1 -> a -> b -> c -> d -> e -> m f)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> m ()
sizipWith5M_ f v1 v2 v3 v4 v5 =
S.zipWith5M_
(uncurry f)
(toStreamIxM v1)
(toStreamM v2)
(toStreamM v3)
(toStreamM v4)
(toStreamM v5)
{-# INLINE sizipWith5M_ #-}
sizipWith6M_ ::
( S.Stream ra Ix1 a
, S.Stream rb Ix1 b
, S.Stream rc Ix1 c
, S.Stream rd Ix1 d
, S.Stream re Ix1 e
, S.Stream rf Ix1 f
, Monad m
)
=> (Ix1 -> a -> b -> c -> d -> e -> f -> m g)
-> Vector ra a
-> Vector rb b
-> Vector rc c
-> Vector rd d
-> Vector re e
-> Vector rf f
-> m ()
sizipWith6M_ f v1 v2 v3 v4 v5 v6 =
S.zipWith6M_
(uncurry f)
(toStreamIxM v1)
(toStreamM v2)
(toStreamM v3)
(toStreamM v4)
(toStreamM v5)
(toStreamM v6)
{-# INLINE sizipWith6M_ #-}
sfoldl :: Stream r ix e => (a -> e -> a) -> a -> Array r ix e -> a
sfoldl f acc = S.unId . S.foldl f acc . toStream
{-# INLINE sfoldl #-}
sfoldlM :: (Stream r ix e, Monad m) => (a -> e -> m a) -> a -> Array r ix e -> m a
sfoldlM f acc = S.foldlM f acc . S.transStepsId . toStream
{-# INLINE sfoldlM #-}
sfoldlM_ :: (Stream r ix e, Monad m) => (a -> e -> m a) -> a -> Array r ix e -> m ()
sfoldlM_ f acc = void . sfoldlM f acc
{-# INLINE sfoldlM_ #-}
sfoldl1' :: Stream r ix e => (e -> e -> e) -> Array r ix e -> e
sfoldl1' f = either throw id . sfoldl1M (\e -> pure . f e)
{-# INLINE sfoldl1' #-}
sfoldl1M :: (Stream r ix e, MonadThrow m) => (e -> e -> m e) -> Array r ix e -> m e
sfoldl1M f arr = do
let str = S.transStepsId $ toStream arr
nullStream <- S.null str
when nullStream $ throwM $ SizeEmptyException (size arr)
S.foldl1M f str
{-# INLINE sfoldl1M #-}
sfoldl1M_ :: (Stream r ix e, MonadThrow m) => (e -> e -> m e) -> Array r ix e -> m ()
sfoldl1M_ f = void . sfoldl1M f
{-# INLINE sfoldl1M_ #-}
sifoldl :: Stream r ix e => (a -> ix -> e -> a) -> a -> Array r ix e -> a
sifoldl f acc = S.unId . S.foldl (\a (ix, e) -> f a ix e) acc . toStreamIx
{-# INLINE sifoldl #-}
sifoldlM :: (Stream r ix e, Monad m) => (a -> ix -> e -> m a) -> a -> Array r ix e -> m a
sifoldlM f acc = S.foldlM (\a (ix, e) -> f a ix e) acc . S.transStepsId . toStreamIx
{-# INLINE sifoldlM #-}
sifoldlM_ :: (Stream r ix e, Monad m) => (a -> ix -> e -> m a) -> a -> Array r ix e -> m ()
sifoldlM_ f acc = void . sifoldlM f acc
{-# INLINE sifoldlM_ #-}
sor :: Stream r ix Bool => Array r ix Bool -> Bool
sor = S.unId . S.or . toStream
{-# INLINE sor #-}
sand :: Stream r ix Bool => Array r ix Bool -> Bool
sand = S.unId . S.and . toStream
{-# INLINE sand #-}
sany :: Stream r ix e => (e -> Bool) -> Array r ix e -> Bool
sany f = S.unId . S.or . S.map f . toStream
{-# INLINE sany #-}
sall :: Stream r ix e => (e -> Bool) -> Array r ix e -> Bool
sall f = S.unId . S.and . S.map f . toStream
{-# INLINE sall #-}
ssum :: (Num e, Stream r ix e) => Array r ix e -> e
ssum = sfoldl (+) 0
{-# INLINE ssum #-}
sproduct :: (Num e, Stream r ix e) => Array r ix e -> e
sproduct = sfoldl (*) 1
{-# INLINE sproduct #-}
smaximum' :: (Ord e, Stream r ix e) => Array r ix e -> e
smaximum' = sfoldl1' max
{-# INLINE smaximum' #-}
smaximumM :: (Ord e, Stream r ix e, MonadThrow m) => Array r ix e -> m e
smaximumM = sfoldl1M (\e acc -> pure (max e acc))
{-# INLINE smaximumM #-}
sminimum' :: (Ord e, Stream r ix e) => Array r ix e -> e
sminimum' = sfoldl1' min
{-# INLINE sminimum' #-}
sminimumM :: (Ord e, Stream r ix e, MonadThrow m) => Array r ix e -> m e
sminimumM = sfoldl1M (\e acc -> pure (min e acc))
{-# INLINE sminimumM #-}
takeS :: Stream r ix e => Sz1 -> Array r ix e -> Array DS Ix1 e
takeS n = fromSteps . S.take (unSz n) . S.toStream
{-# INLINE takeS #-}
{-# DEPRECATED takeS "In favor of `stake`" #-}
dropS :: Stream r ix e => Sz1 -> Array r ix e -> Array DS Ix1 e
dropS n = fromSteps . S.drop (unSz n) . S.toStream
{-# INLINE dropS #-}
{-# DEPRECATED dropS "In favor of `sdrop`" #-}
unfoldr :: (s -> Maybe (e, s)) -> s -> Vector DS e
unfoldr = sunfoldr
{-# INLINE unfoldr #-}
{-# DEPRECATED unfoldr "In favor of `sunfoldr`" #-}
unfoldrN :: Sz1 -> (s -> Maybe (e, s)) -> s -> Vector DS e
unfoldrN = unfoldrN
{-# INLINE unfoldrN #-}
{-# DEPRECATED unfoldrN "In favor of `sunfoldrN`" #-}
filterM :: (S.Stream r ix e, Applicative f) => (e -> f Bool) -> Array r ix e -> f (Vector DS e)
filterM f arr = DSArray <$> S.filterA f (S.toStream arr)
{-# INLINE filterM #-}
{-# DEPRECATED filterM "In favor of `sfilterM`" #-}
filterS :: S.Stream r ix e => (e -> Bool) -> Array r ix e -> Array DS Ix1 e
filterS = sfilter
{-# INLINE filterS #-}
{-# DEPRECATED filterS "In favor of `sfilter`" #-}
mapMaybeS :: S.Stream r ix a => (a -> Maybe b) -> Array r ix a -> Vector DS b
mapMaybeS = smapMaybe
{-# INLINE mapMaybeS #-}
{-# DEPRECATED mapMaybeS "In favor of `smapMaybe`" #-}
catMaybesS :: S.Stream r ix (Maybe a) => Array r ix (Maybe a) -> Vector DS a
catMaybesS = scatMaybes
{-# INLINE catMaybesS #-}
{-# DEPRECATED catMaybesS "In favor of `scatMaybes`" #-}
mapMaybeM ::
(S.Stream r ix a, Applicative f) => (a -> f (Maybe b)) -> Array r ix a -> f (Vector DS b)
mapMaybeM = smapMaybeM
{-# INLINE mapMaybeM #-}
{-# DEPRECATED mapMaybeM "In favor of `smapMaybeM`" #-}
traverseS :: (S.Stream r ix a, Applicative f) => (a -> f b) -> Array r ix a -> f (Vector DS b)
traverseS = straverse
{-# INLINE traverseS #-}
{-# DEPRECATED traverseS "In favor of `straverse`" #-}
imapMaybeS :: Source r ix a => (ix -> a -> Maybe b) -> Array r ix a -> Array DS Ix1 b
imapMaybeS f arr =
mapMaybeS (uncurry f) $ A.makeArrayR D (getComp arr) (size arr) $ \ix -> (ix, unsafeIndex arr ix)
{-# INLINE imapMaybeS #-}
{-# DEPRECATED imapMaybeS "In favor of `simapMaybe`" #-}
imapMaybeM ::
(Source r ix a, Applicative f) => (ix -> a -> f (Maybe b)) -> Array r ix a -> f (Array DS Ix1 b)
imapMaybeM f arr =
mapMaybeM (uncurry f) $ A.makeArrayR D (getComp arr) (size arr) $ \ix -> (ix, unsafeIndex arr ix)
{-# INLINE imapMaybeM #-}
{-# DEPRECATED imapMaybeM "In favor of `simapMaybeM`" #-}
ifilterS :: Source r ix a => (ix -> a -> Bool) -> Array r ix a -> Array DS Ix1 a
ifilterS f =
imapMaybeS $ \ix e ->
if f ix e
then Just e
else Nothing
{-# INLINE ifilterS #-}
{-# DEPRECATED ifilterS "In favor of `sifilter`" #-}
ifilterM ::
(Source r ix a, Applicative f) => (ix -> a -> f Bool) -> Array r ix a -> f (Array DS Ix1 a)
ifilterM f =
imapMaybeM $ \ix e ->
(\p ->
if p
then Just e
else Nothing) <$>
f ix e
{-# INLINE ifilterM #-}
{-# DEPRECATED ifilterM "In favor of `sifilterM`" #-}