@@ -61,6 +61,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
6161
6262 -- ** Reallocation
6363 , realloc
64+ , uninit
6465
6566 -- ** Appending elements
6667 , snocWith
@@ -229,7 +230,9 @@ bottomElement =
229230
230231-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
231232-- that we do not have to write undefined or error in the whole array.
232−-- | @new count@ allocates an empty array that can hold 'count' items.
233+
234+-- | @new count@ allocates a zero length array that can be extended to hold
235+-- up to 'count' items without reallocating.
233236--
234237-- /Pre-release/
235238{-# INLINE new #-}
@@ -254,13 +257,14 @@ new n@(I# n#) =
254257{-# INLINE putIndexUnsafe #-}
255258putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m ()
256259putIndexUnsafe i MutArray {..} x =
257− liftIO
260+ assert (i >= 0 && i < arrLen)
261+ (liftIO
258262 $ IO
259263 $ \s# ->
260264 case i + arrStart of
261265 I# n# ->
262266 let s1# = writeArray# arrContents# n# x s#
263− in (# s1#, () #)
267+ in (# s1#, () #))
264268
265269invalidIndex :: String -> Int -> a
266270invalidIndex label i =
@@ -410,6 +414,17 @@ snocWith sizer arr@MutArray {..} x = do
410414snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
411415snoc = snocWith (* 2)
412416
417+-- | Make the uninitialized memory in the array available for use extending it
418+-- by the supplied length beyond the current length of the array. The array may
419+-- be reallocated.
420+--
421+{-# INLINE uninit #-}
422+uninit :: MonadIO m => MutArray a -> Int -> m (MutArray a)
423+uninit arr@MutArray{..} len =
424+ if arrStart + arrLen + len <= arrTrueLen
425+ then return $ arr {arrLen = arrLen + len}
426+ else realloc (arrLen + len) arr
427+
413428-------------------------------------------------------------------------------
414429-- Random reads
415430-------------------------------------------------------------------------------
@@ -586,7 +601,8 @@ reader = Producer.simplify producer
586601-- | Put a sub range of a source array into a subrange of a destination array.
587602-- This is not safe as it does not check the bounds.
588603{-# INLINE putSliceUnsafe #-}
589−putSliceUnsafe :: MonadIO m => MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
604+putSliceUnsafe :: MonadIO m =>
605+ MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
590606putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
591607 assertM(len <= arrLen dst)
592608 assertM(len <= arrLen src)