@@ -622,6 +622,11 @@ lookup' h k m = lookupCont (\_ -> Nothing) (\v _i -> Just v) h k m
622
622
-- If a collision did not occur then it will have the Int value (-1).
623
623
data LookupRes a = Absent | Present a ! Int
624
624
625
+ lookupResToMaybe :: LookupRes a -> Maybe a
626
+ lookupResToMaybe Absent = Nothing
627
+ lookupResToMaybe (Present x _) = Just x
628
+ {-# INLINE lookupResToMaybe #-}
629
+
625
630
-- Internal helper for lookup. This version takes the precomputed hash so
626
631
-- that functions that make multiple calls to lookup and related functions
627
632
-- (insert, delete) only need to calculate the hash once.
@@ -1279,11 +1284,16 @@ alterF :: (Functor f, Eq k, Hashable k)
1279
1284
alterF f = \ ! k ! m ->
1280
1285
let
1281
1286
! h = hash k
1282
- mv = lookup' h k m
1287
+ lookupRes = lookupRecordCollision h k m
1288
+ mv = lookupResToMaybe lookupRes
1283
1289
in (<$> f mv) $ \ fres ->
1284
1290
case fres of
1285
- Nothing -> maybe m (const (delete' h k m)) mv
1286
- Just v' -> insert' h k v' m
1291
+ Nothing -> case lookupRes of
1292
+ Absent -> m
1293
+ Present _ i -> deleteKeyExists i h k m
1294
+ Just v' -> case lookupRes of
1295
+ Absent -> insertNewKey h k v' m
1296
+ Present _ i -> insertKeyExists i h k v' m
1287
1297
1288
1298
-- We unconditionally rewrite alterF in RULES, but we expose an
1289
1299
-- unfolding just in case it's used in some way that prevents the
@@ -1403,9 +1413,7 @@ alterFEager f !k m = (<$> f mv) $ \fres ->
1403
1413
1404
1414
where ! h = hash k
1405
1415
! lookupRes = lookupRecordCollision h k m
1406
- ! mv = case lookupRes of
1407
- Absent -> Nothing
1408
- Present v _ -> Just v
1416
+ ! mv = lookupResToMaybe lookupRes
1409
1417
{-# INLINABLE alterFEager #-}
1410
1418
#endif
1411
1419
0 commit comments