on master help sign in
Make Array APIs uniform
commitad90f17e5738bf89a2b428f509704f669a596133 authorHarendra Kumar <harendra@composewell.com> dateTue 18 Jul 2023, 02:17 (3 years ago) parent3187e37
8 files changed+80−48
benchmark/Streamly/Benchmark/Data/Array/Mut.hs+2−2 core/src/Streamly/Data/Array/Generic.hs+4 core/src/Streamly/Data/MutArray.hs+8−2 core/src/Streamly/Data/MutArray/Generic.hs+20−11 core/src/Streamly/Internal/Data/Array/Generic.hs+1 core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+26−15 core/src/Streamly/Internal/Data/Array/Mut.hs+1−1 core/src/Streamly/Internal/Data/Array/Mut/Type.hs+18−17
benchmark/Streamly/Benchmark/Data/Array/Mut.hs+2−2
@@ -181,11 +181,11 @@ unfoldReadRevDrain = drain . Stream.unfold MArray.readerRev
181181
182182{-# INLINE toStreamDRevDrain #-}
183183toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
184−toStreamDRevDrain = drain . MArray.toStreamDRev
184+toStreamDRevDrain = drain . MArray.readRev
185185
186186{-# INLINE toStreamDDrain #-}
187187toStreamDDrain :: MonadIO m => Stream Int -> m ()
188−toStreamDDrain = drain . MArray.toStreamD
188+toStreamDDrain = drain . MArray.read
189189
190190{-# INLINE unfoldFold #-}
191191unfoldFold :: MonadIO m => Stream Int -> m Int
core/src/Streamly/Data/Array/Generic.hs+4
@@ -22,12 +22,16 @@ module Streamly.Data.Array.Generic
2222 , A.writeN
2323 , A.write
2424
25+ -- * Conversion
26+ , A.toList
27+
2528 -- * Streams
2629 , A.read
2730 , A.readRev
2831
2932 -- * Unfolds
3033 , A.reader
34+ -- , A.readerRev
3135
3236 -- * Random Access
3337 , A.length
core/src/Streamly/Data/MutArray.hs+8−2
@@ -46,13 +46,15 @@ module Streamly.Data.MutArray
4646 , snoc
4747
4848 -- * Appending streams
49− , writeAppendN
50− , writeAppend
49+ , writeAppendN -- XXX Rename to writeSnocN?
50+ , writeAppend -- XXX Rename to writeSnoc?
5151
5252 -- * Inplace mutation
5353 , putIndex
5454 , putIndexUnsafe
55+ , modifyIndex
5556 , modifyIndexUnsafe
57+ , modify
5658
5759 -- * Random access
5860 , getIndex
@@ -61,6 +63,10 @@ module Streamly.Data.MutArray
6163 -- * Conversion
6264 , toList
6365
66+ -- * Streams
67+ , read
68+ , readRev
69+
6470 -- * Unfolds
6571 , reader
6672 , readerRev
core/src/Streamly/Data/MutArray/Generic.hs+20−11
@@ -23,27 +23,36 @@ module Streamly.Data.MutArray.Generic
2323 MutArray
2424
2525 -- * Construction
26+ , new
27+ , fromListN
28+ , fromList
2629 , writeN
30+ , write
2731
2832 -- * Appending elements
29− , new
3033 , snoc
3134
35+ -- * Inplace mutation
36+ , putIndex
37+ , putIndexUnsafe
38+ , modifyIndex
39+ , modifyIndexUnsafe
40+ -- , modify
41+
42+ -- * Random reads
43+ , getIndex
44+ , getIndexUnsafe
45+
3246 -- * Conversion
3347 , toList
3448
35− -- * Unfolds
36− , reader
37−
38− -- * Streams
49+ -- * Streams
3950 , read
51+ , readRev
4052
41− -- * Random reads
42− , getIndex
43−
44− -- * Inplace mutation
45− , putIndex
46− , modifyIndex
53+ -- * Unfolds
54+ , reader
55+ -- , readerRev
4756
4857 -- * Size
4958 , length
core/src/Streamly/Internal/Data/Array/Generic.hs+1
@@ -210,6 +210,7 @@ getIndexUnsafe i arr =
210210
211211-- | Lookup the element at the given index. Index starts from 0.
212212--
213+{-# INLINE getIndex #-}
213214getIndex :: Int -> Array a -> Maybe a
214215getIndex i arr@Array {..} =
215216 if i >= 0 && i < arrLen
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+26−15
@@ -28,15 +28,14 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
2828 , writeWith
2929 , write
3030 , fromStreamN
31+ , fromStream
3132
3233 -- , writeRevN
3334 -- , writeRev
3435
3536 -- ** From containers
36− -- , fromListN
37− -- , fromList
38− -- , fromStreamDN
39− -- , fromStreamD
37+ , fromListN
38+ , fromList
4039
4140 -- * Random writes
4241 , putIndex
@@ -529,10 +528,10 @@ getSlice index len arr@MutArray{..} =
529528toList :: MonadIO m => MutArray a -> m [a]
530529toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
531530
532−-- | Generates a stream from the elements of @MutArray@
531+-- | Generates a stream from the elements of a @MutArray@.
532+--
533+-- >>> read = Stream.unfold MutArray.reader
533534--
534−-- @read = D.unfold reader@
535−-- /Pre-release/
536535{-# INLINE_NORMAL read #-}
537536read :: MonadIO m => MutArray a -> D.Stream m a
538537read arr@MutArray{..} =
@@ -639,6 +638,26 @@ writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
639638write :: MonadIO m => Fold m a (MutArray a)
640639write = writeWith arrayChunkSize
641640
641+-- | Create a 'MutArray' from the first @n@ elements of a stream. The
642+-- array is allocated to size @n@, if the stream terminates before @n@
643+-- elements then the array may hold less than @n@ elements.
644+--
645+{-# INLINE fromStreamN #-}
646+fromStreamN :: MonadIO m => Int -> Stream m a -> m (MutArray a)
647+fromStreamN n = D.fold (writeN n)
648+
649+{-# INLINE fromStream #-}
650+fromStream :: MonadIO m => Stream m a -> m (MutArray a)
651+fromStream = D.fold write
652+
653+{-# INLINABLE fromListN #-}
654+fromListN :: MonadIO m => Int -> [a] -> m (MutArray a)
655+fromListN n xs = fromStreamN n $ D.fromList xs
656+
657+{-# INLINABLE fromList #-}
658+fromList :: MonadIO m => [a] -> m (MutArray a)
659+fromList xs = fromStream $ D.fromList xs
660+
642661-------------------------------------------------------------------------------
643662-- Unfolds
644663-------------------------------------------------------------------------------
@@ -795,11 +814,3 @@ strip p arr = liftIO $ do
795814 if p r
796815 then getIndexL (idx + 1)
797816 else return idx
798−
799−-- | Create a 'MutArray' from the first @n@ elements of a stream. The
800−-- array is allocated to size @n@, if the stream terminates before @n@
801−-- elements then the array may hold less than @n@ elements.
802−--
803−{-# INLINE fromStreamN #-}
804−fromStreamN :: MonadIO m => Int -> Stream m a -> m (MutArray a)
805−fromStreamN n = D.fold (writeN n)
core/src/Streamly/Internal/Data/Array/Mut.hs+1−1
@@ -38,7 +38,7 @@ splitOn :: (MonadIO m, Unbox a) =>
3838 (a -> Bool) -> MutArray a -> Stream m (MutArray a)
3939splitOn predicate arr =
4040 fmap (\(i, len) -> getSliceUnsafe i len arr)
41− $ D.sliceOnSuffix predicate (toStreamD arr)
41+ $ D.sliceOnSuffix predicate (read arr)
4242
4343-- | Generate a stream of array slice descriptors ((index, len)) of specified
4444-- length from an array, starting from the supplied array index. The last slice
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+18−17
@@ -119,8 +119,8 @@ module Streamly.Internal.Data.Array.Mut.Type
119119 , toStreamDRevWith
120120 , toStreamKWith
121121 , toStreamKRevWith
122− , toStreamD
123− , toStreamDRev
122+ , read
123+ , readRev
124124 , toStreamK
125125 , toStreamKRev
126126 , toList
@@ -526,6 +526,8 @@ putIndices arr = FL.foldlM' step (return ())
526526
527527-- | Modify a given index of an array using a modifier function.
528528--
529+-- Unsafe because it does not check the bounds of the array.
530+--
529531-- /Pre-release/
530532modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
531533 Int -> MutArray a -> (a -> (a, b)) -> m b
@@ -552,7 +554,6 @@ modifyIndex i MutArray{..} f = do
552554 return res
553555 else invalidIndex "modifyIndex" i
554556
555−
556557-- | Modify the array indices generated by the supplied stream.
557558--
558559-- /Pre-release/
@@ -571,6 +572,8 @@ modifyIndices arr f = FL.foldlM' step initial
571572
572573-- | Modify each element of an array using the supplied modifier function.
573574--
575+-- This is an in-place equivalent of an immutable map operation.
576+--
574577-- /Pre-release/
575578modify :: forall m a. (MonadIO m, Unbox a)
576579 => MutArray a -> (a -> a) -> m ()
@@ -1549,14 +1552,13 @@ toStreamDWith liftio MutArray{..} = D.Stream step arrStart
15491552 r <- peekWith arrContents p
15501553 return $ D.Yield r (INDEX_NEXT(p,a))
15511554
1552−-- | Use the 'reader' unfold instead.
1555+-- | Convert a 'MutArray' into a stream.
15531556--
1554−-- @toStreamD = D.unfold reader@
1557+-- >>> read = Stream.unfold MutArray.reader
15551558--
1556−-- We can try this if the unfold has any performance issues.
1557−{-# INLINE_NORMAL toStreamD #-}
1558−toStreamD :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1559−toStreamD = toStreamDWith liftIO
1559+{-# INLINE_NORMAL read #-}
1560+read :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1561+read = toStreamDWith liftIO
15601562
15611563{-# INLINE toStreamKWith #-}
15621564toStreamKWith ::
@@ -1591,14 +1593,13 @@ toStreamDRevWith liftio MutArray{..} =
15911593 r <- peekWith arrContents p
15921594 return $ D.Yield r (INDEX_PREV(p,a))
15931595
1594−-- | Use the 'readerRev' unfold instead.
1596+-- | Convert a 'MutArray' into a stream in reverse order.
15951597--
1596−-- @toStreamDRev = D.unfold readerRev@
1598+-- >>> readRev = Stream.unfold MutArray.readerRev
15971599--
1598−-- We can try this if the unfold has any perf issues.
1599−{-# INLINE_NORMAL toStreamDRev #-}
1600−toStreamDRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1601−toStreamDRev = toStreamDRevWith liftIO
1600+{-# INLINE_NORMAL readRev #-}
1601+readRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1602+readRev = toStreamDRevWith liftIO
16021603
16031604{-# INLINE toStreamKRevWith #-}
16041605toStreamKRevWith ::
@@ -1629,12 +1630,12 @@ toStreamKRev = toStreamKRevWith liftIO
16291630-- | Strict left fold of an array.
16301631{-# INLINE_NORMAL foldl' #-}
16311632foldl' :: (MonadIO m, Unbox a) => (b -> a -> b) -> b -> MutArray a -> m b
1632−foldl' f z arr = D.foldl' f z $ toStreamD arr
1633+foldl' f z arr = D.foldl' f z $ read arr
16331634
16341635-- | Right fold of an array.
16351636{-# INLINE_NORMAL foldr #-}
16361637foldr :: (MonadIO m, Unbox a) => (a -> b -> b) -> b -> MutArray a -> m b
1637−foldr f z arr = D.foldr f z $ toStreamD arr
1638+foldr f z arr = D.foldr f z $ read arr
16381639
16391640-------------------------------------------------------------------------------
16401641-- Folds