on master help sign in
Rename mutable array type to MutArray (#2272)
commit34b2b4987629420cb3140e0b32a2b46daf9e5981 authorRanjeet Ranjan <ranjeetr@composewell.com> dateThu 2 Mar 2023, 09:40 (4 years ago) parent88c041b
12 files changed+269−265
benchmark/Streamly/Benchmark/Data/Array/Mut.hs+4−3 benchmark/Streamly/Benchmark/Data/Fold.hs+2−2 core/src/Streamly/Data/Array/Mut.hs+1−1 core/src/Streamly/Internal/Data/Array/Mut.hs+4−4 core/src/Streamly/Internal/Data/Array/Mut/Stream.hs+10−10 core/src/Streamly/Internal/Data/Array/Mut/Type.hs+225−225 core/src/Streamly/Internal/Data/Array/Type.hs+7−7 core/src/Streamly/Internal/Data/Fold.hs+5−4 core/src/Streamly/Internal/Data/Ring/Unboxed.hs+5−5 core/src/Streamly/Internal/Data/Stream/Chunked.hs+2−1 src/Streamly/Internal/Network/Socket.hs+1−1 test/Streamly/Test/Data/Array.hs+3−2
benchmark/Streamly/Benchmark/Data/Array/Mut.hs+4−3
@@ -42,6 +42,7 @@ import Prelude
4242 , id
4343 , undefined
4444 )
45+import Streamly.Internal.Data.Array.Mut (MutArray)
4546
4647import qualified Streamly.Internal.Data.Array as Array
4748import qualified Streamly.Internal.Data.Array.Mut as MArray
@@ -51,9 +52,9 @@ import qualified Streamly.Internal.Data.Stream.StreamD as Stream
5152import Gauge
5253import Streamly.Benchmark.Common hiding (benchPureSrc)
5354
54−type Stream = MArray.Array
55+type Stream = MutArray
5556
56−instance NFData (MArray.Array a) where
57+instance NFData (MutArray a) where
5758 {-# INLINE rnf #-}
5859 rnf _ = ()
5960
@@ -249,7 +250,7 @@ o_1_space_transformationX4 value =
249250 ]
250251
251252o_1_space_serial_marray ::
252− Int -> (MArray.Array Int, Array.Array Int) -> [Benchmark]
253+ Int -> (MutArray Int, Array.Array Int) -> [Benchmark]
253254o_1_space_serial_marray value ~(array, indices) =
254255 [ benchIO' "partitionBy (< 0)" (const (return array))
255256 $ MArray.partitionBy (< 0)
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2
@@ -28,8 +28,8 @@ import System.Random (randomRIO)
2828import Streamly.Internal.Data.Stream.StreamD (Stream)
2929import Streamly.Internal.Data.Fold (Fold(..))
3030import Streamly.Internal.Data.IsMap.HashMap ()
31+import Streamly.Internal.Data.Array.Mut (MutArray)
3132
32−import qualified Streamly.Internal.Data.Array.Mut.Type as MArray
3333import qualified Streamly.Internal.Data.Fold as FL
3434import qualified Streamly.Internal.Data.Fold.Container as FL
3535import qualified Streamly.Internal.Data.Unfold as Unfold
@@ -291,7 +291,7 @@ unfoldMany val =
291291moduleName :: String
292292moduleName = "Data.Fold"
293293
294−instance NFData (MArray.Array a) where
294+instance NFData (MutArray a) where
295295 {-# INLINE rnf #-}
296296 rnf _ = ()
297297
core/src/Streamly/Data/Array/Mut.hs+1−1
@@ -14,7 +14,7 @@
1414module Streamly.Data.Array.Mut
1515 (
1616 -- * Types
17− Array
17+ MutArray
1818
1919 -- * Construction
2020
core/src/Streamly/Internal/Data/Array/Mut.hs+4−4
@@ -49,7 +49,7 @@ import Streamly.Internal.Data.Array.Mut.Type
4949-- /Pre-release/
5050{-# INLINE splitOn #-}
5151splitOn :: (MonadIO m, Unbox a) =>
52− (a -> Bool) -> Array a -> Stream m (Array a)
52+ (a -> Bool) -> MutArray a -> Stream m (MutArray a)
5353splitOn predicate arr =
5454 fmap (\(i, len) -> getSliceUnsafe i len arr)
5555 $ D.sliceOnSuffix predicate (toStreamD arr)
@@ -63,7 +63,7 @@ splitOn predicate arr =
6363genSlicesFromLen :: forall m a. (Monad m, Unbox a)
6464 => Int -- ^ from index
6565 -> Int -- ^ length of the slice
66− -> Unfold m (Array a) (Int, Int)
66+ -> Unfold m (MutArray a) (Int, Int)
6767genSlicesFromLen from len =
6868 let fromThenTo n = (from, from + len, n - 1)
6969 mkSlice n i = return (i, min len (n - i))
@@ -80,7 +80,7 @@ genSlicesFromLen from len =
8080getSlicesFromLen :: forall m a. (Monad m, Unbox a)
8181 => Int -- ^ from index
8282 -> Int -- ^ length of the slice
83− -> Unfold m (Array a) (Array a)
83+ -> Unfold m (MutArray a) (MutArray a)
8484getSlicesFromLen from len =
8585 let mkSlice arr (i, n) = return $ getSliceUnsafe i n arr
8686 in Unfold.mapM2 mkSlice (genSlicesFromLen from len)
@@ -95,6 +95,6 @@ getSlicesFromLen from len =
9595--
9696-- /Pre-release/
9797{-# INLINE fromStream #-}
98−fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (Array a)
98+fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
9999fromStream = fromStreamD
100100-- fromStream (Stream m) = P.fold write m
core/src/Streamly/Internal/Data/Array/Mut/Stream.hs+10−10
@@ -32,7 +32,7 @@ import Control.Monad (when)
3232import Data.Bifunctor (first)
3333import Data.Proxy (Proxy(..))
3434import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
35−import Streamly.Internal.Data.Array.Mut.Type (Array(..))
35+import Streamly.Internal.Data.Array.Mut.Type (MutArray(..))
3636import Streamly.Internal.Data.Fold.Type (Fold(..))
3737import Streamly.Internal.Data.Parser (ParseError)
3838import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
@@ -53,7 +53,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
5353-- /Pre-release/
5454{-# INLINE arraysOf #-}
5555arraysOf :: (MonadIO m, Unbox a)
56− => Int -> Stream m a -> Stream m (Array a)
56+ => Int -> Stream m a -> Stream m (MutArray a)
5757arraysOf = MArray.arraysOf
5858
5959-------------------------------------------------------------------------------
@@ -82,7 +82,7 @@ data SpliceState s arr
8282-- @since 0.7.0
8383{-# INLINE_NORMAL packArraysChunksOf #-}
8484packArraysChunksOf :: (MonadIO m, Unbox a)
85− => Int -> D.Stream m (Array a) -> D.Stream m (Array a)
85+ => Int -> D.Stream m (MutArray a) -> D.Stream m (MutArray a)
8686packArraysChunksOf n (D.Stream step state) =
8787 D.Stream step' (SpliceInitial state)
8888
@@ -130,7 +130,7 @@ packArraysChunksOf n (D.Stream step state) =
130130--
131131{-# INLINE_NORMAL lpackArraysChunksOf #-}
132132lpackArraysChunksOf :: (MonadIO m, Unbox a)
133− => Int -> Fold m (Array a) () -> Fold m (Array a) ()
133+ => Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
134134lpackArraysChunksOf n (Fold step1 initial1 extract1) =
135135 Fold step initial extract
136136
@@ -192,7 +192,7 @@ lpackArraysChunksOf n (Fold step1 initial1 extract1) =
192192-- /Internal/
193193{-# INLINE compact #-}
194194compact :: (MonadIO m, Unbox a)
195− => Int -> Stream m (Array a) -> Stream m (Array a)
195+ => Int -> Stream m (MutArray a) -> Stream m (MutArray a)
196196compact = packArraysChunksOf
197197
198198-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
@@ -205,7 +205,7 @@ compact = packArraysChunksOf
205205{-# INLINE_NORMAL compactLEParserD #-}
206206compactLEParserD ::
207207 forall m a. (MonadIO m, Unbox a)
208− => Int -> ParserD.Parser (Array a) m (Array a)
208+ => Int -> ParserD.Parser (MutArray a) m (MutArray a)
209209compactLEParserD n = ParserD.Parser step initial extract
210210
211211 where
@@ -255,7 +255,7 @@ compactLEParserD n = ParserD.Parser step initial extract
255255{-# INLINE_NORMAL compactGEFold #-}
256256compactGEFold ::
257257 forall m a. (MonadIO m, Unbox a)
258− => Int -> FL.Fold m (Array a) (Array a)
258+ => Int -> FL.Fold m (MutArray a) (MutArray a)
259259compactGEFold n = Fold step initial extract
260260
261261 where
@@ -299,7 +299,7 @@ compactGEFold n = Fold step initial extract
299299--
300300-- /Internal/
301301compactLE :: (MonadIO m, Unbox a) =>
302− Int -> Stream m (Array a) -> Stream m (Either ParseError (Array a))
302+ Int -> Stream m (MutArray a) -> Stream m (Either ParseError (MutArray a))
303303compactLE n = D.parseManyD (compactLEParserD n)
304304
305305-- | Like 'compactLE' but generates arrays of exactly equal to the size
@@ -308,7 +308,7 @@ compactLE n = D.parseManyD (compactLEParserD n)
308308-- /Unimplemented/
309309{-# INLINE compactEQ #-}
310310compactEQ :: -- (MonadIO m, Unbox a) =>
311− Int -> Stream m (Array a) -> Stream m (Array a)
311+ Int -> Stream m (MutArray a) -> Stream m (MutArray a)
312312compactEQ _n _xs = undefined
313313 -- IsStream.fromStreamD $ D.foldMany (compactEQFold n) (IsStream.toStreamD xs)
314314
@@ -319,5 +319,5 @@ compactEQ _n _xs = undefined
319319{-# INLINE compactGE #-}
320320compactGE ::
321321 (MonadIO m, Unbox a)
322− => Int -> Stream m (Array a) -> Stream m (Array a)
322+ => Int -> Stream m (MutArray a) -> Stream m (MutArray a)
323323compactGE n = D.foldMany (compactGEFold n)
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+225−225
@@ -1,6 +1,6 @@
11{-# LANGUAGE UnboxedTuples #-}
22-- |
3−-- Module : Streamly.Internal.Data.Array.Mut.Type
3+-- Module : Streamly.Internal.Data.MutArray.Mut.Type
44-- Copyright : (c) 2020 Composewell Technologies
55-- License : BSD3-3-Clause
66-- Maintainer : streamly@composewell.com
@@ -16,7 +16,7 @@ module Streamly.Internal.Data.Array.Mut.Type
1616 (
1717 -- * Type
1818 -- $arrayNotes
19− Array (..)
19+ MutArray (..)
2020 , MutableByteArray
2121 , touch
2222 , pin
@@ -260,7 +260,7 @@ import Prelude hiding
260260
261261-- $setup
262262-- >>> :m
263−-- >>> import qualified Streamly.Internal.Data.Array.Mut.Type as Array
263+-- >>> import qualified Streamly.Internal.Data.Array.Mut.Type as MutArray
264264-- >>> import qualified Streamly.Internal.Data.Stream as Stream
265265-- >>> import qualified Streamly.Internal.Data.Stream.StreamD as StreamD
266266-- >>> import qualified Streamly.Internal.Data.Fold as Fold
@@ -299,13 +299,13 @@ memcmp p1 p2 len = do
299299 return $ r == 0
300300
301301-------------------------------------------------------------------------------
302−-- Array Data Type
302+-- MutArray Data Type
303303-------------------------------------------------------------------------------
304304
305305-- $arrayNotes
306306--
307−-- We can use an 'Unboxed' constraint in the Array type and the constraint can
308−-- be automatically provided to a function that pattern matches on the Array
307+-- We can use an 'Unboxed' constraint in the MutArray type and the constraint can
308+-- be automatically provided to a function that pattern matches on the MutArray
309309-- type. However, it has huge performance cost, so we do not use it.
310310-- Investigate a GHC improvement possiblity.
311311
@@ -328,13 +328,13 @@ memcmp p1 p2 len = do
328328-- and performance impact due to reallocations when growing or shrinking the
329329-- array.
330330--
331−data Array a =
331+data MutArray a =
332332#ifdef DEVBUILD
333333 Unbox a =>
334334#endif
335335 -- The array is a range into arrContents. arrContents may be a superset of
336336 -- the slice represented by the array. All offsets are in bytes.
337− Array
337+ MutArray
338338 { arrContents :: {-# UNPACK #-} !MutableByteArray
339339 , arrStart :: {-# UNPACK #-} !Int -- ^ index into arrContents
340340 , arrEnd :: {-# UNPACK #-} !Int -- ^ index into arrContents
@@ -348,14 +348,14 @@ data Array a =
348348-------------------------------------------------------------------------------
349349
350350{-# INLINE pin #-}
351−pin :: Array a -> IO (Array a)
352−pin arr@Array{..} = do
351+pin :: MutArray a -> IO (MutArray a)
352+pin arr@MutArray{..} = do
353353 contents <- Unboxed.pin arrContents
354354 return $ arr {arrContents = contents}
355355
356356{-# INLINE unpin #-}
357−unpin :: Array a -> IO (Array a)
358−unpin arr@Array{..} = do
357+unpin :: MutArray a -> IO (MutArray a)
358+unpin arr@MutArray{..} = do
359359 contents <- Unboxed.unpin arrContents
360360 return $ arr {arrContents = contents}
361361
@@ -387,11 +387,11 @@ unpin arr@Array{..} = do
387387-- /Pre-release/
388388{-# INLINE newArrayWith #-}
389389newArrayWith :: forall m a. (MonadIO m, Unbox a)
390− => (Int -> Int -> m MutableByteArray) -> Int -> Int -> m (Array a)
390+ => (Int -> Int -> m MutableByteArray) -> Int -> Int -> m (MutArray a)
391391newArrayWith alloc alignSize count = do
392392 let size = max (count * SIZE_OF(a)) 0
393393 contents <- alloc size alignSize
394− return $ Array
394+ return $ MutArray
395395 { arrContents = contents
396396 , arrStart = 0
397397 , arrEnd = 0
@@ -402,8 +402,8 @@ nil ::
402402#ifdef DEVBUILD
403403 Unbox a =>
404404#endif
405− Array a
406−nil = Array Unboxed.nil 0 0 0
405+ MutArray a
406+nil = MutArray Unboxed.nil 0 0 0
407407
408408
409409-- | Allocates a pinned empty array that can hold 'count' items. The memory of
@@ -416,10 +416,10 @@ newPinnedBytes :: MonadIO m =>
416416#ifdef DEVBUILD
417417 Unbox a =>
418418#endif
419− Int -> m (Array a)
419+ Int -> m (MutArray a)
420420newPinnedBytes bytes = do
421421 contents <- liftIO $ Unboxed.newPinnedBytes bytes
422− return $ Array
422+ return $ MutArray
423423 { arrContents = contents
424424 , arrStart = 0
425425 , arrEnd = 0
@@ -431,7 +431,7 @@ newPinnedBytes bytes = do
431431--
432432-- /Internal/
433433{-# INLINE newAlignedPinned #-}
434−newAlignedPinned :: (MonadIO m, Unbox a) => Int -> Int -> m (Array a)
434+newAlignedPinned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
435435newAlignedPinned =
436436 newArrayWith (\s a -> liftIO $ Unboxed.newAlignedPinnedBytes s a)
437437
@@ -442,7 +442,7 @@ newAlignedPinned =
442442-- instance of the type.
443443--
444444{-# INLINE newPinned #-}
445−newPinned :: forall m a. (MonadIO m, Unbox a) => Int -> m (Array a)
445+newPinned :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
446446newPinned =
447447 newArrayWith
448448 (\s _ -> liftIO $ Unboxed.newPinnedBytes s)
@@ -452,19 +452,19 @@ newPinned =
452452-- of the array is uninitialized.
453453--
454454{-# INLINE new #-}
455−new :: (MonadIO m, Unbox a) => Int -> m (Array a)
455+new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
456456new =
457457 newArrayWith
458458 (\s _ -> liftIO $ Unboxed.newUnpinnedBytes s)
459459 (error "new: alignment is not used in unpinned arrays.")
460460
461−-- | Allocate an Array of the given size and run an IO action passing the array
461+-- | Allocate an MutArray of the given size and run an IO action passing the array
462462-- start pointer.
463463--
464464-- /Internal/
465465{-# INLINE withNewArrayUnsafe #-}
466466withNewArrayUnsafe ::
467− (MonadIO m, Unbox a) => Int -> (Ptr a -> m ()) -> m (Array a)
467+ (MonadIO m, Unbox a) => Int -> (Ptr a -> m ()) -> m (MutArray a)
468468withNewArrayUnsafe count f = do
469469 arr <- newPinned count
470470 asPtrUnsafe arr
@@ -480,8 +480,8 @@ withNewArrayUnsafe count f = do
480480-- /Pre-release/
481481{-# INLINE putIndexUnsafe #-}
482482putIndexUnsafe :: forall m a. (MonadIO m, Unbox a)
483− => Int -> Array a -> a -> m ()
484−putIndexUnsafe i Array{..} x = do
483+ => Int -> MutArray a -> a -> m ()
484+putIndexUnsafe i MutArray{..} x = do
485485 let index = INDEX_OF(arrStart, i, a)
486486 assert (i >= 0 && INDEX_VALID(index, arrEnd, a)) (return ())
487487 liftIO $ pokeWith arrContents index x
@@ -493,13 +493,13 @@ invalidIndex label i =
493493-- | /O(1)/ Write the given element at the given index in the array.
494494-- Performs in-place mutation of the array.
495495--
496−-- >>> putIndex ix arr val = Array.modifyIndex ix arr (const (val, ()))
497−-- >>> f = Array.putIndices
496+-- >>> putIndex ix arr val = MutArray.modifyIndex ix arr (const (val, ()))
497+-- >>> f = MutArray.putIndices
498498-- >>> putIndex ix arr val = Stream.fold (f arr) (Stream.fromPure (ix, val))
499499--
500500{-# INLINE putIndex #-}
501−putIndex :: forall m a. (MonadIO m, Unbox a) => Int -> Array a -> a -> m ()
502−putIndex i Array{..} x = do
501+putIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m ()
502+putIndex i MutArray{..} x = do
503503 let index = INDEX_OF(arrStart,i,a)
504504 if i >= 0 && INDEX_VALID(index,arrEnd,a)
505505 then liftIO $ pokeWith arrContents index x
@@ -511,7 +511,7 @@ putIndex i Array{..} x = do
511511-- /Pre-release/
512512{-# INLINE putIndices #-}
513513putIndices :: forall m a. (MonadIO m, Unbox a)
514− => Array a -> Fold m (Int, a) ()
514+ => MutArray a -> Fold m (Int, a) ()
515515putIndices arr = FL.foldlM' step (return ())
516516
517517 where
@@ -522,8 +522,8 @@ putIndices arr = FL.foldlM' step (return ())
522522--
523523-- /Pre-release/
524524modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
525− Int -> Array a -> (a -> (a, b)) -> m b
526−modifyIndexUnsafe i Array{..} f = liftIO $ do
525+ Int -> MutArray a -> (a -> (a, b)) -> m b
526+modifyIndexUnsafe i MutArray{..} f = liftIO $ do
527527 let index = INDEX_OF(arrStart,i,a)
528528 assert (i >= 0 && INDEX_NEXT(index,a) <= arrEnd) (return ())
529529 r <- peekWith arrContents index
@@ -535,8 +535,8 @@ modifyIndexUnsafe i Array{..} f = liftIO $ do
535535--
536536-- /Pre-release/
537537modifyIndex :: forall m a b. (MonadIO m, Unbox a) =>
538− Int -> Array a -> (a -> (a, b)) -> m b
539−modifyIndex i Array{..} f = do
538+ Int -> MutArray a -> (a -> (a, b)) -> m b
539+modifyIndex i MutArray{..} f = do
540540 let index = INDEX_OF(arrStart,i,a)
541541 if i >= 0 && INDEX_VALID(index,arrEnd,a)
542542 then liftIO $ do
@@ -552,7 +552,7 @@ modifyIndex i Array{..} f = do
552552-- /Pre-release/
553553{-# INLINE modifyIndices #-}
554554modifyIndices :: forall m a . (MonadIO m, Unbox a)
555− => Array a -> (Int -> a -> a) -> Fold m Int ()
555+ => MutArray a -> (Int -> a -> a) -> Fold m Int ()
556556modifyIndices arr f = FL.foldlM' step initial
557557
558558 where
@@ -567,8 +567,8 @@ modifyIndices arr f = FL.foldlM' step initial
567567--
568568-- /Pre-release/
569569modify :: forall m a. (MonadIO m, Unbox a)
570− => Array a -> (a -> a) -> m ()
571−modify Array{..} f = liftIO $
570+ => MutArray a -> (a -> a) -> m ()
571+modify MutArray{..} f = liftIO $
572572 go arrStart
573573
574574 where
@@ -602,8 +602,8 @@ swapArrayByteIndices _ arrContents i1 i2 = do
602602-- /Pre-release/
603603{-# INLINE unsafeSwapIndices #-}
604604unsafeSwapIndices :: forall m a. (MonadIO m, Unbox a)
605− => Int -> Int -> Array a -> m ()
606−unsafeSwapIndices i1 i2 Array{..} = liftIO $ do
605+ => Int -> Int -> MutArray a -> m ()
606+unsafeSwapIndices i1 i2 MutArray{..} = liftIO $ do
607607 let t1 = INDEX_OF(arrStart,i1,a)
608608 t2 = INDEX_OF(arrStart,i2,a)
609609 swapArrayByteIndices (Proxy :: Proxy a) arrContents t1 t2
@@ -612,8 +612,8 @@ unsafeSwapIndices i1 i2 Array{..} = liftIO $ do
612612--
613613-- /Pre-release/
614614swapIndices :: forall m a. (MonadIO m, Unbox a)
615− => Int -> Int -> Array a -> m ()
616−swapIndices i1 i2 Array{..} = liftIO $ do
615+ => Int -> Int -> MutArray a -> m ()
616+swapIndices i1 i2 MutArray{..} = liftIO $ do
617617 let t1 = INDEX_OF(arrStart,i1,a)
618618 t2 = INDEX_OF(arrStart,i2,a)
619619 when (i1 < 0 || INDEX_INVALID(t1,arrEnd,a))
@@ -717,8 +717,8 @@ roundDownTo elemSize size = size - (size `mod` elemSize)
717717-- which complicates some inspection tests.
718718--
719719{-# NOINLINE reallocExplicit #-}
720−reallocExplicit :: Int -> Int -> Array a -> IO (Array a)
721−reallocExplicit elemSize newCapacityInBytes Array{..} = do
720+reallocExplicit :: Int -> Int -> MutArray a -> IO (MutArray a)
721+reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
722722 assertM(arrEnd <= arrBound)
723723
724724 -- Allocate new array
@@ -739,7 +739,7 @@ reallocExplicit elemSize newCapacityInBytes Array{..} = do
739739 IO $ \s# -> (# copyMutableByteArray# mbarrFrom# oldStartInBytes#
740740 mbarrTo# 0# newLenInBytes# s#, () #)
741741
742− return $ Array
742+ return $ MutArray
743743 { arrStart = 0
744744 , arrContents = contents
745745 , arrEnd = newLenInBytes
@@ -755,7 +755,7 @@ reallocExplicit elemSize newCapacityInBytes Array{..} = do
755755-- 'largeObjectThreshold' then it is rounded up to the block size (4K).
756756--
757757{-# INLINABLE realloc #-}
758−realloc :: forall m a. (MonadIO m, Unbox a) => Int -> Array a -> m (Array a)
758+realloc :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
759759realloc bytes arr = liftIO $ reallocExplicit (SIZE_OF(a)) bytes arr
760760
761761-- | @reallocWith label capSizer minIncrBytes array@. The label is used
@@ -765,8 +765,8 @@ reallocWith :: forall m a. (MonadIO m , Unbox a) =>
765765 String
766766 -> (Int -> Int)
767767 -> Int
768− -> Array a
769− -> m (Array a)
768+ -> MutArray a
769+ -> m (MutArray a)
770770reallocWith label capSizer minIncrBytes arr = do
771771 let oldSizeBytes = arrEnd arr - arrStart arr
772772 newCapBytes = capSizer oldSizeBytes
@@ -796,8 +796,8 @@ reallocWith label capSizer minIncrBytes arr = do
796796-- /Pre-release/
797797{-# INLINE resize #-}
798798resize :: forall m a. (MonadIO m, Unbox a) =>
799− Int -> Array a -> m (Array a)
800−resize nElems arr@Array{..} = do
799+ Int -> MutArray a -> m (MutArray a)
800+resize nElems arr@MutArray{..} = do
801801 let req = SIZE_OF(a) * nElems
802802 len = arrEnd - arrStart
803803 if req < len
@@ -810,8 +810,8 @@ resize nElems arr@Array{..} = do
810810-- /Pre-release/
811811{-# INLINE resizeExp #-}
812812resizeExp :: forall m a. (MonadIO m, Unbox a) =>
813− Int -> Array a -> m (Array a)
814−resizeExp nElems arr@Array{..} = do
813+ Int -> MutArray a -> m (MutArray a)
814+resizeExp nElems arr@MutArray{..} = do
815815 let req = roundUpLargeArray (SIZE_OF(a) * nElems)
816816 req1 =
817817 if req > largeObjectThreshold
@@ -831,8 +831,8 @@ resizeExp nElems arr@Array{..} = do
831831--
832832-- /Pre-release/
833833{-# INLINE rightSize #-}
834−rightSize :: forall m a. (MonadIO m, Unbox a) => Array a -> m (Array a)
835−rightSize arr@Array{..} = do
834+rightSize :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m (MutArray a)
835+rightSize arr@MutArray{..} = do
836836 assert (arrEnd <= arrBound) (return ())
837837 let start = arrStart
838838 len = arrEnd - start
@@ -854,7 +854,7 @@ rightSize arr@Array{..} = do
854854-- bound pointers. Using fully mutable handle will ensure that we do not have
855855-- multiple references to the same array of different lengths lying around and
856856-- potentially misused. In that case "snoc" need not return a new array (snoc
857−-- :: Array a -> a -> m ()), it will just modify the old reference. The array
857+-- :: MutArray a -> a -> m ()), it will just modify the old reference. The array
858858-- length will be mutable. This means the length function would also be
859859-- monadic. Mutable arrays would behave more like files that grow in that
860860-- case.
@@ -863,8 +863,8 @@ rightSize arr@Array{..} = do
863863--
864864-- /Internal/
865865{-# INLINE snocNewEnd #-}
866−snocNewEnd :: (MonadIO m, Unbox a) => Int -> Array a -> a -> m (Array a)
867−snocNewEnd newEnd arr@Array{..} x = liftIO $ do
866+snocNewEnd :: (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m (MutArray a)
867+snocNewEnd newEnd arr@MutArray{..} x = liftIO $ do
868868 assert (newEnd <= arrBound) (return ())
869869 pokeWith arrContents arrEnd x
870870 return $ arr {arrEnd = newEnd}
@@ -876,8 +876,8 @@ snocNewEnd newEnd arr@Array{..} x = liftIO $ do
876876-- /Internal/
877877{-# INLINE snocUnsafe #-}
878878snocUnsafe :: forall m a. (MonadIO m, Unbox a) =>
879− Array a -> a -> m (Array a)
880−snocUnsafe arr@Array{..} = snocNewEnd (INDEX_NEXT(arrEnd,a)) arr
879+ MutArray a -> a -> m (MutArray a)
880+snocUnsafe arr@MutArray{..} = snocNewEnd (INDEX_NEXT(arrEnd,a)) arr
881881
882882-- | Like 'snoc' but does not reallocate when pre-allocated array capacity
883883-- becomes full.
@@ -885,8 +885,8 @@ snocUnsafe arr@Array{..} = snocNewEnd (INDEX_NEXT(arrEnd,a)) arr
885885-- /Internal/
886886{-# INLINE snocMay #-}
887887snocMay :: forall m a. (MonadIO m, Unbox a) =>
888− Array a -> a -> m (Maybe (Array a))
889−snocMay arr@Array{..} x = liftIO $ do
888+ MutArray a -> a -> m (Maybe (MutArray a))
889+snocMay arr@MutArray{..} x = liftIO $ do
890890 let newEnd = INDEX_NEXT(arrEnd,a)
891891 if newEnd <= arrBound
892892 then Just <$> snocNewEnd newEnd arr x
@@ -896,9 +896,9 @@ snocMay arr@Array{..} x = liftIO $ do
896896{-# NOINLINE snocWithRealloc #-}
897897snocWithRealloc :: forall m a. (MonadIO m, Unbox a) =>
898898 (Int -> Int)
899− -> Array a
899+ -> MutArray a
900900 -> a
901− -> m (Array a)
901+ -> m (MutArray a)
902902snocWithRealloc sizer arr x = do
903903 arr1 <- liftIO $ reallocWith "snocWith" sizer (SIZE_OF(a)) arr
904904 snocUnsafe arr1 x
@@ -919,9 +919,9 @@ snocWithRealloc sizer arr x = do
919919{-# INLINE snocWith #-}
920920snocWith :: forall m a. (MonadIO m, Unbox a) =>
921921 (Int -> Int)
922− -> Array a
922+ -> MutArray a
923923 -> a
924− -> m (Array a)
924+ -> m (MutArray a)
925925snocWith allocSize arr x = liftIO $ do
926926 let newEnd = INDEX_NEXT(arrEnd arr,a)
927927 if newEnd <= arrBound arr
@@ -939,7 +939,7 @@ snocWith allocSize arr x = liftIO $ do
939939--
940940-- /Pre-release/
941941{-# INLINE snocLinear #-}
942−snocLinear :: forall m a. (MonadIO m, Unbox a) => Array a -> a -> m (Array a)
942+snocLinear :: forall m a. (MonadIO m, Unbox a) => MutArray a -> a -> m (MutArray a)
943943snocLinear = snocWith (+ allocBytesToBytes (undefined :: a) arrayChunkBytes)
944944
945945-- | The array is mutated to append an additional element to it. If there is no
@@ -951,12 +951,12 @@ snocLinear = snocWith (+ allocBytesToBytes (undefined :: a) arrayChunkBytes)
951951--
952952-- Note that the returned array may be a mutated version of the original array.
953953--
954−-- >>> snoc = Array.snocWith (* 2)
954+-- >>> snoc = MutArray.snocWith (* 2)
955955--
956956-- Performs O(n * log n) copies to grow, but is liberal with memory allocation.
957957--
958958{-# INLINE snoc #-}
959−snoc :: forall m a. (MonadIO m, Unbox a) => Array a -> a -> m (Array a)
959+snoc :: forall m a. (MonadIO m, Unbox a) => MutArray a -> a -> m (MutArray a)
960960snoc = snocWith f
961961
962962 where
@@ -976,8 +976,8 @@ snoc = snocWith f
976976--
977977-- Unsafe because it does not check the bounds of the array.
978978{-# INLINE_NORMAL getIndexUnsafe #-}
979−getIndexUnsafe :: forall m a. (MonadIO m, Unbox a) => Int -> Array a -> m a
980−getIndexUnsafe i Array{..} = do
979+getIndexUnsafe :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
980+getIndexUnsafe i MutArray{..} = do
981981 let index = INDEX_OF(arrStart,i,a)
982982 assert (i >= 0 && INDEX_VALID(index,arrEnd,a)) (return ())
983983 liftIO $ peekWith arrContents index
@@ -985,8 +985,8 @@ getIndexUnsafe i Array{..} = do
985985-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
986986--
987987{-# INLINE getIndex #-}
988−getIndex :: forall m a. (MonadIO m, Unbox a) => Int -> Array a -> m a
989−getIndex i Array{..} = do
988+getIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
989+getIndex i MutArray{..} = do
990990 let index = INDEX_OF(arrStart,i,a)
991991 if i >= 0 && INDEX_VALID(index,arrEnd,a)
992992 then liftIO $ peekWith arrContents index
@@ -998,8 +998,8 @@ getIndex i Array{..} = do
998998-- Slightly faster than computing the forward index and using getIndex.
999999--
10001000{-# INLINE getIndexRev #-}
1001−getIndexRev :: forall m a. (MonadIO m, Unbox a) => Int -> Array a -> m a
1002−getIndexRev i Array{..} = do
1001+getIndexRev :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
1002+getIndexRev i MutArray{..} = do
10031003 let index = RINDEX_OF(arrEnd,i,a)
10041004 if i >= 0 && index >= arrStart
10051005 then liftIO $ peekWith arrContents index
@@ -1009,18 +1009,18 @@ data GetIndicesState contents start end st =
10091009 GetIndicesState contents start end st
10101010
10111011-- | Given an unfold that generates array indices, read the elements on those
1012−-- indices from the supplied Array. An error is thrown if an index is out of
1012+-- indices from the supplied MutArray. An error is thrown if an index is out of
10131013-- bounds.
10141014--
10151015-- /Pre-release/
10161016{-# INLINE getIndicesD #-}
10171017getIndicesD :: (Monad m, Unbox a) =>
1018− (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (Array a) a
1018+ (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
10191019getIndicesD liftio (D.Stream stepi sti) = Unfold step inject
10201020
10211021 where
10221022
1023− inject (Array contents start end _) =
1023+ inject (MutArray contents start end _) =
10241024 return $ GetIndicesState contents start end sti
10251025
10261026 {-# INLINE_LATE step #-}
@@ -1028,13 +1028,13 @@ getIndicesD liftio (D.Stream stepi sti) = Unfold step inject
10281028 r <- stepi defState st
10291029 case r of
10301030 D.Yield i s -> do
1031− x <- liftio $ getIndex i (Array contents start end undefined)
1031+ x <- liftio $ getIndex i (MutArray contents start end undefined)
10321032 return $ D.Yield x (GetIndicesState contents start end s)
10331033 D.Skip s -> return $ D.Skip (GetIndicesState contents start end s)
10341034 D.Stop -> return D.Stop
10351035
10361036{-# INLINE getIndices #-}
1037−getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (Array a) a
1037+getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
10381038getIndices = getIndicesD liftIO
10391039
10401040-------------------------------------------------------------------------------
@@ -1054,16 +1054,16 @@ getIndices = getIndicesD liftIO
10541054getSliceUnsafe :: forall a. Unbox a
10551055 => Int -- ^ from index
10561056 -> Int -- ^ length of the slice
1057− -> Array a
1058− -> Array a
1059−getSliceUnsafe index len (Array contents start e _) =
1057+ -> MutArray a
1058+ -> MutArray a
1059+getSliceUnsafe index len (MutArray contents start e _) =
10601060 let fp1 = INDEX_OF(start,index,a)
10611061 end = fp1 + (len * SIZE_OF(a))
10621062 in assert
10631063 (index >= 0 && len >= 0 && end <= e)
10641064 -- Note: In a slice we always use bound = end so that the slice
10651065 -- user cannot overwrite elements beyond the end of the slice.
1066− (Array contents fp1 end end)
1066+ (MutArray contents fp1 end end)
10671067
10681068-- | /O(1)/ Slice an array in constant time. Throws an error if the slice
10691069-- extends out of the array bounds.
@@ -1073,15 +1073,15 @@ getSliceUnsafe index len (Array contents start e _) =
10731073getSlice :: forall a. Unbox a =>
10741074 Int -- ^ from index
10751075 -> Int -- ^ length of the slice
1076− -> Array a
1077− -> Array a
1078−getSlice index len (Array contents start e _) =
1076+ -> MutArray a
1077+ -> MutArray a
1078+getSlice index len (MutArray contents start e _) =
10791079 let fp1 = INDEX_OF(start,index,a)
10801080 end = fp1 + (len * SIZE_OF(a))
10811081 in if index >= 0 && len >= 0 && end <= e
10821082 -- Note: In a slice we always use bound = end so that the slice user
10831083 -- cannot overwrite elements beyond the end of the slice.
1084− then Array contents fp1 end end
1084+ then MutArray contents fp1 end end
10851085 else error
10861086 $ "getSlice: invalid slice, index "
10871087 ++ show index ++ " length " ++ show len
@@ -1098,8 +1098,8 @@ getSlice index len (Array contents start e _) =
10981098-- of cache locality and when you do not want to allocate additional memory.
10991099--
11001100{-# INLINE reverse #-}
1101−reverse :: forall m a. (MonadIO m, Unbox a) => Array a -> m ()
1102−reverse Array{..} = liftIO $ do
1101+reverse :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m ()
1102+reverse MutArray{..} = liftIO $ do
11031103 let l = arrStart
11041104 h = INDEX_PREV(arrEnd,a)
11051105 in swap l h
@@ -1116,7 +1116,7 @@ reverse Array{..} = liftIO $ do
11161116--
11171117-- /Unimplemented/
11181118{-# INLINE permute #-}
1119−permute :: Array a -> m Bool
1119+permute :: MutArray a -> m Bool
11201120permute = undefined
11211121
11221122-- | Partition an array into two halves using a partitioning predicate. The
@@ -1126,14 +1126,14 @@ permute = undefined
11261126-- /Pre-release/
11271127{-# INLINE partitionBy #-}
11281128partitionBy :: forall m a. (MonadIO m, Unbox a)
1129− => (a -> Bool) -> Array a -> m (Array a, Array a)
1130−partitionBy f arr@Array{..} = liftIO $ do
1129+ => (a -> Bool) -> MutArray a -> m (MutArray a, MutArray a)
1130+partitionBy f arr@MutArray{..} = liftIO $ do
11311131 if arrStart >= arrEnd
11321132 then return (arr, arr)
11331133 else do
11341134 ptr <- go arrStart (INDEX_PREV(arrEnd,a))
1135− let pl = Array arrContents arrStart ptr ptr
1136− pr = Array arrContents ptr arrEnd arrEnd
1135+ let pl = MutArray arrContents arrStart ptr ptr
1136+ pr = MutArray arrContents ptr arrEnd arrEnd
11371137 return (pl, pr)
11381138
11391139 where
@@ -1193,7 +1193,7 @@ partitionBy f arr@Array{..} = liftIO $ do
11931193--
11941194-- /Unimplemented/
11951195{-# INLINE shuffleBy #-}
1196−shuffleBy :: (a -> a -> m Bool) -> Array a -> Array a -> m ()
1196+shuffleBy :: (a -> a -> m Bool) -> MutArray a -> MutArray a -> m ()
11971197shuffleBy = undefined
11981198
11991199-- XXX we can also make the folds partial by stopping at a certain level.
@@ -1209,7 +1209,7 @@ shuffleBy = undefined
12091209-- /Unimplemented/
12101210{-# INLINABLE divideBy #-}
12111211divideBy ::
1212− Int -> (Array a -> m (Array a, Array a)) -> Array a -> m ()
1212+ Int -> (MutArray a -> m (MutArray a, MutArray a)) -> MutArray a -> m ()
12131213divideBy = undefined
12141214
12151215-- | @mergeBy level merge array@ performs a pairwise bottom up fold recursively
@@ -1221,7 +1221,7 @@ divideBy = undefined
12211221-- sort.
12221222--
12231223-- /Unimplemented/
1224−mergeBy :: Int -> (Array a -> Array a -> m ()) -> Array a -> m ()
1224+mergeBy :: Int -> (MutArray a -> MutArray a -> m ()) -> MutArray a -> m ()
12251225mergeBy = undefined
12261226
12271227-------------------------------------------------------------------------------
@@ -1231,8 +1231,8 @@ mergeBy = undefined
12311231-- | /O(1)/ Get the byte length of the array.
12321232--
12331233{-# INLINE byteLength #-}
1234−byteLength :: Array a -> Int
1235−byteLength Array{..} =
1234+byteLength :: MutArray a -> Int
1235+byteLength MutArray{..} =
12361236 let len = arrEnd - arrStart
12371237 in assert (len >= 0) len
12381238
@@ -1247,7 +1247,7 @@ byteLength Array{..} =
12471247-- involves a costly division operation.
12481248--
12491249{-# INLINE length #-}
1250−length :: forall a. Unbox a => Array a -> Int
1250+length :: forall a. Unbox a => MutArray a -> Int
12511251length arr =
12521252 let elemSize = SIZE_OF(a)
12531253 blen = byteLength arr
@@ -1258,8 +1258,8 @@ length arr =
12581258--
12591259-- /Pre-release/
12601260{-# INLINE byteCapacity #-}
1261−byteCapacity :: Array a -> Int
1262−byteCapacity Array{..} =
1261+byteCapacity :: MutArray a -> Int
1262+byteCapacity MutArray{..} =
12631263 let len = arrBound - arrStart
12641264 in assert (len >= 0) len
12651265
@@ -1268,8 +1268,8 @@ byteCapacity Array{..} =
12681268--
12691269-- /Pre-release/
12701270{-# INLINE bytesFree #-}
1271−bytesFree :: Array a -> Int
1272−bytesFree Array{..} =
1271+bytesFree :: MutArray a -> Int
1272+bytesFree MutArray{..} =
12731273 let n = arrBound - arrEnd
12741274 in assert (n >= 0) n
12751275
@@ -1287,12 +1287,12 @@ data GroupState s contents start end bound
12871287-- | @arraysOf n stream@ groups the input stream into a stream of
12881288-- arrays of size n.
12891289--
1290−-- @arraysOf n = StreamD.foldMany (Array.writeN n)@
1290+-- @arraysOf n = StreamD.foldMany (MutArray.writeN n)@
12911291--
12921292-- /Pre-release/
12931293{-# INLINE_NORMAL arraysOf #-}
12941294arraysOf :: forall m a. (MonadIO m, Unbox a)
1295− => Int -> D.Stream m a -> D.Stream m (Array a)
1295+ => Int -> D.Stream m a -> D.Stream m (MutArray a)
12961296-- XXX the idiomatic implementation leads to large regression in the D.reverse'
12971297-- benchmark. It seems it has difficulty producing optimized code when
12981298-- converting to StreamK. Investigate GHC optimizations.
@@ -1306,10 +1306,10 @@ arraysOf n (D.Stream step state) =
13061306 step' _ (GroupStart st) = do
13071307 when (n <= 0) $
13081308 -- XXX we can pass the module string from the higher level API
1309− error $ "Streamly.Internal.Data.Array.Mut.Type.arraysOf: "
1309+ error $ "Streamly.Internal.Data.MutArray.Mut.Type.arraysOf: "
13101310 ++ "the size of arrays [" ++ show n
13111311 ++ "] must be a natural number"
1312− (Array contents start end bound :: Array a) <- liftIO $ newPinned n
1312+ (MutArray contents start end bound :: MutArray a) <- liftIO $ newPinned n
13131313 return $ D.Skip (GroupBuffer st contents start end bound)
13141314
13151315 step' gst (GroupBuffer st contents start end bound) = do
@@ -1331,7 +1331,7 @@ arraysOf n (D.Stream step state) =
13311331 $ D.Skip (GroupYield contents start end bound GroupFinish)
13321332
13331333 step' _ (GroupYield contents start end bound next) =
1334− return $ D.Yield (Array contents start end bound) next
1334+ return $ D.Yield (MutArray contents start end bound) next
13351335
13361336 step' _ GroupFinish = return D.Stop
13371337
@@ -1339,7 +1339,7 @@ arraysOf n (D.Stream step state) =
13391339-- | Buffer the stream into arrays in memory.
13401340{-# INLINE arrayStreamKFromStreamD #-}
13411341arrayStreamKFromStreamD :: forall m a. (MonadIO m, Unbox a) =>
1342− D.Stream m a -> m (K.Stream m (Array a))
1342+ D.Stream m a -> m (K.Stream m (MutArray a))
13431343arrayStreamKFromStreamD =
13441344 let n = allocBytesToElemCount (undefined :: a) defaultChunkSize
13451345 in D.foldr K.cons K.nil . arraysOf n
@@ -1360,7 +1360,7 @@ data FlattenState s contents a =
13601360--
13611361{-# INLINE_NORMAL flattenArrays #-}
13621362flattenArrays :: forall m a. (MonadIO m, Unbox a)
1363− => D.Stream m (Array a) -> D.Stream m a
1363+ => D.Stream m (MutArray a) -> D.Stream m a
13641364flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
13651365
13661366 where
@@ -1369,7 +1369,7 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
13691369 step' gst (OuterLoop st) = do
13701370 r <- step (adaptState gst) st
13711371 return $ case r of
1372− D.Yield Array{..} s ->
1372+ D.Yield MutArray{..} s ->
13731373 D.Skip (InnerLoop s arrContents arrStart arrEnd)
13741374 D.Skip s -> D.Skip (OuterLoop s)
13751375 D.Stop -> D.Stop
@@ -1389,7 +1389,7 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
13891389--
13901390{-# INLINE_NORMAL flattenArraysRev #-}
13911391flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1392− => D.Stream m (Array a) -> D.Stream m a
1392+ => D.Stream m (MutArray a) -> D.Stream m a
13931393flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
13941394
13951395 where
@@ -1398,7 +1398,7 @@ flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
13981398 step' gst (OuterLoop st) = do
13991399 r <- step (adaptState gst) st
14001400 return $ case r of
1401− D.Yield Array{..} s ->
1401+ D.Yield MutArray{..} s ->
14021402 let p = INDEX_PREV(arrEnd,a)
14031403 in D.Skip (InnerLoop s arrContents p arrStart)
14041404 D.Skip s -> D.Skip (OuterLoop s)
@@ -1421,21 +1421,21 @@ data ArrayUnsafe a = ArrayUnsafe
14211421 {-# UNPACK #-} !Int -- index 1
14221422 {-# UNPACK #-} !Int -- index 2
14231423
1424−toArrayUnsafe :: Array a -> ArrayUnsafe a
1425−toArrayUnsafe (Array contents start end _) = ArrayUnsafe contents start end
1424+toArrayUnsafe :: MutArray a -> ArrayUnsafe a
1425+toArrayUnsafe (MutArray contents start end _) = ArrayUnsafe contents start end
14261426
14271427fromArrayUnsafe ::
14281428#ifdef DEVBUILD
14291429 Unbox a =>
14301430#endif
1431− ArrayUnsafe a -> Array a
1431+ ArrayUnsafe a -> MutArray a
14321432fromArrayUnsafe (ArrayUnsafe contents start end) =
1433− Array contents start end end
1433+ MutArray contents start end end
14341434
14351435{-# INLINE_NORMAL producerWith #-}
14361436producerWith ::
14371437 forall m a. (Monad m, Unbox a)
1438− => (forall b. IO b -> m b) -> Producer m (Array a) a
1438+ => (forall b. IO b -> m b) -> Producer m (MutArray a) a
14391439producerWith liftio = Producer step (return . toArrayUnsafe) extract
14401440 where
14411441
@@ -1455,23 +1455,23 @@ producerWith liftio = Producer step (return . toArrayUnsafe) extract
14551455-- | Resumable unfold of an array.
14561456--
14571457{-# INLINE_NORMAL producer #-}
1458−producer :: forall m a. (MonadIO m, Unbox a) => Producer m (Array a) a
1458+producer :: forall m a. (MonadIO m, Unbox a) => Producer m (MutArray a) a
14591459producer = producerWith liftIO
14601460
14611461-- | Unfold an array into a stream.
14621462--
14631463{-# INLINE_NORMAL reader #-}
1464−reader :: forall m a. (MonadIO m, Unbox a) => Unfold m (Array a) a
1464+reader :: forall m a. (MonadIO m, Unbox a) => Unfold m (MutArray a) a
14651465reader = Producer.simplify producer
14661466
14671467{-# INLINE_NORMAL readerRevWith #-}
14681468readerRevWith ::
14691469 forall m a. (Monad m, Unbox a)
1470− => (forall b. IO b -> m b) -> Unfold m (Array a) a
1470+ => (forall b. IO b -> m b) -> Unfold m (MutArray a) a
14711471readerRevWith liftio = Unfold step inject
14721472 where
14731473
1474− inject (Array contents start end _) =
1474+ inject (MutArray contents start end _) =
14751475 let p = INDEX_PREV(end,a)
14761476 in return $ ArrayUnsafe contents start p
14771477
@@ -1484,7 +1484,7 @@ readerRevWith liftio = Unfold step inject
14841484-- | Unfold an array into a stream in reverse order.
14851485--
14861486{-# INLINE_NORMAL readerRev #-}
1487−readerRev :: forall m a. (MonadIO m, Unbox a) => Unfold m (Array a) a
1487+readerRev :: forall m a. (MonadIO m, Unbox a) => Unfold m (MutArray a) a
14881488readerRev = readerRevWith liftIO
14891489
14901490-------------------------------------------------------------------------------
@@ -1495,8 +1495,8 @@ readerRev = readerRevWith liftIO
14951495-- Use foldr/build fusion to fuse with list consumers
14961496-- This can be useful when using the IsList instance
14971497{-# INLINE_LATE toListFB #-}
1498−toListFB :: forall a b. Unbox a => (a -> b -> b) -> b -> Array a -> b
1499−toListFB c n Array{..} = go arrStart
1498+toListFB :: forall a b. Unbox a => (a -> b -> b) -> b -> MutArray a -> b
1499+toListFB c n MutArray{..} = go arrStart
15001500 where
15011501
15021502 go p | assert (p <= arrEnd) (p == arrEnd) = n
@@ -1516,11 +1516,11 @@ toListFB c n Array{..} = go arrStart
15161516
15171517-- XXX Monadic foldr/build fusion?
15181518-- Reference: https://www.researchgate.net/publication/220676509_Monadic_augment_and_generalised_short_cut_fusion
1519−-- | Convert an 'Array' into a list.
1519+-- | Convert an 'MutArray' into a list.
15201520--
15211521{-# INLINE toList #-}
1522−toList :: forall m a. (MonadIO m, Unbox a) => Array a -> m [a]
1523−toList Array{..} = liftIO $ go arrStart
1522+toList :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m [a]
1523+toList MutArray{..} = liftIO $ go arrStart
15241524 where
15251525
15261526 go p | assert (p <= arrEnd) (p == arrEnd) = return []
@@ -1531,8 +1531,8 @@ toList Array{..} = liftIO $ go arrStart
15311531{-# INLINE_NORMAL toStreamDWith #-}
15321532toStreamDWith ::
15331533 forall m a. (Monad m, Unbox a)
1534− => (forall b. IO b -> m b) -> Array a -> D.Stream m a
1535−toStreamDWith liftio Array{..} = D.Stream step arrStart
1534+ => (forall b. IO b -> m b) -> MutArray a -> D.Stream m a
1535+toStreamDWith liftio MutArray{..} = D.Stream step arrStart
15361536
15371537 where
15381538
@@ -1548,14 +1548,14 @@ toStreamDWith liftio Array{..} = D.Stream step arrStart
15481548--
15491549-- We can try this if the unfold has any performance issues.
15501550{-# INLINE_NORMAL toStreamD #-}
1551−toStreamD :: forall m a. (MonadIO m, Unbox a) => Array a -> D.Stream m a
1551+toStreamD :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
15521552toStreamD = toStreamDWith liftIO
15531553
15541554{-# INLINE toStreamKWith #-}
15551555toStreamKWith ::
15561556 forall m a. (Monad m, Unbox a)
1557− => (forall b. IO b -> m b) -> Array a -> K.Stream m a
1558−toStreamKWith liftio Array{..} = go arrStart
1557+ => (forall b. IO b -> m b) -> MutArray a -> K.Stream m a
1558+toStreamKWith liftio MutArray{..} = go arrStart
15591559
15601560 where
15611561
@@ -1565,14 +1565,14 @@ toStreamKWith liftio Array{..} = go arrStart
15651565 in liftio elemM `K.consM` go (INDEX_NEXT(p,a))
15661566
15671567{-# INLINE toStreamK #-}
1568−toStreamK :: forall m a. (MonadIO m, Unbox a) => Array a -> K.Stream m a
1568+toStreamK :: forall m a. (MonadIO m, Unbox a) => MutArray a -> K.Stream m a
15691569toStreamK = toStreamKWith liftIO
15701570
15711571{-# INLINE_NORMAL toStreamDRevWith #-}
15721572toStreamDRevWith ::
15731573 forall m a. (Monad m, Unbox a)
1574− => (forall b. IO b -> m b) -> Array a -> D.Stream m a
1575−toStreamDRevWith liftio Array{..} =
1574+ => (forall b. IO b -> m b) -> MutArray a -> D.Stream m a
1575+toStreamDRevWith liftio MutArray{..} =
15761576 let p = INDEX_PREV(arrEnd,a)
15771577 in D.Stream step p
15781578
@@ -1590,14 +1590,14 @@ toStreamDRevWith liftio Array{..} =
15901590--
15911591-- We can try this if the unfold has any perf issues.
15921592{-# INLINE_NORMAL toStreamDRev #-}
1593−toStreamDRev :: forall m a. (MonadIO m, Unbox a) => Array a -> D.Stream m a
1593+toStreamDRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
15941594toStreamDRev = toStreamDRevWith liftIO
15951595
15961596{-# INLINE toStreamKRevWith #-}
15971597toStreamKRevWith ::
15981598 forall m a. (Monad m, Unbox a)
1599− => (forall b. IO b -> m b) -> Array a -> K.Stream m a
1600−toStreamKRevWith liftio Array {..} =
1599+ => (forall b. IO b -> m b) -> MutArray a -> K.Stream m a
1600+toStreamKRevWith liftio MutArray {..} =
16011601 let p = INDEX_PREV(arrEnd,a)
16021602 in go p
16031603
@@ -1609,24 +1609,24 @@ toStreamKRevWith liftio Array {..} =
16091609 in liftio elemM `K.consM` go (INDEX_PREV(p,a))
16101610
16111611{-# INLINE toStreamKRev #-}
1612−toStreamKRev :: forall m a. (MonadIO m, Unbox a) => Array a -> K.Stream m a
1612+toStreamKRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> K.Stream m a
16131613toStreamKRev = toStreamKRevWith liftIO
16141614
16151615-------------------------------------------------------------------------------
16161616-- Folding
16171617-------------------------------------------------------------------------------
16181618
1619−-- XXX Need something like "Array m a" enforcing monadic action to avoid the
1619+-- XXX Need something like "MutArray m a" enforcing monadic action to avoid the
16201620-- possibility of such APIs.
16211621--
16221622-- | Strict left fold of an array.
16231623{-# INLINE_NORMAL foldl' #-}
1624−foldl' :: (MonadIO m, Unbox a) => (b -> a -> b) -> b -> Array a -> m b
1624+foldl' :: (MonadIO m, Unbox a) => (b -> a -> b) -> b -> MutArray a -> m b
16251625foldl' f z arr = D.foldl' f z $ toStreamD arr
16261626
16271627-- | Right fold of an array.
16281628{-# INLINE_NORMAL foldr #-}
1629−foldr :: (MonadIO m, Unbox a) => (a -> b -> b) -> b -> Array a -> m b
1629+foldr :: (MonadIO m, Unbox a) => (a -> b -> b) -> b -> MutArray a -> m b
16301630foldr f z arr = D.foldr f z $ toStreamD arr
16311631
16321632-------------------------------------------------------------------------------
@@ -1652,8 +1652,8 @@ foldr f z arr = D.foldr f z $ toStreamD arr
16521652{-# INLINE_NORMAL writeAppendNUnsafe #-}
16531653writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
16541654 Int
1655− -> m (Array a)
1656− -> Fold m a (Array a)
1655+ -> m (MutArray a)
1656+ -> Fold m a (MutArray a)
16571657writeAppendNUnsafe n action =
16581658 fmap fromArrayUnsafe $ FL.foldlM' step initial
16591659
@@ -1661,7 +1661,7 @@ writeAppendNUnsafe n action =
16611661
16621662 initial = do
16631663 assert (n >= 0) (return ())
1664− arr@(Array _ _ end bound) <- action
1664+ arr@(MutArray _ _ end bound) <- action
16651665 let free = bound - end
16661666 needed = n * SIZE_OF(a)
16671667 -- XXX We can also reallocate if the array has too much free space,
@@ -1679,11 +1679,11 @@ writeAppendNUnsafe n action =
16791679-- | Append @n@ elements to an existing array. Any free space left in the array
16801680-- after appending @n@ elements is lost.
16811681--
1682−-- >>> writeAppendN n initial = Fold.take n (Array.writeAppendNUnsafe n initial)
1682+-- >>> writeAppendN n initial = Fold.take n (MutArray.writeAppendNUnsafe n initial)
16831683--
16841684{-# INLINE_NORMAL writeAppendN #-}
16851685writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1686− Int -> m (Array a) -> Fold m a (Array a)
1686+ Int -> m (MutArray a) -> Fold m a (MutArray a)
16871687writeAppendN n initial = FL.take n (writeAppendNUnsafe n initial)
16881688
16891689-- | @writeAppendWith realloc action@ mutates the array generated by @action@ to
@@ -1693,12 +1693,12 @@ writeAppendN n initial = FL.take n (writeAppendNUnsafe n initial)
16931693--
16941694-- Note that the returned array may be a mutated version of original array.
16951695--
1696−-- >>> writeAppendWith sizer = Fold.foldlM' (Array.snocWith sizer)
1696+-- >>> writeAppendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
16971697--
16981698-- /Pre-release/
16991699{-# INLINE writeAppendWith #-}
17001700writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
1701− (Int -> Int) -> m (Array a) -> Fold m a (Array a)
1701+ (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
17021702writeAppendWith sizer = FL.foldlM' (snocWith sizer)
17031703
17041704-- | @append action@ mutates the array generated by @action@ to append the
@@ -1707,11 +1707,11 @@ writeAppendWith sizer = FL.foldlM' (snocWith sizer)
17071707--
17081708-- Note that the returned array may be a mutated version of original array.
17091709--
1710−-- >>> writeAppend = Array.writeAppendWith (* 2)
1710+-- >>> writeAppend = MutArray.writeAppendWith (* 2)
17111711--
17121712{-# INLINE writeAppend #-}
17131713writeAppend :: forall m a. (MonadIO m, Unbox a) =>
1714− m (Array a) -> Fold m a (Array a)
1714+ m (MutArray a) -> Fold m a (MutArray a)
17151715writeAppend = writeAppendWith (* 2)
17161716
17171717-- XXX We can carry bound as well in the state to make sure we do not lose the
@@ -1720,12 +1720,12 @@ writeAppend = writeAppendWith (* 2)
17201720-- | Like 'writeNUnsafe' but takes a new array allocator @alloc size@ function
17211721-- as argument.
17221722--
1723−-- >>> writeNWithUnsafe alloc n = Array.writeAppendNUnsafe (alloc n) n
1723+-- >>> writeNWithUnsafe alloc n = MutArray.writeAppendNUnsafe (alloc n) n
17241724--
17251725-- /Pre-release/
17261726{-# INLINE_NORMAL writeNWithUnsafe #-}
17271727writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
1728− => (Int -> m (Array a)) -> Int -> Fold m a (Array a)
1728+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
17291729writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
17301730
17311731 where
@@ -1743,33 +1743,33 @@ writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
17431743-- conditional in the step function blocks fusion causing 10x performance
17441744-- slowdown.
17451745--
1746−-- >>> writeNUnsafe = Array.writeNWithUnsafe Array.newPinned
1746+-- >>> writeNUnsafe = MutArray.writeNWithUnsafe MutArray.newPinned
17471747--
17481748{-# INLINE_NORMAL writeNUnsafe #-}
17491749writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
1750− => Int -> Fold m a (Array a)
1750+ => Int -> Fold m a (MutArray a)
17511751writeNUnsafe = writeNWithUnsafe newPinned
17521752
17531753-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
17541754-- allocated using the @alloc@ function.
17551755--
1756−-- >>> writeNWith alloc n = Fold.take n (Array.writeNWithUnsafe alloc n)
1757−-- >>> writeNWith alloc n = Array.writeAppendN (alloc n) n
1756+-- >>> writeNWith alloc n = Fold.take n (MutArray.writeNWithUnsafe alloc n)
1757+-- >>> writeNWith alloc n = MutArray.writeAppendN (alloc n) n
17581758--
17591759{-# INLINE_NORMAL writeNWith #-}
17601760writeNWith :: forall m a. (MonadIO m, Unbox a)
1761− => (Int -> m (Array a)) -> Int -> Fold m a (Array a)
1761+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
17621762writeNWith alloc n = FL.take n (writeNWithUnsafe alloc n)
17631763
17641764-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
1765−-- 'Array'.
1765+-- 'MutArray'.
17661766--
1767−-- >>> writeN = Array.writeNWith Array.newPinned
1768−-- >>> writeN n = Fold.take n (Array.writeNUnsafe n)
1769−-- >>> writeN n = Array.writeAppendN n (Array.newPinned n)
1767+-- >>> writeN = MutArray.writeNWith MutArray.newPinned
1768+-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
1769+-- >>> writeN n = MutArray.writeAppendN n (MutArray.newPinned n)
17701770--
17711771{-# INLINE_NORMAL writeN #-}
1772−writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
1772+writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
17731773writeN = writeNWith newPinned
17741774
17751775-- | Like writeNWithUnsafe but writes the array in reverse order.
@@ -1777,12 +1777,12 @@ writeN = writeNWith newPinned
17771777-- /Internal/
17781778{-# INLINE_NORMAL writeRevNWithUnsafe #-}
17791779writeRevNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
1780− => (Int -> m (Array a)) -> Int -> Fold m a (Array a)
1780+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
17811781writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
17821782
17831783 where
17841784
1785− toArrayUnsafeRev (Array contents _ _ bound) =
1785+ toArrayUnsafeRev (MutArray contents _ _ bound) =
17861786 ArrayUnsafe contents bound bound
17871787
17881788 initial = toArrayUnsafeRev <$> alloc (max n 0)
@@ -1798,34 +1798,34 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
17981798-- /Internal/
17991799{-# INLINE_NORMAL writeRevNWith #-}
18001800writeRevNWith :: forall m a. (MonadIO m, Unbox a)
1801− => (Int -> m (Array a)) -> Int -> Fold m a (Array a)
1801+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
18021802writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
18031803
18041804-- | Like writeN but writes the array in reverse order.
18051805--
18061806-- /Pre-release/
18071807{-# INLINE_NORMAL writeRevN #-}
1808−writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
1808+writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
18091809writeRevN = writeRevNWith newPinned
18101810
18111811-- | @writeNAligned align n@ folds a maximum of @n@ elements from the input
1812−-- stream to an 'Array' aligned to the given size.
1812+-- stream to an 'MutArray' aligned to the given size.
18131813--
1814−-- >>> writeNAligned align = Array.writeNWith (Array.newAlignedPinned align)
1815−-- >>> writeNAligned align n = Array.writeAppendN n (Array.newAlignedPinned align n)
1814+-- >>> writeNAligned align = MutArray.writeNWith (MutArray.newAlignedPinned align)
1815+-- >>> writeNAligned align n = MutArray.writeAppendN n (MutArray.newAlignedPinned align n)
18161816--
18171817-- /Pre-release/
18181818--
18191819{-# INLINE_NORMAL writeNAligned #-}
18201820writeNAligned :: forall m a. (MonadIO m, Unbox a)
1821− => Int -> Int -> Fold m a (Array a)
1821+ => Int -> Int -> Fold m a (MutArray a)
18221822writeNAligned align = writeNWith (newAlignedPinned align)
18231823
18241824-- XXX Buffer to a list instead?
18251825--
18261826-- | Buffer a stream into a stream of arrays.
18271827--
1828−-- >>> writeChunks n = Fold.many (Array.writeN n) Fold.toStreamK
1828+-- >>> writeChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
18291829--
18301830-- Breaking an array into an array stream can be useful to consume a large
18311831-- array sequentially such that memory of the array is released incrementatlly.
@@ -1836,7 +1836,7 @@ writeNAligned align = writeNWith (newAlignedPinned align)
18361836--
18371837{-# INLINE_NORMAL writeChunks #-}
18381838writeChunks :: (MonadIO m, Unbox a) =>
1839− Int -> Fold m a (K.Stream n (Array a))
1839+ Int -> Fold m a (K.Stream n (MutArray a))
18401840writeChunks n = FL.many (writeN n) FL.toStreamK
18411841
18421842-- XXX Compare writeWith with fromStreamD which uses an array of streams
@@ -1862,28 +1862,28 @@ writeChunks n = FL.many (writeN n) FL.toStreamK
18621862--
18631863-- /Caution! Do not use this on infinite streams./
18641864--
1865−-- >>> f n = Array.writeAppendWith (* 2) (Array.newPinned n)
1866−-- >>> writeWith n = Fold.rmapM Array.rightSize (f n)
1867−-- >>> writeWith n = Fold.rmapM Array.fromArrayStreamK (Array.writeChunks n)
1865+-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.newPinned n)
1866+-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
1867+-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
18681868--
18691869-- /Pre-release/
18701870{-# INLINE_NORMAL writeWith #-}
18711871writeWith :: forall m a. (MonadIO m, Unbox a)
1872− => Int -> Fold m a (Array a)
1872+ => Int -> Fold m a (MutArray a)
18731873-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (newPinned n)
18741874writeWith elemCount =
18751875 FL.rmapM extract $ FL.foldlM' step initial
18761876
18771877 where
18781878
1879− insertElem (Array contents start end bound) x = do
1879+ insertElem (MutArray contents start end bound) x = do
18801880 liftIO $ pokeWith contents end x
1881− return $ Array contents start (INDEX_NEXT(end,a)) bound
1881+ return $ MutArray contents start (INDEX_NEXT(end,a)) bound
18821882
18831883 initial = do
18841884 when (elemCount < 0) $ error "writeWith: elemCount is negative"
18851885 liftIO $ newPinned elemCount
1886− step arr@(Array _ start end bound) x
1886+ step arr@(MutArray _ start end bound) x
18871887 | INDEX_NEXT(end,a) > bound = do
18881888 let oldSize = end - start
18891889 newSize = max (oldSize * 2) 1
@@ -1900,7 +1900,7 @@ writeWith elemCount =
19001900-- /Caution! Do not use this on infinite streams./
19011901--
19021902{-# INLINE write #-}
1903−write :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
1903+write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
19041904write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
19051905
19061906-------------------------------------------------------------------------------
@@ -1909,14 +1909,14 @@ write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
19091909
19101910-- | Use the 'writeN' fold instead.
19111911--
1912−-- >>> fromStreamDN n = StreamD.fold (Array.writeN n)
1912+-- >>> fromStreamDN n = StreamD.fold (MutArray.writeN n)
19131913--
19141914{-# INLINE_NORMAL fromStreamDN #-}
19151915fromStreamDN :: forall m a. (MonadIO m, Unbox a)
1916− => Int -> D.Stream m a -> m (Array a)
1916+ => Int -> D.Stream m a -> m (MutArray a)
19171917-- fromStreamDN n = D.fold (writeN n)
19181918fromStreamDN limit str = do
1919− (arr :: Array a) <- liftIO $ newPinned limit
1919+ (arr :: MutArray a) <- liftIO $ newPinned limit
19201920 end <- D.foldlM' (fwrite (arrContents arr)) (return $ arrEnd arr) $ D.take limit str
19211921 return $ arr {arrEnd = end}
19221922
@@ -1926,19 +1926,19 @@ fromStreamDN limit str = do
19261926 liftIO $ pokeWith arrContents ptr x
19271927 return $ INDEX_NEXT(ptr,a)
19281928
1929−-- | Create an 'Array' from the first N elements of a list. The array is
1929+-- | Create an 'MutArray' from the first N elements of a list. The array is
19301930-- allocated to size N, if the list terminates before N elements then the
19311931-- array may hold less than N elements.
19321932--
19331933{-# INLINABLE fromListN #-}
1934−fromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (Array a)
1934+fromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
19351935fromListN n xs = fromStreamDN n $ D.fromList xs
19361936
19371937-- | Like fromListN but writes the array in reverse order.
19381938--
19391939-- /Pre-release/
19401940{-# INLINE fromListRevN #-}
1941−fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (Array a)
1941+fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
19421942fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
19431943
19441944-------------------------------------------------------------------------------
@@ -1946,7 +1946,7 @@ fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
19461946-------------------------------------------------------------------------------
19471947
19481948{-# INLINE arrayStreamKLength #-}
1949−arrayStreamKLength :: (Monad m, Unbox a) => K.Stream m (Array a) -> m Int
1949+arrayStreamKLength :: (Monad m, Unbox a) => K.Stream m (MutArray a) -> m Int
19501950arrayStreamKLength as = K.foldl' (+) 0 (K.map length as)
19511951
19521952-- | Convert an array stream to an array. Note that this requires peak memory
@@ -1954,7 +1954,7 @@ arrayStreamKLength as = K.foldl' (+) 0 (K.map length as)
19541954--
19551955{-# INLINE fromArrayStreamK #-}
19561956fromArrayStreamK :: (Unbox a, MonadIO m) =>
1957− K.Stream m (Array a) -> m (Array a)
1957+ K.Stream m (MutArray a) -> m (MutArray a)
19581958fromArrayStreamK as = do
19591959 len <- arrayStreamKLength as
19601960 fromStreamDN len $ D.unfoldMany reader $ D.fromStreamK as
@@ -1970,16 +1970,16 @@ fromArrayStreamK as = do
19701970-- the chunking approach. However, if we have to shrink in the end then it may
19711971-- result in an extra copy of the entire data.
19721972--
1973−-- >>> fromStreamD = StreamD.fold Array.write
1973+-- >>> fromStreamD = StreamD.fold MutArray.write
19741974--
19751975{-# INLINE fromStreamD #-}
1976−fromStreamD :: (MonadIO m, Unbox a) => D.Stream m a -> m (Array a)
1976+fromStreamD :: (MonadIO m, Unbox a) => D.Stream m a -> m (MutArray a)
19771977fromStreamD m = arrayStreamKFromStreamD m >>= fromArrayStreamK
19781978
1979−-- | Create an 'Array' from a list. The list must be of finite size.
1979+-- | Create an 'MutArray' from a list. The list must be of finite size.
19801980--
19811981{-# INLINE fromList #-}
1982−fromList :: (MonadIO m, Unbox a) => [a] -> m (Array a)
1982+fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
19831983fromList xs = fromStreamD $ D.fromList xs
19841984
19851985-- XXX We are materializing the whole list first for getting the length. Check
@@ -1987,7 +1987,7 @@ fromList xs = fromStreamD $ D.fromList xs
19871987
19881988-- | Like 'fromList' but writes the contents of the list in reverse order.
19891989{-# INLINE fromListRev #-}
1990−fromListRev :: (MonadIO m, Unbox a) => [a] -> m (Array a)
1990+fromListRev :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
19911991fromListRev xs = fromListRevN (Prelude.length xs) xs
19921992
19931993-------------------------------------------------------------------------------
@@ -1997,7 +1997,7 @@ fromListRev xs = fromListRevN (Prelude.length xs) xs
19971997-- | Put a sub range of a source array into a subrange of a destination array.
19981998-- This is not safe as it does not check the bounds.
19991999{-# INLINE putSliceUnsafe #-}
2000−putSliceUnsafe :: MonadIO m => Array a -> Int -> Array a -> Int -> Int -> m ()
2000+putSliceUnsafe :: MonadIO m => MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
20012001putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
20022002 assertM(lenBytes <= arrBound dst - dstStartBytes)
20032003 assertM(lenBytes <= arrEnd src - srcStartBytes)
@@ -2016,7 +2016,7 @@ spliceCopy :: forall m a. MonadIO m =>
20162016#ifdef DEVBUILD
20172017 Unbox a =>
20182018#endif
2019− Array a -> Array a -> m (Array a)
2019+ MutArray a -> MutArray a -> m (MutArray a)
20202020spliceCopy arr1 arr2 = liftIO $ do
20212021 let start1 = arrStart arr1
20222022 start2 = arrStart arr2
@@ -2024,7 +2024,7 @@ spliceCopy arr1 arr2 = liftIO $ do
20242024 len2 = arrEnd arr2 - start2
20252025 newArrContents <- liftIO $ Unboxed.newPinnedBytes (len1 + len2)
20262026 let len = len1 + len2
2027− newArr = Array newArrContents 0 len len
2027+ newArr = MutArray newArrContents 0 len len
20282028 putSliceUnsafe arr1 start1 newArr 0 len1
20292029 putSliceUnsafe arr2 start2 newArr len1 len2
20302030 return newArr
@@ -2034,7 +2034,7 @@ spliceCopy arr1 arr2 = liftIO $ do
20342034-- corruption or if you are lucky a segfault.
20352035{-# INLINE spliceUnsafe #-}
20362036spliceUnsafe :: MonadIO m =>
2037− Array a -> Array a -> m (Array a)
2037+ MutArray a -> MutArray a -> m (MutArray a)
20382038spliceUnsafe dst src =
20392039 liftIO $ do
20402040 let startSrc = arrStart src
@@ -2054,8 +2054,8 @@ spliceUnsafe dst src =
20542054-- /Pre-release/
20552055{-# INLINE spliceWith #-}
20562056spliceWith :: forall m a. (MonadIO m, Unbox a) =>
2057− (Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
2058−spliceWith sizer dst@(Array _ start end bound) src = do
2057+ (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
2058+spliceWith sizer dst@(MutArray _ start end bound) src = do
20592059{-
20602060 let f = writeAppendWith (`sizer` byteLength src) (return dst)
20612061 in D.fold f (toStreamD src)
@@ -2083,11 +2083,11 @@ spliceWith sizer dst@(Array _ start end bound) src = do
20832083--
20842084-- Note that the returned array may be a mutated version of first array.
20852085--
2086−-- >>> splice = Array.spliceWith (+)
2086+-- >>> splice = MutArray.spliceWith (+)
20872087--
20882088-- /Pre-release/
20892089{-# INLINE splice #-}
2090−splice :: (MonadIO m, Unbox a) => Array a -> Array a -> m (Array a)
2090+splice :: (MonadIO m, Unbox a) => MutArray a -> MutArray a -> m (MutArray a)
20912091splice = spliceWith (+)
20922092
20932093-- | Like 'append' but the growth of the array is exponential. Whenever a new
@@ -2097,11 +2097,11 @@ splice = spliceWith (+)
20972097--
20982098-- Note that the returned array may be a mutated version of first array.
20992099--
2100−-- >>> spliceExp = Array.spliceWith (\l1 l2 -> max (l1 * 2) (l1 + l2))
2100+-- >>> spliceExp = MutArray.spliceWith (\l1 l2 -> max (l1 * 2) (l1 + l2))
21012101--
21022102-- /Pre-release/
21032103{-# INLINE spliceExp #-}
2104−spliceExp :: (MonadIO m, Unbox a) => Array a -> Array a -> m (Array a)
2104+spliceExp :: (MonadIO m, Unbox a) => MutArray a -> MutArray a -> m (MutArray a)
21052105spliceExp = spliceWith (\l1 l2 -> max (l1 * 2) (l1 + l2))
21062106
21072107-------------------------------------------------------------------------------
@@ -2111,8 +2111,8 @@ spliceExp = spliceWith (\l1 l2 -> max (l1 * 2) (l1 + l2))
21112111-- | Drops the separator byte
21122112{-# INLINE breakOn #-}
21132113breakOn :: MonadIO m
2114− => Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
2115−breakOn sep arr@Array{..} = asPtrUnsafe arr $ \p -> liftIO $ do
2114+ => Word8 -> MutArray Word8 -> m (MutArray Word8, Maybe (MutArray Word8))
2115+breakOn sep arr@MutArray{..} = asPtrUnsafe arr $ \p -> liftIO $ do
21162116 -- XXX Instead of using asPtrUnsafe (pinning memory) we can pass unlifted
21172117 -- Addr# to memchr and it should be safe (from ghc 8.4).
21182118 -- XXX We do not need memchr here, we can use a Haskell equivalent.
@@ -2122,13 +2122,13 @@ breakOn sep arr@Array{..} = asPtrUnsafe arr $ \p -> liftIO $ do
21222122 if loc == nullPtr
21232123 then (arr, Nothing)
21242124 else
2125− ( Array
2125+ ( MutArray
21262126 { arrContents = arrContents
21272127 , arrStart = arrStart
21282128 , arrEnd = arrStart + sepIndex -- exclude the separator
21292129 , arrBound = arrStart + sepIndex
21302130 }
2131− , Just $ Array
2131+ , Just $ MutArray
21322132 { arrContents = arrContents
21332133 , arrStart = arrStart + (sepIndex + 1)
21342134 , arrEnd = arrEnd
@@ -2139,8 +2139,8 @@ breakOn sep arr@Array{..} = asPtrUnsafe arr $ \p -> liftIO $ do
21392139-- | Create two slices of an array without copying the original array. The
21402140-- specified index @i@ is the first index of the second slice.
21412141--
2142−splitAt :: forall a. Unbox a => Int -> Array a -> (Array a, Array a)
2143−splitAt i arr@Array{..} =
2142+splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
2143+splitAt i arr@MutArray{..} =
21442144 let maxIndex = length arr - 1
21452145 in if i < 0
21462146 then error "sliceAt: negative array index"
@@ -2149,13 +2149,13 @@ splitAt i arr@Array{..} =
21492149 ++ " is beyond the maximum index " ++ show maxIndex
21502150 else let off = i * SIZE_OF(a)
21512151 p = arrStart + off
2152− in ( Array
2152+ in ( MutArray
21532153 { arrContents = arrContents
21542154 , arrStart = arrStart
21552155 , arrEnd = p
21562156 , arrBound = p
21572157 }
2158− , Array
2158+ , MutArray
21592159 { arrContents = arrContents
21602160 , arrStart = p
21612161 , arrEnd = arrEnd
@@ -2178,20 +2178,20 @@ castUnsafe ::
21782178#ifdef DEVBUILD
21792179 Unbox b =>
21802180#endif
2181− Array a -> Array b
2182−castUnsafe (Array contents start end bound) =
2183− Array contents start end bound
2181+ MutArray a -> MutArray b
2182+castUnsafe (MutArray contents start end bound) =
2183+ MutArray contents start end bound
21842184
2185−-- | Cast an @Array a@ into an @Array Word8@.
2185+-- | Cast an @MutArray a@ into an @MutArray Word8@.
21862186--
2187−asBytes :: Array a -> Array Word8
2187+asBytes :: MutArray a -> MutArray Word8
21882188asBytes = castUnsafe
21892189
21902190-- | Cast an array having elements of type @a@ into an array having elements of
21912191-- type @b@. The length of the array should be a multiple of the size of the
21922192-- target element otherwise 'Nothing' is returned.
21932193--
2194−cast :: forall a b. Unbox b => Array a -> Maybe (Array b)
2194+cast :: forall a b. Unbox b => MutArray a -> Maybe (MutArray b)
21952195cast arr =
21962196 let len = byteLength arr
21972197 r = len `mod` SIZE_OF(b)
@@ -2216,7 +2216,7 @@ cast arr =
22162216-- asPtr, the unsafety is implicit for any pointer operations. And we are safe
22172217-- from Haskell perspective because we will be pinning the memory.
22182218
2219−-- | Use an @Array a@ as @Ptr a@. This is useful when we want to pass an array
2219+-- | Use an @MutArray a@ as @Ptr a@. This is useful when we want to pass an array
22202220-- as a pointer to some operating system call or to a "safe" FFI call.
22212221--
22222222-- If the array is not pinned it is copied to pinned memory before passing it
@@ -2231,7 +2231,7 @@ cast arr =
22312231--
22322232-- /Pre-release/
22332233--
2234−asPtrUnsafe :: MonadIO m => Array a -> (Ptr a -> m b) -> m b
2234+asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
22352235asPtrUnsafe arr f = do
22362236 let contents = arrContents arr
22372237 !ptr = Ptr (byteArrayContents#
@@ -2252,7 +2252,7 @@ asPtrUnsafe arr f = do
22522252--
22532253-- /Pre-release/
22542254{-# INLINE cmp #-}
2255−cmp :: MonadIO m => Array a -> Array a -> m Ordering
2255+cmp :: MonadIO m => MutArray a -> MutArray a -> m Ordering
22562256cmp arr1 arr2 =
22572257 liftIO
22582258 $ do
@@ -2285,8 +2285,8 @@ cmp arr1 arr2 =
22852285-- /Pre-release/
22862286{-# INLINE strip #-}
22872287strip :: forall a m. (Unbox a, MonadIO m) =>
2288− (a -> Bool) -> Array a -> m (Array a)
2289−strip eq arr@Array{..} = liftIO $ do
2288+ (a -> Bool) -> MutArray a -> m (MutArray a)
2289+strip eq arr@MutArray{..} = liftIO $ do
22902290 st <- getStart arrStart
22912291 end <- getLast arrEnd st
22922292 return arr {arrStart = st, arrEnd = end, arrBound = end}
@@ -2328,7 +2328,7 @@ strip eq arr@Array{..} = liftIO $ do
23282328--
23292329-- /Pre-release/
23302330{-# INLINE bubble #-}
2331−bubble :: (MonadIO m, Unbox a) => (a -> a -> Ordering) -> Array a -> m ()
2331+bubble :: (MonadIO m, Unbox a) => (a -> a -> Ordering) -> MutArray a -> m ()
23322332bubble cmp0 arr =
23332333 when (l > 1) $ do
23342334 x <- getIndexUnsafe (l - 1) arr
core/src/Streamly/Internal/Data/Array/Type.hs+7−7
@@ -88,7 +88,7 @@ import GHC.Exts (IsList, IsString(..))
8888
8989import GHC.IO (unsafePerformIO)
9090import GHC.Ptr (Ptr(..))
91−import Streamly.Internal.Data.Array.Mut.Type (MutableByteArray)
91+import Streamly.Internal.Data.Array.Mut.Type (MutArray(..), MutableByteArray)
9292import Streamly.Internal.Data.Fold.Type (Fold(..))
9393import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
9494import Streamly.Internal.Data.Unboxed (Unbox, peekWith, sizeOf)
@@ -170,15 +170,15 @@ asPtrUnsafe arr = MA.asPtrUnsafe (unsafeThaw arr)
170170--
171171-- /Pre-release/
172172{-# INLINE unsafeFreeze #-}
173−unsafeFreeze :: MA.Array a -> Array a
174−unsafeFreeze (MA.Array ac as ae _) = Array ac as ae
173+unsafeFreeze :: MutArray a -> Array a
174+unsafeFreeze (MutArray ac as ae _) = Array ac as ae
175175
176176-- | Similar to 'unsafeFreeze' but uses 'MA.rightSize' on the mutable array
177177-- first.
178178{-# INLINE unsafeFreezeWithShrink #-}
179−unsafeFreezeWithShrink :: Unbox a => MA.Array a -> Array a
179+unsafeFreezeWithShrink :: Unbox a => MutArray a -> Array a
180180unsafeFreezeWithShrink arr = unsafePerformIO $ do
181− MA.Array ac as ae _ <- MA.rightSize arr
181+ MutArray ac as ae _ <- MA.rightSize arr
182182 return $ Array ac as ae
183183
184184-- | Makes a mutable array using the underlying memory of the immutable array.
@@ -190,8 +190,8 @@ unsafeFreezeWithShrink arr = unsafePerformIO $ do
190190--
191191-- /Pre-release/
192192{-# INLINE unsafeThaw #-}
193−unsafeThaw :: Array a -> MA.Array a
194−unsafeThaw (Array ac as ae) = MA.Array ac as ae ae
193+unsafeThaw :: Array a -> MutArray a
194+unsafeThaw (Array ac as ae) = MutArray ac as ae ae
195195
196196-------------------------------------------------------------------------------
197197-- Pinning & Unpinning
core/src/Streamly/Internal/Data/Fold.hs+5−4
@@ -321,6 +321,7 @@ import Data.Int (Int64)
321321import Data.Proxy (Proxy(..))
322322import Data.Word (Word32)
323323import Foreign.Storable (Storable, peek)
324+import Streamly.Internal.Data.Array.Mut.Type (MutArray(..))
324325import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
325326import Streamly.Internal.Data.Pipe.Type (Pipe (..), PipeState(..))
326327import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
@@ -2450,7 +2451,7 @@ unfoldMany (Unfold ustep inject) (Fold fstep initial extract) =
24502451bottomBy :: (MonadIO m, Unbox a) =>
24512452 (a -> a -> Ordering)
24522453 -> Int
2453− -> Fold m a (MA.Array a)
2454+ -> Fold m a (MutArray a)
24542455bottomBy cmp n = Fold step initial extract
24552456
24562457 where
@@ -2496,7 +2497,7 @@ bottomBy cmp n = Fold step initial extract
24962497topBy :: (MonadIO m, Unbox a) =>
24972498 (a -> a -> Ordering)
24982499 -> Int
2499− -> Fold m a (MA.Array a)
2500+ -> Fold m a (MutArray a)
25002501topBy cmp = bottomBy (flip cmp)
25012502
25022503-- | Fold the input stream to top n elements.
@@ -2511,7 +2512,7 @@ topBy cmp = bottomBy (flip cmp)
25112512--
25122513-- /Pre-release/
25132514{-# INLINE top #-}
2514−top :: (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (MA.Array a)
2515+top :: (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (MutArray a)
25152516top = bottomBy $ flip compare
25162517
25172518-- | Fold the input stream to bottom n elements.
@@ -2526,7 +2527,7 @@ top = bottomBy $ flip compare
25262527--
25272528-- /Pre-release/
25282529{-# INLINE bottom #-}
2529−bottom :: (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (MA.Array a)
2530+bottom :: (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (MutArray a)
25302531bottom = bottomBy compare
25312532
25322533------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Ring/Unboxed.hs+5−5
@@ -82,7 +82,7 @@ import Foreign.Ptr (plusPtr, minusPtr, castPtr)
8282import Streamly.Internal.Data.Unboxed as Unboxed (Unbox, peekWith)
8383import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
8484import GHC.Ptr (Ptr(..))
85−import Streamly.Internal.Data.Array.Mut.Type (Array)
85+import Streamly.Internal.Data.Array.Mut.Type (MutArray)
8686import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..), lmap)
8787import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
8888import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
@@ -190,7 +190,7 @@ writeN = undefined
190190-------------------------------------------------------------------------------
191191
192192-- | Cast a mutable array to a ring array.
193−fromArray :: Array a -> Ring a
193+fromArray :: MutArray a -> Ring a
194194fromArray = undefined
195195
196196-------------------------------------------------------------------------------
@@ -337,7 +337,7 @@ read = Unfold step return
337337-- /Unimplemented/
338338{-# INLINE_NORMAL readRev #-}
339339readRev :: -- forall m a. (MonadIO m, Storable a) =>
340− Unfold m (Array a) a
340+ Unfold m (MutArray a) a
341341readRev = undefined
342342
343343-------------------------------------------------------------------------------
@@ -353,7 +353,7 @@ readRev = undefined
353353-- /Unimplemented/
354354{-# INLINE_NORMAL ringsOf #-}
355355ringsOf :: -- forall m a. (MonadIO m, Storable a) =>
356− Int -> Stream m a -> Stream m (Array a)
356+ Int -> Stream m a -> Stream m (MutArray a)
357357ringsOf = undefined -- Stream.scan (writeN n)
358358
359359-------------------------------------------------------------------------------
@@ -557,7 +557,7 @@ data Tuple4' a b c d = Tuple4' !a !b !c !d deriving Show
557557-- of the ring at a particular time.
558558{-# INLINE slidingWindowWith #-}
559559slidingWindowWith :: forall m a b. (MonadIO m, Storable a, Unbox a)
560− => Int -> Fold m ((a, Maybe a), m (Array a)) b -> Fold m a b
560+ => Int -> Fold m ((a, Maybe a), m (MutArray a)) b -> Fold m a b
561561slidingWindowWith n (Fold step1 initial1 extract1) = Fold step initial extract
562562
563563 where
core/src/Streamly/Internal/Data/Stream/Chunked.hs+2−1
@@ -78,6 +78,7 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
7878
7979import Streamly.Data.Fold (Fold)
8080import Streamly.Internal.Data.Array.Type (Array(..))
81+import Streamly.Internal.Data.Array.Mut.Type (MutArray)
8182import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
8283import Streamly.Internal.Data.Parser (ParseError(..))
8384import Streamly.Internal.Data.Stream.StreamD (Stream)
@@ -474,7 +475,7 @@ splitAtArrayListRev n ls
474475-- stream.
475476{-# INLINE spliceArraysLenUnsafe #-}
476477spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
477− => Int -> Stream m (MA.Array a) -> m (MA.Array a)
478+ => Int -> Stream m (MutArray a) -> m (MutArray a)
478479spliceArraysLenUnsafe len buffered = do
479480 arr <- liftIO $ MA.newPinned len
480481 D.foldlM' MA.spliceUnsafe (return arr) buffered
src/Streamly/Internal/Network/Socket.hs+1−1
@@ -99,7 +99,7 @@ import qualified Streamly.Data.Fold as FL
9999import qualified Streamly.Internal.Data.Array.Type as A
100100 (unsafeFreeze, asPtrUnsafe, byteLength, writeNUnsafe, arraysOf)
101101import qualified Streamly.Internal.Data.Array.Mut as MArray
102− (Array(..), newPinnedBytes, asPtrUnsafe)
102+ (MutArray(..), newPinnedBytes, asPtrUnsafe)
103103import qualified Streamly.Internal.Data.Stream.StreamD as S
104104import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
105105 (Stream(..), Step(..))
test/Streamly/Test/Data/Array.hs+3−2
@@ -15,6 +15,7 @@ import Data.Word(Word8)
1515import Foreign.Storable (peek)
1616import GHC.Ptr (plusPtr)
1717import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
18+import Streamly.Internal.Data.Array.Mut.Type (MutArray)
1819import Test.QuickCheck (chooseInt, listOf)
1920
2021import qualified Streamly.Internal.Data.Array as A
@@ -159,8 +160,8 @@ testBubbleDesc = testBubbleWith False
159160
160161testByteLengthWithMA :: forall a. Unbox a => a -> IO ()
161162testByteLengthWithMA _ = do
162− arrA <- MA.newPinned 100 :: IO (MA.Array a)
163− let arrW8 = MA.castUnsafe arrA :: MA.Array Word8
163+ arrA <- MA.newPinned 100 :: IO (MutArray a)
164+ let arrW8 = MA.castUnsafe arrA :: MutArray Word8
164165 MA.byteLength arrA `shouldBe` MA.length arrW8
165166
166167testBreakOn :: [Word8] -> Word8 -> [Word8] -> Maybe [Word8] -> IO ()