Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindIndices optimized using findIndex and inlining #270

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Data/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ findIndex k (BS x l) = accursedUnutterablePerformIO $ withForeignPtr x $ \f -> g
if k w
then return (Just n)
else go (ptr `plusPtr` 1) (n+1)
{-# INLINE findIndex #-}
{-# INLINE [1] findIndex #-}

-- | /O(n)/ The 'findIndexEnd' function takes a predicate and a 'ByteString' and
-- returns the index of the last element in the ByteString
Expand All @@ -1200,9 +1200,29 @@ findIndexEnd k (BS x l) = accursedUnutterablePerformIO $ withForeignPtr x $ \ f
findIndices :: (Word8 -> Bool) -> ByteString -> [Int]
findIndices p ps = loop 0 ps
where
loop !n !qs | null qs = []
| p (unsafeHead qs) = n : loop (n+1) (unsafeTail qs)
| otherwise = loop (n+1) (unsafeTail qs)
loop !n !qs = case findIndex p qs of
Just !i ->
let !j = n+i
in j : loop (j+1) (unsafeDrop (i+1) qs)
Nothing -> []
{-# INLINE [1] findIndices #-}


#if MIN_VERSION_base(4,9,0)
{-# RULES
"ByteString specialise findIndex (x ==)" forall x. findIndex (x`eqWord8`) = elemIndex x
"ByteString specialise findIndex (== x)" forall x. findIndex (`eqWord8`x) = elemIndex x
"ByteString specialise findIndices (x ==)" forall x. findIndices (x`eqWord8`) = elemIndices x
"ByteString specialise findIndices (== x)" forall x. findIndices (`eqWord8`x) = elemIndices x
#-}
#else
{-# RULES
"ByteString specialise findIndex (x ==)" forall x. findIndex (x==) = elemIndex x
"ByteString specialise findIndex (== x)" forall x. findIndex (==x) = elemIndex x
"ByteString specialise findIndices (x ==)" forall x. findIndices (x==) = elemIndices x
"ByteString specialise findIndices (== x)" forall x. findIndices (==x) = elemIndices x
#-}
#endif

-- ---------------------------------------------------------------------
-- Searching ByteStrings
Expand Down
28 changes: 27 additions & 1 deletion Data/ByteString/Char8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,38 @@ elemIndices = B.elemIndices . c2w
-- returns the index of the first element in the ByteString satisfying the predicate.
findIndex :: (Char -> Bool) -> ByteString -> Maybe Int
findIndex f = B.findIndex (f . w2c)
{-# INLINE findIndex #-}
{-# INLINE [1] findIndex #-}

-- | The 'findIndices' function extends 'findIndex', by returning the
-- indices of all elements satisfying the predicate, in ascending order.
findIndices :: (Char -> Bool) -> ByteString -> [Int]
findIndices f = B.findIndices (f . w2c)
{-# INLINE [1] findIndices #-}

#if MIN_VERSION_base(4,9,0)
{-# RULES
"ByteString specialise findIndex (x==)" forall x.
findIndex (x `eqChar`) = elemIndex x
"ByteString specialise findIndex (==x)" forall x.
findIndex (`eqChar` x) = elemIndex x
"ByteString specialise findIndices (x==)" forall x.
findIndices (x `eqChar`) = elemIndices x
"ByteString specialise findIndices (==x)" forall x.
findIndices (`eqChar` x) = elemIndices x
#-}
#else
{-# RULES
"ByteString specialise findIndex (x==)" forall x.
findIndex (x==) = elemIndex x
"ByteString specialise findIndex (==x)" forall x.
findIndex (==x) = elemIndex x
"ByteString specialise findIndices (x==)" forall x.
findIndices (x==) = elemIndices x
"ByteString specialise findIndices (==x)" forall x.
findIndices (==x) = elemIndices x
#-}
#endif


-- | count returns the number of times its argument appears in the ByteString
--
Expand Down
1 change: 1 addition & 0 deletions Data/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ findIndices k cs0 = findIndices' 0 cs0
where findIndices' _ Empty = []
findIndices' n (Chunk c cs) = L.map ((+n).fromIntegral) (S.findIndices k c)
++ findIndices' (n + fromIntegral (S.length c)) cs
{-# INLINE findIndices #-}
archaephyrryx marked this conversation as resolved.
Show resolved Hide resolved

-- ---------------------------------------------------------------------
-- Searching ByteStrings
Expand Down
1 change: 1 addition & 0 deletions Data/ByteString/Lazy/Char8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ findIndex f = L.findIndex (f . w2c)
-- indices of all elements satisfying the predicate, in ascending order.
findIndices :: (Char -> Bool) -> ByteString -> [Int64]
findIndices f = L.findIndices (f . w2c)
{-# INLINE findIndices #-}

-- | count returns the number of times its argument appears in the ByteString
--
Expand Down