Skip to content

Commit 24aec04

Browse files
committed
add basic support for issubset issupperset set/frozenset builtins
1 parent c6c7421 commit 24aec04

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,41 @@ PBaseSet doIterable(PBaseSet container, HashingStorage dictStorage, Object itera
287287
public static BinaryUnionNode create() {
288288
return BinaryUnionNodeGen.create();
289289
}
290+
}
291+
292+
@Builtin(name = "issubset", fixedNumOfArguments = 2)
293+
@GenerateNodeFactory
294+
abstract static class IsSubsetNode extends PythonBinaryBuiltinNode {
295+
@Specialization
296+
boolean isSubSet(PBaseSet self, PBaseSet other,
297+
@Cached("create()") HashingStorageNodes.KeysIsSubsetNode isSubsetNode) {
298+
return isSubsetNode.execute(self.getDictStorage(), other.getDictStorage());
299+
}
290300

301+
@Specialization
302+
boolean isSubSet(PBaseSet self, String other,
303+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
304+
@Cached("create()") HashingStorageNodes.KeysIsSubsetNode isSubsetNode) {
305+
PSet otherSet = constructSetNode.executeWith(other);
306+
return isSubsetNode.execute(self.getDictStorage(), otherSet.getDictStorage());
307+
}
308+
}
309+
310+
@Builtin(name = "issuperset", fixedNumOfArguments = 2)
311+
@GenerateNodeFactory
312+
abstract static class IsSupersetNode extends PythonBinaryBuiltinNode {
313+
@Specialization
314+
boolean isSuperSet(PBaseSet self, PBaseSet other,
315+
@Cached("create()") HashingStorageNodes.KeysIsSupersetNode isSupersetNode) {
316+
return isSupersetNode.execute(self.getDictStorage(), other.getDictStorage());
317+
}
318+
319+
@Specialization
320+
boolean isSuperSet(PBaseSet self, String other,
321+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
322+
@Cached("create()") HashingStorageNodes.KeysIsSupersetNode isSupersetNode) {
323+
PSet otherSet = constructSetNode.executeWith(other);
324+
return isSupersetNode.execute(self.getDictStorage(), otherSet.getDictStorage());
325+
}
291326
}
292327
}

0 commit comments

Comments
 (0)