-- |
-- Module      : Streamly.Internal.Data.Array.Stream.Foreign
-- Copyright   : (c) 2019 Composewell Technologies
-- License     : BSD3-3-Clause
-- Maintainer  : streamly@composewell.com
-- Stability   : pre-release
-- Portability : GHC
--
-- Combinators to efficiently manipulate streams of immutable arrays.
--
module Streamly.Internal.Data.Array.Stream.Foreign
    (
    -- * Creation
      arraysOf

    -- * Flattening to elements
    , concat
    , concatRev
    , interpose
    , interposeSuffix
    , intercalateSuffix
    , unlines

    -- * Elimination
    -- ** Element Folds
    , fold
    , parse
    , parseD

    -- ** Array Folds
    , foldArr
    , foldArr_
    -- , parseArr
    , parseArrD
    , foldArrMany

    , toArray

    -- * Compaction
    , lpackArraysChunksOf
    , compact

    -- * Splitting
    , splitOn
    , splitOnSuffix
    )
where

#include "ArrayMacros.h"
#include "inline.hs"

import Data.Bifunctor (second)
import Control.Exception (assert)
import Control.Monad.Catch (MonadThrow, throwM)
import Control.Monad.IO.Class (MonadIO(..))
#if __GLASGOW_HASKELL__ < 808
import Data.Semigroup (Semigroup(..))
#endif
import Data.Word (Word8)
import Foreign.ForeignPtr (touchForeignPtr)
import Foreign.Ptr (minusPtr, plusPtr, castPtr)
import Foreign.Storable (Storable(..))
import Fusion.Plugin.Types (Fuse(..))
import GHC.Exts (SpecConstrAnnotation(..))
import GHC.ForeignPtr (ForeignPtr(..))
import GHC.Ptr (Ptr(..))
import GHC.Types (SPEC(..))
import Prelude hiding (null, last, (!!), read, concat, unlines)

import Streamly.Internal.Data.Array.Foreign.Mut.Type
    (arrayToFptrContents, fptrToArrayContents, touch)
import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Parser (ParseError(..))
import Streamly.Internal.Data.Stream.Serial (SerialT)
import Streamly.Internal.Data.Stream.IsStream.Type
    (IsStream, fromStreamD, toStreamD)
import Streamly.Internal.Data.SVar (adaptState, defState)
import Streamly.Internal.Data.Array.Foreign.Mut.Type
    (memcpy, allocBytesToElemCount)

import qualified Streamly.Internal.Data.Array.Foreign as A
import qualified Streamly.Internal.Data.Array.Foreign as Array
import qualified Streamly.Internal.Data.Array.Foreign.Type as A
import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as AS
import qualified Streamly.Internal.Data.Array.Stream.Fold.Foreign as ASF
import qualified Streamly.Internal.Data.Fold.Type as FL
import qualified Streamly.Internal.Data.Parser as PR
import qualified Streamly.Internal.Data.Parser.ParserD as PRD
import qualified Streamly.Internal.Data.Stream.IsStream as S
import qualified Streamly.Internal.Data.Stream.StreamD as D

-- XXX Since these are immutable arrays MonadIO constraint can be removed from
-- most places.

-------------------------------------------------------------------------------
-- Generation
-------------------------------------------------------------------------------

-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
-- @n@ elements each.
--
-- > arraysOf n = Stream.chunksOf n (Array.writeN n)
--
-- /Pre-release/
{-# INLINE arraysOf #-}
arraysOf :: (IsStream t, MonadIO m, Storable a)
    => Int -> t m a -> t m (Array a)
arraysOf :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, Storable a) =>
Int -> t m a -> t m (Array a)
arraysOf Int
n t m a
str = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
A.arraysOf Int
n (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
str)

-------------------------------------------------------------------------------
-- Append
-------------------------------------------------------------------------------

-- XXX efficiently compare two streams of arrays. Two streams can have chunks
-- of different sizes, we can handle that in the stream comparison abstraction.
-- This could be useful e.g. to fast compare whether two files differ.

-- | Convert a stream of arrays into a stream of their elements.
--
-- Same as the following:
--
-- > concat = Stream.unfoldMany Array.read
--
-- @since 0.7.0
{-# INLINE concat #-}
concat :: (IsStream t, Monad m, Storable a) => t m (Array a) -> t m a
-- concat m = fromStreamD $ A.flattenArrays (toStreamD m)
-- concat m = fromStreamD $ D.concatMap A.toStreamD (toStreamD m)
concat :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m, Storable a) =>
t m (Array a) -> t m a
concat t m (Array a)
m = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.read (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (Array a)
m)

-- | Convert a stream of arrays into a stream of their elements reversing the
-- contents of each array before flattening.
--
-- > concatRev = Stream.unfoldMany Array.readRev
--
-- @since 0.7.0
{-# INLINE concatRev #-}
concatRev :: (IsStream t, Monad m, Storable a) => t m (Array a) -> t m a
-- concatRev m = fromStreamD $ A.flattenArraysRev (toStreamD m)
concatRev :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m, Storable a) =>
t m (Array a) -> t m a
concatRev t m (Array a)
m = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.readRev (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (Array a)
m)

-------------------------------------------------------------------------------
-- Intersperse and append
-------------------------------------------------------------------------------

-- | Flatten a stream of arrays after inserting the given element between
-- arrays.
--
-- /Pre-release/
{-# INLINE interpose #-}
interpose :: (Monad m, IsStream t, Storable a) => a -> t m (Array a) -> t m a
interpose :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t, Storable a) =>
a -> t m (Array a) -> t m a
interpose a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) c b.
(IsStream t, Monad m) =>
c -> Unfold m b c -> t m b -> t m c
S.interpose a
x forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.read

{-# INLINE intercalateSuffix #-}
intercalateSuffix :: (Monad m, IsStream t, Storable a)
    => Array a -> t m (Array a) -> t m a
intercalateSuffix :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t, Storable a) =>
Array a -> t m (Array a) -> t m a
intercalateSuffix = forall (t :: (* -> *) -> * -> *) (m :: * -> *) b c.
(IsStream t, Monad m) =>
Unfold m b c -> b -> t m b -> t m c
S.intercalateSuffix forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.read

