Skip to content

Use smart insertion and deletion in alterF #289

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
14 changes: 10 additions & 4 deletions Data/HashMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,10 +1305,16 @@ alterF :: (Functor f, Eq k, Hashable k)
alterF f = \ !k !m ->
let
!h = hash k
mv = lookup' h k m
in (<$> f mv) $ \case
Nothing -> maybe m (const (delete' h k m)) mv
Just v' -> insert' h k v' m
lookupRes = lookupRecordCollision h k m
mv = lookupResToMaybe lookupRes
in (<$> f mv) $ \fres ->
case fres of
Nothing -> case lookupRes of
Absent -> m
Present _ i -> deleteKeyExists i h k m
Just v' -> case lookupRes of
Absent -> insertNewKey h k v' m
Present _ i -> insertKeyExists i h k v' m

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