@@ -1213,6 +1213,21 @@ difference a b = foldlWithKey' go empty a
1213
1213
_ -> m
1214
1214
{-# INLINABLE difference #-}
1215
1215
1216
+ -- | /O(n*log m)/ Difference of two maps. Return elements of the first map
1217
+ -- not existing in the second. Returns a tuple with the size of the resulting
1218
+ -- hashmap, and the hashmap itself.
1219
+ differenceInternal
1220
+ :: (Eq k , Hashable k )
1221
+ => HashMap k v
1222
+ -> HashMap k w
1223
+ -> (Int , HashMap k v )
1224
+ differenceInternal a b = foldlWithKey' go (0 , empty) a
1225
+ where
1226
+ go p@ (sz, m) k v = case lookup k b of
1227
+ Nothing -> let (s, m') = insertInternal k v m in (sz + s, m')
1228
+ _ -> p
1229
+ {-# INLINABLE differenceInternal #-}
1230
+
1216
1231
-- | /O(n*log m)/ Difference with a combining function. When two equal keys are
1217
1232
-- encountered, the combining function is applied to the values of these keys.
1218
1233
-- If it returns 'Nothing', the element is discarded (proper set difference). If
@@ -1225,6 +1240,27 @@ differenceWith f a b = foldlWithKey' go empty a
1225
1240
Just w -> maybe m (\ y -> insert k y m) (f v w)
1226
1241
{-# INLINABLE differenceWith #-}
1227
1242
1243
+ -- | /O(n*log m)/ Difference with a combining function. When two equal keys are
1244
+ -- encountered, the combining function is applied to the values of these keys.
1245
+ -- If it returns 'Nothing', the element is discarded (proper set difference). If
1246
+ -- it returns (@'Just' y@), the element is updated with a new value @y@.
1247
+ -- Returns a tuple with the size of the resulting hashmap, and the hashmap
1248
+ -- itself.
1249
+ differenceWithInternal
1250
+ :: (Eq k , Hashable k )
1251
+ => (v -> w -> Maybe v )
1252
+ -> HashMap k v
1253
+ -> HashMap k w
1254
+ -> (Int , HashMap k v )
1255
+ differenceWithInternal f a b = foldlWithKey' go (0 , empty) a
1256
+ where
1257
+ go p@ (sz, m) k v = case lookup k b of
1258
+ Nothing -> let (s, m') = insertInternal k v m in (sz + s, m')
1259
+ Just w -> maybe p (\ y -> let (s, m') = insertInternal k y m
1260
+ in (sz + s, m'))
1261
+ (f v w)
1262
+ {-# INLINABLE differenceWithInternal #-}
1263
+
1228
1264
-- | /O(n*log m)/ Intersection of two maps. Return elements of the first
1229
1265
-- map for keys existing in the second.
1230
1266
intersection :: (Eq k , Hashable k ) => HashMap k v -> HashMap k w -> HashMap k v
@@ -1235,6 +1271,22 @@ intersection a b = foldlWithKey' go empty a
1235
1271
_ -> m
1236
1272
{-# INLINABLE intersection #-}
1237
1273
1274
+ -- | /O(n*log m)/ Intersection of two maps. Return elements of the first
1275
+ -- map for keys existing in the second.
1276
+ --- The result is a tuple with the size of the resulting hashmap, and the
1277
+ -- hashmap itself.
1278
+ insersectionInternal
1279
+ :: (Eq k , Hashable k )
1280
+ => HashMap k v
1281
+ -> HashMap k w
1282
+ -> (Int , HashMap k v )
1283
+ insersectionInternal a b = foldlWithKey' go (0 , empty) a
1284
+ where
1285
+ go p@ (sz, m) k v = case lookup k b of
1286
+ Just _ -> let (s, m') = insertInternal k v m in (sz + s, m')
1287
+ _ -> p
1288
+ {-# INLINABLE insersectionInternal #-}
1289
+
1238
1290
-- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
1239
1291
-- the provided function is used to combine the values from the two
1240
1292
-- maps.
@@ -1247,6 +1299,20 @@ intersectionWith f a b = foldlWithKey' go empty a
1247
1299
_ -> m
1248
1300
{-# INLINABLE intersectionWith #-}
1249
1301
1302
+ -- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
1303
+ -- the provided function is used to combine the values from the two
1304
+ -- maps.
1305
+ -- The result is a tuple with the size of the resulting hashmap, and the
1306
+ -- hashmap itself.
1307
+ insersectionWithInternal :: (Eq k , Hashable k ) => (v1 -> v2 -> v3 ) -> HashMap k v1
1308
+ -> HashMap k v2 -> (Int , HashMap k v3 )
1309
+ insersectionWithInternal f a b = foldlWithKey' go (0 , empty) a
1310
+ where
1311
+ go p@ (sz, m) k v = case lookup k b of
1312
+ Just w -> let (s, m') = insertInternal k (f v w) m in (sz + s, m')
1313
+ _ -> p
1314
+ {-# INLINABLE insersectionWithInternal #-}
1315
+
1250
1316
-- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
1251
1317
-- the provided function is used to combine the values from the two
1252
1318
-- maps.
@@ -1259,6 +1325,20 @@ intersectionWithKey f a b = foldlWithKey' go empty a
1259
1325
_ -> m
1260
1326
{-# INLINABLE intersectionWithKey #-}
1261
1327
1328
+ -- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
1329
+ -- the provided function is used to combine the values from the two
1330
+ -- maps.
1331
+ -- The result is a tuple with the size of the resulting hashmap, and the
1332
+ -- hashmap itself.
1333
+ intersectionWithKeyInternal :: (Eq k , Hashable k ) => (k -> v1 -> v2 -> v3 )
1334
+ -> HashMap k v1 -> HashMap k v2 -> (Int , HashMap k v3 )
1335
+ intersectionWithKeyInternal f a b = foldlWithKey' go (0 , empty) a
1336
+ where
1337
+ go p@ (sz, m) k v = case lookup k b of
1338
+ Just w -> let (s, m') = insertInternal k (f k v w) m in (sz+ s, m')
1339
+ _ -> p
1340
+ {-# INLINABLE intersectionWithKeyInternal #-}
1341
+
1262
1342
------------------------------------------------------------------------
1263
1343
-- * Folds
1264
1344
0 commit comments