-- | Flatten a stream of arrays appending the given element after each
-- array.
--
-- @since 0.7.0
{-# INLINE interposeSuffix #-}
interposeSuffix :: (Monad m, IsStream t, Storable a)
    => a -> t m (Array a) -> t m a
-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
interposeSuffix :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t, Storable a) =>
a -> t m (Array a) -> t m a
interposeSuffix a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) c b.
(IsStream t, Monad m) =>
c -> Unfold m b c -> t m b -> t m c
S.interposeSuffix a
x forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.read

data FlattenState s a =
      OuterLoop s
    | InnerLoop s !MA.ArrayContents !(Ptr a) !(Ptr a)

-- XXX Remove monadIO constraint
{-# INLINE_NORMAL unlines #-}
unlines :: forall m a. (MonadIO m, Storable a)
    => a -> D.Stream m (Array a) -> D.Stream m a
unlines :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
a -> Stream m (Array a) -> Stream m a
unlines a
sep (D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) = forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {a}.
State Stream m a
-> FlattenState s a -> m (Step (FlattenState s a) a)
step' (forall s a. s -> FlattenState s a
OuterLoop s
state)
    where
    {-# INLINE_LATE step' #-}
    step' :: State Stream m a
-> FlattenState s a -> m (Step (FlattenState s a) a)
step' State Stream m a
gst (OuterLoop s
st) = do
        Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s (Array a)
r of
            D.Yield Array{Ptr a
ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
..} s
s ->
                forall s a. s -> Step s a
D.Skip (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoop s
s ArrayContents
arrContents Ptr a
arrStart Ptr a
aEnd)
            D.Skip s
s -> forall s a. s -> Step s a
D.Skip (forall s a. s -> FlattenState s a
OuterLoop s
s)
            Step s (Array a)
D.Stop -> forall s a. Step s a
D.Stop

    step' State Stream m a
_ (InnerLoop s
st ArrayContents
_ Ptr a
p Ptr a
end) | Ptr a
p forall a. Eq a => a -> a -> Bool
== Ptr a
end =
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
sep forall a b. (a -> b) -> a -> b
$ forall s a. s -> FlattenState s a
OuterLoop s
st

    step' State Stream m a
_ (InnerLoop s
st ArrayContents
contents Ptr a
p Ptr a
end) = do
        a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
                    a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
                    ArrayContents -> IO ()
touch ArrayContents
contents
                    forall (m :: * -> *) a. Monad m => a -> m a
return a
r
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoop s
st ArrayContents
contents (PTR_NEXT(p,a)) end)

-------------------------------------------------------------------------------
-- Compact
-------------------------------------------------------------------------------

-- XXX These would not be needed once we implement compactLEFold, see
-- module Streamly.Internal.Data.Array.Stream.Mut.Foreign
--
{-# INLINE_NORMAL packArraysChunksOf #-}
packArraysChunksOf :: (MonadIO m, Storable a)
    => Int -> D.Stream m (Array a) -> D.Stream m (Array a)
packArraysChunksOf :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m (Array a) -> Stream m (Array a)
packArraysChunksOf Int
n Stream m (Array a)
str =
    forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. Array a -> Array a
A.unsafeFreeze forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m (Array a) -> Stream m (Array a)
AS.packArraysChunksOf Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. Array a -> Array a
A.unsafeThaw Stream m (Array a)
str

-- XXX instead of writing two different versions of this operation, we should
-- write it as a pipe.
{-# INLINE_NORMAL lpackArraysChunksOf #-}
lpackArraysChunksOf :: (MonadIO m, Storable a)
    => Int -> Fold m (Array a) () -> Fold m (Array a) ()
lpackArraysChunksOf :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m (Array a) () -> Fold m (Array a) ()
lpackArraysChunksOf Int
n Fold m (Array a) ()
fld =
    forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
FL.lmap forall a. Array a -> Array a
A.unsafeThaw forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m (Array a) () -> Fold m (Array a) ()
AS.lpackArraysChunksOf Int
n (forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
FL.lmap forall a. Array a -> Array a
A.unsafeFreeze Fold m (Array a) ()
fld)

-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
-- maximum specified size in bytes.
--
-- @since 0.7.0
{-# INLINE compact #-}
compact :: (MonadIO m, Storable a)
    => Int -> SerialT m (Array a) -> SerialT m (Array a)
compact :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> SerialT m (Array a) -> SerialT m (Array a)
compact Int
n SerialT m (Array a)
xs = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m (Array a) -> Stream m (Array a)
packArraysChunksOf Int
n (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD SerialT m (Array a)
xs)

-------------------------------------------------------------------------------
-- Split
-------------------------------------------------------------------------------

data SplitState s arr
    = Initial s
    | Buffering s arr
    | Splitting s arr
    | Yielding arr (SplitState s arr)
    | Finishing

-- | Split a stream of arrays on a given separator byte, dropping the separator
-- and coalescing all the arrays between two separators into a single array.
--
-- @since 0.7.0
{-# INLINE_NORMAL _splitOn #-}
_splitOn
    :: MonadIO m
    => Word8
    -> D.Stream m (Array Word8)
    -> D.Stream m (Array Word8)
_splitOn :: forall (m :: * -> *).
MonadIO m =>
Word8 -> Stream m (Array Word8) -> Stream m (Array Word8)
_splitOn Word8
byte (D.Stream State Stream m (Array Word8) -> s -> m (Step s (Array Word8))
step s
state) = forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array Word8)
-> SplitState s (Array Word8)
-> m (Step (SplitState s (Array Word8)) (Array Word8))
step' (forall s arr. s -> SplitState s arr
Initial s
state)

    where

    {-# INLINE_LATE step' #-}
    step' :: State Stream m (Array Word8)
-> SplitState s (Array Word8)
-> m (Step (SplitState s (Array Word8)) (Array Word8))
step' State Stream m (Array Word8)
gst (Initial s
st) = do
        Step s (Array Word8)
r <- State Stream m (Array Word8) -> s -> m (Step s (Array Word8))
step State Stream m (Array Word8)
gst s
st
        case Step s (Array Word8)
r of
            D.Yield Array Word8
arr s
s -> do
                (Array Word8
arr1, Maybe (Array Word8)
marr2) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte Array Word8
arr
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Array Word8)
marr2 of
                    Maybe (Array Word8)
Nothing   -> forall s a. s -> Step s a
D.Skip (forall s arr. s -> arr -> SplitState s arr
Buffering s
s Array Word8
arr1)
                    Just Array Word8
arr2 -> forall s a. s -> Step s a
D.Skip (forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
arr1 (forall s arr. s -> arr -> SplitState s arr
Splitting s
s Array Word8
arr2))
            D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s arr. s -> SplitState s arr
Initial s
s)
            Step s (Array Word8)
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop

    step' State Stream m (Array Word8)
gst (Buffering s
st Array Word8
buf) = do
        Step s (Array Word8)
r <- State Stream m (Array Word8) -> s -> m (Step s (Array Word8))
step State Stream m (Array Word8)
gst s
st
        case Step s (Array Word8)
r of
            D.Yield Array Word8
arr s
s -> do
                (Array Word8
arr1, Maybe (Array Word8)
marr2) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte Array Word8
arr
                Array Word8
buf' <- forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
A.splice Array Word8
buf Array Word8
arr1
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Array Word8)
marr2 of
                    Maybe (Array Word8)
