@@ -12,7 +12,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
1212(
1313 -- * Type
1414 -- $arrayNotes
15− Array (..)
15+ MutArray (..)
1616
1717 -- * Constructing and Writing
1818 -- ** Construction
@@ -191,11 +191,11 @@ import Prelude hiding (read)
191191-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
192192
193193-------------------------------------------------------------------------------
194−-- Array Data Type
194+-- MutArray Data Type
195195-------------------------------------------------------------------------------
196196
197−data Array a =
198− Array
197+data MutArray a =
198+ MutArray
199199 { arrContents# :: MutableArray# RealWorld a
200200 -- ^ The internal contents of the array representing the entire array.
201201
@@ -233,14 +233,14 @@ bottomElement =
233233--
234234-- /Pre-release/
235235{-# INLINE new #-}
236−new :: forall m a. MonadIO m => Int -> m (Array a)
236+new :: forall m a. MonadIO m => Int -> m (MutArray a)
237237new n@(I# n#) =
238238 liftIO
239239 $ IO
240240 $ \s# ->
241241 case newArray# n# bottomElement s# of
242242 (# s1#, arr# #) ->
243− let ma = Array arr# 0 0 n
243+ let ma = MutArray arr# 0 0 n
244244 in (# s1#, ma #)
245245
246246-------------------------------------------------------------------------------
@@ -252,8 +252,8 @@ new n@(I# n#) =
252252--
253253-- /Pre-release/
254254{-# INLINE putIndexUnsafe #-}
255−putIndexUnsafe :: forall m a. MonadIO m => Int -> Array a -> a -> m ()
256−putIndexUnsafe i Array {..} x =
255+putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m ()
256+putIndexUnsafe i MutArray {..} x =
257257 liftIO
258258 $ IO
259259 $ \s# ->
@@ -273,8 +273,8 @@ invalidIndex label i =
273273--
274274-- /Pre-release/
275275{-# INLINE putIndex #-}
276−putIndex :: MonadIO m => Int -> Array a -> a -> m ()
277−putIndex i arr@Array {..} x =
276+putIndex :: MonadIO m => Int -> MutArray a -> a -> m ()
277+putIndex i arr@MutArray {..} x =
278278 if i >= 0 && i < arrLen
279279 then putIndexUnsafe i arr x
280280 else invalidIndex "putIndex" i
@@ -285,8 +285,8 @@ putIndex i arr@Array {..} x =
285285-- Unsafe because it does not check the bounds of the array.
286286--
287287-- /Pre-release/
288−modifyIndexUnsafe :: MonadIO m => Int -> Array a -> (a -> (a, b)) -> m b
289−modifyIndexUnsafe i Array {..} f = do
288+modifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
289+modifyIndexUnsafe i MutArray {..} f = do
290290 liftIO
291291 $ IO
292292 $ \s# ->
@@ -301,8 +301,8 @@ modifyIndexUnsafe i Array {..} f = do
301301-- | Modify a given index of an array using a modifier function.
302302--
303303-- /Pre-release/
304−modifyIndex :: MonadIO m => Int -> Array a -> (a -> (a, b)) -> m b
305−modifyIndex i arr@Array {..} f = do
304+modifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
305+modifyIndex i arr@MutArray {..} f = do
306306 if i >= 0 && i < arrLen
307307 then modifyIndexUnsafe i arr f
308308 else invalidIndex "modifyIndex" i
@@ -316,7 +316,7 @@ modifyIndex i arr@Array {..} f = do
316316-- the reallocated size is less than the original array it results in a
317317-- truncated version of the original array.
318318--
319−realloc :: MonadIO m => Int -> Array a -> m (Array a)
319+realloc :: MonadIO m => Int -> MutArray a -> m (MutArray a)
320320realloc n arr = do
321321 arr1 <- new n
322322 let !newLen@(I# newLen#) = min n (arrLen arr)
@@ -331,7 +331,7 @@ realloc n arr = do
331331 in (# s1#, arr1 {arrLen = newLen, arrTrueLen = n} #)
332332
333333reallocWith ::
334− MonadIO m => String -> (Int -> Int) -> Int -> Array a -> m (Array a)
334+ MonadIO m => String -> (Int -> Int) -> Int -> MutArray a -> m (MutArray a)
335335reallocWith label sizer reqSize arr = do
336336 let oldSize = arrLen arr
337337 newSize = sizer oldSize
@@ -360,15 +360,15 @@ reallocWith label sizer reqSize arr = do
360360--
361361-- /Internal/
362362{-# INLINE snocUnsafe #-}
363−snocUnsafe :: MonadIO m => Array a -> a -> m (Array a)
364−snocUnsafe arr@Array {..} a = do
363+snocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)
364+snocUnsafe arr@MutArray {..} a = do
365365 assert (arrStart + arrLen < arrTrueLen) (return ())
366366 putIndexUnsafe arrLen arr a
367367 return $ arr {arrLen = arrLen + 1}
368368
369369-- NOINLINE to move it out of the way and not pollute the instruction cache.
370370{-# NOINLINE snocWithRealloc #-}
371−snocWithRealloc :: MonadIO m => (Int -> Int) -> Array a -> a -> m (Array a)
371+snocWithRealloc :: MonadIO m => (Int -> Int) -> MutArray a -> a -> m (MutArray a)
372372snocWithRealloc sizer arr x = do
373373 arr1 <- reallocWith "snocWithRealloc" sizer 1 arr
374374 snocUnsafe arr1 x
@@ -384,8 +384,8 @@ snocWithRealloc sizer arr x = do
384384--
385385-- /Pre-release/
386386{-# INLINE snocWith #-}
387−snocWith :: MonadIO m => (Int -> Int) -> Array a -> a -> m (Array a)
388−snocWith sizer arr@Array {..} x = do
387+snocWith :: MonadIO m => (Int -> Int) -> MutArray a -> a -> m (MutArray a)
388+snocWith sizer arr@MutArray {..} x = do
389389 if arrStart + arrLen < arrTrueLen
390390 then snocUnsafe arr x
391391 else snocWithRealloc sizer arr x
@@ -407,7 +407,7 @@ snocWith sizer arr@Array {..} x = do
407407--
408408-- /Pre-release/
409409{-# INLINE snoc #-}
410−snoc :: MonadIO m => Array a -> a -> m (Array a)
410+snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
411411snoc = snocWith (* 2)
412412
413413-------------------------------------------------------------------------------
@@ -418,8 +418,8 @@ snoc = snocWith (* 2)
418418--
419419-- Unsafe because it does not check the bounds of the array.
420420{-# INLINE_NORMAL getIndexUnsafe #-}
421−getIndexUnsafe :: MonadIO m => Int -> Array a -> m a
422−getIndexUnsafe n Array {..} =
421+getIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a
422+getIndexUnsafe n MutArray {..} =
423423 liftIO
424424 $ IO
425425 $ \s# ->
@@ -429,8 +429,8 @@ getIndexUnsafe n Array {..} =
429429-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
430430--
431431{-# INLINE getIndex #-}
432−getIndex :: MonadIO m => Int -> Array a -> m a
433−getIndex i arr@Array {..} =
432+getIndex :: MonadIO m => Int -> MutArray a -> m a
433+getIndex i arr@MutArray {..} =
434434 if i >= 0 && i < arrLen
435435 then getIndexUnsafe i arr
436436 else invalidIndex "getIndex" i
@@ -452,9 +452,9 @@ getIndex i arr@Array {..} =
452452getSliceUnsafe
453453 :: Int -- ^ from index
454454 -> Int -- ^ length of the slice
455− -> Array a
456− -> Array a
457−getSliceUnsafe index len arr@Array {..} =
455+ -> MutArray a
456+ -> MutArray a
457+getSliceUnsafe index len arr@MutArray {..} =
458458 assert (index >= 0 && len >= 0 && index + len <= arrLen)
459459 $ arr {arrStart = arrStart + index, arrLen = len}
460460
@@ -467,9 +467,9 @@ getSliceUnsafe index len arr@Array {..} =
467467getSlice
468468 :: Int -- ^ from index
469469 -> Int -- ^ length of the slice
470− -> Array a
471− -> Array a
472−getSlice index len arr@Array{..} =
470+ -> MutArray a
471+ -> MutArray a
472+getSlice index len arr@MutArray{..} =
473473 if index >= 0 && len >= 0 && index + len <= arrLen
474474 then arr {arrStart = arrStart + index, arrLen = len}
475475 else error
@@ -484,8 +484,8 @@ getSlice index len arr@Array{..} =
484484--
485485-- /Pre-release/
486486{-# INLINE toList #-}
487−toList :: MonadIO m => Array a -> m [a]
488−toList arr@Array{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
487+toList :: MonadIO m => MutArray a -> m [a]
488+toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
489489
490490-- | Use the 'read' unfold instead.
491491--
@@ -493,13 +493,13 @@ toList arr@Array{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
493493--
494494-- We can try this if the unfold has any performance issues.
495495{-# INLINE_NORMAL toStreamD #-}
496−toStreamD :: MonadIO m => Array a -> D.Stream m a
497−toStreamD arr@Array{..} =
496+toStreamD :: MonadIO m => MutArray a -> D.Stream m a
497+toStreamD arr@MutArray{..} =
498498 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
499499
500500{-# INLINE toStreamK #-}
501−toStreamK :: MonadIO m => Array a -> K.StreamK m a
502−toStreamK arr@Array{..} = K.unfoldrM step 0
501+toStreamK :: MonadIO m => MutArray a -> K.StreamK m a
502+toStreamK arr@MutArray{..} = K.unfoldrM step 0
503503
504504 where
505505
@@ -522,7 +522,7 @@ toStreamK arr@Array{..} = K.unfoldrM step 0
522522--
523523-- /Pre-release/
524524{-# INLINE_NORMAL writeNUnsafe #-}
525−writeNUnsafe :: MonadIO m => Int -> Fold m a (Array a)
525+writeNUnsafe :: MonadIO m => Int -> Fold m a (MutArray a)
526526writeNUnsafe n = Fold step initial return
527527
528528 where
@@ -538,7 +538,7 @@ writeNUnsafe n = Fold step initial return
538538--
539539-- /Pre-release/
540540{-# INLINE_NORMAL writeN #-}
541−writeN :: MonadIO m => Int -> Fold m a (Array a)
541+writeN :: MonadIO m => Int -> Fold m a (MutArray a)
542542writeN n = FL.take n $ writeNUnsafe n
543543
544544-------------------------------------------------------------------------------
@@ -548,7 +548,7 @@ writeN n = FL.take n $ writeNUnsafe n
548548-- | Resumable unfold of an array.
549549--
550550{-# INLINE_NORMAL producerWith #-}
551−producerWith :: Monad m => (forall b. IO b -> m b) -> Producer m (Array a) a
551+producerWith :: Monad m => (forall b. IO b -> m b) -> Producer m (MutArray a) a
552552producerWith liftio = Producer step inject extract
553553
554554 where
@@ -570,13 +570,13 @@ producerWith liftio = Producer step inject extract
570570-- | Resumable unfold of an array.
571571--
572572{-# INLINE_NORMAL producer #-}
573−producer :: MonadIO m => Producer m (Array a) a
573+producer :: MonadIO m => Producer m (MutArray a) a
574574producer = producerWith liftIO
575575
576576-- | Unfold an array into a stream.
577577--
578578{-# INLINE_NORMAL reader #-}
579−reader :: MonadIO m => Unfold m (Array a) a
579+reader :: MonadIO m => Unfold m (MutArray a) a
580580reader = Producer.simplify producer
581581
582582--------------------------------------------------------------------------------
@@ -586,7 +586,7 @@ reader = Producer.simplify producer
586586-- | Put a sub range of a source array into a subrange of a destination array.
587587-- This is not safe as it does not check the bounds.
588588{-# INLINE putSliceUnsafe #-}
589−putSliceUnsafe :: MonadIO m => Array a -> Int -> Array a -> Int -> Int -> m ()
589+putSliceUnsafe :: MonadIO m => MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
590590putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
591591 assertM(len <= arrLen dst)
592592 assertM(len <= arrLen src)
@@ -600,7 +600,7 @@ putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
600600 , () #)
601601
602602{-# INLINE clone #-}
603−clone :: MonadIO m => Array a -> m (Array a)
603+clone :: MonadIO m => MutArray a -> m (MutArray a)
604604clone src = liftIO $ do
605605 let len = arrLen src
606606 dst <- new len