From 8bcfcd3d178c6697248f5a924d6ababedd0f517a Mon Sep 17 00:00:00 2001 From: Linus Wagner Date: Thu, 7 Mar 2024 22:35:42 +0100 Subject: [PATCH] Add intersection and isDisjoined to set lib --- src/org/rascalmpl/library/Set.rsc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/org/rascalmpl/library/Set.rsc b/src/org/rascalmpl/library/Set.rsc index 10e0f819cdd..82903b81253 100644 --- a/src/org/rascalmpl/library/Set.rsc +++ b/src/org/rascalmpl/library/Set.rsc @@ -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; +} \ No newline at end of file