Skip to content

Commit 2579feb

Browse files
committed
Refactoring GT, LT, GE and LE list nodes to reduce code duplication.
1 parent d7b9db6 commit 2579feb

File tree

1 file changed

+26
-65
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list

1 file changed

+26
-65
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,114 +1549,75 @@ PNotImplemented contains(@SuppressWarnings("unused") Object self, @SuppressWarni
15491549

15501550
@Builtin(name = __LT__, fixedNumOfArguments = 2)
15511551
@GenerateNodeFactory
1552-
abstract static class LtNode extends PythonBinaryBuiltinNode {
1552+
abstract static class LtNode extends ListComparisonNode {
15531553

15541554
@Specialization
15551555
boolean contains(PList self, PList other,
15561556
@Cached("create(__EQ__, __EQ__, __EQ__)") BinaryComparisonNode eqNode,
15571557
@Cached("create(__LT__, __GT__, __LT__)") BinaryComparisonNode ltNode) {
1558-
int len = self.len();
1559-
int len2 = other.len();
1560-
int min = Math.min(len, len2);
1561-
for (int i = 0; i < min; i++) {
1562-
Object left = self.getItem(i);
1563-
Object right = other.getItem(i);
1564-
if (!eqNode.executeBool(left, right)) {
1565-
return ltNode.executeBool(left, right);
1566-
}
1567-
}
1568-
return len < len2;
1569-
}
1570-
1571-
@Fallback
1572-
PNotImplemented contains(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused") Object other) {
1573-
return PNotImplemented.NOT_IMPLEMENTED;
1558+
return doComparison(self, other, eqNode, ltNode);
15741559
}
15751560
}
15761561

15771562
@Builtin(name = __GT__, fixedNumOfArguments = 2)
15781563
@GenerateNodeFactory
1579-
abstract static class GtNode extends PythonBinaryBuiltinNode {
1564+
abstract static class GtNode extends ListComparisonNode {
15801565

15811566
@Specialization
15821567
boolean contains(PList self, PList other,
15831568
@Cached("create(__EQ__, __EQ__, __EQ__)") BinaryComparisonNode eqNode,
15841569
@Cached("create(__GT__, __LT__, __GT__)") BinaryComparisonNode gtNode) {
1585-
int len = self.len();
1586-
int len2 = other.len();
1587-
int min = Math.min(len, len2);
1588-
for (int i = 0; i < min; i++) {
1589-
Object left = self.getItem(i);
1590-
Object right = other.getItem(i);
1591-
if (!eqNode.executeBool(left, right)) {
1592-
return gtNode.executeBool(left, right);
1593-
}
1594-
}
1595-
return len > len2;
1596-
}
1597-
1598-
@Fallback
1599-
PNotImplemented contains(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused") Object other) {
1600-
return PNotImplemented.NOT_IMPLEMENTED;
1570+
return doComparison(self, other, eqNode, gtNode);
16011571
}
16021572
}
16031573

16041574
@Builtin(name = __GE__, fixedNumOfArguments = 2)
16051575
@GenerateNodeFactory
1606-
abstract static class GeNode extends PythonBinaryBuiltinNode {
1576+
abstract static class GeNode extends ListComparisonNode {
16071577

16081578
@Specialization
16091579
boolean doPTuple(PList left, PList right,
16101580
@Cached("create(__EQ__, __EQ__, __EQ__)") BinaryComparisonNode eqNode,
16111581
@Cached("create(__GE__, __LE__, __GE__)") BinaryComparisonNode geNode) {
1612-
int llen = left.len();
1613-
int rlen = right.len();
1614-
int min = Math.min(llen, rlen);
1615-
for (int i = 0; i < min; i++) {
1616-
Object oleft = left.getItem(i);
1617-
Object oright = right.getItem(i);
1618-
if (!eqNode.executeBool(oleft, oright)) {
1619-
return geNode.executeBool(oleft, oright);
1620-
}
1621-
}
1622-
return llen >= rlen;
1582+
return doComparison(left, right, eqNode, geNode);
16231583
}
1624-
1625-
@Fallback
1626-
@SuppressWarnings("unused")
1627-
PNotImplemented doOther(Object left, Object right) {
1628-
return PNotImplemented.NOT_IMPLEMENTED;
1629-
}
1630-
16311584
}
16321585

16331586
@Builtin(name = __LE__, fixedNumOfArguments = 2)
16341587
@GenerateNodeFactory
1635-
abstract static class LeNode extends PythonBinaryBuiltinNode {
1588+
abstract static class LeNode extends ListComparisonNode {
16361589

16371590
@Specialization
1638-
boolean doPTuple(PList left, PList right,
1591+
boolean doPList(PList left, PList right,
16391592
@Cached("create(__EQ__, __EQ__, __EQ__)") BinaryComparisonNode eqNode,
1640-
@Cached("create(__LE__, __GE__, __LE__)") BinaryComparisonNode geNode) {
1641-
int llen = left.len();
1642-
int rlen = right.len();
1643-
int min = Math.min(llen, rlen);
1593+
@Cached("create(__LE__, __GE__, __LE__)") BinaryComparisonNode leNode) {
1594+
return doComparison(left, right, eqNode, leNode);
1595+
}
1596+
}
1597+
1598+
abstract static class ListComparisonNode extends PythonBinaryBuiltinNode {
1599+
1600+
static boolean doComparison(PList self, PList other,
1601+
BinaryComparisonNode eqNode,
1602+
BinaryComparisonNode compNode) {
1603+
int len = self.len();
1604+
int len2 = other.len();
1605+
int min = Math.min(len, len2);
16441606
for (int i = 0; i < min; i++) {
1645-
Object oleft = left.getItem(i);
1646-
Object oright = right.getItem(i);
1647-
if (!eqNode.executeBool(oleft, oright)) {
1648-
return geNode.executeBool(oleft, oright);
1607+
Object left = self.getItem(i);
1608+
Object right = other.getItem(i);
1609+
if (!eqNode.executeBool(left, right)) {
1610+
return compNode.executeBool(left, right);
16491611
}
16501612
}
1651-
return llen <= rlen;
1613+
return compNode.executeBool(len, len2);
16521614
}
16531615

16541616
@Fallback
16551617
@SuppressWarnings("unused")
16561618
PNotImplemented doOther(Object left, Object right) {
16571619
return PNotImplemented.NOT_IMPLEMENTED;
16581620
}
1659-
16601621
}
16611622

16621623
@Builtin(name = __CONTAINS__, fixedNumOfArguments = 2)

0 commit comments

Comments
 (0)