From 5b3da8f97619f0ab6b148e9c44fc98fdf0bf7a2a Mon Sep 17 00:00:00 2001 From: Kushagra Gupta <10558687+kushagarr@users.noreply.github.com> Date: Sun, 10 Nov 2024 12:50:27 +0530 Subject: [PATCH] Update IntMap.updateMin and friends (#1065) Return an empty map for an input empty map instead of calling error. This matches the behavior of Data.Map. --- containers-tests/tests/intmap-properties.hs | 12 ++++++++++++ containers/src/Data/IntMap/Internal.hs | 4 ++-- containers/src/Data/IntMap/Strict/Internal.hs | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/containers-tests/tests/intmap-properties.hs b/containers-tests/tests/intmap-properties.hs index 38f88e451..f15a2dcd9 100644 --- a/containers-tests/tests/intmap-properties.hs +++ b/containers-tests/tests/intmap-properties.hs @@ -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")] @@ -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")] @@ -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")] @@ -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") diff --git a/containers/src/Data/IntMap/Internal.hs b/containers/src/Data/IntMap/Internal.hs index bf1508c41..eadb054d9 100644 --- a/containers/src/Data/IntMap/Internal.hs +++ b/containers/src/Data/IntMap/Internal.hs @@ -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. -- @@ -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) diff --git a/containers/src/Data/IntMap/Strict/Internal.hs b/containers/src/Data/IntMap/Strict/Internal.hs index e4e81a9ac..cc17d9aac 100644 --- a/containers/src/Data/IntMap/Strict/Internal.hs +++ b/containers/src/Data/IntMap/Strict/Internal.hs @@ -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. -- @@ -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. --