From dadafb773f49b8bd398abe5162ecbaaf91d54b80 Mon Sep 17 00:00:00 2001 From: Pilow Date: Tue, 5 Sep 2023 11:23:39 +0200 Subject: [PATCH] set : add difference, disjunction, toArray --- .../java/leekscript/runner/LeekFunctions.java | 3 ++ .../runner/values/SetLeekValue.java | 32 +++++++++++++++++++ src/test/java/test/TestSet.java | 11 ++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/leekscript/runner/LeekFunctions.java b/src/main/java/leekscript/runner/LeekFunctions.java index 1a61fdc0..f55ac744 100644 --- a/src/main/java/leekscript/runner/LeekFunctions.java +++ b/src/main/java/leekscript/runner/LeekFunctions.java @@ -244,6 +244,9 @@ public class LeekFunctions { method("setIsSubsetOf", "Set", Type.BOOL, new Type[] { Type.SET, Type.SET }); method("setUnion", "Set", Type.SET, new Type[] { Type.SET, Type.SET }); method("setIntersection", "Set", Type.SET, new Type[] { Type.SET, Type.SET }); + method("setDifference", "Set", Type.SET, new Type[] { Type.SET, Type.SET }); + method("setDisjunction", "Set", Type.SET, new Type[] { Type.SET, Type.SET }); + method("setToArray", "Set", Type.ARRAY, new Type[] { Type.SET }); /** * Interval functions diff --git a/src/main/java/leekscript/runner/values/SetLeekValue.java b/src/main/java/leekscript/runner/values/SetLeekValue.java index 65770f65..a4279133 100644 --- a/src/main/java/leekscript/runner/values/SetLeekValue.java +++ b/src/main/java/leekscript/runner/values/SetLeekValue.java @@ -146,4 +146,36 @@ public SetLeekValue setIntersection(AI ai, SetLeekValue set) throws LeekRunExcep r.retainAll(set); return r; } + + public SetLeekValue setDifference(AI ai, SetLeekValue set) throws LeekRunException { + ai.ops((this.size() + set.size()) * 2); + var r = new SetLeekValue(ai); + r.addAll(this); + r.removeAll(set); + return r; + } + + public SetLeekValue setDisjunction(AI ai, SetLeekValue set) throws LeekRunException { + ai.ops((this.size() + set.size()) * 4); + var r = new SetLeekValue(ai); + for (var e : this) { + if (!set.contains(e)) r.add(e); + } + for (var e : set) { + if (!this.contains(e)) r.add(e); + } + return r; + } + + public ArrayLeekValue setToArray(AI ai) throws LeekRunException { + ai.ops(this.size() * 2); + var r = new ArrayLeekValue(ai, this.toArray()); + return r; + } + + public LegacyArrayLeekValue setToArray_v1_3(AI ai) throws LeekRunException { + ai.ops(this.size() * 2); + var r = new LegacyArrayLeekValue(ai, this.toArray()); + return r; + } } diff --git a/src/test/java/test/TestSet.java b/src/test/java/test/TestSet.java index 744e2fda..d93107ed 100644 --- a/src/test/java/test/TestSet.java +++ b/src/test/java/test/TestSet.java @@ -81,7 +81,16 @@ public void run() throws Exception { section("Set.union()"); code("var s1 = <1, 2, 3> var s2 = <4, 5, 6> return setUnion(s1, s2)").equals("<1, 2, 3, 4, 5, 6>"); - section("Set.union()"); + section("Set.intersection()"); code("var s1 = <1, 2, 3> var s2 = <2, 3, 4> return setIntersection(s1, s2)").debug().equals("<2, 3>"); + + section("Set.difference()"); + code("var s1 = <1, 2, 3> var s2 = <2, 3, 4> return setDifference(s1, s2)").debug().equals("<1>"); + + section("Set.disjunction()"); + code("var s1 = <1, 2, 3> var s2 = <2, 3, 4> return setDisjunction(s1, s2)").debug().equals("<1, 4>"); + + section("Set.toArray()"); + code("var s = <1, 2, 3> return setToArray(s)").equals("[1, 2, 3]"); } }