#include "inline.hs"
module Streamly.Internal.Data.Array.Foreign.Mut
(
module Streamly.Internal.Data.Array.Foreign.Mut.Type
, splitOn
, genSlicesFromLen
, getSlicesFromLen
, fromStream
)
where
import Control.Monad.IO.Class (MonadIO(..))
import Foreign.Storable (Storable)
import Streamly.Internal.Data.Stream.Serial (SerialT(..))
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import qualified Streamly.Internal.Data.Stream.StreamD as D
import qualified Streamly.Internal.Data.Unfold as Unfold
import Prelude hiding (foldr, length, read, splitAt)
import Streamly.Internal.Data.Array.Foreign.Mut.Type
{-# INLINE splitOn #-}
splitOn :: (MonadIO m, Storable a) =>
(a -> Bool) -> Array a -> SerialT m (Array a)
splitOn :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(a -> Bool) -> Array a -> SerialT m (Array a)
splitOn a -> Bool
predicate Array a
arr =
forall (m :: * -> *) a. Stream m a -> SerialT m a
SerialT
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
D.toStreamK
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Int
i, Int
len) -> forall a. Storable a => Int -> Int -> Array a -> Array a
getSliceUnsafe Int
i Int
len Array a
arr)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m (Int, Int)
D.sliceOnSuffix a -> Bool
predicate (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr)
{-# INLINE genSlicesFromLen #-}
genSlicesFromLen :: forall m a. (Monad m, Storable a)
=> Int
-> Int
-> Unfold m (Array a) (Int, Int)
genSlicesFromLen :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Int -> Int -> Unfold m (Array a) (Int, Int)
genSlicesFromLen Int
from Int
len =
let fromThenTo :: c -> (Int, Int, c)
fromThenTo c
n = (Int
from, Int
from forall a. Num a => a -> a -> a
+ Int
len, c
n forall a. Num a => a -> a -> a
- c
1)
mkSlice :: Int -> Int -> m (Int, Int)
mkSlice Int
n Int
i = forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i, forall a. Ord a => a -> a -> a
min Int
len (Int
n forall a. Num a => a -> a -> a
- Int
i))
in forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap forall a. Storable a => Array a -> Int
length
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
Unfold.mapMWithInput forall {m :: * -> *}. Monad m => Int -> Int -> m (Int, Int)
mkSlice
forall a b. (a -> b) -> a -> b
$ forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap forall {c}. Num c => c -> (Int, Int, c)
fromThenTo forall a (m :: * -> *).
(Enumerable a, Monad m) =>
Unfold m (a, a, a) a
Unfold.enumerateFromThenTo
{-# INLINE getSlicesFromLen #-}
getSlicesFromLen :: forall m a. (Monad m, Storable a)
=> Int
-> Int
-> Unfold m (Array a) (Array a)
getSlicesFromLen :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Int -> Int -> Unfold m (Array a) (Array a)
getSlicesFromLen Int
from Int
len =
let mkSlice :: Array a -> (Int, Int) -> m (Array a)
mkSlice Array a
arr (Int
i, Int
n) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Int -> Int -> Array a -> Array a
getSliceUnsafe Int
i Int
n Array a
arr
in forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
Unfold.mapMWithInput forall {m :: * -> *} {a}.
(Monad m, Storable a) =>
Array a -> (Int, Int) -> m (Array a)
mkSlice (forall (m :: * -> *) a.
(Monad m, Storable a) =>
Int -> Int -> Unfold m (Array a) (Int, Int)
genSlicesFromLen Int
from Int
len)
{-# INLINE fromStream #-}
fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
fromStream :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m a -> m (Array a)
fromStream (SerialT Stream m a
m) = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m a -> m (Array a)
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => Stream m a -> Stream m a
D.fromStreamK Stream m a
m