Skip to content

Commit

Permalink
Fix disjoint and intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus Wagner committed Mar 10, 2024
1 parent 64fe9e7 commit 2f2aa75
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/org/rascalmpl/library/Set.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -516,26 +516,20 @@ 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);
public set[&T] intersection({set[&T] firstSet, *set[&T] otherSets}) = (firstSet | it & elem | elem <- otherSets);
public set[&T] intersection({}) = {};


@synopsis{Checks if all sets in the set are pairwise disjoined.}
@synopsis{Checks if all sets in the set are pairwise disjoint.}
@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}});
isDisjoint([{1,2}, {3,4}, {5,6}]);
isDisjoint([{1,2}, {1,4}, {5,6}]);
isDisjoint([{1,2}, {1,4}, {1,6}]);
```
}
public bool isDisjoined(set[set[&T]] sets) {
list[set[&T]] setsAsList = toList(sets);

for (elem1 <- [0..size(setsAsList)-1]) {
for (elem2 <- [elem1+1..size(setsAsList)]) {
if (setsAsList[elem1] & setsAsList[elem2] != {}) return false;
}
}

return true;
}
public bool isDisjoint([set[&T] a, set[&T] b, *_]) = false when a & b != {}; // will backtrack to other pairs of a and b while the condition fails
public default bool isDisjoint([set[&T] a, set[&T] b, *_]) = true;
public bool isDisjoint([set[&T] a]) = true;
public bool isDisjoint([]) = true;

0 comments on commit 2f2aa75

Please sign in to comment.