Skip to content

Commit 4a15a7a

Browse files
committed
Review suggestions: prefer toplevel functions to internal workers
1 parent 2c6d4a0 commit 4a15a7a

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
@@ -1956,38 +1956,41 @@ withoutKeys m (Set.Bin _ k ls rs) = case splitMember k m of
19561956
-- @
19571957
partitionKeys :: forall k a. Ord k => Map k a -> Set k -> (Map k a, Map k a)
19581958
partitionKeys xs ys =
1959-
case go xs ys of
1959+
case partitionKeysWorker xs ys of
19601960
xs' :*: ys' -> (xs', ys')
1961-
where
1962-
go :: Map k a -> Set k -> StrictPair (Map k a) (Map k a)
1963-
go Tip _ = Tip :*: Tip
1964-
go m Set.Tip = Tip :*: m
1965-
go m@(Bin _ k x lm rm) s@Set.Bin{} =
1966-
case b of
1967-
True -> with :*: without
1968-
where
1969-
with =
1970-
if lmWith `ptrEq` lm && rmWith `ptrEq` rm
1971-
then m
1972-
else link k x lmWith rmWith
1973-
without =
1974-
link2 lmWithout rmWithout
1975-
False -> with :*: without
1976-
where
1977-
with = link2 lmWith rmWith
1978-
without =
1979-
if lmWithout `ptrEq` lm && rmWithout `ptrEq` rm
1980-
then m
1981-
else link k x lmWithout rmWithout
1982-
where
1983-
!(lmWith :*: lmWithout) = go lm ls'
1984-
!(rmWith :*: rmWithout) = go rm rs'
1985-
1986-
!(!ls', b, !rs') = Set.splitMember k s
19871961
#if __GLASGOW_HASKELL__
19881962
{-# INLINABLE partitionKeys #-}
19891963
#endif
19901964

1965+
partitionKeysWorker :: Ord k => Map k a -> Set k -> StrictPair (Map k a) (Map k a)
1966+
partitionKeysWorker Tip _ = Tip :*: Tip
1967+
partitionKeysWorker m Set.Tip = Tip :*: m
1968+
partitionKeysWorker m@(Bin _ k x lm rm) s@Set.Bin{} =
1969+
case b of
1970+
True -> with :*: without
1971+
where
1972+
with =
1973+
if lmWith `ptrEq` lm && rmWith `ptrEq` rm
1974+
then m
1975+
else link k x lmWith rmWith
1976+
without =
1977+
link2 lmWithout rmWithout
1978+
False -> with :*: without
1979+
where
1980+
with = link2 lmWith rmWith
1981+
without =
1982+
if lmWithout `ptrEq` lm && rmWithout `ptrEq` rm
1983+
then m
1984+
else link k x lmWithout rmWithout
1985+
where
1986+
!(lmWith :*: lmWithout) = partitionKeysWorker lm ls'
1987+
!(rmWith :*: rmWithout) = partitionKeysWorker rm rs'
1988+
1989+
!(!ls', b, !rs') = Set.splitMember k s
1990+
#if __GLASGOW_HASKELL__
1991+
{-# INLINABLE partitionKeysWorker #-}
1992+
#endif
1993+
19911994
-- | \(O(n+m)\). Difference with a combining function.
19921995
-- When two equal keys are
19931996
-- 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
@@ -1316,23 +1316,25 @@ splitS x (Bin _ y l r)
13161316
EQ -> (l :*: r)
13171317
{-# INLINABLE splitS #-}
13181318

1319+
splitMemberS :: Ord a => a -> Set a -> StrictTriple (Set a) Bool (Set a)
1320+
splitMemberS x = go
1321+
where
1322+
go Tip = StrictTriple Tip False Tip
1323+
go (Bin _ y l r) = case compare x y of
1324+
LT -> let StrictTriple lt found gt = splitMemberS x l
1325+
in StrictTriple lt found (link y gt r)
1326+
GT -> let StrictTriple lt found gt = splitMemberS x r
1327+
in StrictTriple (link y l lt) found gt
1328+
EQ -> StrictTriple l True r
1329+
#if __GLASGOW_HASKELL__
1330+
{-# INLINABLE splitMemberS #-}
1331+
#endif
1332+
13191333
-- | \(O(\log n)\). Performs a 'split' but also returns whether the pivot
13201334
-- element was found in the original set.
13211335
splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
1322-
splitMember k0 s = case go k0 s of
1336+
splitMember k0 s = case splitMemberS k0 s of
13231337
StrictTriple l b r -> (l, b, r)
1324-
where
1325-
go :: Ord a => a -> Set a -> StrictTriple (Set a) Bool (Set a)
1326-
go _ Tip = StrictTriple Tip False Tip
1327-
go x (Bin _ y l r)
1328-
= case compare x y of
1329-
LT -> let StrictTriple lt found gt = go x l
1330-
!gt' = link y gt r
1331-
in StrictTriple lt found gt'
1332-
GT -> let StrictTriple lt found gt = go x r
1333-
!lt' = link y l lt
1334-
in StrictTriple lt' found gt
1335-
EQ -> StrictTriple l True r
13361338
#if __GLASGOW_HASKELL__
13371339
{-# INLINABLE splitMember #-}
13381340
#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)