module Streamly.Internal.Data.Array.Mut
(
module Streamly.Internal.Data.Array.Mut.Type
, splitOn
, genSlicesFromLen
, getSlicesFromLen
, fromStream
)
where
#include "inline.hs"
import Control.Monad.IO.Class (MonadIO(..))
import Streamly.Internal.Data.Unboxed (Unbox)
import Streamly.Internal.Data.Stream.StreamD (Stream)
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.Mut.Type
{-# INLINE splitOn #-}
splitOn :: (MonadIO m, Unbox a) =>
(a -> Bool) -> MutArray a -> Stream m (MutArray a)
splitOn :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(a -> Bool) -> MutArray a -> Stream m (MutArray a)
splitOn a -> Bool
predicate MutArray a
arr =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Int
i, Int
len) -> forall a. Unbox a => Int -> Int -> MutArray a -> MutArray a
getSliceUnsafe Int
i Int
len MutArray 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, Unbox a) =>
MutArray a -> Stream m a
toStreamD MutArray a
arr)
{-# INLINE genSlicesFromLen #-}
genSlicesFromLen :: forall m a. (Monad m, Unbox a)
=> Int
-> Int
-> Unfold m (MutArray a) (Int, Int)
genSlicesFromLen :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Int -> Int -> Unfold m (MutArray 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. Unbox a => MutArray 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.mapM2 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, Unbox a)
=> Int
-> Int
-> Unfold m (MutArray a) (MutArray a)
getSlicesFromLen :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Int -> Int -> Unfold m (MutArray a) (MutArray a)
getSlicesFromLen Int
from Int
len =
let mkSlice :: MutArray a -> (Int, Int) -> m (MutArray a)
mkSlice MutArray a
arr (Int
i, Int
n) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> Int -> MutArray a -> MutArray a
getSliceUnsafe Int
i Int
n MutArray a
arr
in forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
Unfold.mapM2 forall {m :: * -> *} {a}.
(Monad m, Unbox a) =>
MutArray a -> (Int, Int) -> m (MutArray a)
mkSlice (forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Int -> Int -> Unfold m (MutArray a) (Int, Int)
genSlicesFromLen Int
from Int
len)
{-# INLINE fromStream #-}
fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
fromStream :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (MutArray a)
fromStream = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (MutArray a)
fromStreamD