Nothing -> forall s a. s -> Step s a
D.Skip (forall s arr. s -> arr -> SplitState s arr
Buffering s
s Array Word8
buf')
                    Just Array Word8
x -> forall s a. s -> Step s a
D.Skip (forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
buf' (forall s arr. s -> arr -> SplitState s arr
Splitting s
s Array Word8
x))
            D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s arr. s -> arr -> SplitState s arr
Buffering s
s Array Word8
buf)
            Step s (Array Word8)
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
                if forall a. Array a -> Int
A.byteLength Array Word8
buf forall a. Eq a => a -> a -> Bool
== Int
0
                then forall s a. Step s a
D.Stop
                else forall s a. s -> Step s a
D.Skip (forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
buf forall s arr. SplitState s arr
Finishing)

    step' State Stream m (Array Word8)
_ (Splitting s
st Array Word8
buf) = do
        (Array Word8
arr1, Maybe (Array Word8)
marr2) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte Array Word8
buf
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Array Word8)
marr2 of
                Maybe (Array Word8)
Nothing -> forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall s arr. s -> arr -> SplitState s arr
Buffering s
st Array Word8
arr1
                Just Array Word8
arr2 -> forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
arr1 (forall s arr. s -> arr -> SplitState s arr
Splitting s
st Array Word8
arr2)

    step' State Stream m (Array Word8)
