Skip to content

Commit 497aba5

Browse files
committed
Use smart insertion and deletion in alterF
1 parent f4bf21d commit 497aba5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Data/HashMap/Internal.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,10 +1305,16 @@ alterF :: (Functor f, Eq k, Hashable k)
13051305
alterF f = \ !k !m ->
13061306
let
13071307
!h = hash k
1308-
mv = lookup' h k m
1309-
in (<$> f mv) $ \case
1310-
Nothing -> maybe m (const (delete' h k m)) mv
1311-
Just v' -> insert' h k v' m
1308+
lookupRes = lookupRecordCollision h k m
1309+
mv = lookupResToMaybe lookupRes
1310+
in (<$> f mv) $ \fres ->
1311+
case fres of
1312+
Nothing -> case lookupRes of
1313+
Absent -> m
1314+
Present _ i -> deleteKeyExists i h k m
1315+
Just v' -> case lookupRes of
1316+
Absent -> insertNewKey h k v' m
1317+
Present _ i -> insertKeyExists i h k v' m
13121318

13131319
-- We unconditionally rewrite alterF in RULES, but we expose an
13141320
-- unfolding just in case it's used in some way that prevents the

0 commit comments

Comments
 (0)