@@ -12,7 +12,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
14 -- $arrayNotes
14 -- $arrayNotes
15− Array (..)
15+ MutArray (..)
17 -- * Constructing and Writing
17 -- * Constructing and Writing
18 -- ** Construction
18 -- ** Construction
@@ -191,11 +191,11 @@ import Prelude hiding (read)
191-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
191-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
193-------------------------------------------------------------------------------
193-------------------------------------------------------------------------------
194−-- Array Data Type
194+-- MutArray Data Type
195-------------------------------------------------------------------------------
195-------------------------------------------------------------------------------
197−data Array a =
197+data MutArray a =
199 { arrContents# :: MutableArray# RealWorld a
199 { arrContents# :: MutableArray# RealWorld a
200 -- ^ The internal contents of the array representing the entire array.
200 -- ^ The internal contents of the array representing the entire array.
@@ -233,14 +233,14 @@ bottomElement =
234-- /Pre-release/
234-- /Pre-release/
235{-# INLINE new #-}
235{-# INLINE new #-}
236−new :: forall m a. MonadIO m => Int -> m (Array a)
236+new :: forall m a. MonadIO m => Int -> m (MutArray a)
237new n@(I# n#) =
237new n@(I# n#) =
241 case newArray# n# bottomElement s# of
241 case newArray# n# bottomElement s# of
242 (# s1#, arr# #) ->
242 (# s1#, arr# #) ->
243− let ma = Array arr# 0 0 n
243+ let ma = MutArray arr# 0 0 n
244 in (# s1#, ma #)
244 in (# s1#, ma #)
246-------------------------------------------------------------------------------
246-------------------------------------------------------------------------------
@@ -252,8 +252,8 @@ new n@(I# n#) =
253-- /Pre-release/
253-- /Pre-release/
254{-# INLINE putIndexUnsafe #-}
254{-# INLINE putIndexUnsafe #-}
255−putIndexUnsafe :: forall m a. MonadIO m => Int -> Array a -> a -> m ()
255+putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m ()
256−putIndexUnsafe i Array {..} x =
256+putIndexUnsafe i MutArray {..} x =
@@ -273,8 +273,8 @@ invalidIndex label i =
274-- /Pre-release/
274-- /Pre-release/
275{-# INLINE putIndex #-}
275{-# INLINE putIndex #-}
276−putIndex :: MonadIO m => Int -> Array a -> a -> m ()
276+putIndex :: MonadIO m => Int -> MutArray a -> a -> m ()
277−putIndex i arr@Array {..} x =
277+putIndex i arr@MutArray {..} x =
278 if i >= 0 && i < arrLen
278 if i >= 0 && i < arrLen
279 then putIndexUnsafe i arr x
279 then putIndexUnsafe i arr x
280 else invalidIndex "putIndex" i
280 else invalidIndex "putIndex" i
@@ -285,8 +285,8 @@ putIndex i arr@Array {..} x =
285-- Unsafe because it does not check the bounds of the array.
285-- Unsafe because it does not check the bounds of the array.
287-- /Pre-release/
287-- /Pre-release/
288−modifyIndexUnsafe :: MonadIO m => Int -> Array a -> (a -> (a, b)) -> m b
288+modifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
289−modifyIndexUnsafe i Array {..} f = do
289+modifyIndexUnsafe i MutArray {..} f = do
@@ -301,8 +301,8 @@ modifyIndexUnsafe i Array {..} f = do
301-- | Modify a given index of an array using a modifier function.
301-- | Modify a given index of an array using a modifier function.
303-- /Pre-release/
303-- /Pre-release/
304−modifyIndex :: MonadIO m => Int -> Array a -> (a -> (a, b)) -> m b
304+modifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
305−modifyIndex i arr@Array {..} f = do
305+modifyIndex i arr@MutArray {..} f = do
306 if i >= 0 && i < arrLen
306 if i >= 0 && i < arrLen
307 then modifyIndexUnsafe i arr f
307 then modifyIndexUnsafe i arr f
308 else invalidIndex "modifyIndex" i
308 else invalidIndex "modifyIndex" i
@@ -316,7 +316,7 @@ modifyIndex i arr@Array {..} f = do
316-- the reallocated size is less than the original array it results in a
316-- the reallocated size is less than the original array it results in a
317-- truncated version of the original array.
317-- truncated version of the original array.
319−realloc :: MonadIO m => Int -> Array a -> m (Array a)
319+realloc :: MonadIO m => Int -> MutArray a -> m (MutArray a)
320realloc n arr = do
320realloc n arr = do
321 arr1 <- new n
321 arr1 <- new n
322 let !newLen@(I# newLen#) = min n (arrLen arr)
322 let !newLen@(I# newLen#) = min n (arrLen arr)
@@ -331,7 +331,7 @@ realloc n arr = do
331 in (# s1#, arr1 {arrLen = newLen, arrTrueLen = n} #)
331 in (# s1#, arr1 {arrLen = newLen, arrTrueLen = n} #)
333reallocWith ::
333reallocWith ::
334− MonadIO m => String -> (Int -> Int) -> Int -> Array a -> m (Array a)
334+ MonadIO m => String -> (Int -> Int) -> Int -> MutArray a -> m (MutArray a)
335reallocWith label sizer reqSize arr = do
335reallocWith label sizer reqSize arr = do
336 let oldSize = arrLen arr
336 let oldSize = arrLen arr
337 newSize = sizer oldSize
337 newSize = sizer oldSize
@@ -360,15 +360,15 @@ reallocWith label sizer reqSize arr = do
361-- /Internal/
361-- /Internal/
362{-# INLINE snocUnsafe #-}
362{-# INLINE snocUnsafe #-}
363−snocUnsafe :: MonadIO m => Array a -> a -> m (Array a)
363+snocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)
364−snocUnsafe arr@Array {..} a = do
364+snocUnsafe arr@MutArray {..} a = do
365 assert (arrStart + arrLen < arrTrueLen) (return ())
365 assert (arrStart + arrLen < arrTrueLen) (return ())
366 putIndexUnsafe arrLen arr a
366 putIndexUnsafe arrLen arr a
367 return $ arr {arrLen = arrLen + 1}
367 return $ arr {arrLen = arrLen + 1}
369-- NOINLINE to move it out of the way and not pollute the instruction cache.
369-- NOINLINE to move it out of the way and not pollute the instruction cache.
370{-# NOINLINE snocWithRealloc #-}
370{-# 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)
372snocWithRealloc sizer arr x = do
372snocWithRealloc sizer arr x = do
373 arr1 <- reallocWith "snocWithRealloc" sizer 1 arr
373 arr1 <- reallocWith "snocWithRealloc" sizer 1 arr
374 snocUnsafe arr1 x
374 snocUnsafe arr1 x
@@ -384,8 +384,8 @@ snocWithRealloc sizer arr x = do
385-- /Pre-release/
385-- /Pre-release/
386{-# INLINE snocWith #-}
386{-# INLINE snocWith #-}
387−snocWith :: MonadIO m => (Int -> Int) -> Array a -> a -> m (Array a)
387+snocWith :: MonadIO m => (Int -> Int) -> MutArray a -> a -> m (MutArray a)
388−snocWith sizer arr@Array {..} x = do
388+snocWith sizer arr@MutArray {..} x = do
389 if arrStart + arrLen < arrTrueLen
389 if arrStart + arrLen < arrTrueLen
390 then snocUnsafe arr x
390 then snocUnsafe arr x
391 else snocWithRealloc sizer arr x
391 else snocWithRealloc sizer arr x
@@ -407,7 +407,7 @@ snocWith sizer arr@Array {..} x = do
408-- /Pre-release/
408-- /Pre-release/
409{-# INLINE snoc #-}
409{-# INLINE snoc #-}
410−snoc :: MonadIO m => Array a -> a -> m (Array a)
410+snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
411snoc = snocWith (* 2)
411snoc = snocWith (* 2)
413-------------------------------------------------------------------------------
413-------------------------------------------------------------------------------
@@ -418,8 +418,8 @@ snoc = snocWith (* 2)
419-- Unsafe because it does not check the bounds of the array.
419-- Unsafe because it does not check the bounds of the array.
420{-# INLINE_NORMAL getIndexUnsafe #-}
420{-# INLINE_NORMAL getIndexUnsafe #-}
421−getIndexUnsafe :: MonadIO m => Int -> Array a -> m a
421+getIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a
422−getIndexUnsafe n Array {..} =
422+getIndexUnsafe n MutArray {..} =
@@ -429,8 +429,8 @@ getIndexUnsafe n Array {..} =
429-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
429-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
431{-# INLINE getIndex #-}
431{-# INLINE getIndex #-}
432−getIndex :: MonadIO m => Int -> Array a -> m a
432+getIndex :: MonadIO m => Int -> MutArray a -> m a
433−getIndex i arr@Array {..} =
433+getIndex i arr@MutArray {..} =
434 if i >= 0 && i < arrLen
434 if i >= 0 && i < arrLen
435 then getIndexUnsafe i arr
435 then getIndexUnsafe i arr
436 else invalidIndex "getIndex" i
436 else invalidIndex "getIndex" i
@@ -452,9 +452,9 @@ getIndex i arr@Array {..} =
452getSliceUnsafe
452getSliceUnsafe
453 :: Int -- ^ from index
453 :: Int -- ^ from index
454 -> Int -- ^ length of the slice
454 -> Int -- ^ length of the slice
455− -> Array a
455+ -> MutArray a
456− -> Array a
456+ -> MutArray a
457−getSliceUnsafe index len arr@Array {..} =
457+getSliceUnsafe index len arr@MutArray {..} =
458 assert (index >= 0 && len >= 0 && index + len <= arrLen)
458 assert (index >= 0 && len >= 0 && index + len <= arrLen)
459 $ arr {arrStart = arrStart + index, arrLen = len}
459 $ arr {arrStart = arrStart + index, arrLen = len}
@@ -467,9 +467,9 @@ getSliceUnsafe index len arr@Array {..} =
468 :: Int -- ^ from index
468 :: Int -- ^ from index
469 -> Int -- ^ length of the slice
469 -> Int -- ^ length of the slice
470− -> Array a
470+ -> MutArray a
471− -> Array a
471+ -> MutArray a
472−getSlice index len arr@Array{..} =
472+getSlice index len arr@MutArray{..} =
473 if index >= 0 && len >= 0 && index + len <= arrLen
473 if index >= 0 && len >= 0 && index + len <= arrLen
474 then arr {arrStart = arrStart + index, arrLen = len}
474 then arr {arrStart = arrStart + index, arrLen = len}
475 else error
475 else error
@@ -484,8 +484,8 @@ getSlice index len arr@Array{..} =
485-- /Pre-release/
485-- /Pre-release/
486{-# INLINE toList #-}
486{-# INLINE toList #-}
487−toList :: MonadIO m => Array a -> m [a]
487+toList :: MonadIO m => MutArray a -> m [a]
488−toList arr@Array{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
488+toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
490-- | Use the 'read' unfold instead.
490-- | Use the 'read' unfold instead.
@@ -493,13 +493,13 @@ toList arr@Array{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
494-- We can try this if the unfold has any performance issues.
494-- We can try this if the unfold has any performance issues.
495{-# INLINE_NORMAL toStreamD #-}
495{-# INLINE_NORMAL toStreamD #-}
496−toStreamD :: MonadIO m => Array a -> D.Stream m a
496+toStreamD :: MonadIO m => MutArray a -> D.Stream m a
497−toStreamD arr@Array{..} =
497+toStreamD arr@MutArray{..} =
498 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
498 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
500{-# INLINE toStreamK #-}
500{-# INLINE toStreamK #-}
501−toStreamK :: MonadIO m => Array a -> K.StreamK m a
501+toStreamK :: MonadIO m => MutArray a -> K.StreamK m a
502−toStreamK arr@Array{..} = K.unfoldrM step 0
502+toStreamK arr@MutArray{..} = K.unfoldrM step 0
@@ -522,7 +522,7 @@ toStreamK arr@Array{..} = K.unfoldrM step 0
523-- /Pre-release/
523-- /Pre-release/
524{-# INLINE_NORMAL writeNUnsafe #-}
524{-# INLINE_NORMAL writeNUnsafe #-}
525−writeNUnsafe :: MonadIO m => Int -> Fold m a (Array a)
525+writeNUnsafe :: MonadIO m => Int -> Fold m a (MutArray a)
526writeNUnsafe n = Fold step initial return
526writeNUnsafe n = Fold step initial return
@@ -538,7 +538,7 @@ writeNUnsafe n = Fold step initial return
539-- /Pre-release/
539-- /Pre-release/
540{-# INLINE_NORMAL writeN #-}
540{-# INLINE_NORMAL writeN #-}
541−writeN :: MonadIO m => Int -> Fold m a (Array a)
541+writeN :: MonadIO m => Int -> Fold m a (MutArray a)
542writeN n = FL.take n $ writeNUnsafe n
542writeN n = FL.take n $ writeNUnsafe n
544-------------------------------------------------------------------------------
544-------------------------------------------------------------------------------
@@ -548,7 +548,7 @@ writeN n = FL.take n $ writeNUnsafe n
548-- | Resumable unfold of an array.
548-- | Resumable unfold of an array.
550{-# INLINE_NORMAL producerWith #-}
550{-# 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
552producerWith liftio = Producer step inject extract
552producerWith liftio = Producer step inject extract
@@ -570,13 +570,13 @@ producerWith liftio = Producer step inject extract
570-- | Resumable unfold of an array.
570-- | Resumable unfold of an array.
572{-# INLINE_NORMAL producer #-}
572{-# INLINE_NORMAL producer #-}
573−producer :: MonadIO m => Producer m (Array a) a
573+producer :: MonadIO m => Producer m (MutArray a) a
574producer = producerWith liftIO
574producer = producerWith liftIO
576-- | Unfold an array into a stream.
576-- | Unfold an array into a stream.
578{-# INLINE_NORMAL reader #-}
578{-# INLINE_NORMAL reader #-}
579−reader :: MonadIO m => Unfold m (Array a) a
579+reader :: MonadIO m => Unfold m (MutArray a) a
580reader = Producer.simplify producer
580reader = Producer.simplify producer
582--------------------------------------------------------------------------------
582--------------------------------------------------------------------------------
@@ -586,7 +586,7 @@ reader = Producer.simplify producer
586-- | Put a sub range of a source array into a subrange of a destination array.
586-- | 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.
587-- This is not safe as it does not check the bounds.
588{-# INLINE putSliceUnsafe #-}
588{-# 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 ()
590putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
590putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
591 assertM(len <= arrLen dst)
591 assertM(len <= arrLen dst)
592 assertM(len <= arrLen src)
592 assertM(len <= arrLen src)
@@ -600,7 +600,7 @@ putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
602{-# INLINE clone #-}
602{-# INLINE clone #-}
603−clone :: MonadIO m => Array a -> m (Array a)
603+clone :: MonadIO m => MutArray a -> m (MutArray a)
604clone src = liftIO $ do
604clone src = liftIO $ do
605 let len = arrLen src
605 let len = arrLen src
606 dst <- new len
606 dst <- new len