_ (Yielding Array Word8
arr SplitState s (Array Word8)
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield Array Word8
arr SplitState s (Array Word8)
next
    step' State Stream m (Array Word8)
_ SplitState s (Array Word8)
Finishing = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop

-- XXX Remove MonadIO constraint.
-- | Split a stream of arrays on a given separator byte, dropping the separator
-- and coalescing all the arrays between two separators into a single array.
--
-- @since 0.7.0
{-# INLINE splitOn #-}
splitOn
    :: (IsStream t, MonadIO m)
    => Word8
    -> t m (Array Word8)
    -> t m (Array Word8)
splitOn :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Word8 -> t m (Array Word8) -> t m (Array Word8)
splitOn Word8
byte t m (Array Word8)
s =
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (f :: * -> *) a.
Monad m =>
(f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
D.splitInnerBy (forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte) forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
A.splice forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (Array Word8)
s

{-# INLINE splitOnSuffix #-}
splitOnSuffix
    :: (IsStream t, MonadIO m)
    => Word8
    -> t m (Array Word8)
    -> t m (Array Word8)
-- splitOn byte s = fromStreamD $ A.splitOn byte $ toStreamD s
splitOnSuffix :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Word8 -> t m (Array Word8) -> t m (Array Word8)
splitOnSuffix Word8
byte t m (Array Word8)
s =
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Eq (f a), Monoid (f a)) =>
(f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
D.splitInnerBySuffix (forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte) forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
A.splice forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (Array Word8)
s

-------------------------------------------------------------------------------
-- Elimination - Running folds
-------------------------------------------------------------------------------

-- XXX This should be written using CPS (as foldK) if we want it to scale wrt
-- to the number of times it can be called on the same stream.
--
{-# INLINE_NORMAL foldD #-}
foldD :: forall m a b. (MonadIO m, Storable a) =>
    Fold m a b -> D.Stream m (Array a) -> m (b, D.Stream m (Array a))
foldD :: forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
Fold m a b -> Stream m (Array a) -> m (b, Stream m (Array a))
foldD (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract) stream :: Stream m (Array a)
stream@(D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) = do
    Step s b
res <- m (Step s b)
initial
    case Step s b
res of
        FL.Partial s
fs -> SPEC -> s -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
state s
fs
        FL.Done b
fb -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (b
fb, Stream m (Array a)
stream)

    where

    {-# INLINE go #-}
    go :: SPEC -> s -> s -> m (b, Stream m (Array a))
go !SPEC
_ s
st !s
fs = do
        Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
        case Step s (Array a)
r of
            D.Yield (Array ArrayContents
contents Ptr a
start (Ptr Addr#
end)) s
s ->
                let fp :: ForeignPtr a
fp = forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
end (ArrayContents -> ForeignPtrContents
arrayToFptrContents ArrayContents
contents)
                 in SPEC
-> s -> ForeignPtr a -> Ptr a -> s -> m (b, Stream m (Array a))
goArray SPEC
SPEC s
s forall {a}. ForeignPtr a
fp Ptr a
start s
fs
            D.Skip s
s -> SPEC -> s -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s s
fs
            Step s (Array a)
D.Stop -> do
                b
b <- s -> m b
extract s
fs
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a. Monad m => Stream m a
D.nil)

    goArray :: SPEC
-> s -> ForeignPtr a -> Ptr a -> s -> m (b, Stream m (Array a))
goArray !SPEC
_ s
s fp :: ForeignPtr a
fp@(ForeignPtr Addr#
end ForeignPtrContents
_) !Ptr a
cur !s
fs
        | Ptr a
cur forall a. Eq a => a -> a -> Bool
== forall a. Addr# -> Ptr a
Ptr Addr#
end = do
            forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr a
fp
            SPEC -> s -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s s
fs
    goArray !SPEC
_ s
st fp :: ForeignPtr a
fp@(ForeignPtr Addr#
end ForeignPtrContents
contents) !Ptr a
cur !s
fs = do
        a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr a
cur
        Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
        let next :: Ptr b
next = PTR_NEXT(cur,a)
        case Step s b
res of
            FL.Done b
b -> do
                let arr :: Array a
arr = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array (ForeignPtrContents -> ArrayContents
fptrToArrayContents ForeignPtrContents
contents) forall {b}. Ptr b
next (forall a. Addr# -> Ptr a
Ptr Addr#
end)
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (b
b, forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
D.cons forall {a}. Array a
arr (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
st))
            FL.Partial s
fs1 -> SPEC
-> s -> ForeignPtr a -> Ptr a -> s -> m (b, Stream m (Array a))
goArray SPEC
SPEC s
st ForeignPtr a
fp forall {b}. Ptr b
next s
fs1

-- | Fold an array stream using the supplied 'Fold'. Returns the fold result
-- and the unconsumed stream.
--
-- /Internal/
--
{-# INLINE_NORMAL fold #-}
fold ::
       (MonadIO m, Storable a)
    => FL.Fold m a b
    -> SerialT m (A.Array a)
    -> m (b, SerialT m (A.Array a))
fold :: forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
Fold m a b -> SerialT m (Array a) -> m (b, SerialT m (Array a))
fold Fold m a b
f SerialT m (Array a)
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
Fold m a b -> Stream m (Array a) -> m (b, Stream m (Array a))
foldD Fold m a b
f (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD SerialT m (Array a)
s)

-------------------------------------------------------------------------------
-- Fold to a single Array
-------------------------------------------------------------------------------

-- When we have to take an array partially, take the last part of the array.
{-# INLINE takeArrayListRev #-}
takeArrayListRev :: forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev :: forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev = forall a. Storable a => Int -> [Array a] -> [Array a]
go

    where

    go :: Int -> [Array a] -> [Array a]
go Int
_ [] = []
    go Int
n [Array a]
_ | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = []
    go Int
n (Array a
x:[Array a]
xs) =
        let len :: Int
len = forall a. Storable a => Array a -> Int
Array.length Array a
x
        in if Int
n forall a. Ord a => a -> a -> Bool
> Int
len
           then Array a
x forall a. a -> [a] -> [a]
: Int -> [Array a] -> [Array a]
go (Int
n forall a. Num a => a -> a -> a
- Int
len) [Array a]
xs
           else if Int
n forall a. Eq a => a -> a -> Bool
== Int
len
           then [Array a
x]
           else let !(Array ArrayContents
contents Ptr a
_ Ptr a
end) = Array a
x
                    !start :: Ptr b
start = Ptr a
end forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall a. Num a => a -> a
negate (Int
n forall a. Num a => a -> a -> a
* SIZE_OF(a))
                 in [forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents forall {b}. Ptr b
start Ptr a
end]

-- When we have to take an array partially, take the last part of the array in
-- the first split.
{-# INLINE splitAtArrayListRev #-}
splitAtArrayListRev :: forall a. Storable a =>
    Int -> [Array a] -> ([Array a],[Array a])
splitAtArrayListRev :: forall a. Storable a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n [Array a]
ls
  | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = ([], [Array a]
ls)
  | Bool
otherwise = Int -> [Array a] -> ([Array a], [Array a])
go Int
n [Array a]
ls
    where
        go :: Int -> [Array a] -> ([Array a], [Array a])
        go :: Int -> [Array a] -> ([Array a], [Array a])
go Int
_  []     = ([], [])
        go Int
m (Array a
x:[Array a]
xs) =
            let len :: Int
len = forall a. Storable a => Array a -> Int
Array.length Array a
x
                ([Array a]
xs', [Array a]
xs'') = Int -> [Array a] -> ([Array a], [Array a])
go (Int
m forall a. Num a => a -> a -> a
- Int
len) [Array a]
xs
             in if Int
m forall a. Ord a => a -> a -> Bool
> Int
len
                then (Array a
xforall a. a -> [a] -> [a]
:[Array a]
xs', [Array a]
xs'')
                else if Int
m forall a. Eq a => a -> a -> Bool
== Int
len
                then ([Array a
x],[Array a]
xs)
                else let !(Array ArrayContents
contents Ptr a
start Ptr a
end) = Array a
x
                         end1 :: Ptr b
end1 = Ptr a
end forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall a. Num a => a -> a
negate (Int
m forall a. Num a => a -> a -> a
* SIZE_OF(a))
                         arr2 :: Array a
arr2 = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
start forall {b}. Ptr b
end1
                         arr1 :: Array a
arr1 = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents forall {b}. Ptr b
end1 Ptr a
end
                      in ([Array a
arr1], Array a
arr2forall a. a -> [a] -> [a]
:[Array a]
xs)

-------------------------------------------------------------------------------
-- Fold to a single Array
-------------------------------------------------------------------------------

-- XXX Both of these implementations of splicing seem to perform equally well.
-- We need to perform benchmarks over a range of sizes though.

-- CAUTION! length must more than equal to lengths of all the arrays in the
-- stream.
{-# INLINE spliceArraysLenUnsafe #-}
spliceArraysLenUnsafe :: (MonadIO m, Storable a)
    => Int -> SerialT m (MA.Array a) -> m (MA.Array a)
spliceArraysLenUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> SerialT m (Array a) -> m (Array a)
spliceArraysLenUnsafe Int
len SerialT m (Array a)
buffered = do
    Array a
arr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
MA.newArray Int
len
    Ptr a
end <- forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> SerialT m a -> m b
S.foldlM' forall {m :: * -> *} {a} {a} {b}.
MonadIO m =>
Ptr a -> Array a -> m (Ptr b)
writeArr (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Array a -> Ptr a
MA.aEnd Array a
arr) SerialT m (Array a)
buffered
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Array a
arr {aEnd :: Ptr a
MA.aEnd = Ptr a
end}

    where

    writeArr :: Ptr a -> Array a -> m (Ptr b)
writeArr Ptr a
dst (MA.Array ArrayContents
ac Ptr a
src Ptr a
ae Ptr a
_) =
        forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
            let count :: Int
count = Ptr a
ae forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
src
            Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy (forall a b. Ptr a -> Ptr b
castPtr Ptr a
dst) (forall a b. Ptr a -> Ptr b
castPtr Ptr a
src) Int
count
            ArrayContents -> IO ()
touch ArrayContents
ac
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Ptr a
dst forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
count

{-# INLINE _spliceArrays #-}
_spliceArrays :: (MonadIO m, Storable a)
    => SerialT m (Array a) -> m (Array a)
_spliceArrays :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m (Array a)
_spliceArrays SerialT m (Array a)
s = do
    SerialT m (Array a)
buffered <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> SerialT m a -> m b
S.foldr forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
S.cons forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
S.nil SerialT m (Array a)
s
    Int
len <- forall (m :: * -> *) a. (Monad m, Num a) => SerialT m a -> m a
S.sum (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map forall a. Storable a => Array a -> Int
Array.length SerialT m (Array a)
buffered)
    Array a
arr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
MA.newArray Int
len
    Ptr a
end <- forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> SerialT m a -> m b
S.foldlM' forall {m :: * -> *} {a} {a} {b}.
MonadIO m =>
Ptr a -> Array a -> m (Ptr b)
writeArr (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Array a -> Ptr a
MA.aEnd Array a
arr) SerialT m (Array a)
s
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Array a -> Array a
A.unsafeFreeze forall a b. (a -> b) -> a -> b
$ Array a
arr {aEnd :: Ptr a
MA.aEnd = Ptr a
end}

    where

    writeArr :: Ptr a -> Array a -> m (Ptr b)
writeArr Ptr a
dst (Array ArrayContents
ac Ptr a
src Ptr a
ae) =
        forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
            let count :: Int
count = Ptr a
ae forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
src
            Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy (forall a b. Ptr a -> Ptr b
castPtr Ptr a
dst) (forall a b. Ptr a -> Ptr b
castPtr Ptr a
src) Int
count
            ArrayContents -> IO ()
touch ArrayContents
ac
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Ptr a
dst forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
count

{-# INLINE _spliceArraysBuffered #-}
_spliceArraysBuffered :: (MonadIO m, Storable a)
    => SerialT m (Array a) -> m (Array a)
_spliceArraysBuffered :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m (Array a)
_spliceArraysBuffered SerialT m (Array a)
s = do
    SerialT m (Array a)
buffered <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> SerialT m a -> m b
S.foldr forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
S.cons forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
S.nil SerialT m (Array a)
s
    Int
len <- forall (m :: * -> *) a. (Monad m, Num a) => SerialT m a -> m a
S.sum (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map forall a. Storable a => Array a -> Int
Array.length SerialT m (Array a)
buffered)
    forall a. Array a -> Array a
A.unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> SerialT m (Array a) -> m (Array a)
spliceArraysLenUnsafe Int
len (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map forall a. Array a -> Array a
A.unsafeThaw SerialT m (Array a)
s)

{-# INLINE spliceArraysRealloced #-}
spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
    => SerialT m (Array a) -> m (Array a)
spliceArraysRealloced :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m (Array a)
spliceArraysRealloced SerialT m (Array a)
s = do
    let n :: Int
n = forall a. Storable a => a -> Int -> Int
allocBytesToElemCount (forall a. HasCallStack => a
undefined :: a) (Int
4 forall a. Num a => a -> a -> a
* Int
1024)
        idst :: m (Array a)
idst = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
MA.newArray Int
n

    Array a
arr <- forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> SerialT m a -> m b
S.foldlM' forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
MA.spliceExp m (Array a)
idst (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map forall a. Array a -> Array a
A.unsafeThaw SerialT m (Array a)
s)
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Array a -> Array a
A.unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> m (Array a)
MA.rightSize Array a
arr

-- XXX This should just be "fold A.write"
--
-- | Given a stream of arrays, splice them all together to generate a single
-- array. The stream must be /finite/.
--
-- @since 0.7.0
{-# INLINE toArray #-}
toArray :: (MonadIO m, Storable a) => SerialT m (Array a) -> m (Array a)
toArray :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m (Array a)
toArray = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m (Array a)
spliceArraysRealloced
-- spliceArrays = _spliceArraysBuffered

-- exponentially increasing sizes of the chunks upto the max limit.
-- XXX this will be easier to implement with parsers/terminating folds
-- With this we should be able to reduce the number of chunks/allocations.
-- The reallocation/copy based toArray can also be implemented using this.
--
{-
{-# INLINE toArraysInRange #-}
toArraysInRange :: (IsStream t, MonadIO m, Storable a)
    => Int -> Int -> Fold m (Array a) b -> Fold m a b
toArraysInRange low high (Fold step initial extract) =
-}

{-
-- | Fold the input to a pure buffered stream (List) of arrays.
{-# INLINE _toArraysOf #-}
_toArraysOf :: (MonadIO m, Storable a)
    => Int -> Fold m a (SerialT Identity (Array a))
_toArraysOf n = FL.chunksOf n (A.writeNF n) FL.toStream
-}

-------------------------------------------------------------------------------
-- Elimination - running element parsers
-------------------------------------------------------------------------------

-- GHC parser does not accept {-# ANN type [] NoSpecConstr #-}, so we need
-- to make a newtype.
{-# ANN type List NoSpecConstr #-}
newtype List a = List {forall a. List a -> [a]
getList :: [a]}

-- This can be generalized to any type provided it can be unfolded to a stream
-- and it can be combined using a semigroup operation.
--
-- XXX This should be written using CPS (as parseK) if we want it to scale wrt
-- to the number of times it can be called on the same stream.
{-# INLINE_NORMAL parseD #-}
parseD ::
       forall m a b. (MonadIO m, MonadThrow m, Storable a)
    => PRD.Parser m a b
    -> D.Stream m (Array.Array a)
    -> m (b, D.Stream m (Array.Array a))
parseD :: forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Parser m a b -> Stream m (Array a) -> m (b, Stream m (Array a))
parseD (PRD.Parser s -> a -> m (Step s b)
pstep m (Initial s b)
initial s -> m b
extract) stream :: Stream m (Array a)
stream@(D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) = do
    Initial s b
res <- m (Initial s b)
initial
    case Initial s b
res of
        PRD.IPartial s
s -> SPEC -> s -> List a -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
state (forall a. [a] -> List a
List []) s
s
        PRD.IDone b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Stream m (Array a)
stream)
        PRD.IError String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

    where

    -- "backBuf" contains last few items in the stream that we may have to
    -- backtrack to.
    --
    -- XXX currently we are using a dumb list based approach for backtracking
    -- buffer. This can be replaced by a sliding/ring buffer using Data.Array.
    -- That will allow us more efficient random back and forth movement.
    go :: SPEC -> s -> List a -> s -> m (b, Stream m (Array a))
go !SPEC
_ s
st List a
backBuf !s
pst = do
        Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
        case Step s (Array a)
r of
            D.Yield (Array ArrayContents
contents Ptr a
start (Ptr Addr#
end)) s
s ->
                SPEC
-> s
-> List a
-> ForeignPtr a
-> Ptr a
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC s
s List a
backBuf
                    (forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
end (ArrayContents -> ForeignPtrContents
arrayToFptrContents ArrayContents
contents)) Ptr a
start s
pst
            D.Skip s
s -> SPEC -> s -> List a -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s List a
backBuf s
pst
            Step s (Array a)
D.Stop -> do
                b
b <- s -> m b
extract s
pst
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a. Monad m => Stream m a
D.nil)

    -- Use strictness on "cur" to keep it unboxed
    gobuf :: SPEC
-> s
-> List a
-> ForeignPtr a
-> Ptr a
-> s
-> m (b, Stream m (Array a))
gobuf !SPEC
_ s
s List a
backBuf fp :: ForeignPtr a
fp@(ForeignPtr Addr#
end ForeignPtrContents
_) !Ptr a
cur !s
pst
        | Ptr a
cur forall a. Eq a => a -> a -> Bool
== forall a. Addr# -> Ptr a
Ptr Addr#
end = do
            forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr a
fp
            SPEC -> s -> List a -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s List a
backBuf s
pst
    gobuf !SPEC
_ s
s List a
backBuf fp :: ForeignPtr a
fp@(ForeignPtr Addr#
end ForeignPtrContents
contents) !Ptr a
cur !s
pst = do
        a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr a
cur
        Step s b
pRes <- s -> a -> m (Step s b)
pstep s
pst a
x
        let next :: Ptr b
next = PTR_NEXT(cur,a)
        case Step s b
pRes of
            PR.Partial Int
0 s
pst1 ->
                 SPEC
-> s
-> List a
-> ForeignPtr a
-> Ptr a
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC s
s (forall a. [a] -> List a
List []) ForeignPtr a
fp forall {b}. Ptr b
next s
pst1
            PR.Partial Int
n s
pst1 -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)
                    arr0 :: Array a
arr0 = forall a. Storable a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
                    arr1 :: Array a
arr1 = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array (ForeignPtrContents -> ArrayContents
fptrToArrayContents ForeignPtrContents
contents) forall {b}. Ptr b
next (forall a. Addr# -> Ptr a
Ptr Addr#
end)
                    src :: Array a
src = Array a
arr0 forall a. Semigroup a => a -> a -> a
<> forall {a}. Array a
arr1
                let !(Array ArrayContents
cont1 Ptr a
start (Ptr Addr#
end1)) = Array a
src
                    fp1 :: ForeignPtr a
fp1 = forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
end1 (ArrayContents -> ForeignPtrContents
arrayToFptrContents ArrayContents
cont1)
                SPEC
-> s
-> List a
-> ForeignPtr a
-> Ptr a
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC s
s (forall a. [a] -> List a
List []) forall {a}. ForeignPtr a
fp1 Ptr a
start s
pst1
            PR.Continue Int
0 s
pst1 ->
                SPEC
-> s
-> List a
-> ForeignPtr a
-> Ptr a
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC s
s (forall a. [a] -> List a
List (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)) ForeignPtr a
fp forall {b}. Ptr b
next s
pst1
            PR.Continue Int
n s
pst1 -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let ([a]
src0, [a]
buf1) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
n (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)
                    arr0 :: Array a
arr0 = forall a. Storable a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
                    arr1 :: Array a
arr1 = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array (ForeignPtrContents -> ArrayContents
fptrToArrayContents ForeignPtrContents
contents) forall {b}. Ptr b
next (forall a. Addr# -> Ptr a
Ptr Addr#
end)
                    src :: Array a
src = Array a
arr0 forall a. Semigroup a => a -> a -> a
<> forall {a}. Array a
arr1
                let !(Array ArrayContents
cont1 Ptr a
start (Ptr Addr#
end1)) = Array a
src
                    fp1 :: ForeignPtr a
fp1 = forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
end1 (ArrayContents -> ForeignPtrContents
arrayToFptrContents ArrayContents
cont1)
                SPEC
-> s
-> List a
-> ForeignPtr a
-> Ptr a
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC s
s (forall a. [a] -> List a
List [a]
buf1) forall {a}. ForeignPtr a
fp1 Ptr a
start s
pst1
            PR.Done Int
0 b
b -> do
                let arr :: Array a
arr = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array (ForeignPtrContents -> ArrayContents
fptrToArrayContents ForeignPtrContents
contents) forall {b}. Ptr b
next (forall a. Addr# -> Ptr a
Ptr Addr#
end)
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
D.cons forall {a}. Array a
arr (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
s))
            PR.Done Int
n b
b -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n (a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List a
backBuf)
                    -- XXX create the array in reverse instead
                    arr0 :: Array a
arr0 = forall a. Storable a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
                    arr1 :: Array a
arr1 = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array (ForeignPtrContents -> ArrayContents
fptrToArrayContents ForeignPtrContents
contents) forall {b}. Ptr b
next (forall a. Addr# -> Ptr a
Ptr Addr#
end)
                    -- XXX Use StreamK to avoid adding arbitrary layers of
                    -- constructors every time.
                    str :: Stream m (Array a)
str = forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
D.cons Array a
arr0 (forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
D.cons forall {a}. Array a
arr1 (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
s))
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Stream m (Array a)
str)
            PR.Error String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

-- | Parse an array stream using the supplied 'Parser'.  Returns the parse
-- result and the unconsumed stream. Throws 'ParseError' if the parse fails.
--
-- /Internal/
--
{-# INLINE_NORMAL parse #-}
parse ::
       (MonadIO m, MonadThrow m, Storable a)
    => PRD.Parser m a b
    -> SerialT m (A.Array a)
    -> m (b, SerialT m (A.Array a))
parse :: forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Parser m a b -> SerialT m (Array a) -> m (b, SerialT m (Array a))
parse Parser m a b
p SerialT m (Array a)
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Parser m a b -> Stream m (Array a) -> m (b, Stream m (Array a))
parseD Parser m a b
p (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD SerialT m (Array a)
s)

-------------------------------------------------------------------------------
-- Elimination - Running Array Folds and parsers
-------------------------------------------------------------------------------

{-# INLINE_NORMAL parseArrD #-}
parseArrD ::
       forall m a b. (MonadIO m, MonadThrow m, Storable a)
    => PRD.Parser m (Array a) b
    -> D.Stream m (Array.Array a)
    -> m (b, D.Stream m (Array.Array a))
parseArrD :: forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Parser m (Array a) b
-> Stream m (Array a) -> m (b, Stream m (Array a))
parseArrD (PRD.Parser s -> Array a -> m (Step s b)
pstep m (Initial s b)
initial s -> m b
extract) stream :: Stream m (Array a)
stream@(D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) = do
    Initial s b
res <- m (Initial s b)
initial
    case Initial s b
res of
        PRD.IPartial s
s -> SPEC -> s -> List (Array a) -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
state (forall a. [a] -> List a
List []) s
s
        PRD.IDone b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Stream m (Array a)
stream)
        PRD.IError String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

    where

    -- "backBuf" contains last few items in the stream that we may have to
    -- backtrack to.
    --
    -- XXX currently we are using a dumb list based approach for backtracking
    -- buffer. This can be replaced by a sliding/ring buffer using Data.Array.
    -- That will allow us more efficient random back and forth movement.
    go :: SPEC -> s -> List (Array a) -> s -> m (b, Stream m (Array a))
go !SPEC
_ s
st List (Array a)
backBuf !s
pst = do
        Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
        case Step s (Array a)
r of
            D.Yield Array a
x s
s -> SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC [Array a
x] s
s List (Array a)
backBuf s
pst
            D.Skip s
s -> SPEC -> s -> List (Array a) -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s List (Array a)
backBuf s
pst
            Step s (Array a)
D.Stop -> do
                b
b <- s -> m b
extract s
pst
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a. Monad m => Stream m a
D.nil)

    gobuf :: SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (b, Stream m (Array a))
gobuf !SPEC
_ [] s
s List (Array a)
backBuf !s
pst = SPEC -> s -> List (Array a) -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s List (Array a)
backBuf s
pst
    gobuf !SPEC
_ (Array a
x:[Array a]
xs) s
s List (Array a)
backBuf !s
pst = do
        Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
        case Step s b
pRes of
            PR.Partial Int
0 s
pst1 ->
                 SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
xs s
s (forall a. [a] -> List a
List []) s
pst1
            PR.Partial Int
n s
pst1 -> do
                forall a. HasCallStack => Bool -> a -> a
assert
                    (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
                    (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let src0 :: [Array a]
src0 = forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
                    src :: [Array a]
src  = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
                SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
src s
s (forall a. [a] -> List a
List []) s
pst1
            PR.Continue Int
0 s
pst1 ->
                SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
xs s
s (forall a. [a] -> List a
List (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)) s
pst1
            PR.Continue Int
n s
pst1 -> do
                forall a. HasCallStack => Bool -> a -> a
assert
                    (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
                    (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let ([Array a]
src0, [Array a]
buf1) = forall a. Storable a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
                    src :: [Array a]
src  = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
                SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
src s
s (forall a. [a] -> List a
List [Array a]
buf1) s
pst1
            PR.Done Int
0 b
b ->
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
s)
            PR.Done Int
n b
b -> do
                forall a. HasCallStack => Bool -> a -> a
assert
                    (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
                    (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let src0 :: [Array a]
src0 = forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
                    src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
                forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
D.append (forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src) (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
s))
            PR.Error String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

{-
-- | Parse an array stream using the supplied 'Parser'.  Returns the parse
-- result and the unconsumed stream. Throws 'ParseError' if the parse fails.
--
-- /Internal/
--
{-# INLINE parseArr #-}
parseArr ::
       (MonadIO m, MonadThrow m, Storable a)
    => ASF.Parser m a b
    -> SerialT m (A.Array a)
    -> m (b, SerialT m (A.Array a))
parseArr p s = fmap fromStreamD <$> parseD p (toStreamD s)
-}

-- | Fold an array stream using the supplied array stream 'Fold'.
--
-- /Pre-release/
--
{-# INLINE foldArr #-}
foldArr :: (MonadIO m, MonadThrow m, Storable a) =>
    ASF.Fold m a b -> SerialT m (A.Array a) -> m b
foldArr :: forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Fold m a b -> SerialT m (Array a) -> m b
foldArr (ASF.Fold Parser m (Array a) b
p) SerialT m (Array a)
s = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Parser m (Array a) b
-> Stream m (Array a) -> m (b, Stream m (Array a))
parseArrD Parser m (Array a) b
p (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD SerialT m (Array a)
s)

-- | Like 'fold' but also returns the remaining stream.
--
-- /Pre-release/
--
{-# INLINE foldArr_ #-}
foldArr_ :: (MonadIO m, MonadThrow m, Storable a) =>
    ASF.Fold m a b -> SerialT m (A.Array a) -> m (b, SerialT m (A.Array a))
foldArr_ :: forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Fold m a b -> SerialT m (Array a) -> m (b, SerialT m (Array a))
foldArr_ (ASF.Fold Parser m (Array a) b
p) SerialT m (Array a)
s = forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
(MonadIO m, MonadThrow m, Storable a) =>
Parser m (Array a) b
-> Stream m (Array a) -> m (b, Stream m (Array a))
parseArrD Parser m (Array a) b
p (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD SerialT m (Array a)
s)

{-# ANN type ParseChunksState Fuse #-}
data ParseChunksState x inpBuf st pst =
      ParseChunksInit inpBuf st
    | ParseChunksInitLeftOver inpBuf
    | ParseChunksStream st inpBuf !pst
    | ParseChunksBuf inpBuf st inpBuf !pst
    | ParseChunksYield x (ParseChunksState x inpBuf st pst)

{-# INLINE_NORMAL foldArrManyD #-}
foldArrManyD
    :: (MonadThrow m, Storable a)
    => ASF.Fold m a b
    -> D.Stream m (Array a)
    -> D.Stream m b
foldArrManyD :: forall (m :: * -> *) a b.
(MonadThrow m, Storable a) =>
Fold m a b -> Stream m (Array a) -> Stream m b
foldArrManyD (ASF.Fold (PRD.Parser s -> Array a -> m (Step s b)
pstep m (Initial s b)
initial s -> m b
extract)) (D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) =
    forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {a}.
State Stream m a
-> ParseChunksState b [Array a] s s
-> m (Step (ParseChunksState b [Array a] s s) b)
stepOuter (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [] s
state)

    where

    {-# INLINE_LATE stepOuter #-}
    -- Buffer is empty, get the first element from the stream, initialize the
    -- fold and then go to stream processing loop.
    stepOuter :: State Stream m a
-> ParseChunksState b [Array a] s s
-> m (Step (ParseChunksState b [Array a] s s) b)
stepOuter State Stream m a
gst (ParseChunksInit [] s
st) = do
        Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        case Step s (Array a)
r of
            D.Yield Array a
x s
s -> do
                Initial s b
res <- m (Initial s b)
initial
                case Initial s b
res of
                    PRD.IPartial s
ps ->
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a
x] s
s [] s
ps
                    PRD.IDone b
pb ->
                        let next :: ParseChunksState x [Array a] s pst
next = forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a
x] s
s
                         in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
pb forall {x} {pst}. ParseChunksState x [Array a] s pst
next
                    PRD.IError String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err
            D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [] s
s
            Step s (Array a)
D.Stop   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop

    -- Buffer is not empty, go to buffered processing loop
    stepOuter State Stream m a
_ (ParseChunksInit [Array a]
src s
st) = do
        Initial s b
res <- m (Initial s b)
initial
        case Initial s b
res of
            PRD.IPartial s
ps ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
st [] s
ps
            PRD.IDone b
pb ->
                let next :: ParseChunksState x [Array a] s pst
next = forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
src s
st
                 in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
pb forall {x} {pst}. ParseChunksState x [Array a] s pst
next
            PRD.IError String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

    -- XXX we just discard any leftover input at the end
    stepOuter State Stream m a
_ (ParseChunksInitLeftOver [Array a]
_) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop

    -- Buffer is empty, process elements from the stream
    stepOuter State Stream m a
gst (ParseChunksStream s
st [Array a]
backBuf s
pst) = do
        Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        case Step s (Array a)
r of
            D.Yield Array a
x s
s -> do
                Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
                case Step s b
pRes of
                    PR.Partial Int
0 s
pst1 ->
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s [] s
pst1
                    PR.Partial Int
n s
pst1 -> do
                        forall a. HasCallStack => Bool -> a -> a
assert
                            (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)))
                            (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                        let src0 :: [Array a]
src0 = forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
                            src :: [Array a]
src  = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [] s
pst1
                    PR.Continue Int
0 s
pst1 ->
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf) s
pst1
                    PR.Continue Int
n s
pst1 -> do
                        forall a. HasCallStack => Bool -> a -> a
assert
                            (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)))
                            (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                        let ([Array a]
src0, [Array a]
buf1) = forall a. Storable a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
                            src :: [Array a]
src  = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [Array a]
buf1 s
pst1
                    PR.Done Int
0 b
b -> do
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$
                            forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
b (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [] s
s)
                    PR.Done Int
n b
b -> do
                        forall a. HasCallStack => Bool -> a -> a
assert
                            (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)))
                            (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                        let src0 :: [Array a]
src0 = forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
                        let src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
                        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$
                            forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
b (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
src s
s)
                    PR.Error String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err
            D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s [Array a]
backBuf s
pst
            Step s (Array a)
D.Stop   -> do
                b
b <- s -> m b
extract s
pst
                let src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
backBuf
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$
                    forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
b (forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver [Array a]
src)

    -- go back to stream processing mode
    stepOuter State Stream m a
_ (ParseChunksBuf [] s
s [Array a]
buf s
pst) =
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s [Array a]
buf s
pst

    -- buffered processing loop
    stepOuter State Stream m a
_ (ParseChunksBuf (Array a
x:[Array a]
xs) s
s [Array a]
backBuf s
pst) = do
        Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
        case Step s b
pRes of
            PR.Partial Int
0 s
pst1 ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
xs s
s [] s
pst1
            PR.Partial Int
n s
pst1 -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let src0 :: [Array a]
src0 = forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
                    src :: [Array a]
src  = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [] s
pst1
            PR.Continue Int
0 s
pst1 ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
xs s
s (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf) s
pst1
            PR.Continue Int
n s
pst1 -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let ([Array a]
src0, [Array a]
buf1) = forall a. Storable a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
                    src :: [Array a]
src  = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [Array a]
buf1 s
pst1
            PR.Done Int
0 b
b ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
b (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
xs s
s)
            PR.Done Int
n b
b -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Storable a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                let src0 :: [Array a]
src0 = forall a. Storable a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
                    src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield b
b (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
src s
s)
            PR.Error String
err -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

    stepOuter State Stream m a
_ (ParseChunksYield b
a ParseChunksState b [Array a] s s
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield b
a ParseChunksState b [Array a] s s
next

-- | Apply an array stream 'Fold' repeatedly on an array stream and emit the
-- fold outputs in the output stream.
--
-- See "Streamly.Prelude.foldMany" for more details.
--
-- /Pre-release/
{-# INLINE foldArrMany #-}
foldArrMany
    :: (IsStream t, MonadThrow m, Storable a)
    => ASF.Fold m a b
    -> t m (Array a)
    -> t m b
foldArrMany :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadThrow m, Storable a) =>
Fold m a b -> t m (Array a) -> t m b
foldArrMany Fold m a b
p t m (Array a)
m = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
(MonadThrow m, Storable a) =>
Fold m a b -> Stream m (Array a) -> Stream m b
foldArrManyD Fold m a b
p (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (Array a)
m)