on master help sign in
Move the putIndices API to the mutable array module
commit0d157ccfa5a4ca7f1c5ea663be14fb1889c454b7 authorHarendra Kumar <harendra@composewell.com> dateFri 10 Mar 2023, 11:49 (4 years ago) parentaad6d62
2 files changed+17−21
core/src/Streamly/Internal/Data/Array/Generic.hs−18 core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+17−3
core/src/Streamly/Internal/Data/Array/Generic.hs−18
@@ -42,7 +42,6 @@ module Streamly.Internal.Data.Array.Generic
4242 -- * Random Access
4343 , getIndexUnsafe
4444 , strip
45− , putIndices
4645 )
4746where
4847
@@ -309,23 +308,6 @@ strip p arr =
309308 then getIndexL (idx + 1)
310309 else idx
311310
312−-- | Write an input stream of (index, value) pairs to an array. Throws an
313−-- error if any index is out of bounds.
314−--
315−-- /Pre-release/
316−{-# INLINE putIndices #-}
317−putIndices :: MonadIO m
318− => Array a -> Fold m (Int, a) ()
319−putIndices arr = FL.rmapM (\ _ -> return ()) (FL.foldlM' step initial)
320−
321− where
322−
323− initial = return $ unsafeThaw arr
324−
325− step marr (i, x) = do
326− MArray.putIndexUnsafe i marr x
327− return marr
328−
329311-------------------------------------------------------------------------------
330312-- Instances
331313-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+17−3
@@ -41,7 +41,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
4141 -- * Random writes
4242 , putIndex
4343 , putIndexUnsafe
44− -- , putIndices
44+ , putIndices
4545 -- , putFromThenTo
4646 -- , putFrom -- start writing at the given position
4747 -- , putUpto -- write from beginning up to the given position
@@ -283,6 +283,19 @@ putIndex i arr@MutArray {..} x =
283283 then putIndexUnsafe i arr x
284284 else invalidIndex "putIndex" i
285285
286+-- | Write an input stream of (index, value) pairs to an array. Throws an
287+-- error if any index is out of bounds.
288+--
289+-- /Pre-release/
290+{-# INLINE putIndices #-}
291+putIndices :: MonadIO m
292+ => MutArray a -> Fold m (Int, a) ()
293+putIndices arr = FL.foldlM' step (return ())
294+
295+ where
296+
297+ step () (i, x) = liftIO (putIndex i arr x)
298+
286299-- | Modify a given index of an array using a modifier function without checking
287300-- the bounds.
288301--
@@ -367,8 +380,9 @@ reallocWith label sizer reqSize arr = do
367380snocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)
368381snocUnsafe arr@MutArray {..} a = do
369382 assert (arrStart + arrLen < arrTrueLen) (return ())
370− putIndexUnsafe arrLen arr a
371− return $ arr {arrLen = arrLen + 1}
383+ let arr1 = arr {arrLen = arrLen + 1}
384+ putIndexUnsafe arrLen arr1 a
385+ return arr1
372386
373387-- NOINLINE to move it out of the way and not pollute the instruction cache.
374388{-# NOINLINE snocWithRealloc #-}