-- |
-- Module      : Streamly.Internal.Data.Array.Mut
-- Copyright   : (c) 2020 Composewell Technologies
-- License     : BSD-3-Clause
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--
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

-- | Split the array into a stream of slices using a predicate. The element
-- matching the predicate is dropped.
--
-- /Pre-release/
{-# 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)

-- | Generate a stream of array slice descriptors ((index, len)) of specified
-- length from an array, starting from the supplied array index. The last slice
-- may be shorter than the requested length depending on the array length.
--
-- /Pre-release/
{-# INLINE genSlicesFromLen #-}
genSlicesFromLen :: forall m a. (Monad m, Unbox a)
    => Int -- ^ from index
    -> Int -- ^ length of the slice
    -> 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

-- | Generate a stream of slices of specified length from an array, starting
-- from the supplied array index. The last slice may be shorter than the
-- requested length depending on the array length.
--
-- /Pre-release/
{-# INLINE getSlicesFromLen #-}
getSlicesFromLen :: forall m a. (Monad m, Unbox a)
    => Int -- ^ from index
    -> Int -- ^ length of the slice
    -> 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)

-- | Create an 'Array' from a stream. This is useful when we want to create a
-- single array from a stream of unknown size. 'writeN' is at least twice
-- as efficient when the size is already known.
--
-- Note that if the input stream is too large memory allocation for the array
-- may fail.  When the stream size is not known, `chunksOf` followed by
-- processing of indvidual arrays in the resulting stream should be preferred.
--
-- /Pre-release/
{-# 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
-- fromStream (Stream m) = P.fold write m