{-# LANGUAGE FlexibleContexts #-}
module Data.Massiv.Vector.Unsafe (
unsafeHead,
unsafeLast,
unsafeIndexM,
unsafeHeadM,
unsafeLastM,
unsafeInit,
unsafeTail,
unsafeTake,
unsafeDrop,
unsafeUnfoldrN,
unsafeUnfoldrNM,
unsafeFromListN,
) where
import Data.Coerce
import Data.Massiv.Array.Delayed.Stream
import Data.Massiv.Core.Common
import qualified Data.Massiv.Vector.Stream as S
unsafeHead :: Source r e => Vector r e -> e
unsafeHead :: forall r e. Source r e => Vector r e -> e
unsafeHead = (forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
`unsafeLinearIndex` Ix1
0)
{-# INLINE unsafeHead #-}
unsafeLast :: Source r e => Vector r e -> e
unsafeLast :: forall r e. Source r e => Vector r e -> e
unsafeLast Vector r e
v = forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Vector r e
v (forall ix. Sz ix -> ix
unSz (forall r ix e. Size r => Array r ix e -> Sz ix
size Vector r e
v) forall a. Num a => a -> a -> a
- Ix1
1)
{-# INLINE unsafeLast #-}
unsafeIndexM :: (Source r e, Monad m) => Vector r e -> Ix1 -> m e
unsafeIndexM :: forall r e (m :: * -> *).
(Source r e, Monad m) =>
Vector r e -> Ix1 -> m e
unsafeIndexM Vector r e
v Ix1
i = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Vector r e
v Ix1
i
{-# INLINE unsafeIndexM #-}
unsafeHeadM :: (Monad m, Source r e) => Vector r e -> m e
unsafeHeadM :: forall (m :: * -> *) r e.
(Monad m, Source r e) =>
Vector r e -> m e
unsafeHeadM Vector r e
v = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall r e. Source r e => Vector r e -> e
unsafeHead Vector r e
v
{-# INLINE unsafeHeadM #-}
unsafeLastM :: (Monad m, Source r e) => Vector r e -> m e
unsafeLastM :: forall (m :: * -> *) r e.
(Monad m, Source r e) =>
Vector r e -> m e
unsafeLastM Vector r e
v = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall r e. Source r e => Vector r e -> e
unsafeLast Vector r e
v
{-# INLINE unsafeLastM #-}
unsafeInit :: Source r e => Vector r e -> Vector r e
unsafeInit :: forall r e. Source r e => Vector r e -> Vector r e
unsafeInit Vector r e
v = forall r e ix.
(Source r e, Index ix) =>
Ix1 -> Sz Ix1 -> Array r ix e -> Array r Ix1 e
unsafeLinearSlice Ix1
0 (forall ix. ix -> Sz ix
SafeSz (coerce :: forall a b. Coercible a b => a -> b
coerce (forall r ix e. Size r => Array r ix e -> Sz ix
size Vector r e
v) forall a. Num a => a -> a -> a
- Ix1
1)) Vector r e
v
{-# INLINE unsafeInit #-}
unsafeTail :: Source r e => Vector r e -> Vector r e
unsafeTail :: forall r e. Source r e => Vector r e -> Vector r e
unsafeTail = forall r e. Source r e => Sz Ix1 -> Vector r e -> Vector r e
unsafeDrop forall ix. Index ix => Sz ix
oneSz
{-# INLINE unsafeTail #-}
unsafeTake :: Source r e => Sz1 -> Vector r e -> Vector r e
unsafeTake :: forall r e. Source r e => Sz Ix1 -> Vector r e -> Vector r e
unsafeTake = forall r e ix.
(Source r e, Index ix) =>
Ix1 -> Sz Ix1 -> Array r ix e -> Array r Ix1 e
unsafeLinearSlice Ix1
0
{-# INLINE unsafeTake #-}
unsafeDrop :: Source r e => Sz1 -> Vector r e -> Vector r e
unsafeDrop :: forall r e. Source r e => Sz Ix1 -> Vector r e -> Vector r e
unsafeDrop (Sz Ix1
d) Vector r e
v = forall r e ix.
(Source r e, Index ix) =>
Ix1 -> Sz Ix1 -> Array r ix e -> Array r Ix1 e
unsafeLinearSlice Ix1
d (forall ix. ix -> Sz ix
SafeSz (coerce :: forall a b. Coercible a b => a -> b
coerce (forall r ix e. Size r => Array r ix e -> Sz ix
size Vector r e
v) forall a. Num a => a -> a -> a
- Ix1
d)) Vector r e
v
{-# INLINE unsafeDrop #-}
unsafeFromListN :: Sz1 -> [e] -> Vector DS e
unsafeFromListN :: forall e. Sz Ix1 -> [e] -> Vector DS e
unsafeFromListN Sz Ix1
n = forall e. Steps Id e -> Vector DS e
fromSteps forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) e. Monad m => Sz Ix1 -> [e] -> Steps m e
S.unsafeFromListN Sz Ix1
n
{-# INLINE unsafeFromListN #-}
unsafeUnfoldrN
:: Sz1
-> (s -> Maybe (e, s))
-> s
-> Vector DS e
unsafeUnfoldrN :: forall s e. Sz Ix1 -> (s -> Maybe (e, s)) -> s -> Vector DS e
unsafeUnfoldrN Sz Ix1
n s -> Maybe (e, s)
f = forall e. Steps Id e -> Vector DS e
DSArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s e.
Monad m =>
Sz Ix1 -> (s -> Maybe (e, s)) -> s -> Steps m e
S.unsafeUnfoldrN Sz Ix1
n s -> Maybe (e, s)
f
{-# INLINE unsafeUnfoldrN #-}
unsafeUnfoldrNM :: Monad m => Sz1 -> (s -> m (Maybe (e, s))) -> s -> m (Vector DS e)
unsafeUnfoldrNM :: forall (m :: * -> *) s e.
Monad m =>
Sz Ix1 -> (s -> m (Maybe (e, s))) -> s -> m (Vector DS e)
unsafeUnfoldrNM Sz Ix1
n s -> m (Maybe (e, s))
f = forall (m :: * -> *) e. Monad m => Steps m e -> m (Vector DS e)
fromStepsM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s e.
Monad m =>
Sz Ix1 -> (s -> m (Maybe (e, s))) -> s -> Steps m e
S.unsafeUnfoldrNM Sz Ix1
n s -> m (Maybe (e, s))
f
{-# INLINE unsafeUnfoldrNM #-}