Skip to content

Commit a0bf2ed

Browse files
committed
[GR-10847] add support for super/sub set
PullRequest: graalpython/117
2 parents b1daaf0 + 1e39c25 commit a0bf2ed

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_set.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,21 @@ def test_difference_update():
156156
s = set('abcdefghih')
157157
s.difference_update(C('cdc'), C('aba'))
158158
assert s == set('efghih')
159+
160+
161+
def test_sub_and_super():
162+
for thetype in [set, frozenset]:
163+
p, q, r = map(thetype, ['ab', 'abcde', 'def'])
164+
assert p < q
165+
assert p <= q
166+
assert q <= q
167+
assert q > p
168+
assert q >= p
169+
assert not q < r
170+
assert not q <= r
171+
assert not q > r
172+
assert not q >= r
173+
assert set('a').issubset('abc')
174+
assert set('abc').issuperset('a')
175+
assert not set('a').issubset('cbs')
176+
assert not set('cbs').issuperset('a')

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorageNodes.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.GetItemNodeGen;
6161
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.InitNodeGen;
6262
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.KeysEqualsNodeGen;
63+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.KeysIsSubsetNodeGen;
6364
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.SetItemNodeGen;
6465
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.UnionNodeGen;
6566
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -1386,6 +1387,48 @@ public static ExclusiveOrNode create() {
13861387
}
13871388
}
13881389

