on master help sign in
Make Array APIs uniform
ad90f17Harendra Kumar
benchmark/Streamly/Benchmark/Data/Array/Mut.hs+2−2
@@ -181,11 +181,11 @@ unfoldReadRevDrain = drain . Stream.unfold MArray.readerRev
181
181
182{-# INLINE toStreamDRevDrain #-}
182{-# INLINE toStreamDRevDrain #-}
183toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
183toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
184−toStreamDRevDrain = drain . MArray.toStreamDRev
184+toStreamDRevDrain = drain . MArray.readRev
185
185
186{-# INLINE toStreamDDrain #-}
186{-# INLINE toStreamDDrain #-}
187toStreamDDrain :: MonadIO m => Stream Int -> m ()
187toStreamDDrain :: MonadIO m => Stream Int -> m ()
188−toStreamDDrain = drain . MArray.toStreamD
188+toStreamDDrain = drain . MArray.read
189
189
190{-# INLINE unfoldFold #-}
190{-# INLINE unfoldFold #-}
191unfoldFold :: MonadIO m => Stream Int -> m Int
191unfoldFold :: MonadIO m => Stream Int -> m Int
core/src/Streamly/Data/Array/Generic.hs+4
@@ -22,12 +22,16 @@ module Streamly.Data.Array.Generic
22 , A.writeN
22 , A.writeN
23 , A.write
23 , A.write
24
24
25+ -- * Conversion
26+ , A.toList
27+
25 -- * Streams
28 -- * Streams
26 , A.read
29 , A.read
27 , A.readRev
30 , A.readRev
28
31
29 -- * Unfolds
32 -- * Unfolds
30 , A.reader
33 , A.reader
34+ -- , A.readerRev
31
35
32 -- * Random Access
36 -- * Random Access
33 , A.length
37 , A.length
core/src/Streamly/Data/MutArray.hs+8−2
@@ -46,13 +46,15 @@ module Streamly.Data.MutArray
46 , snoc
46 , snoc
47
47
48 -- * Appending streams
48 -- * Appending streams
49− , writeAppendN
49+ , writeAppendN -- XXX Rename to writeSnocN?
50− , writeAppend
50+ , writeAppend -- XXX Rename to writeSnoc?
51
51
52 -- * Inplace mutation
52 -- * Inplace mutation
53 , putIndex
53 , putIndex
54 , putIndexUnsafe
54 , putIndexUnsafe
55+ , modifyIndex
55 , modifyIndexUnsafe
56 , modifyIndexUnsafe
57+ , modify
56
58
57 -- * Random access
59 -- * Random access
58 , getIndex
60 , getIndex
@@ -61,6 +63,10 @@ module Streamly.Data.MutArray
61 -- * Conversion
63 -- * Conversion
62 , toList
64 , toList
63
65
66+ -- * Streams
67+ , read
68+ , readRev
69+
64 -- * Unfolds
70 -- * Unfolds
65 , reader
71 , reader
66 , readerRev
72 , readerRev
core/src/Streamly/Data/MutArray/Generic.hs+20−11
@@ -23,27 +23,36 @@ module Streamly.Data.MutArray.Generic
23 MutArray
23 MutArray
24
24
25 -- * Construction
25 -- * Construction
26+ , new
27+ , fromListN
28+ , fromList
26 , writeN
29 , writeN
30+ , write
27
31
28 -- * Appending elements
32 -- * Appending elements
29− , new
30 , snoc
33 , snoc
31
34
35+ -- * Inplace mutation
36+ , putIndex
37+ , putIndexUnsafe
38+ , modifyIndex
39+ , modifyIndexUnsafe
40+ -- , modify
41+
42+ -- * Random reads
43+ , getIndex
44+ , getIndexUnsafe
45+
32 -- * Conversion
46 -- * Conversion
33 , toList
47 , toList
34
48
35− -- * Unfolds
36− , reader
37−
38− -- * Streams
49+ -- * Streams
39 , read
50 , read
51+ , readRev
40
52
41− -- * Random reads
53+ -- * Unfolds
42− , getIndex
54+ , reader
43−
55+ -- , readerRev
44− -- * Inplace mutation
45− , putIndex
46− , modifyIndex
47
56
48 -- * Size
57 -- * Size
49 , length
58 , length
core/src/Streamly/Internal/Data/Array/Generic.hs+1
@@ -210,6 +210,7 @@ getIndexUnsafe i arr =
210
210
211-- | Lookup the element at the given index. Index starts from 0.
211-- | Lookup the element at the given index. Index starts from 0.
212--
212--
213+{-# INLINE getIndex #-}
213getIndex :: Int -> Array a -> Maybe a
214getIndex :: Int -> Array a -> Maybe a
214getIndex i arr@Array {..} =
215getIndex i arr@Array {..} =
215 if i >= 0 && i < arrLen
216 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
28 , writeWith
28 , writeWith
29 , write
29 , write
30 , fromStreamN
30 , fromStreamN
31+ , fromStream
31
32
32 -- , writeRevN
33 -- , writeRevN
33 -- , writeRev
34 -- , writeRev
34
35
35 -- ** From containers
36 -- ** From containers
36− -- , fromListN
37+ , fromListN
37− -- , fromList
38+ , fromList
38− -- , fromStreamDN
39− -- , fromStreamD
40
39
41 -- * Random writes
40 -- * Random writes
42 , putIndex
41 , putIndex
@@ -529,10 +528,10 @@ getSlice index len arr@MutArray{..} =
529toList :: MonadIO m => MutArray a -> m [a]
528toList :: MonadIO m => MutArray a -> m [a]
530toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
529toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
531
530
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
533--
534--
534−-- @read = D.unfold reader@
535−-- /Pre-release/
536{-# INLINE_NORMAL read #-}
535{-# INLINE_NORMAL read #-}
537read :: MonadIO m => MutArray a -> D.Stream m a
536read :: MonadIO m => MutArray a -> D.Stream m a
538read arr@MutArray{..} =
537read arr@MutArray{..} =
@@ -639,6 +638,26 @@ writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
639write :: MonadIO m => Fold m a (MutArray a)
638write :: MonadIO m => Fold m a (MutArray a)
640write = writeWith arrayChunkSize
639write = writeWith arrayChunkSize
641
640
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+
642-------------------------------------------------------------------------------
661-------------------------------------------------------------------------------
643-- Unfolds
662-- Unfolds
644-------------------------------------------------------------------------------
663-------------------------------------------------------------------------------
@@ -795,11 +814,3 @@ strip p arr = liftIO $ do
795 if p r
814 if p r
796 then getIndexL (idx + 1)
815 then getIndexL (idx + 1)
797 else return idx
816 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) =>
38 (a -> Bool) -> MutArray a -> Stream m (MutArray a)
38 (a -> Bool) -> MutArray a -> Stream m (MutArray a)
39splitOn predicate arr =
39splitOn predicate arr =
40 fmap (\(i, len) -> getSliceUnsafe i len arr)
40 fmap (\(i, len) -> getSliceUnsafe i len arr)
41− $ D.sliceOnSuffix predicate (toStreamD arr)
41+ $ D.sliceOnSuffix predicate (read arr)
42
42
43-- | Generate a stream of array slice descriptors ((index, len)) of specified
43-- | Generate a stream of array slice descriptors ((index, len)) of specified
44-- length from an array, starting from the supplied array index. The last slice
44-- 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
119 , toStreamDRevWith
119 , toStreamDRevWith
120 , toStreamKWith
120 , toStreamKWith
121 , toStreamKRevWith
121 , toStreamKRevWith
122− , toStreamD
122+ , read
123− , toStreamDRev
123+ , readRev
124 , toStreamK
124 , toStreamK
125 , toStreamKRev
125 , toStreamKRev
126 , toList
126 , toList
@@ -526,6 +526,8 @@ putIndices arr = FL.foldlM' step (return ())
526
526
527-- | Modify a given index of an array using a modifier function.
527-- | Modify a given index of an array using a modifier function.
528--
528--
529+-- Unsafe because it does not check the bounds of the array.
530+--
529-- /Pre-release/
531-- /Pre-release/
530modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
532modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
531 Int -> MutArray a -> (a -> (a, b)) -> m b
533 Int -> MutArray a -> (a -> (a, b)) -> m b
@@ -552,7 +554,6 @@ modifyIndex i MutArray{..} f = do
552 return res
554 return res
553 else invalidIndex "modifyIndex" i
555 else invalidIndex "modifyIndex" i
554
556
555−
556-- | Modify the array indices generated by the supplied stream.
557-- | Modify the array indices generated by the supplied stream.
557--
558--
558-- /Pre-release/
559-- /Pre-release/
@@ -571,6 +572,8 @@ modifyIndices arr f = FL.foldlM' step initial
571
572
572-- | Modify each element of an array using the supplied modifier function.
573-- | Modify each element of an array using the supplied modifier function.
573--
574--
575+-- This is an in-place equivalent of an immutable map operation.
576+--
574-- /Pre-release/
577-- /Pre-release/
575modify :: forall m a. (MonadIO m, Unbox a)
578modify :: forall m a. (MonadIO m, Unbox a)
576 => MutArray a -> (a -> a) -> m ()
579 => MutArray a -> (a -> a) -> m ()
@@ -1549,14 +1552,13 @@ toStreamDWith liftio MutArray{..} = D.Stream step arrStart
1549 r <- peekWith arrContents p
1552 r <- peekWith arrContents p
1550 return $ D.Yield r (INDEX_NEXT(p,a))
1553 return $ D.Yield r (INDEX_NEXT(p,a))
1551
1554
1552−-- | Use the 'reader' unfold instead.
1555+-- | Convert a 'MutArray' into a stream.
1553--
1556--
1554−-- @toStreamD = D.unfold reader@
1557+-- >>> read = Stream.unfold MutArray.reader
1555--
1558--
1556−-- We can try this if the unfold has any performance issues.
1557−{-# INLINE_NORMAL toStreamD #-}
1559+{-# INLINE_NORMAL read #-}
1558−toStreamD :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1560+read :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1559−toStreamD = toStreamDWith liftIO
1561+read = toStreamDWith liftIO
1560
1562
1561{-# INLINE toStreamKWith #-}
1563{-# INLINE toStreamKWith #-}
1562toStreamKWith ::
1564toStreamKWith ::
@@ -1591,14 +1593,13 @@ toStreamDRevWith liftio MutArray{..} =
1591 r <- peekWith arrContents p
1593 r <- peekWith arrContents p
1592 return $ D.Yield r (INDEX_PREV(p,a))
1594 return $ D.Yield r (INDEX_PREV(p,a))
1593
1595
1594−-- | Use the 'readerRev' unfold instead.
1596+-- | Convert a 'MutArray' into a stream in reverse order.
1595--
1597--
1596−-- @toStreamDRev = D.unfold readerRev@
1598+-- >>> readRev = Stream.unfold MutArray.readerRev
1597--
1599--
1598−-- We can try this if the unfold has any perf issues.
1599−{-# INLINE_NORMAL toStreamDRev #-}
1600+{-# INLINE_NORMAL readRev #-}
1600−toStreamDRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1601+readRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
1601−toStreamDRev = toStreamDRevWith liftIO
1602+readRev = toStreamDRevWith liftIO
1602
1603
1603{-# INLINE toStreamKRevWith #-}
1604{-# INLINE toStreamKRevWith #-}
1604toStreamKRevWith ::
1605toStreamKRevWith ::
@@ -1629,12 +1630,12 @@ toStreamKRev = toStreamKRevWith liftIO
1629-- | Strict left fold of an array.
1630-- | Strict left fold of an array.
1630{-# INLINE_NORMAL foldl' #-}
1631{-# INLINE_NORMAL foldl' #-}
1631foldl' :: (MonadIO m, Unbox a) => (b -> a -> b) -> b -> MutArray a -> m b
1632foldl' :: (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
1633
1634
1634-- | Right fold of an array.
1635-- | Right fold of an array.
1635{-# INLINE_NORMAL foldr #-}
1636{-# INLINE_NORMAL foldr #-}
1636foldr :: (MonadIO m, Unbox a) => (a -> b -> b) -> b -> MutArray a -> m b
1637foldr :: (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
1638
1639
1639-------------------------------------------------------------------------------
1640-------------------------------------------------------------------------------
1640-- Folds
1641-- Folds