on master help sign in
Rename Generic Mutable Array type to MutArray (#2300)
561b13bRanjeet Ranjan
core/src/Streamly/Data/Array/Generic/Mut.hs+1−1
@@ -11,7 +11,7 @@
11module Streamly.Data.Array.Generic.Mut
11module Streamly.Data.Array.Generic.Mut
12(
12(
13 -- * Type
13 -- * Type
14− Array
14+ MutArray
15
15
16 -- * Construction
16 -- * Construction
17 , writeN
17 , writeN
core/src/Streamly/Internal/Data/Array/Generic.hs+4−4
@@ -88,11 +88,11 @@ data Array a =
88 -- ^ The length of this slice.
88 -- ^ The length of this slice.
89 }
89 }
90
90
91−unsafeFreeze :: MArray.Array a -> Array a
91+unsafeFreeze :: MArray.MutArray a -> Array a
92−unsafeFreeze (MArray.Array cont# arrS arrL _) = Array cont# arrS arrL
92+unsafeFreeze (MArray.MutArray cont# arrS arrL _) = Array cont# arrS arrL
93
93
94−unsafeThaw :: Array a -> MArray.Array a
94+unsafeThaw :: Array a -> MArray.MutArray a
95−unsafeThaw (Array cont# arrS arrL) = MArray.Array cont# arrS arrL arrL
95+unsafeThaw (Array cont# arrS arrL) = MArray.MutArray cont# arrS arrL arrL
96
96
97{-# NOINLINE nil #-}
97{-# NOINLINE nil #-}
98nil :: Array a
98nil :: Array a
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+45−45
@@ -12,7 +12,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
12(
12(
13 -- * Type
13 -- * Type
14 -- $arrayNotes
14 -- $arrayNotes
15− Array (..)
15+ MutArray (..)
16
16
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
192
192
193-------------------------------------------------------------------------------
193-------------------------------------------------------------------------------
194−-- Array Data Type
194+-- MutArray Data Type
195-------------------------------------------------------------------------------
195-------------------------------------------------------------------------------
196
196
197−data Array a =
197+data MutArray a =
198− Array
198+ MutArray
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.
201
201
@@ -233,14 +233,14 @@ bottomElement =
233--
233--
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#) =
238 liftIO
238 liftIO
239 $ IO
239 $ IO
240 $ \s# ->
240 $ \s# ->
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 #)
245
245
246-------------------------------------------------------------------------------
246-------------------------------------------------------------------------------
@@ -252,8 +252,8 @@ new n@(I# n#) =
252--
252--
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 =
257 liftIO
257 liftIO
258 $ IO
258 $ IO
259 $ \s# ->
259 $ \s# ->
@@ -273,8 +273,8 @@ invalidIndex label i =
273--
273--
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.
286--
286--
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
290 liftIO
290 liftIO
291 $ IO
291 $ IO
292 $ \s# ->
292 $ \s# ->
@@ -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.
302--
302--
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.
318--
318--
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} #)
332
332
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
360--
360--
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}
368
368
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
384--
384--
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
407--
407--
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)
412
412
413-------------------------------------------------------------------------------
413-------------------------------------------------------------------------------
@@ -418,8 +418,8 @@ snoc = snocWith (* 2)
418--
418--
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 {..} =
423 liftIO
423 liftIO
424 $ IO
424 $ IO
425 $ \s# ->
425 $ \s# ->
@@ -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.
430--
430--
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}
460
460
@@ -467,9 +467,9 @@ getSliceUnsafe index len arr@Array {..} =
467getSlice
467getSlice
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{..} =
484--
484--
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)]
489
489
490-- | Use the 'read' unfold instead.
490-- | Use the 'read' unfold instead.
491--
491--
@@ -493,13 +493,13 @@ toList arr@Array{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
493--
493--
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)
499
499
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
503
503
504 where
504 where
505
505
@@ -522,7 +522,7 @@ toStreamK arr@Array{..} = K.unfoldrM step 0
522--
522--
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
527
527
528 where
528 where
@@ -538,7 +538,7 @@ writeNUnsafe n = Fold step initial return
538--
538--
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
543
543
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.
549--
549--
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
553
553
554 where
554 where
@@ -570,13 +570,13 @@ producerWith liftio = Producer step inject extract
570-- | Resumable unfold of an array.
570-- | Resumable unfold of an array.
571--
571--
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
575
575
576-- | Unfold an array into a stream.
576-- | Unfold an array into a stream.
577--
577--
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
581
581
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
600 , () #)
600 , () #)
601
601
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
core/src/Streamly/Internal/Data/Ring.hs+2−2
@@ -15,13 +15,13 @@ module Streamly.Internal.Data.Ring
15
15
16import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef, IORef)
16import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef, IORef)
17import Streamly.Internal.Data.Array.Generic.Mut.Type
17import Streamly.Internal.Data.Array.Generic.Mut.Type
18− ( Array(..)
18+ ( MutArray(..)
19 , new
19 , new
20 , putIndexUnsafe
20 , putIndexUnsafe
21 )
21 )
22
22
23data Ring a = Ring
23data Ring a = Ring
24− { arr :: Array a
24+ { arr :: MutArray a
25 , ringHead :: IORef Int -- current index to be over-written
25 , ringHead :: IORef Int -- current index to be over-written
26 , ringMax :: !Int -- first index beyond allocated memory
26 , ringMax :: !Int -- first index beyond allocated memory
27 }
27 }