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 :: (a -> Bool) -> MutArray a -> Stream m (MutArray a)
splitOn a -> Bool
predicate MutArray a
arr =
((Int, Int) -> MutArray a)
-> Stream m (Int, Int) -> Stream m (MutArray a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Int
i, Int
len) -> Int -> Int -> MutArray a -> MutArray a
forall a. Unbox a => Int -> Int -> MutArray a -> MutArray a
getSliceUnsafe Int
i Int
len MutArray a
arr)
(Stream m (Int, Int) -> Stream m (MutArray a))
-> Stream m (Int, Int) -> Stream m (MutArray a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> Stream m a -> Stream m (Int, Int)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m (Int, Int)
D.sliceOnSuffix a -> Bool
predicate (MutArray a -> Stream m a
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 :: 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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len, c
n c -> c -> c
forall a. Num a => a -> a -> a
- c
1)
mkSlice :: Int -> Int -> m (Int, Int)
mkSlice Int
n Int
i = (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i, Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
len (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i))
in (MutArray a -> Int)
-> Unfold m Int (Int, Int) -> Unfold m (MutArray a) (Int, Int)
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap MutArray a -> Int
forall a. Unbox a => MutArray a -> Int
length
(Unfold m Int (Int, Int) -> Unfold m (MutArray a) (Int, Int))
-> Unfold m Int (Int, Int) -> Unfold m (MutArray a) (Int, Int)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> m (Int, Int))
-> Unfold m Int Int -> Unfold m Int (Int, Int)
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
Unfold.mapM2 Int -> Int -> m (Int, Int)
forall (m :: * -> *). Monad m => Int -> Int -> m (Int, Int)
mkSlice
(Unfold m Int Int -> Unfold m Int (Int, Int))
-> Unfold m Int Int -> Unfold m Int (Int, Int)
forall a b. (a -> b) -> a -> b
$ (Int -> (Int, Int, Int))
-> Unfold m (Int, Int, Int) Int -> Unfold m Int Int
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap Int -> (Int, Int, Int)
forall c. Num c => c -> (Int, Int, c)
fromThenTo Unfold m (Int, Int, Int) Int
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 :: 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) = MutArray a -> m (MutArray a)
forall (m :: * -> *) a. Monad m => a -> m a
return (MutArray a -> m (MutArray a)) -> MutArray a -> m (MutArray a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> MutArray a -> MutArray a
forall a. Unbox a => Int -> Int -> MutArray a -> MutArray a
getSliceUnsafe Int
i Int
n MutArray a
arr
in (MutArray a -> (Int, Int) -> m (MutArray a))
-> Unfold m (MutArray a) (Int, Int)
-> Unfold m (MutArray a) (MutArray a)
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
Unfold.mapM2 MutArray a -> (Int, Int) -> m (MutArray a)
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
MutArray a -> (Int, Int) -> m (MutArray a)
mkSlice (Int -> Int -> Unfold m (MutArray a) (Int, Int)
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 :: Stream m a -> m (MutArray a)
fromStream = Stream m a -> m (MutArray a)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (MutArray a)
fromStreamD