Skip to content

Commit 5b3da8f

Browse files
authored
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.
1 parent 7e7ce15 commit 5b3da8f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

containers-tests/tests/intmap-properties.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,9 @@ test_updateMin = do
10791079
updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3, "Xb"), (5, "a")]
10801080
updateMin (\ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton 5 "a"
10811081

1082+
updateMin (\ a -> Just ("X" ++ a)) (empty :: SMap) @?= empty
1083+
updateMin (\ _ -> Nothing) (empty :: SMap) @?= empty
1084+
10821085
test_updateMax :: Assertion
10831086
test_updateMax = do
10841087
updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) @?= fromList [(3, "b"), (5, "Xa")]
@@ -1087,6 +1090,9 @@ test_updateMax = do
10871090
updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3, "b"), (5, "Xa")]
10881091
updateMax (\ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton (-3) "b"
10891092

1093+
updateMax (\ a -> Just ("X" ++ a)) (empty :: SMap) @?= empty
1094+
updateMax (\ _ -> Nothing) (empty :: SMap) @?= empty
1095+
10901096
test_updateMinWithKey :: Assertion
10911097
test_updateMinWithKey = do
10921098
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
10951101
updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3,"-3:b"), (5,"a")]
10961102
updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton 5 "a"
10971103

1104+
updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (empty :: SMap) @?= empty
1105+
updateMinWithKey (\ _ _ -> Nothing) (empty :: SMap) @?= empty
1106+
10981107
test_updateMaxWithKey :: Assertion
10991108
test_updateMaxWithKey = do
11001109
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
11031112
updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (-3,"b")]) @?= fromList [(-3,"b"), (5,"5:a")]
11041113
updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (-3,"b")]) @?= singleton (-3) "b"
11051114

1115+
updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (empty :: SMap) @?= empty
1116+
updateMaxWithKey (\ _ _ -> Nothing) (empty :: SMap) @?= empty
1117+
11061118
test_minView :: Assertion
11071119
test_minView = do
11081120
minView (fromList [(5,"a"), (3,"b")]) @?= Just ("b", singleton 5 "a")

containers/src/Data/IntMap/Internal.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ updateMinWithKey f t =
21862186
go f' (Tip k y) = case f' k y of
21872187
Just y' -> Tip k y'
21882188
Nothing -> Nil
2189-
go _ Nil = error "updateMinWithKey Nil"
2189+
go _ Nil = Nil
21902190

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

22072207

22082208
data View a = View {-# UNPACK #-} !Key a !(IntMap a)

containers/src/Data/IntMap/Strict/Internal.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ updateMinWithKey f t =
771771
go f' (Tip k y) = case f' k y of
772772
Just !y' -> Tip k y'
773773
Nothing -> Nil
774-
go _ Nil = error "updateMinWithKey Nil"
774+
go _ Nil = Nil
775775

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

792792
-- | \(O(\log n)\). Update the value at the maximal key.
793793
--

0 commit comments

Comments
 (0)