Skip to content

Commit

Permalink
set : add difference, disjunction, toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Sep 5, 2023
1 parent 8ec9f48 commit dadafb7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/leekscript/runner/LeekFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/leekscript/runner/values/SetLeekValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 10 additions & 1 deletion src/test/java/test/TestSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
}
}

0 comments on commit dadafb7

Please sign in to comment.