on master help sign in
Move "strip" to Mutable array module
e01057fHarendra Kumar
core/src/Streamly/Internal/Data/Array/Generic.hs+5−29
@@ -38,6 +38,7 @@ module Streamly.Internal.Data.Array.Generic
38
38
39 -- * Random Access
39 -- * Random Access
40 , getIndexUnsafe
40 , getIndexUnsafe
41+ , getSliceUnsafe
41 , strip
42 , strip
42 )
43 )
43where
44where
@@ -217,9 +218,9 @@ writeLastN n = FL.rmapM f (RB.writeLastN n)
217 arr <- RB.toMutArray 0 n rb
218 arr <- RB.toMutArray 0 n rb
218 return $ unsafeFreeze arr
219 return $ unsafeFreeze arr
219
220
220−{-# INLINE sliceUnsafe #-}
221+{-# INLINE getSliceUnsafe #-}
221−sliceUnsafe :: Int -> Int -> Array a -> Array a
222+getSliceUnsafe :: Int -> Int -> Array a -> Array a
222−sliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
223+getSliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
223
224
224-- XXX This is not efficient as it copies the array. We should support array
225-- XXX This is not efficient as it copies the array. We should support array
225-- slicing so that we can just refer to the underlying array memory instead of
226-- 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
229-- holds true. Returns a slice of the original array.
230-- holds true. Returns a slice of the original array.
230{-# INLINE strip #-}
231{-# INLINE strip #-}
231strip :: (a -> Bool) -> Array a -> Array a
232strip :: (a -> Bool) -> Array a -> Array a
232−strip p arr =
233+strip p arr = unsafeFreeze $ unsafePerformIO $ MArray.strip p (unsafeThaw 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
258
234
259-------------------------------------------------------------------------------
235-------------------------------------------------------------------------------
260-- Instances
236-- Instances
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+34
@@ -108,6 +108,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
108 , length
108 , length
109
109
110 -- * In-place Mutation Algorithms
110 -- * In-place Mutation Algorithms
111+ , strip
111 -- , reverse
112 -- , reverse
112 -- , permute
113 -- , permute
113 -- , partitionBy
114 -- , partitionBy
@@ -250,6 +251,8 @@ new n@(I# n#) =
250 let ma = MutArray arr# 0 0 n
251 let ma = MutArray arr# 0 0 n
251 in (# s1#, ma #)
252 in (# s1#, ma #)
252
253
254+-- XXX This could be pure?
255+
253-- |
256-- |
254-- Definition:
257-- Definition:
255--
258--
@@ -766,3 +769,34 @@ eq a1 a2 =
766 if v1 == v2
769 if v1 == v2
767 then loop (i - 1)
770 then loop (i - 1)
768 else return False
771 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