@@ -236,6 +236,15 @@ compact :: (MonadIO m, Unbox a)
236 => Int -> Stream m (Array a) -> Stream m (Array a)
236 => Int -> Stream m (Array a) -> Stream m (Array a)
237compact = packArraysChunksOf
237compact = packArraysChunksOf
239+-- | Given a stream of arrays, splice them all together to generate a single
240+-- array. The stream must be /finite/.
243+{-# INLINE toArray #-}
244+toArray :: (MonadIO m, Unbox a) => Stream m (Array a) -> m (Array a)
246+ fmap A.unsafeFreeze $ MA.fromArrayStreamRealloced (fmap A.unsafeThaw s)
239-------------------------------------------------------------------------------
248-------------------------------------------------------------------------------
241-------------------------------------------------------------------------------
250-------------------------------------------------------------------------------
@@ -439,7 +448,7 @@ foldBreak = foldBreakK
439-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
448-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
441-------------------------------------------------------------------------------
450-------------------------------------------------------------------------------
442−-- Fold to a single Array
451+-- Elimination - running element parsers
443-------------------------------------------------------------------------------
452-------------------------------------------------------------------------------
445-- When we have to take an array partially, take the last part of the array.
454-- When we have to take an array partially, take the last part of the array.
@@ -485,83 +494,6 @@ splitAtArrayListRev n ls
485 arr1 = Array contents end1 end
494 arr1 = Array contents end1 end
486 in ([arr1], arr2:xs)
495 in ([arr1], arr2:xs)
488−-------------------------------------------------------------------------------
489−-- Fold to a single Array
490−-------------------------------------------------------------------------------
494−-- XXX Both of these implementations of splicing seem to perform equally well.
495−-- We need to perform benchmarks over a range of sizes though.
497−-- CAUTION! length must more than equal to lengths of all the arrays in the
499−{-# INLINE spliceArraysLenUnsafe #-}
500−spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
501− => Int -> Stream m (MutArray a) -> m (MutArray a)
502−spliceArraysLenUnsafe len buffered = do
503− -- XXX The new array's pinned state should depend on the first element
504− -- of the stream. We should uncons the stream and do the required.
506− D.foldlM' MA.spliceUnsafe (return arr) buffered
508−{-# INLINE _spliceArrays #-}
509−_spliceArrays :: (MonadIO m, Unbox a)
510− => Stream m (Array a) -> m (Array a)
512− buffered <- D.foldr K.cons K.nil s
513− len <- K.fold FL.sum (fmap Array.length buffered)
514− -- XXX The new array's pinned state should depend on the first element
515− -- of the stream. We should uncons the stream and do the required.
517− final <- D.foldlM' writeArr (return arr) (toStream buffered)
518− return $ A.unsafeFreeze final
522− writeArr dst arr = MA.spliceUnsafe dst (A.unsafeThaw arr)
524−{-# INLINE _spliceArraysBuffered #-}
525−_spliceArraysBuffered :: (MonadIO m, Unbox a)
526− => Stream m (Array a) -> m (Array a)
527−_spliceArraysBuffered s = do
528− buffered <- D.foldr K.cons K.nil s
529− len <- K.fold FL.sum (fmap Array.length buffered)
531− spliceArraysLenUnsafe len (fmap A.unsafeThaw (toStream buffered))
535−{-# INLINE spliceArraysRealloced #-}
536−spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
537− => Stream m (Array a) -> m (Array a)
538−spliceArraysRealloced s = do
541− Just (a, strm) -> do
545− (pure (A.unsafeThaw a))
546− (fmap A.unsafeThaw strm)
547− liftIO $ A.unsafeFreeze <$> MA.rightSize arr
548− Nothing -> pure A.nil
550−-- XXX This should just be "fold A.write"
552−-- | Given a stream of arrays, splice them all together to generate a single
553−-- array. The stream must be /finite/.
556−{-# INLINE toArray #-}
557−toArray :: (MonadIO m, Unbox a) => Stream m (Array a) -> m (Array a)
558−toArray = spliceArraysRealloced
559−-- spliceArrays = _spliceArraysBuffered
561−-------------------------------------------------------------------------------
562−-- Elimination - running element parsers
563−-------------------------------------------------------------------------------
565-- GHC parser does not accept {-# ANN type [] NoSpecConstr #-}, so we need
497-- GHC parser does not accept {-# ANN type [] NoSpecConstr #-}, so we need
566-- to make a newtype.
498-- to make a newtype.
567{-# ANN type List NoSpecConstr #-}
499{-# ANN type List NoSpecConstr #-}