Skip to content

Commit 1e39c25

Browse files
committed
add basic support for < > <= >= set and frozenset ops
1 parent 24aec04 commit 1e39c25

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

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

Lines changed: 77 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;
@@ -324,4 +327,78 @@ boolean isSuperSet(PBaseSet self, String other,
324327
return isSupersetNode.execute(self.getDictStorage(), otherSet.getDictStorage());
325328
}
326329
}
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+
}
394+
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+
}
403+
}
327404
}

0 commit comments

Comments
 (0)