on master help sign in
Rename unfolds by convention in Array/MutArray
commitf4b1b1bdf14a510468c8dd6548bafdfa5dd20b5d authorHarendra Kumar <harendra@composewell.com> dateMon 1 Jan 2024, 09:08 (3 years ago) parent1184527
2 files changed+51−25
core/src/Streamly/Internal/Data/Array.hs+32−18 core/src/Streamly/Internal/Data/MutArray/Type.hs+19−7
core/src/Streamly/Internal/Data/Array.hs+32−18
@@ -2,7 +2,6 @@
22-- |
33-- Module : Streamly.Internal.Data.Array
44-- Copyright : (c) 2019 Composewell Technologies
5−--
65-- License : BSD3
76-- Maintainer : streamly@composewell.com
87-- Stability : experimental
@@ -27,21 +26,23 @@ module Streamly.Internal.Data.Array
2726 -- , (!!)
2827 , getIndex
2928 , getIndexRev
30− , last -- XXX getIndexLast?
31− , getIndices
32− , getIndicesFromThenTo
29+ , last -- XXX getLastIndex?
3330 -- , getIndicesFrom -- read from a given position to the end of file
3431 -- , getIndicesUpto -- read from beginning up to the given position
3532 -- , getIndicesFromTo
3633 -- , getIndicesFromRev -- read from a given position to the beginning of file
3734 -- , getIndicesUptoRev -- read from end to the given position in file
35+ , indexReader
36+ , indexReaderFromThenTo
3837
3938 -- * Size
4039 , null
4140
4241 -- * Search
4342 , binarySearch
44− , findIndicesOf -- see splitOn, rename to indicesOf
43+ , findIndicesOf
44+ -- getIndicesOf
45+ , indexFinder -- see splitOn
4546 -- , findIndexOf
4647 -- , find
4748
@@ -74,6 +75,7 @@ module Streamly.Internal.Data.Array
7475 -- * Deprecated
7576 , genSlicesFromLen
7677 , getSlicesFromLen
78+ , getIndices
7779 )
7880where
7981
@@ -226,19 +228,26 @@ binarySearch = undefined
226228-- compared to streams by using SIMD instructions.
227229-- We can also return a bit array instead.
228230
231+-- Can use SIMD.
232+
229233-- | Perform a linear search to find all the indices where a given element is
230234-- present in an array.
231235--
232236-- /Unimplemented/
233−findIndicesOf :: (a -> Bool) -> Unfold Identity (Array a) Int
234−findIndicesOf = undefined
237+indexFinder :: (a -> Bool) -> Unfold Identity (Array a) Int
238+indexFinder = undefined
239+
240+-- |
241+-- /Unimplemented/
242+findIndicesOf :: (a -> Bool) -> Array a -> Stream Identity Int
243+findIndicesOf p = Stream.unfold (indexFinder p)
235244
236245{-
237246findIndexOf :: (a -> Bool) -> Array a -> Maybe Int
238−findIndexOf p = Unfold.fold Fold.one . Stream.unfold (findIndicesOf p)
247+findIndexOf p = Unfold.fold Fold.one . Stream.unfold (indexFinder p)
239248
240249find :: (a -> Bool) -> Array a -> Bool
241−find = Unfold.fold Fold.null . Stream.unfold (findIndicesOf p)
250+find = Unfold.fold Fold.null . Stream.unfold (indexFinder p)
242251-}
243252
244253-------------------------------------------------------------------------------
@@ -346,20 +355,25 @@ getIndex i arr =
346355-- @
347356-- read =
348357-- let u = lmap (\arr -> (0, length arr - 1)) Unfold.enumerateFromTo
349−-- in Unfold.lmap f (getIndices arr)
358+-- in Unfold.lmap f (indexReader arr)
350359--
351360-- readRev =
352361-- let i = length arr - 1
353−-- in Unfold.lmap f (getIndicesFromThenTo i (i - 1) 0)
362+-- in Unfold.lmap f (indexReaderFromThenTo i (i - 1) 0)
354363-- @
355364--
356365-- /Pre-release/
357−{-# INLINE getIndices #-}
358−getIndices :: (Monad m, Unbox a) => Stream m Int -> Unfold m (Array a) a
359−getIndices m =
360− let unf = MA.getIndicesWith (return . unsafeInlineIO) m
366+{-# INLINE indexReader #-}
367+indexReader :: (Monad m, Unbox a) => Stream m Int -> Unfold m (Array a) a
368+indexReader m =
369+ let unf = MA.indexReaderWith (return . unsafeInlineIO) m
361370 in Unfold.lmap A.unsafeThaw unf
362371
372+-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
373+{-# DEPRECATED getIndices "Please use getIndices instead." #-}
374+getIndices :: (Monad m, Unbox a) => Stream m Int -> Unfold m (Array a) a
375+getIndices = indexReader
376+
363377-- | Unfolds @(from, then, to, array)@ generating a finite stream whose first
364378-- element is the array value from the index @from@ and the successive elements
365379-- are from the indices in increments of @then@ up to @to@. Index enumeration
@@ -374,9 +388,9 @@ getIndices m =
374388-- @
375389--
376390-- /Unimplemented/
377−{-# INLINE getIndicesFromThenTo #-}
378−getIndicesFromThenTo :: Unfold m (Int, Int, Int, Array a) a
379−getIndicesFromThenTo = undefined
391+{-# INLINE indexReaderFromThenTo #-}
392+indexReaderFromThenTo :: Unfold m (Int, Int, Int, Array a) a
393+indexReaderFromThenTo = undefined
380394
381395-------------------------------------------------------------------------------
382396-- Transform via stream operations
core/src/Streamly/Internal/Data/MutArray/Type.hs+19−7
@@ -123,10 +123,10 @@ module Streamly.Internal.Data.MutArray.Type
123123 -- *** Indexing
124124 , getIndex
125125 , getIndexUnsafe
126− , getIndices -- XXX indexReader
127− , getIndicesWith -- XXX indexReaderWith
128126 -- , getFromThenTo
129127 , getIndexRev
128+ , indexReader
129+ , indexReaderWith
130130
131131 -- *** To Streams
132132 , read
@@ -261,6 +261,8 @@ module Streamly.Internal.Data.MutArray.Type
261261 , fromStreamDN
262262 , fromStreamD
263263 , cmp
264+ , getIndices
265+ , getIndicesWith
264266 )
265267where
266268
@@ -1078,10 +1080,10 @@ getIndexRev i MutArray{..} = do
10781080data GetIndicesState contents start end st =
10791081 GetIndicesState contents start end st
10801082
1081−{-# INLINE getIndicesWith #-}
1082−getIndicesWith :: (Monad m, Unbox a) =>
1083+{-# INLINE indexReaderWith #-}
1084+indexReaderWith :: (Monad m, Unbox a) =>
10831085 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1084−getIndicesWith liftio (D.Stream stepi sti) = Unfold step inject
1086+indexReaderWith liftio (D.Stream stepi sti) = Unfold step inject
10851087
10861088 where
10871089
@@ -1100,14 +1102,24 @@ getIndicesWith liftio (D.Stream stepi sti) = Unfold step inject
11001102 D.Skip s -> return $ D.Skip (GetIndicesState contents start end s)
11011103 D.Stop -> return D.Stop
11021104
1105+{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1106+getIndicesWith :: (Monad m, Unbox a) =>
1107+ (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1108+getIndicesWith = indexReaderWith
1109+
11031110-- | Given an unfold that generates array indices, read the elements on those
11041111-- indices from the supplied MutArray. An error is thrown if an index is out of
11051112-- bounds.
11061113--
11071114-- /Pre-release/
1108−{-# INLINE getIndices #-}
1115+{-# INLINE indexReader #-}
1116+indexReader :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1117+indexReader = getIndicesWith liftIO
1118+
1119+-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
1120+{-# DEPRECATED getIndices "Please use indexReader instead." #-}
11091121getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1110−getIndices = getIndicesWith liftIO
1122+getIndices = indexReader
11111123
11121124-------------------------------------------------------------------------------
11131125-- Subarrays