Skip to content

Commit 8df75c7

Browse files
authored
Merge pull request #331 from treeowl/map-fromAscList-strictify
Make Data.Map.fromDistinct{Asc,Desc}List eager
2 parents 0b6cd9e + 4fcf139 commit 8df75c7

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

Data/Map/Internal.hs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,15 +3431,16 @@ fromDistinctAscList ((kx0, x0) : xs0) = go (1::Int) (Bin 1 kx0 x0 Tip Tip) xs0
34313431
where
34323432
go !_ t [] = t
34333433
go s l ((kx, x) : xs) = case create s xs of
3434-
(r, ys) -> go (s `shiftL` 1) (link kx x l r) ys
3434+
(r :*: ys) -> let !t' = link kx x l r
3435+
in go (s `shiftL` 1) t' ys
34353436

3436-
create !_ [] = (Tip, [])
3437+
create !_ [] = (Tip :*: [])
34373438
create s xs@(x' : xs')
3438-
| s == 1 = case x' of (kx, x) -> (Bin 1 kx x Tip Tip, xs')
3439+
| s == 1 = case x' of (kx, x) -> (Bin 1 kx x Tip Tip :*: xs')
34393440
| otherwise = case create (s `shiftR` 1) xs of
3440-
res@(_, []) -> res
3441-
(l, (ky, y):ys) -> case create (s `shiftR` 1) ys of
3442-
(r, zs) -> (link ky y l r, zs)
3441+
res@(_ :*: []) -> res
3442+
(l :*: (ky, y):ys) -> case create (s `shiftR` 1) ys of
3443+
(r :*: zs) -> (link ky y l r :*: zs)
34433444

34443445
-- | /O(n)/. Build a map from a descending list of distinct elements in linear time.
34453446
-- /The precondition is not checked./
@@ -3456,15 +3457,16 @@ fromDistinctDescList ((kx0, x0) : xs0) = go (1 :: Int) (Bin 1 kx0 x0 Tip Tip) xs
34563457
where
34573458
go !_ t [] = t
34583459
go s r ((kx, x) : xs) = case create s xs of
3459-
(l, ys) -> go (s `shiftL` 1) (link kx x l r) ys
3460+
(l :*: ys) -> let !t' = link kx x l r
3461+
in go (s `shiftL` 1) t' ys
34603462

3461-
create !_ [] = (Tip, [])
3463+
create !_ [] = (Tip :*: [])
34623464
create s xs@(x' : xs')
3463-
| s == 1 = case x' of (kx, x) -> (Bin 1 kx x Tip Tip, xs')
3465+
| s == 1 = case x' of (kx, x) -> (Bin 1 kx x Tip Tip :*: xs')
34643466
| otherwise = case create (s `shiftR` 1) xs of
3465-
res@(_, []) -> res
3466-
(r, (ky, y):ys) -> case create (s `shiftR` 1) ys of
3467-
(l, zs) -> (link ky y l r, zs)
3467+
res@(_ :*: []) -> res
3468+
(r :*: (ky, y):ys) -> case create (s `shiftR` 1) ys of
3469+
(l :*: zs) -> (link ky y l r :*: zs)
34683470

34693471
{-
34703472
-- Functions very similar to these were used to implement

Data/Map/Strict/Internal.hs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,16 +1670,18 @@ fromDistinctAscList [] = Tip
16701670
fromDistinctAscList ((kx0, x0) : xs0) = x0 `seq` go (1::Int) (Bin 1 kx0 x0 Tip Tip) xs0
16711671
where
16721672
go !_ t [] = t
1673-
go s l ((kx, x) : xs) = case create s xs of
1674-
(r, ys) -> x `seq` go (s `shiftL` 1) (link kx x l r) ys
1673+
go s l ((kx, x) : xs) =
1674+
case create s xs of
1675+
(r :*: ys) -> x `seq` let !t' = link kx x l r
1676+
in go (s `shiftL` 1) t' ys
16751677

1676-
create !_ [] = (Tip, [])
1678+
create !_ [] = (Tip :*: [])
16771679
create s xs@(x' : xs')
1678-
| s == 1 = case x' of (kx, x) -> x `seq` (Bin 1 kx x Tip Tip, xs')
1680+
| s == 1 = case x' of (kx, x) -> x `seq` (Bin 1 kx x Tip Tip :*: xs')
16791681
| otherwise = case create (s `shiftR` 1) xs of
1680-
res@(_, []) -> res
1681-
(l, (ky, y):ys) -> case create (s `shiftR` 1) ys of
1682-
(r, zs) -> y `seq` (link ky y l r, zs)
1682+
res@(_ :*: []) -> res
1683+
(l :*: (ky, y):ys) -> case create (s `shiftR` 1) ys of
1684+
(r :*: zs) -> y `seq` (link ky y l r :*: zs)
16831685

