Skip to content

Commit

Permalink
[set] Add methods setContains/Size/IsEmpty/IsSubsetOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazurl authored and 5pilow committed Sep 4, 2023
1 parent 7f76f0b commit c61ba70
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/leekscript/runner/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,8 @@ public boolean operatorIn(Object container, Object value) throws LeekRunExceptio
return ((LegacyArrayLeekValue) container).operatorIn(value);
} else if (container instanceof MapLeekValue) {
return ((MapLeekValue) container).operatorIn(value);
} else if (container instanceof SetLeekValue) {
return ((SetLeekValue) container).operatorIn(value);
}

ops(1);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/leekscript/runner/LeekFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ public class LeekFunctions {
method("setPut", "Set", 3, Type.ANY, new Type[] { Type.SET, Type.ANY }).setMinVersion(4);
method("setRemove", "Set", 2, Type.ANY, new Type[] { Type.SET, Type.ANY }).setMinVersion(4);
method("setClear", "Set", 1, Type.SET, new Type[] { Type.SET }).setMinVersion(4);
method("setContains", "Set", 2, Type.BOOL, new Type[] { Type.SET, Type.ANY }).setMinVersion(4);
method("setSize", "Set", 1, Type.INT, new Type[] { Type.SET }).setMinVersion(4);
method("setIsEmpty", "Set", 2, Type.BOOL, new Type[] { Type.SET }).setMinVersion(4);
method("setIsSubsetOf", "Set", Type.BOOL, new Type[] { Type.SET, Type.SET }).setMinVersion(4);

/**
* Interval functions
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/leekscript/runner/values/SetLeekValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,25 @@ public SetLeekValue setClear(AI ai) throws LeekRunException {
set.clear();
return this;
}

public boolean setContains(AI ai, Object value) throws LeekRunException {
return operatorIn(value);
}

public boolean operatorIn(Object value) throws LeekRunException {
return set.contains(value);
}

public long setSize(AI ai) throws LeekRunException {
return set.size();
}

public boolean setIsEmpty(AI ai) throws LeekRunException {
return set.isEmpty();
}

public boolean setIsSubsetOf(AI ai, SetLeekValue set) throws LeekRunException {
ai.ops(this.set.size() * 2);
return set.set.containsAll(this.set);
}
}
37 changes: 37 additions & 0 deletions src/test/java/test/TestSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,42 @@ public void run() throws Exception {
code_v4_("Set i = <1, 2>; setClear(i); setClear(i); return i").equals("<>");
code_v4_("Set i = <>; setClear(i); return i").equals("<>");
code_v4_("Set i = <'okay', 1>; setClear(i); return i").equals("<>");

section("Set.setContains()");
code_v4_("Set i = <1, 2>; return setContains(i, 1)").equals("true");
code_v4_("Set i = <1, 2>; return setContains(i, 3)").equals("false");
code_v4_("Set i = <>; return setContains(i, 1)").equals("false");
code_v4_("Set i = <'okay', 1>; return setContains(i, 'okay')").equals("true");
code_v4_("Set i = <'okay', 1>; return setContains(i, 'other')").equals("false");

section("Set.in");
code_v4_("Set i = <1, 2>; return 1 in i").equals("true");
code_v4_("Set i = <1, 2>; return 3 in i").equals("false");
code_v4_("Set i = <>; return 1 in i").equals("false");
code_v4_("Set i = <'okay', 1>; return 'okay' in i").equals("true");
code_v4_("Set i = <'okay', 1>; return 'other' in i").equals("false");

section("Set.setSize()");
code_v4_("Set i = <1, 2>; return setSize(i)").equals("2");
code_v4_("Set i = <1, 2>; setRemove(i, 1); return setSize(i)").equals("1");
code_v4_("Set i = <>; return setSize(i)").equals("0");
code_v4_("Set i = <'okay', 1>; return setSize(i)").equals("2");

section("Set.setIsEmpty()");
code_v4_("Set i = <1, 2>; setRemove(i, 1); return setIsEmpty(i)").equals("false");
code_v4_("Set i = <1, 2>; setClear(i); return setIsEmpty(i)").equals("true");
code_v4_("Set i = <1>; setRemove(i, 1); return setIsEmpty(i)").equals("true");
code_v4_("Set i = <>; return setIsEmpty(i)").equals("true");
code_v4_("Set i = <'okay', 1>; return setIsEmpty(i)").equals("false");

section("Set.setSubsetOf()");
code_v4_("Set i = <1, 2>; Set j = <1, 2, 3>; return setIsSubsetOf(i, j)").equals("true");
code_v4_("Set i = <1, 2>; Set j = <1, 2, 3>; return setIsSubsetOf(j, i)").equals("false");
code_v4_("Set i = <1, 2>; Set j = <1, 2>; return setIsSubsetOf(i, j)").equals("true");
code_v4_("Set i = <1, 2>; Set j = <1, 2>; return setIsSubsetOf(j, i)").equals("true");
code_v4_("Set i = <>; Set j = <1, 2>; return setIsSubsetOf(i, j)").equals("true");
code_v4_("Set i = <>; Set j = <1, 2>; return setIsSubsetOf(j, i)").equals("false");
code_v4_("Set i = <>; Set j = <>; return setIsSubsetOf(i, j)").equals("true");

}
}

0 comments on commit c61ba70

Please sign in to comment.