implement set operations on dict

This commit is contained in:
Folkert 2022-07-13 11:33:39 +02:00
parent 8e21fdcb04
commit cf69d41a7b
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 27 additions and 20 deletions

View file

@ -151,7 +151,7 @@ values = \@Dict list ->
# union : Dict k v, Dict k v -> Dict k v # union : Dict k v, Dict k v -> Dict k v
insertAll : Dict k v, Dict k v -> Dict k v insertAll : Dict k v, Dict k v -> Dict k v
insertAll = \xs, @Dict ys -> insertAll = \xs, @Dict ys ->
List.walk ys xs (\state, Pair k v -> Dict.insert state k v) List.walk ys xs (\state, Pair k v -> Dict.insertIfVacant state k v)
# intersection : Dict k v, Dict k v -> Dict k v # intersection : Dict k v, Dict k v -> Dict k v
keepShared : Dict k v, Dict k v -> Dict k v keepShared : Dict k v, Dict k v -> Dict k v
@ -172,3 +172,10 @@ insertFresh = \@Dict list, k, v ->
list list
|> List.append (Pair k v) |> List.append (Pair k v)
|> @Dict |> @Dict
insertIfVacant : Dict k v, k, v -> Dict k v
insertIfVacant = \dict, key, value ->
if Dict.contains dict key then
dict
else
Dict.insert dict key value

View file

@ -1315,12 +1315,11 @@ define_builtins! {
10 DICT_KEYS: "keys" 10 DICT_KEYS: "keys"
11 DICT_VALUES: "values" 11 DICT_VALUES: "values"
12 DICT_UNION: "union" 12 DICT_INSERT_ALL: "insertAll" // union
13 DICT_INTERSECTION: "intersection" 13 DICT_KEEP_SHARED: "keepShared" // intersection
14 DICT_DIFFERENCE: "difference" 14 DICT_REMOVE_ALL: "removeAll" // difference
15 DICT_GET_LOWLEVEL: "getLowlevel" 15 DICT_WITH_CAPACITY: "withCapacity"
16 DICT_WITH_CAPACITY: "withCapacity"
} }
8 SET: "Set" => { 8 SET: "Set" => {
0 SET_SET: "Set" // the Set.Set type alias 0 SET_SET: "Set" // the Set.Set type alias

View file

@ -404,7 +404,7 @@ fn single() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
fn union() { fn insert_all() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -412,7 +412,7 @@ fn union() {
myDict : Dict.Dict I64 {} myDict : Dict.Dict I64 {}
myDict = myDict =
Dict.union (Dict.single 0 {}) (Dict.single 1 {}) Dict.insertAll (Dict.single 0 {}) (Dict.single 1 {})
main = Dict.len myDict main = Dict.len myDict
"# "#
@ -424,7 +424,7 @@ fn union() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
fn union_prefer_first() { fn insert_all_prefer_first() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -432,7 +432,8 @@ fn union_prefer_first() {
myDict : Dict.Dict I64 I64 myDict : Dict.Dict I64 I64
myDict = myDict =
Dict.union (Dict.single 0 100) (Dict.single 0 200) (Dict.single 0 100)
|> Dict.insertAll (Dict.single 0 200)
main = Dict.values myDict main = Dict.values myDict
"# "#
@ -444,7 +445,7 @@ fn union_prefer_first() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
fn intersection() { fn keep_shared() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -467,7 +468,7 @@ fn intersection() {
|> Dict.insert 4 {} |> Dict.insert 4 {}
main = main =
Dict.intersection dict1 dict2 Dict.keepShared dict1 dict2
|> Dict.len |> Dict.len
"# "#
), ),
@ -478,7 +479,7 @@ fn intersection() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
fn intersection_prefer_first() { fn keep_shared_prefer_first() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -501,18 +502,18 @@ fn intersection_prefer_first() {
|> Dict.insert 4 300 |> Dict.insert 4 300
main = main =
Dict.intersection dict1 dict2 Dict.keepShared dict1 dict2
|> Dict.values |> Dict.values
"# "#
), ),
RocList::from_slice(&[4, 2]), RocList::from_slice(&[2, 4]),
RocList<i64> RocList<i64>
); );
} }
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
fn difference() { fn remove_all() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -535,7 +536,7 @@ fn difference() {
|> Dict.insert 4 {} |> Dict.insert 4 {}
main = main =
Dict.difference dict1 dict2 Dict.removeAll dict1 dict2
|> Dict.len |> Dict.len
"# "#
), ),
@ -546,7 +547,7 @@ fn difference() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
fn difference_prefer_first() { fn remove_all_prefer_first() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -569,11 +570,11 @@ fn difference_prefer_first() {
|> Dict.insert 4 300 |> Dict.insert 4 300
main = main =
Dict.difference dict1 dict2 Dict.removeAll dict1 dict2
|> Dict.values |> Dict.values
"# "#
), ),
RocList::from_slice(&[5, 3, 1]), RocList::from_slice(&[1, 5, 3]),
RocList<i64> RocList<i64>
); );
} }