optimize set intersection and union

This commit is contained in:
Brendan Hansknecht 2023-12-03 18:13:05 -08:00
parent 35146d1a0b
commit a0854a10e7
No known key found for this signature in database
GPG key ID: 0EA784685083E75B

View file

@ -622,7 +622,10 @@ values = \@Dict { data } ->
## ```
insertAll : Dict k v, Dict k v -> Dict k v where k implements Hash & Eq
insertAll = \xs, ys ->
walk ys xs insert
if len ys > len xs then
insertAll ys xs
else
walk ys xs insert
## Combine two dictionaries by keeping the [intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory))
## of all the key-value pairs. This means that we keep only those pairs
@ -644,15 +647,18 @@ insertAll = \xs, ys ->
## ```
keepShared : Dict k v, Dict k v -> Dict k v where k implements Hash & Eq
keepShared = \xs, ys ->
walk
xs
(empty {})
(\state, k, v ->
if contains ys k then
insert state k v
else
state
)
if len ys < len xs then
keepShared ys xs
else
walk
xs
(withCapacity (len xs))
(\state, k, v ->
if contains ys k then
insert state k v
else
state
)
## Remove the key-value pairs in the first input that are also in the second
## using the [set difference](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement)