@@ -48,13 +48,15 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
48 , writeWith
48 , writeWith
51− -- , writeRevN
51+ , writeRevN
52 -- , writeRev
52 -- , writeRev
54 -- ** From containers
54 -- ** From containers
55 , fromForeignPtrUnsafe
55 , fromForeignPtrUnsafe
56 , fromListN
56 , fromListN
57 , fromList
57 , fromList
58 , fromStreamDN
60 , fromStreamDN
59 , fromStreamD
61 , fromStreamD
@@ -265,6 +267,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
265#ifdef USE_FOREIGN_PTR
267#ifdef USE_FOREIGN_PTR
266import qualified Streamly.Internal.Foreign.Malloc as Malloc
268import qualified Streamly.Internal.Foreign.Malloc as Malloc
270+import qualified Prelude
269import Prelude hiding
272import Prelude hiding
270 (length, foldr, read, unlines, splitAt, reverse, truncate)
273 (length, foldr, read, unlines, splitAt, reverse, truncate)
@@ -1901,6 +1904,43 @@ writeNWith alloc n = FL.take n (writeNWithUnsafe alloc n)
1901writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
1904writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
1902writeN = writeNWith newArray
1905writeN = writeNWith newArray
1907+-- | Like writeNWithUnsafe but writes the array in reverse order.
1910+{-# INLINE_NORMAL writeRevNWithUnsafe #-}
1911+writeRevNWithUnsafe :: forall m a. (MonadIO m, Storable a)
1912+ => (Int -> m (Array a)) -> Int -> Fold m a (Array a)
1913+writeRevNWithUnsafe alloc n = Fold step initial (return . fromArrayUnsafe)
1917+ toArrayUnsafeRev (Array contents _ _ bound) =
1918+ ArrayUnsafe contents bound bound
1920+ initial = FL.Partial . toArrayUnsafeRev <$> alloc (max n 0)
1922+ step (ArrayUnsafe contents start end) x = do
1923+ let ptr = PTR_PREV(start,a)
1924+ liftIO $ poke ptr x >> touch contents
1927+ $ ArrayUnsafe contents ptr end
1929+-- | Like writeNWith but writes the array in reverse order.
1932+{-# INLINE_NORMAL writeRevNWith #-}
1933+writeRevNWith :: forall m a. (MonadIO m, Storable a)
1934+ => (Int -> m (Array a)) -> Int -> Fold m a (Array a)
1935+writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
1937+-- | Like writeN but writes the array in reverse order.
1940+{-# INLINE_NORMAL writeRevN #-}
1941+writeRevN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
1942+writeRevN = writeRevNWith newArray
1904-- | @writeNAligned align n@ folds a maximum of @n@ elements from the input
1944-- | @writeNAligned align n@ folds a maximum of @n@ elements from the input
1905-- stream to an 'Array' aligned to the given size.
1945-- stream to an 'Array' aligned to the given size.
@@ -2052,6 +2092,13 @@ fromStreamDN limit str = do
2052fromListN :: (MonadIO m, Storable a) => Int -> [a] -> m (Array a)
2092fromListN :: (MonadIO m, Storable a) => Int -> [a] -> m (Array a)
2053fromListN n xs = fromStreamDN n $ D.fromList xs
2093fromListN n xs = fromStreamDN n $ D.fromList xs
2095+-- | Like fromListN but writes the array in reverse order.
2098+{-# INLINE fromListRevN #-}
2099+fromListRevN :: (MonadIO m, Storable a) => Int -> [a] -> m (Array a)
2100+fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
2055-------------------------------------------------------------------------------
2102-------------------------------------------------------------------------------
2056-- convert stream to a single array
2103-- convert stream to a single array
2057-------------------------------------------------------------------------------
2104-------------------------------------------------------------------------------
@@ -2094,6 +2141,14 @@ fromStreamD m = arrayStreamKFromStreamD m >>= fromArrayStreamK
2094fromList :: (MonadIO m, Storable a) => [a] -> m (Array a)
2141fromList :: (MonadIO m, Storable a) => [a] -> m (Array a)
2095fromList xs = fromStreamD $ D.fromList xs
2142fromList xs = fromStreamD $ D.fromList xs
2144+-- XXX We are materializing the whole list first for getting the length. Check
2145+-- if the 'fromList' like chunked implementation would fare better.
2147+-- | Like 'fromList' but writes the contents of the list in reverse order.
2148+{-# INLINE fromListRev #-}
2149+fromListRev :: (MonadIO m, Storable a) => [a] -> m (Array a)
2150+fromListRev xs = fromListRevN (Prelude.length xs) xs
2097-------------------------------------------------------------------------------
2152-------------------------------------------------------------------------------
2098-- Combining
2153-- Combining
2099-------------------------------------------------------------------------------
2154-------------------------------------------------------------------------------