@@ -61,6 +61,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
62 -- ** Reallocation
62 -- ** Reallocation
65 -- ** Appending elements
66 -- ** Appending elements
66 , snocWith
67 , snocWith
@@ -229,7 +230,9 @@ bottomElement =
230-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
231-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
231-- that we do not have to write undefined or error in the whole array.
232-- 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.
234+-- | @new count@ allocates a zero length array that can be extended to hold
235+-- up to 'count' items without reallocating.
234-- /Pre-release/
237-- /Pre-release/
235{-# INLINE new #-}
238{-# INLINE new #-}
@@ -254,13 +257,14 @@ new n@(I# n#) =
254{-# INLINE putIndexUnsafe #-}
257{-# INLINE putIndexUnsafe #-}
255putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m ()
258putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m ()
256putIndexUnsafe i MutArray {..} x =
259putIndexUnsafe i MutArray {..} x =
260+ assert (i >= 0 && i < arrLen)
260 case i + arrStart of
264 case i + arrStart of
262 let s1# = writeArray# arrContents# n# x s#
266 let s1# = writeArray# arrContents# n# x s#
263− in (# s1#, () #)
267+ in (# s1#, () #))
265invalidIndex :: String -> Int -> a
269invalidIndex :: String -> Int -> a
266invalidIndex label i =
270invalidIndex label i =
@@ -410,6 +414,17 @@ snocWith sizer arr@MutArray {..} x = do
410snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
414snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
411snoc = snocWith (* 2)
415snoc = snocWith (* 2)
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
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
413-------------------------------------------------------------------------------
428-------------------------------------------------------------------------------
414-- Random reads
429-- Random reads
415-------------------------------------------------------------------------------
430-------------------------------------------------------------------------------
@@ -586,7 +601,8 @@ reader = Producer.simplify producer
586-- | Put a sub range of a source array into a subrange of a destination array.
601-- | Put a sub range of a source array into a subrange of a destination array.
587-- This is not safe as it does not check the bounds.
602-- This is not safe as it does not check the bounds.
588{-# INLINE putSliceUnsafe #-}
603{-# INLINE putSliceUnsafe #-}
604+putSliceUnsafe :: MonadIO m =>
589−putSliceUnsafe :: MonadIO m => MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
605+ MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
590putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
606putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
591 assertM(len <= arrLen dst)
607 assertM(len <= arrLen dst)
592 assertM(len <= arrLen src)
608 assertM(len <= arrLen src)