on defafa6 help sign in
145 lines 5.5 kB history raw
1-- |
2-- Module : Streamly.Internal.Data.MutArray.Stream
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD3-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
7-- Portability : GHC
8--
9-- Combinators to efficiently manipulate streams of mutable arrays.
10--
11-- We can either push these in the MutArray module with a "chunks" prefix or
12-- keep this as a separate module and release it.
13--
14module Streamly.Internal.Data.MutArray.Stream
15{-# DEPRECATED "Please use \"Streamly.Internal.Data.MutArray\" instead." #-}
16 (
17 -- * Generation
18 MArray.chunksOf
19 , MArray.pinnedChunksOf
20 , MArray.writeChunks -- chunksWrite?
21 , MArray.splitOn -- chunksSplitOn
23 -- * Compaction
24 , packArraysChunksOf
25 , MArray.SpliceState (..)
26 , lpackArraysChunksOf
27 , compact -- chunksCompact
28 , compactLE
29 , compactEQ
30 , compactGE
32 -- * Elimination
33 , MArray.flattenArrays -- chunksConcat
34 , MArray.flattenArraysRev -- chunksConcatRev
35 , MArray.fromArrayStreamK -- chunksCoalesce
36 )
37where
39import Control.Monad.IO.Class (MonadIO(..))
40import Streamly.Internal.Data.Unbox (Unbox)
41import Streamly.Internal.Data.MutArray.Type (MutArray(..))
42import Streamly.Internal.Data.Fold.Type (Fold(..))
43import Streamly.Internal.Data.Parser (ParseError)
44import Streamly.Internal.Data.Stream.Type (Stream)
46import qualified Streamly.Internal.Data.MutArray as MArray
47import qualified Streamly.Internal.Data.Fold.Type as FL
48import qualified Streamly.Internal.Data.Parser as ParserD
49import qualified Streamly.Internal.Data.Stream as D
51-------------------------------------------------------------------------------
52-- Compact
53-------------------------------------------------------------------------------
55-- XXX This can be removed once compactLEFold/compactLE are implemented.
56--
57-- | This mutates the first array (if it has space) to append values from the
58-- second one. This would work for immutable arrays as well because an
59-- immutable array never has space so a new array is allocated instead of
60-- mutating it.
61--
62-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
63-- maximum specified size. Note that if a single array is bigger than the
64-- specified size we do not split it to fit. When we coalesce multiple arrays
65-- if the size would exceed the specified size we do not coalesce therefore the
66-- actual array size may be less than the specified chunk size.
67--
68-- @since 0.7.0
69{-# INLINE packArraysChunksOf #-}
70packArraysChunksOf :: (MonadIO m, Unbox a)
71 => Int -> D.Stream m (MutArray a) -> D.Stream m (MutArray a)
72packArraysChunksOf = MArray.compactLE
74-- XXX Remove this once compactLEFold is implemented
75-- lpackArraysChunksOf = Fold.many compactLEFold
76--
77{-# INLINE lpackArraysChunksOf #-}
78lpackArraysChunksOf :: (MonadIO m, Unbox a)
79 => Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
80lpackArraysChunksOf = MArray.lCompactGE
82-- XXX Same as compactLE, to be removed once that is implemented.
83--
84-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
85-- maximum specified size in bytes.
86--
87-- /Internal/
88{-# INLINE compact #-}
89compact :: (MonadIO m, Unbox a)
90 => Int -> Stream m (MutArray a) -> Stream m (MutArray a)
91compact = packArraysChunksOf
93-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
94-- maximum specified size. Note that if a single array is bigger than the
95-- specified size we do not split it to fit. When we coalesce multiple arrays
96-- if the size would exceed the specified size we do not coalesce therefore the
97-- actual array size may be less than the specified chunk size.
98--
99-- /Internal/
100{-# INLINE compactLEParserD #-}
101compactLEParserD ::
102 forall m a. (MonadIO m, Unbox a)
103 => Int -> ParserD.Parser (MutArray a) m (MutArray a)
104compactLEParserD = MArray.pCompactLE
106-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
107-- minimum specified size. Note that if all the arrays in the stream together
108-- are smaller than the specified size the resulting array will be smaller than
109-- the specified size. When we coalesce multiple arrays if the size would exceed
110-- the specified size we stop coalescing further.
111--
112-- /Internal/
113{-# INLINE compactGEFold #-}
114compactGEFold ::
115 forall m a. (MonadIO m, Unbox a)
116 => Int -> FL.Fold m (MutArray a) (MutArray a)
117compactGEFold = MArray.fCompactGE
119-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
120-- maximum specified size in bytes.
121--
122-- /Internal/
123compactLE :: (MonadIO m, Unbox a) =>
124 Int -> Stream m (MutArray a) -> Stream m (Either ParseError (MutArray a))
125compactLE n = D.parseManyD (compactLEParserD n)
127-- | Like 'compactLE' but generates arrays of exactly equal to the size
128-- specified except for the last array in the stream which could be shorter.
129--
130-- /Unimplemented/
131{-# INLINE compactEQ #-}
132compactEQ :: -- (MonadIO m, Unbox a) =>
133 Int -> Stream m (MutArray a) -> Stream m (MutArray a)
134compactEQ _n _xs = undefined
135 -- IsStream.fromStreamD $ D.foldMany (compactEQFold n) (IsStream.toStreamD xs)
137-- | Like 'compactLE' but generates arrays of size greater than or equal to the
138-- specified except for the last array in the stream which could be shorter.
139--
140-- /Internal/
141{-# INLINE compactGE #-}
142compactGE ::
143 (MonadIO m, Unbox a)
144 => Int -> Stream m (MutArray a) -> Stream m (MutArray a)
145compactGE n = D.foldMany (compactGEFold n)