@@ -3702,23 +3702,8 @@ foldlFB = foldlWithKey
37023702-- > valid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
37033703
37043704fromAscList :: Eq k => [(k ,a )] -> Map k a
3705- fromAscList xs
3706- = fromDistinctAscList (combineEq xs)
3707- where
3708- -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
3709- combineEq xs'
3710- = case xs' of
3711- [] -> []
3712- [x] -> [x]
3713- (x: xx) -> combineEq' x xx
3714-
3715- combineEq' z [] = [z]
3716- combineEq' z@ (kz,_) (x@ (kx,xx): xs')
3717- | kx== kz = combineEq' (kx,xx) xs'
3718- | otherwise = z: combineEq' x xs'
3719- #if __GLASGOW_HASKELL__
3720- {-# INLINABLE fromAscList #-}
3721- #endif
3705+ fromAscList xs = fromAscListWithKey (\ _ x _ -> x) xs
3706+ {-# INLINE fromAscList #-} -- INLINE for fusion
37223707
37233708-- | \(O(n)\). Build a map from a descending list in linear time.
37243709-- /The precondition (input list is descending) is not checked./
@@ -3731,22 +3716,8 @@ fromAscList xs
37313716-- @since 0.5.8
37323717
37333718fromDescList :: Eq k => [(k ,a )] -> Map k a
3734- fromDescList xs = fromDistinctDescList (combineEq xs)
3735- where
3736- -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
3737- combineEq xs'
3738- = case xs' of
3739- [] -> []
3740- [x] -> [x]
3741- (x: xx) -> combineEq' x xx
3742-
3743- combineEq' z [] = [z]
3744- combineEq' z@ (kz,_) (x@ (kx,xx): xs')
3745- | kx== kz = combineEq' (kx,xx) xs'
3746- | otherwise = z: combineEq' x xs'
3747- #if __GLASGOW_HASKELL__
3748- {-# INLINABLE fromDescList #-}
3749- #endif
3719+ fromDescList xs = fromDescListWithKey (\ _ x _ -> x) xs
3720+ {-# INLINE fromDescList #-} -- INLINE for fusion
37503721
37513722-- | \(O(n)\). Build a map from an ascending list in linear time with a combining function for equal keys.
37523723-- /The precondition (input list is ascending) is not checked./
@@ -3758,9 +3729,7 @@ fromDescList xs = fromDistinctDescList (combineEq xs)
37583729fromAscListWith :: Eq k => (a -> a -> a ) -> [(k ,a )] -> Map k a
37593730fromAscListWith f xs
37603731 = fromAscListWithKey (\ _ x y -> f x y) xs
3761- #if __GLASGOW_HASKELL__
3762- {-# INLINABLE fromAscListWith #-}
3763- #endif
3732+ {-# INLINE fromAscListWith #-} -- INLINE for fusion
37643733
37653734-- | \(O(n)\). Build a map from a descending list in linear time with a combining function for equal keys.
37663735-- /The precondition (input list is descending) is not checked./
@@ -3776,9 +3745,7 @@ fromAscListWith f xs
37763745fromDescListWith :: Eq k => (a -> a -> a ) -> [(k ,a )] -> Map k a
37773746fromDescListWith f xs
37783747 = fromDescListWithKey (\ _ x y -> f x y) xs
3779- #if __GLASGOW_HASKELL__
3780- {-# INLINABLE fromDescListWith #-}
3781- #endif
3748+ {-# INLINE fromDescListWith #-} -- INLINE for fusion
37823749
37833750-- | \(O(n)\). Build a map from an ascending list in linear time with a
37843751-- combining function for equal keys.
@@ -3792,23 +3759,15 @@ fromDescListWith f xs
37923759-- Also see the performance note on 'fromListWith'.
37933760
37943761fromAscListWithKey :: Eq k => (k -> a -> a -> a ) -> [(k ,a )] -> Map k a
3795- fromAscListWithKey f xs
3796- = fromDistinctAscList (combineEq f xs)
3762+ fromAscListWithKey f xs = ascLinkAll (Foldable. foldl' next Nada xs)
37973763 where
3798- -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
3799- combineEq _ xs'
3800- = case xs' of
3801- [] -> []
3802- [x] -> [x]
3803- (x: xx) -> combineEq' x xx
3804-
3805- combineEq' z [] = [z]
3806- combineEq' z@ (kz,zz) (x@ (kx,xx): xs')
3807- | kx== kz = let yy = f kx xx zz in combineEq' (kx,yy) xs'
3808- | otherwise = z: combineEq' x xs'
3809- #if __GLASGOW_HASKELL__
3810- {-# INLINABLE fromAscListWithKey #-}
3811- #endif
3764+ next stk (! ky, y) = case stk of
3765+ Push kx x l stk'
3766+ | ky == kx -> Push ky (f ky y x) l stk'
3767+ | Tip <- l -> ascLinkTop stk' 1 (singleton kx x) ky y
3768+ | otherwise -> Push ky y Tip stk
3769+ Nada -> Push ky y Tip stk
3770+ {-# INLINE fromAscListWithKey #-} -- INLINE for fusion
38123771
38133772-- | \(O(n)\). Build a map from a descending list in linear time with a
38143773-- combining function for equal keys.
@@ -3822,23 +3781,15 @@ fromAscListWithKey f xs
38223781-- Also see the performance note on 'fromListWith'.
38233782
38243783fromDescListWithKey :: Eq k => (k -> a -> a -> a ) -> [(k ,a )] -> Map k a
3825- fromDescListWithKey f xs
3826- = fromDistinctDescList (combineEq f xs)
3784+ fromDescListWithKey f xs = descLinkAll (Foldable. foldl' next Nada xs)
38273785 where
3828- -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
3829- combineEq _ xs'
3830- = case xs' of
3831- [] -> []
3832- [x] -> [x]
3833- (x: xx) -> combineEq' x xx
3834-
3835- combineEq' z [] = [z]
3836- combineEq' z@ (kz,zz) (x@ (kx,xx): xs')
3837- | kx== kz = let yy = f kx xx zz in combineEq' (kx,yy) xs'
3838- | otherwise = z: combineEq' x xs'
3839- #if __GLASGOW_HASKELL__
3840- {-# INLINABLE fromDescListWithKey #-}
3841- #endif
3786+ next stk (! ky, y) = case stk of
3787+ Push kx x r stk'
3788+ | ky == kx -> Push ky (f ky y x) r stk'
3789+ | Tip <- r -> descLinkTop ky y 1 (singleton kx x) stk'
3790+ | otherwise -> Push ky y Tip stk
3791+ Nada -> Push ky y Tip stk
3792+ {-# INLINE fromDescListWithKey #-} -- INLINE for fusion
38423793
38433794
38443795-- | \(O(n)\). Build a map from an ascending list of distinct elements in linear time.
@@ -3850,7 +3801,7 @@ fromDescListWithKey f xs
38503801
38513802-- See Note [fromDistinctAscList implementation] in Data.Set.Internal.
38523803fromDistinctAscList :: [(k ,a )] -> Map k a
3853- fromDistinctAscList = ascLinkAll . Foldable. foldl' next Nada
3804+ fromDistinctAscList xs = ascLinkAll ( Foldable. foldl' next Nada xs)
38543805 where
38553806 next :: Stack k a -> (k , a ) -> Stack k a
38563807 next (Push kx x Tip stk) (! ky, y) = ascLinkTop stk 1 (singleton kx x) ky y
@@ -3879,7 +3830,7 @@ ascLinkAll stk = foldl'Stack (\r kx x l -> link kx x l r) Tip stk
38793830
38803831-- See Note [fromDistinctAscList implementation] in Data.Set.Internal.
38813832fromDistinctDescList :: [(k ,a )] -> Map k a
3882- fromDistinctDescList = descLinkAll . Foldable. foldl' next Nada
3833+ fromDistinctDescList xs = descLinkAll ( Foldable. foldl' next Nada xs)
38833834 where
38843835 next :: Stack k a -> (k , a ) -> Stack k a
38853836 next (Push ky y Tip stk) (! kx, x) = descLinkTop kx x 1 (singleton ky y) stk
0 commit comments