on master help sign in
Move "strip" to Mutable array module
commite01057fa3c9ebab7a67ace44f88ea20dcd63c6e7 authorHarendra Kumar <harendra@composewell.com> dateFri 10 Mar 2023, 20:47 (4 years ago) parent5598e50
2 files changed+39−29
core/src/Streamly/Internal/Data/Array/Generic.hs+5−29 core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+34
core/src/Streamly/Internal/Data/Array/Generic.hs+5−29
@@ -38,6 +38,7 @@ module Streamly.Internal.Data.Array.Generic
3838
3939 -- * Random Access
4040 , getIndexUnsafe
41+ , getSliceUnsafe
4142 , strip
4243 )
4344where
@@ -217,9 +218,9 @@ writeLastN n = FL.rmapM f (RB.writeLastN n)
217218 arr <- RB.toMutArray 0 n rb
218219 return $ unsafeFreeze arr
219220
220−{-# INLINE sliceUnsafe #-}
221−sliceUnsafe :: Int -> Int -> Array a -> Array a
222−sliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
221+{-# INLINE getSliceUnsafe #-}
222+getSliceUnsafe :: Int -> Int -> Array a -> Array a
223+getSliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
223224
224225-- XXX This is not efficient as it copies the array. We should support array
225226-- slicing so that we can just refer to the underlying array memory instead of
@@ -229,32 +230,7 @@ sliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
229230-- holds true. Returns a slice of the original array.
230231{-# INLINE strip #-}
231232strip :: (a -> Bool) -> Array a -> Array a
232−strip p arr =
233− let lastIndex = length arr - 1
234− indexR = getIndexR lastIndex -- last predicate failing index
235− in if indexR < 0
236− then nil
237− else
238− let indexL = getIndexL 0 -- first predicate failing index
239− in if indexL == 0 && indexR == lastIndex
240− then arr
241− else
242− let newLen = indexR - indexL + 1
243− in sliceUnsafe indexL newLen arr
244−
245− where
246−
247− getIndexR idx
248− | idx < 0 = idx
249− | otherwise =
250− if p (getIndexUnsafe idx arr)
251− then getIndexR (idx - 1)
252− else idx
253−
254− getIndexL idx =
255− if p (getIndexUnsafe idx arr)
256− then getIndexL (idx + 1)
257− else idx
233+strip p arr = unsafeFreeze $ unsafePerformIO $ MArray.strip p (unsafeThaw arr)
258234
259235-------------------------------------------------------------------------------
260236-- Instances
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+34
@@ -108,6 +108,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
108108 , length
109109
110110 -- * In-place Mutation Algorithms
111+ , strip
111112 -- , reverse
112113 -- , permute
113114 -- , partitionBy
@@ -250,6 +251,8 @@ new n@(I# n#) =
250251 let ma = MutArray arr# 0 0 n
251252 in (# s1#, ma #)
252253
254+-- XXX This could be pure?
255+
253256-- |
254257-- Definition:
255258--
@@ -766,3 +769,34 @@ eq a1 a2 =
766769 if v1 == v2
767770 then loop (i - 1)
768771 else return False
772+
773+{-# INLINE strip #-}
774+strip :: MonadIO m => (a -> Bool) -> MutArray a -> m (MutArray a)
775+strip p arr = liftIO $ do
776+ let lastIndex = length arr - 1
777+ indexR <- getIndexR lastIndex -- last predicate failing index
778+ if indexR < 0
779+ then nil
780+ else do
781+ indexL <- getIndexL 0 -- first predicate failing index
782+ if indexL == 0 && indexR == lastIndex
783+ then return arr
784+ else
785+ let newLen = indexR - indexL + 1
786+ in return $ getSliceUnsafe indexL newLen arr
787+
788+ where
789+
790+ getIndexR idx
791+ | idx < 0 = return idx
792+ | otherwise = do
793+ r <- getIndexUnsafe idx arr
794+ if p r
795+ then getIndexR (idx - 1)
796+ else return idx
797+
798+ getIndexL idx = do
799+ r <- getIndexUnsafe idx arr
800+ if p r
801+ then getIndexL (idx + 1)
802+ else return idx