@@ -236,6 +236,15 @@ compact :: (MonadIO m, Unbox a)
236236 => Int -> Stream m (Array a) -> Stream m (Array a)
237237compact = packArraysChunksOf
238238
239+-- | Given a stream of arrays, splice them all together to generate a single
240+-- array. The stream must be /finite/.
241+--
242+-- @since 0.7.0
243+{-# INLINE toArray #-}
244+toArray :: (MonadIO m, Unbox a) => Stream m (Array a) -> m (Array a)
245+toArray s =
246+ fmap A.unsafeFreeze $ MA.fromArrayStreamRealloced (fmap A.unsafeThaw s)
247+
239248-------------------------------------------------------------------------------
240249-- Split
241250-------------------------------------------------------------------------------
@@ -439,7 +448,7 @@ foldBreak = foldBreakK
439448-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
440449
441450-------------------------------------------------------------------------------
442−-- Fold to a single Array
451+-- Elimination - running element parsers
443452-------------------------------------------------------------------------------
444453
445454-- When we have to take an array partially, take the last part of the array.
@@ -485,83 +494,6 @@ splitAtArrayListRev n ls
485494 arr1 = Array contents end1 end
486495 in ([arr1], arr2:xs)
487496
488−-------------------------------------------------------------------------------
489−-- Fold to a single Array
490−-------------------------------------------------------------------------------
491−
492−{-
493−
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.
496−
497−-- CAUTION! length must more than equal to lengths of all the arrays in the
498−-- stream.
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.
505− arr <- undefined
506− D.foldlM' MA.spliceUnsafe (return arr) buffered
507−
508−{-# INLINE _spliceArrays #-}
509−_spliceArrays :: (MonadIO m, Unbox a)
510− => Stream m (Array a) -> m (Array a)
511−_spliceArrays s = do
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.
516− arr <- undefined
517− final <- D.foldlM' writeArr (return arr) (toStream buffered)
518− return $ A.unsafeFreeze final
519−
520− where
521−
522− writeArr dst arr = MA.spliceUnsafe dst (A.unsafeThaw arr)
523−
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)
530− A.unsafeFreeze <$>
531− spliceArraysLenUnsafe len (fmap A.unsafeThaw (toStream buffered))
532−
533−-}
534−
535−{-# INLINE spliceArraysRealloced #-}
536−spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
537− => Stream m (Array a) -> m (Array a)
538−spliceArraysRealloced s = do
539− res <- D.uncons s
540− case res of
541− Just (a, strm) -> do
542− arr <-
543− D.foldlM'
544− MA.spliceExp
545− (pure (A.unsafeThaw a))
546− (fmap A.unsafeThaw strm)
547− liftIO $ A.unsafeFreeze <$> MA.rightSize arr
548− Nothing -> pure A.nil
549−
550−-- XXX This should just be "fold A.write"
551−--
552−-- | Given a stream of arrays, splice them all together to generate a single
553−-- array. The stream must be /finite/.
554−--
555−-- @since 0.7.0
556−{-# INLINE toArray #-}
557−toArray :: (MonadIO m, Unbox a) => Stream m (Array a) -> m (Array a)
558−toArray = spliceArraysRealloced
559−-- spliceArrays = _spliceArraysBuffered
560−
561−-------------------------------------------------------------------------------
562−-- Elimination - running element parsers
563−-------------------------------------------------------------------------------
564−
565497-- GHC parser does not accept {-# ANN type [] NoSpecConstr #-}, so we need
566498-- to make a newtype.
567499{-# ANN type List NoSpecConstr #-}