16841686
-- | /O(n)/. Build a map from a descending list of distinct elements in linear time.
16851687
-- /The precondition is not checked./
@@ -1695,13 +1697,15 @@ fromDistinctDescList [] = Tip
16951697
fromDistinctDescList ((kx0, x0) : xs0) = x0 `seq` go (1::Int) (Bin 1 kx0 x0 Tip Tip) xs0
16961698
where
16971699
go !_ t [] = t
1698-
go s r ((kx, x) : xs) = case create s xs of
1699-
(l, ys) -> x `seq` go (s `shiftL` 1) (link kx x l r) ys
1700+
go s r ((kx, x) : xs) =
1701+
case create s xs of
1702+
(l :*: ys) -> x `seq` let !t' = link kx x l r
1703+
in go (s `shiftL` 1) t' ys
17001704

1701-
create !_ [] = (Tip, [])
1705+
create !_ [] = (Tip :*: [])
17021706
create s xs@(x' : xs')
1703-
| s == 1 = case x' of (kx, x) -> x `seq` (Bin 1 kx x Tip Tip, xs')
1707+
| s == 1 = case x' of (kx, x) -> x `seq` (Bin 1 kx x Tip Tip :*: xs')
17041708
| otherwise = case create (s `shiftR` 1) xs of
1705-
res@(_, []) -> res
1706-
(r, (ky, y):ys) -> case create (s `shiftR` 1) ys of
1707-
(l, zs) -> y `seq` (link ky y l r, zs)
1709+
res@(_ :*: []) -> res
1710+
(r :*: (ky, y):ys) -> case create (s `shiftR` 1) ys of
1711+
(l :*: zs) -> y `seq` (link ky y l r :*: zs)

Data/Set/Internal.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ fromDistinctAscList (x0 : xs0) = go (1::Int) (Bin 1 x0 Tip Tip) xs0
974974
where
975975
go !_ t [] = t
976976
go s l (x : xs) = case create s xs of
977-
(r :*: ys) -> go (s `shiftL` 1) (link x l r) ys
977+
(r :*: ys) -> let !t' = link x l r
978+
in go (s `shiftL` 1) t' ys
978979

979980
create !_ [] = (Tip :*: [])
980981
create s xs@(x : xs')
@@ -995,7 +996,8 @@ fromDistinctDescList (x0 : xs0) = go (1::Int) (Bin 1 x0 Tip Tip) xs0
995996
where
996997
go !_ t [] = t
997998
go s r (x : xs) = case create s xs of
998-
(l :*: ys) -> go (s `shiftL` 1) (link x l r) ys
999+
(l :*: ys) -> let !t' = link x l r
1000+
in go (s `shiftL` 1) t' ys
9991001

10001002
create !_ [] = (Tip :*: [])
10011003
create s xs@(x : xs')

Utils/Containers/Internal/StrictPair.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Utils.Containers.Internal.StrictPair (StrictPair(..), toPair) where
1616
-- @
1717
data StrictPair a b = !a :*: !b
1818

19+
infixr 1 :*:
20+
1921
-- | Convert a strict pair to a standard pair.
2022
toPair :: StrictPair a b -> (a, b)
2123
toPair (x :*: y) = (x, y)

0 commit comments

Comments
 (0)