Skip to content

Commit

Permalink
Update IntMap.updateMin and friends (#1065)
Browse files Browse the repository at this point in the history
Return an empty map for an input empty map instead of calling error.
This matches the behavior of Data.Map.
  • Loading branch information
kushagarr authored Nov 10, 2024
1 parent 7e7ce15 commit 5b3da8f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions containers-tests/tests/intmap-properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,9 @@ test_updateMin = do
updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3, "Xb"), (5, "a")]
updateMin (\ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton 5 "a"

updateMin (\ a -> Just ("X" ++ a)) (empty :: SMap) @?= empty
updateMin (\ _ -> Nothing) (empty :: SMap) @?= empty

test_updateMax :: Assertion
test_updateMax = do
updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) @?= fromList [(3, "b"), (5, "Xa")]
Expand All @@ -1087,6 +1090,9 @@ test_updateMax = do
updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3, "b"), (5, "Xa")]
updateMax (\ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton (-3) "b"

updateMax (\ a -> Just ("X" ++ a)) (empty :: SMap) @?= empty
updateMax (\ _ -> Nothing) (empty :: SMap) @?= empty

test_updateMinWithKey :: Assertion
test_updateMinWithKey = do
updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) @?= fromList [(3,"3:b"), (5,"a")]
Expand All @@ -1095,6 +1101,9 @@ test_updateMinWithKey = do
updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3,"-3:b"), (5,"a")]
updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton 5 "a"

updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (empty :: SMap) @?= empty
updateMinWithKey (\ _ _ -> Nothing) (empty :: SMap) @?= empty

test_updateMaxWithKey :: Assertion
test_updateMaxWithKey = do
updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) @?= fromList [(3,"b"), (5,"5:a")]
Expand All @@ -1103,6 +1112,9 @@ test_updateMaxWithKey = do
updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3,"b"), (5,"5:a")]
updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton (-3) "b"

updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (empty :: SMap) @?= empty
updateMaxWithKey (\ _ _ -> Nothing) (empty :: SMap) @?= empty

test_minView :: Assertion
test_minView = do
minView (fromList [(5,"a"), (3,"b")]) @?= Just ("b", singleton 5 "a")
Expand Down
4 changes: 2 additions & 2 deletions containers/src/Data/IntMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ updateMinWithKey f t =
go f' (Tip k y) = case f' k y of
Just y' -> Tip k y'
Nothing -> Nil
go _ Nil = error "updateMinWithKey Nil"
go _ Nil = Nil

-- | \(O(\min(n,W))\). Update the value at the maximal key.
--
Expand All @@ -2202,7 +2202,7 @@ updateMaxWithKey f t =
go f' (Tip k y) = case f' k y of
Just y' -> Tip k y'
Nothing -> Nil
go _ Nil = error "updateMaxWithKey Nil"
go _ Nil = Nil


data View a = View {-# UNPACK #-} !Key a !(IntMap a)
Expand Down
4 changes: 2 additions & 2 deletions containers/src/Data/IntMap/Strict/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ updateMinWithKey f t =
go f' (Tip k y) = case f' k y of
Just !y' -> Tip k y'
Nothing -> Nil
go _ Nil = error "updateMinWithKey Nil"
go _ Nil = Nil

-- | \(O(\log n)\). Update the value at the maximal key.
--
Expand All @@ -787,7 +787,7 @@ updateMaxWithKey f t =
go f' (Tip k y) = case f' k y of
Just !y' -> Tip k y'
Nothing -> Nil
go _ Nil = error "updateMaxWithKey Nil"
go _ Nil = Nil

-- | \(O(\log n)\). Update the value at the maximal key.
--
Expand Down

0 comments on commit 5b3da8f

Please sign in to comment.