From 2f2aa75a8cf7b87984e52e07fbc5e3a7bd370664 Mon Sep 17 00:00:00 2001 From: Linus Wagner Date: Sun, 10 Mar 2024 14:31:19 +0100 Subject: [PATCH] Fix disjoint and intersection --- src/org/rascalmpl/library/Set.rsc | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/org/rascalmpl/library/Set.rsc b/src/org/rascalmpl/library/Set.rsc index ccaeb3d69d1..f891f0ba6b0 100644 --- a/src/org/rascalmpl/library/Set.rsc +++ b/src/org/rascalmpl/library/Set.rsc @@ -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; -} \ No newline at end of file +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;