@@ -2653,6 +2653,9 @@ mapAccumRWithKey f a t
26532653-- | \(O(n \min(n,W))\).
26542654-- @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@.
26552655--
2656+ -- If `f` is monotonically non-decreasing or monotonically non-increasing, this
2657+ -- function takes \(O(n)\) time.
2658+ --
26562659-- The size of the result may be smaller if @f@ maps two or more distinct
26572660-- keys to the same new key. In this case the value at the greatest of the
26582661-- original keys is retained.
@@ -2662,11 +2665,14 @@ mapAccumRWithKey f a t
26622665-- > mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
26632666
26642667mapKeys :: (Key -> Key ) -> IntMap a -> IntMap a
2665- mapKeys f = fromList . foldrWithKey ( \ k x xs -> (f k, x) : xs) []
2668+ mapKeys f t = finishB (foldlWithKey' ( \ b kx x -> insertB (f kx) x b) emptyB t)
26662669
26672670-- | \(O(n \min(n,W))\).
26682671-- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@.
26692672--
2673+ -- If `f` is monotonically non-decreasing or monotonically non-increasing, this
2674+ -- function takes \(O(n)\) time.
2675+ --
26702676-- The size of the result may be smaller if @f@ maps two or more distinct
26712677-- keys to the same new key. In this case the associated values will be
26722678-- combined using @c@.
@@ -2677,8 +2683,8 @@ mapKeys f = fromList . foldrWithKey (\k x xs -> (f k, x) : xs) []
26772683-- Also see the performance note on 'fromListWith'.
26782684
26792685mapKeysWith :: (a -> a -> a ) -> (Key -> Key ) -> IntMap a -> IntMap a
2680- mapKeysWith c f
2681- = fromListWith c . foldrWithKey ( \ k x xs -> (f k, x) : xs) []
2686+ mapKeysWith c f t =
2687+ finishB (foldlWithKey' ( \ b kx x -> insertWithB c (f kx) x b) emptyB t)
26822688
26832689-- | \(O(n)\).
26842690-- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@
@@ -2700,8 +2706,8 @@ mapKeysWith c f
27002706-- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
27012707
27022708mapKeysMonotonic :: (Key -> Key ) -> IntMap a -> IntMap a
2703- mapKeysMonotonic f
2704- = fromDistinctAscList . foldrWithKey ( \ k x xs -> (f k, x) : xs) []
2709+ mapKeysMonotonic f t =
2710+ ascLinkAll (foldlWithKey' ( \ s kx x -> ascInsert s (f kx) x) MSNada t)
27052711
27062712{- -------------------------------------------------------------------
27072713 Filter
@@ -3487,7 +3493,8 @@ fromListWithKey f xs =
34873493-- > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
34883494
34893495fromAscList :: [(Key ,a )] -> IntMap a
3490- fromAscList xs = fromAscListWithKey (\ _ x _ -> x) xs
3496+ fromAscList xs =
3497+ ascLinkAll (Foldable. foldl' (\ s (ky, y) -> ascInsert s ky y) MSNada xs)
34913498{-# INLINE fromAscList #-} -- Inline for list fusion
34923499
34933500-- | \(O(n)\). Build a map from a list of key\/value pairs where
@@ -3555,6 +3562,17 @@ data MonoState a
35553562 = MSNada
35563563 | MSPush {- # UNPACK #-} !Key a ! (Stack a )
35573564
3565+ -- Insert an entry. The key must be >= the last inserted key. If it is equal
3566+ -- to the previous key, the previous value is replaced.
3567+ ascInsert :: MonoState a -> Int -> a -> MonoState a
3568+ ascInsert s ! ky y = case s of
3569+ MSNada -> MSPush ky y Nada
3570+ MSPush kx x stk
3571+ | kx == ky -> MSPush ky y stk
3572+ | otherwise -> let m = branchMask kx ky
3573+ in MSPush ky y (ascLinkTop stk kx (Tip kx x) m)
3574+ {-# INLINE ascInsert #-}
3575+
35583576ascLinkTop :: Stack a -> Int -> IntMap a -> Int -> Stack a
35593577ascLinkTop stk ! rk r ! rm = case stk of
35603578 Nada -> Push rm r stk
0 commit comments