Skip to content

Commit

Permalink
Inline findIndexL, findIndexR, findIndicesL, findIndicesR
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumlamm committed Jun 21, 2024
1 parent 287bebc commit e489bd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ main = defaultMain $ [10, 100, 1000, 10000, 100000] <&> \n ->
, bench "<|" $ whnf (42 RRB.<|) v
, bench "take" $ whnf (RRB.take idx) v
, bench "drop" $ whnf (RRB.drop idx) v
, bench "splitAt" $ whnf (RRB.splitAt idx) v
, bench "insertAt" $ whnf (RRB.insertAt idx 42) v
, bench "deleteAt" $ whnf (RRB.deleteAt idx) v
, bench "index" $ nf (RRB.lookup idx) v
, bench "adjust" $ whnf (RRB.adjust idx (+ 1)) v
, bench "map" $ whnf (RRB.map (+ 1)) v
, bench "foldl" $ nf (foldl (+) 0) v
, bench "foldr" $ nf (foldr (+) 0) v
, bench "findIndexL" $ nf (RRB.findIndexL (== idx)) v
, bench "findIndexR" $ nf (RRB.findIndexR (== idx)) v
, bench "findIndicesL" $ nf (RRB.findIndicesL (== idx)) v
, bench "findIndicesR" $ nf (RRB.findIndicesR (== idx)) v
]
4 changes: 4 additions & 0 deletions src/Data/RRBVector/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -657,24 +657,28 @@ deleteAt i v = let (left, right) = splitAt (i + 1) v in take i left >< right
-- @since 0.2.1.0
findIndexL :: (a -> Bool) -> Vector a -> Maybe Int
findIndexL f = ifoldr (\i x acc -> if f x then Just i else acc) Nothing
{-# INLINE findIndexL #-}

-- | \(O(n)\). Find the first index from the right that satisfies the predicate.
--
-- @since 0.2.1.0
findIndexR :: (a -> Bool) -> Vector a -> Maybe Int
findIndexR f = ifoldl (\i acc x -> if f x then Just i else acc) Nothing
{-# INLINE findIndexR #-}

-- | \(O(n)\). Find the indices that satisfy the predicate, starting from the left.
--
-- @since 0.2.1.0
findIndicesL :: (a -> Bool) -> Vector a -> [Int]
findIndicesL f = ifoldr (\i x acc -> if f x then i : acc else acc) []
{-# INLINE findIndicesL #-}

-- | \(O(n)\). Find the indices that satisfy the predicate, starting from the right.
--
-- @since 0.2.1.0
findIndicesR :: (a -> Bool) -> Vector a -> [Int]
findIndicesR f = ifoldl (\i acc x -> if f x then i : acc else acc) []
{-# INLINE findIndicesR #-}

-- concatenation

Expand Down

0 comments on commit e489bd2

Please sign in to comment.