1390+
public abstract static class KeysIsSubsetNode extends DictStorageBaseNode {
1391+
1392+
public abstract boolean execute(HashingStorage left, HashingStorage right);
1393+
1394+
@Specialization
1395+
public boolean isSubset(HashingStorage left, HashingStorage right,
1396+
@Cached("create()") ContainsKeyNode containsKeyNode,
1397+
@Cached("createBinaryProfile()") ConditionProfile sizeProfile) {
1398+
if (sizeProfile.profile(left.length() > right.length())) {
1399+
return false;
1400+
}
1401+
1402+
for (Object leftKey : left.keys()) {
1403+
if (!containsKeyNode.execute(right, leftKey)) {
1404+
return false;
1405+
}
1406+
}
1407+
return true;
1408+
}
1409+
1410+
public static KeysIsSubsetNode create() {
1411+
return KeysIsSubsetNodeGen.create();
1412+
}
1413+
}
1414+
1415+
public static class KeysIsSupersetNode extends Node {
1416+
@Child KeysIsSubsetNode isSubsetNode;
1417+
1418+
public boolean execute(HashingStorage left, HashingStorage right) {
1419+
if (isSubsetNode == null) {
1420+
CompilerDirectives.transferToInterpreterAndInvalidate();
1421+
isSubsetNode = insert(KeysIsSubsetNode.create());
1422+
}
1423+
1424+
return isSubsetNode.execute(right, left);
1425+
}
1426+
1427+
public static KeysIsSupersetNode create() {
1428+
return new KeysIsSupersetNode();
1429+
}
1430+
}
1431+
13891432
public abstract static class DiffNode extends DictStorageBaseNode {
13901433

13911434
public abstract HashingStorage execute(HashingStorage left, HashingStorage right);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/FrozenSetBuiltins.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
import static com.oracle.graal.python.nodes.SpecialMethodNames.__AND__;
2929
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CONTAINS__;
3030
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
31+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
32+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__;
3133
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
3234
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
3335
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LE__;
36+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LT__;
3437
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SUB__;
3538

3639
import java.util.List;
@@ -287,6 +290,115 @@ PBaseSet doIterable(PBaseSet container, HashingStorage dictStorage, Object itera
287290
public static BinaryUnionNode create() {
288291
return BinaryUnionNodeGen.create();
289292
}
293+
}
294+
295+
@Builtin(name = "issubset", fixedNumOfArguments = 2)
296+
@GenerateNodeFactory
297+
abstract static class IsSubsetNode extends PythonBinaryBuiltinNode {
298+
@Specialization
299+
boolean isSubSet(PBaseSet self, PBaseSet other,
300+
@Cached("create()") HashingStorageNodes.KeysIsSubsetNode isSubsetNode) {
301+
return isSubsetNode.execute(self.getDictStorage(), other.getDictStorage());
302+
}
303+
304+
@Specialization
305+
boolean isSubSet(PBaseSet self, String other,
306+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
307+
@Cached("create()") HashingStorageNodes.KeysIsSubsetNode isSubsetNode) {
308+
PSet otherSet = constructSetNode.executeWith(other);
309+
return isSubsetNode.execute(self.getDictStorage(), otherSet.getDictStorage());
310+
}
311+
}
312+
313+
@Builtin(name = "issuperset", fixedNumOfArguments = 2)
314+
@GenerateNodeFactory
315+
abstract static class IsSupersetNode extends PythonBinaryBuiltinNode {
316+
@Specialization
317+
boolean isSuperSet(PBaseSet self, PBaseSet other,
318+
@Cached("create()") HashingStorageNodes.KeysIsSupersetNode isSupersetNode) {
319+
return isSupersetNode.execute(self.getDictStorage(), other.getDictStorage());
320+
}
321+
322+
@Specialization
323+
boolean isSuperSet(PBaseSet self, String other,
324+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
325+
@Cached("create()") HashingStorageNodes.KeysIsSupersetNode isSupersetNode) {
326+
PSet otherSet = constructSetNode.executeWith(other);
327+
return isSupersetNode.execute(self.getDictStorage(), otherSet.getDictStorage());
328+
}
329+
}
330+
331+
@Builtin(name = __LE__, fixedNumOfArguments = 2)
332+
@GenerateNodeFactory
333+
abstract static class LessEqualNode extends IsSubsetNode {
334+
}
335+
336+
@Builtin(name = __GE__, fixedNumOfArguments = 2)
337+
@GenerateNodeFactory
338+
abstract static class GreaterEqualNode extends IsSupersetNode {
339+
}
340+
341+
@Builtin(name = __LT__, fixedNumOfArguments = 2)
342+
@GenerateNodeFactory
343+
abstract static class LessThanNode extends PythonBinaryBuiltinNode {
344+
@Child LessEqualNode lessEqualNode;
345+
346+
private LessEqualNode getLessEqualNode() {
347+
if (lessEqualNode == null) {
348+
CompilerDirectives.transferToInterpreterAndInvalidate();
349+
lessEqualNode = insert(FrozenSetBuiltinsFactory.LessEqualNodeFactory.create());
350+
}
351+
return lessEqualNode;
352+
}
353+
354+
@Specialization
355+
boolean isLessThan(PBaseSet self, PBaseSet other,
356+
@Cached("createBinaryProfile()") ConditionProfile sizeProfile) {
357+
if (sizeProfile.profile(self.size() >= other.size())) {
358+
return false;
359+
}
360+
return (Boolean) getLessEqualNode().execute(self, other);
361+
}
362+
363+
@Specialization
364+
boolean isLessThan(PBaseSet self, String other,
365+
@Cached("createBinaryProfile()") ConditionProfile sizeProfile) {
366+
if (sizeProfile.profile(self.size() >= other.length())) {
367+
return false;
368+
}
369+
return (Boolean) getLessEqualNode().execute(self, other);
370+
}
371+
}
372+
373+
@Builtin(name = __GT__, fixedNumOfArguments = 2)
374+
@GenerateNodeFactory
375+
abstract static class GreaterThanNode extends PythonBinaryBuiltinNode {
376+
@Child GreaterEqualNode greaterEqualNode;
377+
378+
private GreaterEqualNode getGreaterEqualNode() {
379+
if (greaterEqualNode == null) {
380+
CompilerDirectives.transferToInterpreterAndInvalidate();
381+
greaterEqualNode = insert(FrozenSetBuiltinsFactory.GreaterEqualNodeFactory.create());
382+
}
383+
return greaterEqualNode;
384+
}
385+
386+
@Specialization
387+
boolean isGreaterThan(PBaseSet self, PBaseSet other,
388+
@Cached("createBinaryProfile()") ConditionProfile sizeProfile) {
389+
if (sizeProfile.profile(self.size() <= other.size())) {
390+
return false;
391+
}
392+
return (Boolean) getGreaterEqualNode().execute(self, other);
393+
}
290394

395+
@Specialization
396+
boolean isGreaterThan(PBaseSet self, String other,
397+
@Cached("createBinaryProfile()") ConditionProfile sizeProfile) {
398+
if (sizeProfile.profile(self.size() <= other.length())) {
399+
return false;
400+
}
401+
return (Boolean) getGreaterEqualNode().execute(self, other);
402+
}
291403
}
292404
}

0 commit comments

Comments
 (0)