on master help sign in
Rename new/write in MutArray/Generic
8927d11Harendra Kumar
core/src/Streamly/Data/MutArray/Generic.hs+8−3
@@ -23,11 +23,11 @@ module Streamly.Data.MutArray.Generic
23 MutArray
23 MutArray
24
24
25 -- * Construction
25 -- * Construction
26− , new
26+ , emptyOf
27 , fromListN
27 , fromListN
28 , fromList
28 , fromList
29− , writeN
29+ , createOf
30− , write
30+ , create
31
31
32 -- * Appending elements
32 -- * Appending elements
33 , snoc
33 , snoc
@@ -56,6 +56,11 @@ module Streamly.Data.MutArray.Generic
56
56
57 -- * Size
57 -- * Size
58 , length
58 , length
59+
60+ -- * Deprecated
61+ , new
62+ , writeN
63+ , write
59 )
64 )
60where
65where
61
66
core/src/Streamly/Internal/Data/Array/Generic.hs+3−3
@@ -106,11 +106,11 @@ nil = unsafePerformIO $ unsafeFreeze <$> MArray.nil
106
106
107{-# INLINE_NORMAL writeN #-}
107{-# INLINE_NORMAL writeN #-}
108writeN :: MonadIO m => Int -> Fold m a (Array a)
108writeN :: MonadIO m => Int -> Fold m a (Array a)
109−writeN = fmap unsafeFreeze <$> MArray.writeN
109+writeN = fmap unsafeFreeze <$> MArray.createOf
110
110
111{-# INLINE_NORMAL writeWith #-}
111{-# INLINE_NORMAL writeWith #-}
112writeWith :: MonadIO m => Int -> Fold m a (Array a)
112writeWith :: MonadIO m => Int -> Fold m a (Array a)
113−writeWith elemCount = unsafeFreeze <$> MArray.writeWith elemCount
113+writeWith elemCount = unsafeFreeze <$> MArray.createWith elemCount
114
114
115-- | Fold the whole input to a single array.
115-- | Fold the whole input to a single array.
116--
116--
@@ -118,7 +118,7 @@ writeWith elemCount = unsafeFreeze <$> MArray.writeWith elemCount
118--
118--
119{-# INLINE write #-}
119{-# INLINE write #-}
120write :: MonadIO m => Fold m a (Array a)
120write :: MonadIO m => Fold m a (Array a)
121−write = fmap unsafeFreeze MArray.write
121+write = fmap unsafeFreeze MArray.create
122
122
123fromPureStream :: Stream Identity a -> Array a
123fromPureStream :: Stream Identity a -> Array a
124fromPureStream x =
124fromPureStream x =
core/src/Streamly/Internal/Data/MutArray/Generic.hs+59−24
@@ -19,14 +19,14 @@ module Streamly.Internal.Data.MutArray.Generic
19 , nil
19 , nil
20
20
21 -- *** Uninitialized Arrays
21 -- *** Uninitialized Arrays
22− , new
22+ , emptyOf
23 -- , newArrayWith
23 -- , newArrayWith
24
24
25 -- *** From streams
25 -- *** From streams
26− , writeNUnsafe
26+ , unsafeCreateOf
27− , writeN
27+ , createOf
28− , writeWith
28+ , createWith -- createOfMin/createMin/createGE?
29− , write
29+ , create
30 , fromStreamN
30 , fromStreamN
31 , fromStream
31 , fromStream
32 , fromPureStream
32 , fromPureStream
@@ -160,6 +160,13 @@ module Streamly.Internal.Data.MutArray.Generic
160 -- , appendSliceFrom
160 -- , appendSliceFrom
161
161
162 , clone
162 , clone
163+
164+ -- * Deprecated
165+ , new
166+ , writeNUnsafe
167+ , writeN
168+ , writeWith
169+ , write
163 )
170 )
164where
171where
165
172
@@ -236,13 +243,13 @@ bottomElement =
236-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
243-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
237-- that we do not have to write undefined or error in the whole array.
244-- that we do not have to write undefined or error in the whole array.
238
245
239−-- | @new count@ allocates a zero length array that can be extended to hold
246+-- | @emptyOf count@ allocates a zero length array that can be extended to hold
240-- up to 'count' items without reallocating.
247-- up to 'count' items without reallocating.
241--
248--
242-- /Pre-release/
249-- /Pre-release/
243−{-# INLINE new #-}
250+{-# INLINE emptyOf #-}
244−new :: MonadIO m => Int -> m (MutArray a)
251+emptyOf :: MonadIO m => Int -> m (MutArray a)
245−new n@(I# n#) =
252+emptyOf n@(I# n#) =
246 liftIO
253 liftIO
247 $ IO
254 $ IO
248 $ \s# ->
255 $ \s# ->
@@ -251,6 +258,12 @@ new n@(I# n#) =
251 let ma = MutArray arr# 0 0 n
258 let ma = MutArray arr# 0 0 n
252 in (# s1#, ma #)
259 in (# s1#, ma #)
253
260
261+-- XXX Deprecate in major
262+-- {-# DEPRECATED new "Please use emptyOf instead." #-}
263+{-# INLINE new #-}
264+new :: MonadIO m => Int -> m (MutArray a)
265+new = emptyOf
266+
254-- XXX This could be pure?
267-- XXX This could be pure?
255
268
256-- |
269-- |
@@ -589,16 +602,16 @@ readRev arr@MutArray{..} =
589arrayChunkSize :: Int
602arrayChunkSize :: Int
590arrayChunkSize = 1024
603arrayChunkSize = 1024
591
604
592−-- | Like 'writeN' but does not check the array bounds when writing. The fold
605+-- | Like 'createOf' but does not check the array bounds when writing. The fold
593-- driver must not call the step function more than 'n' times otherwise it will
606-- driver must not call the step function more than 'n' times otherwise it will
594-- corrupt the memory and crash. This function exists mainly because any
607-- corrupt the memory and crash. This function exists mainly because any
595-- conditional in the step function blocks fusion causing 10x performance
608-- conditional in the step function blocks fusion causing 10x performance
596-- slowdown.
609-- slowdown.
597--
610--
598-- /Pre-release/
611-- /Pre-release/
599−{-# INLINE_NORMAL writeNUnsafe #-}
612+{-# INLINE_NORMAL unsafeCreateOf #-}
600−writeNUnsafe :: MonadIO m => Int -> Fold m a (MutArray a)
613+unsafeCreateOf :: MonadIO m => Int -> Fold m a (MutArray a)
601−writeNUnsafe n = Fold step initial return return
614+unsafeCreateOf n = Fold step initial return return
602
615
603 where
616 where
604
617
@@ -606,36 +619,47 @@ writeNUnsafe n = Fold step initial return return
606
619
607 step arr x = FL.Partial <$> snocUnsafe arr x
620 step arr x = FL.Partial <$> snocUnsafe arr x
608
621
622+{-# DEPRECATED writeNUnsafe "Please use unsafeCreateOf instead." #-}
623+{-# INLINE writeNUnsafe #-}
624+writeNUnsafe :: MonadIO m => Int -> Fold m a (MutArray a)
625+writeNUnsafe = unsafeCreateOf
626+
609−-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
627+-- | @createOf n@ folds a maximum of @n@ elements from the input stream to an
610-- 'Array'.
628-- 'Array'.
611--
629--
612−-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
630+-- >>> createOf n = Fold.take n (MutArray.unsafeCreateOf n)
613--
631--
614-- /Pre-release/
632-- /Pre-release/
615−{-# INLINE_NORMAL writeN #-}
633+{-# INLINE_NORMAL createOf #-}
634+createOf :: MonadIO m => Int -> Fold m a (MutArray a)
635+createOf n = FL.take n $ unsafeCreateOf n
636+
637+-- XXX Deprecate in major
638+-- {-# DEPRECATED writeN "Please use createOf instead." #-}
639+{-# INLINE writeN #-}
616writeN :: MonadIO m => Int -> Fold m a (MutArray a)
640writeN :: MonadIO m => Int -> Fold m a (MutArray a)
617−writeN n = FL.take n $ writeNUnsafe n
641+writeN = createOf
618
642
619-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.pinnedNew n)
643-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.pinnedNew n)
620-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
644-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
621-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
645-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
622
646
623−-- | @writeWith minCount@ folds the whole input to a single array. The array
647+-- | @createWith minCount@ folds the whole input to a single array. The array
624-- starts at a size big enough to hold minCount elements, the size is doubled
648-- starts at a size big enough to hold minCount elements, the size is doubled
625-- every time the array needs to be grown.
649-- every time the array needs to be grown.
626--
650--
627-- /Caution! Do not use this on infinite streams./
651-- /Caution! Do not use this on infinite streams./
628--
652--
629-- /Pre-release/
653-- /Pre-release/
630−{-# INLINE_NORMAL writeWith #-}
654+{-# INLINE_NORMAL createWith #-}
631−writeWith :: MonadIO m => Int -> Fold m a (MutArray a)
655+createWith :: MonadIO m => Int -> Fold m a (MutArray a)
632-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (pinnedNew n)
656-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (pinnedNew n)
633−writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
657+createWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
634
658
635 where
659 where
636
660
637 initial = do
661 initial = do
638− when (elemCount < 0) $ error "writeWith: elemCount is negative"
662+ when (elemCount < 0) $ error "createWith: elemCount is negative"
639 new elemCount
663 new elemCount
640
664
641 step arr@(MutArray _ start end bound) x
665 step arr@(MutArray _ start end bound) x
@@ -649,16 +673,27 @@ writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
649 -- extract = rightSize
673 -- extract = rightSize
650 extract = return
674 extract = return
651
675
676+{-# DEPRECATED writeWith "Please use createWith instead." #-}
677+{-# INLINE writeWith #-}
678+writeWith :: MonadIO m => Int -> Fold m a (MutArray a)
679+writeWith = createWith
680+
652-- | Fold the whole input to a single array.
681-- | Fold the whole input to a single array.
653--
682--
654−-- Same as 'writeWith' using an initial array size of 'arrayChunkSize' bytes
683+-- Same as 'createWith' using an initial array size of 'arrayChunkSize' bytes
655-- rounded up to the element size.
684-- rounded up to the element size.
656--
685--
657-- /Caution! Do not use this on infinite streams./
686-- /Caution! Do not use this on infinite streams./
658--
687--
688+{-# INLINE create #-}
689+create :: MonadIO m => Fold m a (MutArray a)
690+create = writeWith arrayChunkSize
691+
692+-- XXX Deprecate in major
693+-- {-# DEPRECATED write "Please use create instead." #-}
659{-# INLINE write #-}
694{-# INLINE write #-}
660write :: MonadIO m => Fold m a (MutArray a)
695write :: MonadIO m => Fold m a (MutArray a)
661−write = writeWith arrayChunkSize
696+write = create
662
697
663-- | Create a 'MutArray' from the first @n@ elements of a stream. The
698-- | Create a 'MutArray' from the first @n@ elements of a stream. The
664-- array is allocated to size @n@, if the stream terminates before @n@
699-- array is allocated to size @n@, if the stream terminates before @n@