Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning Nil on empty IntMap as done in corresponding functions for Map #1065

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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