on master help sign in
Expose some array APIs
3187e37Ranjeet Kumar Ranjan
core/src/Streamly/Data/Array/Generic.hs+1
@@ -31,6 +31,7 @@ module Streamly.Data.Array.Generic
31
31
32 -- * Random Access
32 -- * Random Access
33 , A.length
33 , A.length
34+ , A.getIndex
34
35
35 -- -- * Folding Arrays
36 -- -- * Folding Arrays
36 -- , A.streamFold
37 -- , A.streamFold
core/src/Streamly/Data/MutArray/Generic.hs+7
@@ -35,15 +35,22 @@ module Streamly.Data.MutArray.Generic
35 -- * Unfolds
35 -- * Unfolds
36 , reader
36 , reader
37
37
38+ -- * Streams
39+ , read
40+
38 -- * Random reads
41 -- * Random reads
39 , getIndex
42 , getIndex
40
43
41 -- * Inplace mutation
44 -- * Inplace mutation
42 , putIndex
45 , putIndex
43 , modifyIndex
46 , modifyIndex
47+
48+ -- * Size
49+ , length
44 )
50 )
45where
51where
46
52
47import Streamly.Internal.Data.Array.Generic.Mut.Type
53import Streamly.Internal.Data.Array.Generic.Mut.Type
54+import Prelude hiding (read, length)
48
55
49#include "DocTestDataMutArrayGeneric.hs"
56#include "DocTestDataMutArrayGeneric.hs"
core/src/Streamly/Internal/Data/Array/Generic.hs+9
@@ -37,6 +37,7 @@ module Streamly.Internal.Data.Array.Generic
37
37
38 -- * Random Access
38 -- * Random Access
39 , getIndexUnsafe
39 , getIndexUnsafe
40+ , getIndex
40 , getSliceUnsafe
41 , getSliceUnsafe
41 , strip
42 , strip
42 )
43 )
@@ -207,6 +208,14 @@ getIndexUnsafe :: Int -> Array a -> a
207getIndexUnsafe i arr =
208getIndexUnsafe i arr =
208 unsafePerformIO $ MArray.getIndexUnsafe i (unsafeThaw arr)
209 unsafePerformIO $ MArray.getIndexUnsafe i (unsafeThaw arr)
209
210
211+-- | Lookup the element at the given index. Index starts from 0.
212+--
213+getIndex :: Int -> Array a -> Maybe a
214+getIndex i arr@Array {..} =
215+ if i >= 0 && i < arrLen
216+ then Just $ getIndexUnsafe i arr
217+ else Nothing
218+
210{-# INLINE writeLastN #-}
219{-# INLINE writeLastN #-}
211writeLastN :: MonadIO m => Int -> Fold m a (Array a)
220writeLastN :: MonadIO m => Int -> Fold m a (Array a)
212writeLastN n = FL.rmapM f (RB.writeLastN n)
221writeLastN n = FL.rmapM f (RB.writeLastN n)
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+17−8
@@ -27,6 +27,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
27 , writeN
27 , writeN
28 , writeWith
28 , writeWith
29 , write
29 , write
30+ , fromStreamN
30
31
31 -- , writeRevN
32 -- , writeRevN
32 -- , writeRev
33 -- , writeRev
@@ -91,7 +92,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
91 , producer -- experimental
92 , producer -- experimental
92
93
93 -- ** To containers
94 -- ** To containers
94− , toStreamD
95+ , read
95 , readRev
96 , readRev
96 , toStreamK
97 , toStreamK
97 -- , toStreamKRev
98 -- , toStreamKRev
@@ -179,6 +180,7 @@ import GHC.Int (Int(..))
179import Streamly.Internal.Data.Fold.Type (Fold(..))
180import Streamly.Internal.Data.Fold.Type (Fold(..))
180import Streamly.Internal.Data.Producer.Type (Producer (..))
181import Streamly.Internal.Data.Producer.Type (Producer (..))
181import Streamly.Internal.Data.Unfold.Type (Unfold(..))
182import Streamly.Internal.Data.Unfold.Type (Unfold(..))
183+import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
182
184
183import qualified Streamly.Internal.Data.Fold.Type as FL
185import qualified Streamly.Internal.Data.Fold.Type as FL
184import qualified Streamly.Internal.Data.Producer as Producer
186import qualified Streamly.Internal.Data.Producer as Producer
@@ -527,14 +529,13 @@ getSlice index len arr@MutArray{..} =
527toList :: MonadIO m => MutArray a -> m [a]
529toList :: MonadIO m => MutArray a -> m [a]
528toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
530toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
529
531
530−-- | Use the 'read' unfold instead.
532+-- | Generates a stream from the elements of @MutArray@
531--
533--
532−-- @toStreamD = D.unfold read@
534+-- @read = D.unfold reader@
533−--
535+-- /Pre-release/
534−-- We can try this if the unfold has any performance issues.
535−{-# INLINE_NORMAL toStreamD #-}
536+{-# INLINE_NORMAL read #-}
536−toStreamD :: MonadIO m => MutArray a -> D.Stream m a
537+read :: MonadIO m => MutArray a -> D.Stream m a
537−toStreamD arr@MutArray{..} =
538+read arr@MutArray{..} =
538 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
539 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
539
540
540-- Check equivalence with StreamK.fromStream . toStreamD and remove
541-- Check equivalence with StreamK.fromStream . toStreamD and remove
@@ -794,3 +795,11 @@ strip p arr = liftIO $ do
794 if p r
795 if p r
795 then getIndexL (idx + 1)
796 then getIndexL (idx + 1)
796 else return idx
797 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)