@@ -38,7 +38,7 @@ module Streamly.Mem.Array.Types
38 , fromStreamDArraysOf
38 , fromStreamDArraysOf
39 , flattenArrays
39 , flattenArrays
40 , flattenArraysRev
40 , flattenArraysRev
41− , coalesceChunksOf
41+ , packArraysChunksOf
42 , groupIOVecsOf
42 , groupIOVecsOf
@@ -485,12 +485,49 @@ fromStreamDN limit str = do
485 liftIO $ poke ptr x
485 liftIO $ poke ptr x
486 return $ ptr `plusPtr` sizeOf (undefined :: a)
486 return $ ptr `plusPtr` sizeOf (undefined :: a)
488+data GroupState s start end bound
490+ | GroupBuffer s start end bound
491+ | GroupYield start end bound (GroupState s start end bound)
488-- | @fromStreamArraysOf n stream@ groups the input stream into a stream of
494-- | @fromStreamArraysOf n stream@ groups the input stream into a stream of
489-- arrays of size n.
495-- arrays of size n.
490−{-# INLINE fromStreamDArraysOf #-}
496+{-# INLINE_NORMAL fromStreamDArraysOf #-}
491fromStreamDArraysOf :: (MonadIO m, Storable a)
497fromStreamDArraysOf :: (MonadIO m, Storable a)
492 => Int -> D.Stream m a -> D.Stream m (Array a)
498 => Int -> D.Stream m a -> D.Stream m (Array a)
493−fromStreamDArraysOf n str = D.groupsOf n (toArrayN n) str
499+-- fromStreamDArraysOf n str = D.groupsOf n (toArrayN n) str
500+fromStreamDArraysOf n (D.Stream step state) =
501+ D.Stream step' (GroupStart state)
505+ {-# INLINE_LATE step' #-}
506+ step' _ (GroupStart st) = do
508+ -- XXX we can pass the module string from the higher level API
509+ error $ "Streamly.Mem.Array.Types.fromStreamDArraysOf: the size of "
510+ ++ "arrays [" ++ show n ++ "] must be a natural number"
511+ Array start end bound <- liftIO $ newArray n
512+ return $ D.Skip (GroupBuffer st start end bound)
514+ step' gst (GroupBuffer st start end bound) = do
515+ r <- step (adaptState gst) st
519+ let end' = end `plusPtr` 1
522+ then D.Skip (GroupYield start end' bound (GroupStart s))
523+ else D.Skip (GroupBuffer s start end' bound)
524+ D.Skip s -> return $ D.Skip (GroupBuffer s start end bound)
525+ D.Stop -> return $ D.Skip (GroupYield start end bound GroupFinish)
527+ step' _ (GroupYield start end bound next) =
528+ return $ D.Yield (Array start end bound) next
530+ step' _ GroupFinish = return D.Stop
495-- XXX concatMap does not seem to have the best possible performance so we have
532-- XXX concatMap does not seem to have the best possible performance so we have
496-- a custom way to concat arrays.
533-- a custom way to concat arrays.
@@ -833,15 +870,20 @@ data SpliceState s arr
833-- maximum specified size.
870-- maximum specified size.
835-- @since 0.7.0
872-- @since 0.7.0
836−{-# INLINE_NORMAL coalesceChunksOf #-}
873+{-# INLINE_NORMAL packArraysChunksOf #-}
837−coalesceChunksOf :: (MonadIO m, Storable a)
874+packArraysChunksOf :: (MonadIO m, Storable a)
838 => Int -> D.Stream m (Array a) -> D.Stream m (Array a)
875 => Int -> D.Stream m (Array a) -> D.Stream m (Array a)
876+packArraysChunksOf n (D.Stream step state) =
839−coalesceChunksOf n (D.Stream step state) = D.Stream step' (SpliceInitial state)
877+ D.Stream step' (SpliceInitial state)
843 {-# INLINE_LATE step' #-}
881 {-# INLINE_LATE step' #-}
844 step' gst (SpliceInitial st) = do
882 step' gst (SpliceInitial st) = do
884+ -- XXX we can pass the module string from the higher level API
885+ error $ "Streamly.Mem.Array.Types.packArraysChunksOf: the size of "
886+ ++ "arrays [" ++ show n ++ "] must be a natural number"
845 r <- step gst st
887 r <- step gst st
846 case r of
888 case r of
847 D.Yield arr s -> return $
889 D.Yield arr s -> return $
@@ -858,7 +900,8 @@ coalesceChunksOf n (D.Stream step state) = D.Stream step' (SpliceInitial state)
858 D.Yield arr s -> do
900 D.Yield arr s -> do
859 let len = byteLength buf + byteLength arr
901 let len = byteLength buf + byteLength arr
860 if len > n
902 if len > n
861− then return $ D.Skip (SpliceYielding buf (SpliceBuffering s arr))
904+ D.Skip (SpliceYielding buf (SpliceBuffering s arr))
863 buf' <- if byteCapacity buf < n
906 buf' <- if byteCapacity buf < n
864 then liftIO $ realloc n buf
907 then liftIO $ realloc n buf
@@ -893,6 +936,14 @@ groupIOVecsOf n maxIOVLen (D.Stream step state) =
894 {-# INLINE_LATE step' #-}
937 {-# INLINE_LATE step' #-}
895 step' gst (GatherInitial st) = do
938 step' gst (GatherInitial st) = do
940+ -- XXX we can pass the module string from the higher level API
941+ error $ "Streamly.Mem.Array.Types.groupIOVecsOf: the size of "
942+ ++ "groups [" ++ show n ++ "] must be a natural number"
943+ when (maxIOVLen <= 0) $
944+ -- XXX we can pass the module string from the higher level API
945+ error $ "Streamly.Mem.Array.Types.groupIOVecsOf: the number of "
946+ ++ "IOVec entries [" ++ show n ++ "] must be a natural number"
896 r <- step (adaptState gst) st
947 r <- step (adaptState gst) st
897 case r of
948 case r of
898 D.Yield arr s -> do
949 D.Yield arr s -> do