Skip to content

Commit b6d6533

Browse files
committed
Review suggestions: prefer toplevel functions to internal workers
1 parent e66de6a commit b6d6533

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

containers/src/Data/Map/Internal.hs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,38 +1979,41 @@ withoutKeys m (Set.Bin _ k ls rs) = case splitMember k m of
19791979
-- @
19801980
partitionKeys :: forall k a. Ord k => Map k a -> Set k -> (Map k a, Map k a)
19811981
partitionKeys xs ys =
1982-
case go xs ys of
1982+
case partitionKeysWorker xs ys of
19831983
xs' :*: ys' -> (xs', ys')
1984-
where
1985-
go :: Map k a -> Set k -> StrictPair (Map k a) (Map k a)
1986-
go Tip _ = Tip :*: Tip
1987-
go m Set.Tip = Tip :*: m
1988-
go m@(Bin _ k x lm rm) s@Set.Bin{} =
1989-
case b of
1990-
True -> with :*: without
1991-
where
1992-
with =
1993-
if lmWith `ptrEq` lm && rmWith `ptrEq` rm
1994-
then m
1995-
else link k x lmWith rmWith
1996-
without =
1997-
link2 lmWithout rmWithout
1998-
False -> with :*: without
1999-
where
2000-
with = link2 lmWith rmWith
2001-
without =
2002-
if lmWithout `ptrEq` lm && rmWithout `ptrEq` rm
2003-
then m
2004-
else link k x lmWithout rmWithout
2005-
where
2006-
!(lmWith :*: lmWithout) = go lm ls'
2007-
!(rmWith :*: rmWithout) = go rm rs'
2008-
2009-
!(!ls', b, !rs') = Set.splitMember k s
20101984
#if __GLASGOW_HASKELL__
20111985
{-# INLINABLE partitionKeys #-}
20121986
#endif
20131987

1988+
partitionKeysWorker :: Ord k => Map k a -> Set k -> StrictPair (Map k a) (Map k a)
1989+
partitionKeysWorker Tip _ = Tip :*: Tip
1990+
partitionKeysWorker m Set.Tip = Tip :*: m
1991+
partitionKeysWorker m@(Bin _ k x lm rm) s@Set.Bin{} =
1992+
case b of
1993+
True -> with :*: without
1994+
where
1995+
with =
1996+
if lmWith `ptrEq` lm && rmWith `ptrEq` rm
1997+
then m
1998+
else link k x lmWith rmWith
1999+
without =
2000+
link2 lmWithout rmWithout
2001+
False -> with :*: without
2002+
where
2003+
with = link2 lmWith rmWith
2004+
without =
2005+
if lmWithout `ptrEq` lm && rmWithout `ptrEq` rm
2006+
then m
2007+
else link k x lmWithout rmWithout
2008+
where
2009+
!(lmWith :*: lmWithout) = partitionKeysWorker lm ls'
2010+
!(rmWith :*: rmWithout) = partitionKeysWorker rm rs'
2011+
2012+
!(!ls', b, !rs') = Set.splitMember k s
2013+
#if __GLASGOW_HASKELL__
2014+
{-# INLINABLE partitionKeysWorker #-}
2015+
#endif
2016+
20142017
-- | \(O(n+m)\). Difference with a combining function.
20152018
-- When two equal keys are
20162019
-- encountered, the combining function is applied to the values of these keys.

containers/src/Data/Set/Internal.hs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,23 +1431,25 @@ splitS x (Bin _ y l r)
14311431
EQ -> (l :*: r)
14321432
{-# INLINABLE splitS #-}
14331433

1434+
splitMemberS :: Ord a => a -> Set a -> StrictTriple (Set a) Bool (Set a)
1435+
splitMemberS x = go
1436+
where
1437+
go Tip = StrictTriple Tip False Tip
1438+
go (Bin _ y l r) = case compare x y of
1439+
LT -> let StrictTriple lt found gt = splitMemberS x l
1440+
in StrictTriple lt found (link y gt r)
1441+
GT -> let StrictTriple lt found gt = splitMemberS x r
1442+
in StrictTriple (link y l lt) found gt
1443+
EQ -> StrictTriple l True r
1444+
#if __GLASGOW_HASKELL__
1445+
{-# INLINABLE splitMemberS #-}
1446+
#endif
1447+
14341448
-- | \(O(\log n)\). Performs a 'split' but also returns whether the pivot
14351449
-- element was found in the original set.
14361450
splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
1437-
splitMember k0 s = case go k0 s of
1451+
splitMember k0 s = case splitMemberS k0 s of
14381452
StrictTriple l b r -> (l, b, r)
1439-
where
1440-
go :: Ord a => a -> Set a -> StrictTriple (Set a) Bool (Set a)
1441-
go _ Tip = StrictTriple Tip False Tip
1442-
go x (Bin _ y l r)
1443-
= case compare x y of
1444-
LT -> let StrictTriple lt found gt = go x l
1445-
!gt' = link y gt r
1446-
in StrictTriple lt found gt'
1447-
GT -> let StrictTriple lt found gt = go x r
1448-
!lt' = link y l lt
1449-
in StrictTriple lt' found gt
1450-
EQ -> StrictTriple l True r
14511453
#if __GLASGOW_HASKELL__
14521454
{-# INLINABLE splitMember #-}
14531455
#endif

containers/src/Utils/Containers/Internal/StrictTriple.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
{-# LANGUAGE Safe #-}
44
#endif
55

6-
#include "containers.h"
7-
86
-- | A strict triple
97

108
module Utils.Containers.Internal.StrictTriple (StrictTriple(..)) where

0 commit comments

Comments
 (0)