Skip to content

Commit

Permalink
Add intersection and isDisjoined to set lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus Wagner committed Mar 7, 2024
1 parent 39f96ec commit 8bcfcd3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/org/rascalmpl/library/Set.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,29 @@ public set[&T] union(set[set[&T]] sets) = {*s | s <- sets};

@synopsis{Compute the Jaccard similarity between two sets.}
real jaccard(set[value] x, set[value] y) = (1. * size(x & y)) / size(x + y);


@synopsis{Calculate the intersection of a set of sets.}
public set[&T] intersection(set[set[&T]] sets) = (getFirstFrom(sets) | it & elem | elem <- sets);


@synopsis{Checks if all sets in the set are pairwise disjoined.}
@examples{
```rascal-shell
import Set;
isDisjoined({{1,2}, {3,4}, {5,6}});
isDisjoined({{1,2}, {1,4}, {5,6}});
isDisjoined({{1,2}, {1,4}, {1,6}});
```
}
public bool isDisjoined(set[set[&T]] sets) {
list[set[&T]] setsAsList = toList(sets);
pairs = {};
for (elem1 <- setsAsList) {
for (elem2 <- setsAsList[1..]) {
if (elem1 & elem2 != {}) return false;
}
}

return true;
}

0 comments on commit 8bcfcd3

Please sign in to comment.