on master help sign in
Rename unfolds by convention in Array/MutArray
f4b1b1bHarendra Kumar
core/src/Streamly/Internal/Data/Array.hs+32−18
@@ -2,7 +2,6 @@
2-- |
2-- |
3-- Module : Streamly.Internal.Data.Array
3-- Module : Streamly.Internal.Data.Array
4-- Copyright : (c) 2019 Composewell Technologies
4-- Copyright : (c) 2019 Composewell Technologies
5−--
6-- License : BSD3
5-- License : BSD3
7-- Maintainer : streamly@composewell.com
6-- Maintainer : streamly@composewell.com
8-- Stability : experimental
7-- Stability : experimental
@@ -27,21 +26,23 @@ module Streamly.Internal.Data.Array
27 -- , (!!)
26 -- , (!!)
28 , getIndex
27 , getIndex
29 , getIndexRev
28 , getIndexRev
30− , last -- XXX getIndexLast?
29+ , last -- XXX getLastIndex?
31− , getIndices
32− , getIndicesFromThenTo
33 -- , getIndicesFrom -- read from a given position to the end of file
30 -- , getIndicesFrom -- read from a given position to the end of file
34 -- , getIndicesUpto -- read from beginning up to the given position
31 -- , getIndicesUpto -- read from beginning up to the given position
35 -- , getIndicesFromTo
32 -- , getIndicesFromTo
36 -- , getIndicesFromRev -- read from a given position to the beginning of file
33 -- , getIndicesFromRev -- read from a given position to the beginning of file
37 -- , getIndicesUptoRev -- read from end to the given position in file
34 -- , getIndicesUptoRev -- read from end to the given position in file
35+ , indexReader
36+ , indexReaderFromThenTo
38
37
39 -- * Size
38 -- * Size
40 , null
39 , null
41
40
42 -- * Search
41 -- * Search
43 , binarySearch
42 , binarySearch
44− , findIndicesOf -- see splitOn, rename to indicesOf
43+ , findIndicesOf
44+ -- getIndicesOf
45+ , indexFinder -- see splitOn
45 -- , findIndexOf
46 -- , findIndexOf
46 -- , find
47 -- , find
47
48
@@ -74,6 +75,7 @@ module Streamly.Internal.Data.Array
74 -- * Deprecated
75 -- * Deprecated
75 , genSlicesFromLen
76 , genSlicesFromLen
76 , getSlicesFromLen
77 , getSlicesFromLen
78+ , getIndices
77 )
79 )
78where
80where
79
81
@@ -226,19 +228,26 @@ binarySearch = undefined
226-- compared to streams by using SIMD instructions.
228-- compared to streams by using SIMD instructions.
227-- We can also return a bit array instead.
229-- We can also return a bit array instead.
228
230
231+-- Can use SIMD.
232+
229-- | Perform a linear search to find all the indices where a given element is
233-- | Perform a linear search to find all the indices where a given element is
230-- present in an array.
234-- present in an array.
231--
235--
232-- /Unimplemented/
236-- /Unimplemented/
233−findIndicesOf :: (a -> Bool) -> Unfold Identity (Array a) Int
237+indexFinder :: (a -> Bool) -> Unfold Identity (Array a) Int
234−findIndicesOf = undefined
238+indexFinder = undefined
239+
240+-- |
241+-- /Unimplemented/
242+findIndicesOf :: (a -> Bool) -> Array a -> Stream Identity Int
243+findIndicesOf p = Stream.unfold (indexFinder p)
235
244
236{-
245{-
237findIndexOf :: (a -> Bool) -> Array a -> Maybe Int
246findIndexOf :: (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)
239
248
240find :: (a -> Bool) -> Array a -> Bool
249find :: (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)
242-}
251-}
243
252
244-------------------------------------------------------------------------------
253-------------------------------------------------------------------------------
@@ -346,20 +355,25 @@ getIndex i arr =
346-- @
355-- @
347-- read =
356-- read =
348-- let u = lmap (\arr -> (0, length arr - 1)) Unfold.enumerateFromTo
357-- let u = lmap (\arr -> (0, length arr - 1)) Unfold.enumerateFromTo
349−-- in Unfold.lmap f (getIndices arr)
358+-- in Unfold.lmap f (indexReader arr)
350--
359--
351-- readRev =
360-- readRev =
352-- let i = length arr - 1
361-- 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)
354-- @
363-- @
355--
364--
356-- /Pre-release/
365-- /Pre-release/
357−{-# INLINE getIndices #-}
366+{-# INLINE indexReader #-}
358−getIndices :: (Monad m, Unbox a) => Stream m Int -> Unfold m (Array a) a
367+indexReader :: (Monad m, Unbox a) => Stream m Int -> Unfold m (Array a) a
359−getIndices m =
368+indexReader m =
360− let unf = MA.getIndicesWith (return . unsafeInlineIO) m
369+ let unf = MA.indexReaderWith (return . unsafeInlineIO) m
361 in Unfold.lmap A.unsafeThaw unf
370 in Unfold.lmap A.unsafeThaw unf
362
371
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+
363-- | Unfolds @(from, then, to, array)@ generating a finite stream whose first
377-- | Unfolds @(from, then, to, array)@ generating a finite stream whose first
364-- element is the array value from the index @from@ and the successive elements
378-- element is the array value from the index @from@ and the successive elements
365-- are from the indices in increments of @then@ up to @to@. Index enumeration
379-- are from the indices in increments of @then@ up to @to@. Index enumeration
@@ -374,9 +388,9 @@ getIndices m =
374-- @
388-- @
375--
389--
376-- /Unimplemented/
390-- /Unimplemented/
377−{-# INLINE getIndicesFromThenTo #-}
391+{-# INLINE indexReaderFromThenTo #-}
378−getIndicesFromThenTo :: Unfold m (Int, Int, Int, Array a) a
392+indexReaderFromThenTo :: Unfold m (Int, Int, Int, Array a) a
379−getIndicesFromThenTo = undefined
393+indexReaderFromThenTo = undefined
380
394
381-------------------------------------------------------------------------------
395-------------------------------------------------------------------------------
382-- Transform via stream operations
396-- Transform via stream operations
core/src/Streamly/Internal/Data/MutArray/Type.hs+19−7
@@ -123,10 +123,10 @@ module Streamly.Internal.Data.MutArray.Type
123 -- *** Indexing
123 -- *** Indexing
124 , getIndex
124 , getIndex
125 , getIndexUnsafe
125 , getIndexUnsafe
126− , getIndices -- XXX indexReader
127− , getIndicesWith -- XXX indexReaderWith
128 -- , getFromThenTo
126 -- , getFromThenTo
129 , getIndexRev
127 , getIndexRev
128+ , indexReader
129+ , indexReaderWith
130
130
131 -- *** To Streams
131 -- *** To Streams
132 , read
132 , read
@@ -261,6 +261,8 @@ module Streamly.Internal.Data.MutArray.Type
261 , fromStreamDN
261 , fromStreamDN
262 , fromStreamD
262 , fromStreamD
263 , cmp
263 , cmp
264+ , getIndices
265+ , getIndicesWith
264 )
266 )
265where
267where
266
268
@@ -1078,10 +1080,10 @@ getIndexRev i MutArray{..} = do
1078data GetIndicesState contents start end st =
1080data GetIndicesState contents start end st =
1079 GetIndicesState contents start end st
1081 GetIndicesState contents start end st
1080
1082
1081−{-# INLINE getIndicesWith #-}
1083+{-# INLINE indexReaderWith #-}
1082−getIndicesWith :: (Monad m, Unbox a) =>
1084+indexReaderWith :: (Monad m, Unbox a) =>
1083 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1085 (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
1085
1087
1086 where
1088 where
1087
1089
@@ -1100,14 +1102,24 @@ getIndicesWith liftio (D.Stream stepi sti) = Unfold step inject
1100 D.Skip s -> return $ D.Skip (GetIndicesState contents start end s)
1102 D.Skip s -> return $ D.Skip (GetIndicesState contents start end s)
1101 D.Stop -> return D.Stop
1103 D.Stop -> return D.Stop
1102
1104
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+
1103-- | Given an unfold that generates array indices, read the elements on those
1110-- | Given an unfold that generates array indices, read the elements on those
1104-- indices from the supplied MutArray. An error is thrown if an index is out of
1111-- indices from the supplied MutArray. An error is thrown if an index is out of
1105-- bounds.
1112-- bounds.
1106--
1113--
1107-- /Pre-release/
1114-- /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." #-}
1109getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1121getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1110−getIndices = getIndicesWith liftIO
1122+getIndices = indexReader
1111
1123
1112-------------------------------------------------------------------------------
1124-------------------------------------------------------------------------------
1113-- Subarrays
1125-- Subarrays