Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #875 from patiences/comparisons-opt
Browse files Browse the repository at this point in the history
Comparisons optimization
  • Loading branch information
freakboy3742 authored Jul 28, 2018
2 parents 47335f6 + 812dbcb commit edbfc47
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 152 deletions.
10 changes: 3 additions & 7 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,7 @@ public static org.python.Object max(org.python.types.Tuple args, org.python.Obje
}

private static boolean compareKeys(org.python.Object first, org.python.Object second) {
return org.python.types.Object.__cmp__(
first,
second,
org.python.types.Object.CMP_OP.GT
).toBoolean();
return org.python.types.Object.__gt__(first, second).toBoolean();
}

@org.python.Method(
Expand Down Expand Up @@ -1584,11 +1580,11 @@ public __SortedObjectComparator(boolean reverse) {
public int compare(org.python.Object o1, org.python.Object o2) {
o1 = applyKey(o1, key);
o2 = applyKey(o2, key);
org.python.Object result = org.python.types.Object.__cmp_bool__(o1, o2, org.python.types.Object.CMP_OP.LT);
org.python.Object result = org.python.types.Object.__lt__(o1, o2);
if (result.toBoolean()) {
return reverse ? 1 : -1;
}
result = org.python.types.Object.__cmp_bool__(o2, o1, org.python.types.Object.CMP_OP.LT);
result = org.python.types.Object.__lt__(o2, o1);
if (result.toBoolean()) {
return reverse ? -1 : 1;
}
Expand Down
44 changes: 20 additions & 24 deletions python/common/org/python/types/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,16 @@ public org.python.Object __lt__(org.python.Object other) {
// check how many items are identical on the lists
int i = 0;
for (i = 0; i < count; i++) {
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_bool__(
this.value.get(i), otherList.value.get(i), org.python.types.Object.CMP_OP.EQ);
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_eq__(
this.value.get(i), otherList.value.get(i));
if (!result.value) {
break;
}
}

// not all items were identical, result is that of the first non-identical item
if (i < count) {
return org.python.types.Object.__cmp_bool__(this.value.get(i), otherList.value.get(i),
org.python.types.Object.CMP_OP.LT);
return org.python.types.Object.__lt__(this.value.get(i), otherList.value.get(i));
}

// all items were identical, break tie by size
Expand All @@ -232,17 +231,16 @@ public org.python.Object __le__(org.python.Object other) {
// check how many items are identical on the lists
int i = 0;
for (i = 0; i < count; i++) {
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_bool__(
this.value.get(i), otherList.value.get(i), org.python.types.Object.CMP_OP.EQ);
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_eq__(
this.value.get(i), otherList.value.get(i));
if (!result.value) {
break;
}
}

// not all items were identical, result is that of the first non-identical item
if (i < count) {
return org.python.types.Object.__cmp_bool__(this.value.get(i), otherList.value.get(i),
org.python.types.Object.CMP_OP.LE);
return org.python.types.Object.__le__(this.value.get(i), otherList.value.get(i));
}

// all items were identical, break tie by size
Expand Down Expand Up @@ -278,17 +276,16 @@ public org.python.Object __gt__(org.python.Object other) {
// check how many items are identical on the lists
int i = 0;
for (i = 0; i < count; i++) {
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_bool__(
this.value.get(i), otherList.value.get(i), org.python.types.Object.CMP_OP.EQ);
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_eq__(
this.value.get(i), otherList.value.get(i));
if (!result.value) {
break;
}
}

// not all items were identical, result is that of the first non-identical item
if (i < count) {
return org.python.types.Object.__cmp_bool__(this.value.get(i), otherList.value.get(i),
org.python.types.Object.CMP_OP.GT);
return org.python.types.Object.__gt__(this.value.get(i), otherList.value.get(i));
}

// all items were identical, break tie by size
Expand All @@ -312,17 +309,16 @@ public org.python.Object __ge__(org.python.Object other) {
// check how many items are identical on the lists
int i = 0;
for (i = 0; i < count; i++) {
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_bool__(
this.value.get(i), otherList.value.get(i), org.python.types.Object.CMP_OP.EQ);
org.python.types.Bool result = (org.python.types.Bool) org.python.types.Object.__cmp_eq__(
this.value.get(i), otherList.value.get(i));
if (!result.value) {
break;
}
}

// not all items were identical, result is that of the first non-identical item
if (i < count) {
return org.python.types.Object.__cmp_bool__(this.value.get(i), otherList.value.get(i),
org.python.types.Object.CMP_OP.GE);
return org.python.types.Object.__ge__(this.value.get(i), otherList.value.get(i));
}

// all items were identical, break tie by size
Expand Down Expand Up @@ -553,8 +549,8 @@ public org.python.Object __reversed__() {
public org.python.Object __contains__(org.python.Object item) {
boolean found = false;
for (int i = 0; i < this.value.size(); i++) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_bool__(
item, this.value.get(i), org.python.types.Object.CMP_OP.EQ)).value) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_eq__(
item, this.value.get(i))).value) {
found = true;
break;
}
Expand Down Expand Up @@ -664,8 +660,8 @@ public org.python.Object copy() {
public org.python.Object count(org.python.Object other) {
int count = 0;
for (int i = 0; i < this.value.size(); i++) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_bool__(
other, this.value.get(i), org.python.types.Object.CMP_OP.EQ)).value) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_eq__(
other, this.value.get(i))).value) {
count++;
}
}
Expand Down Expand Up @@ -763,8 +759,8 @@ public org.python.Object index(org.python.Object item, org.python.Object start,
}

for (int i = iStart; i < Math.min(iEnd, this.value.size()); i++) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_bool__(
item, this.value.get(i), org.python.types.Object.CMP_OP.EQ)).value) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_eq__(
item, this.value.get(i))).value) {
return org.python.types.Int.getInt(i);
}
}
Expand Down Expand Up @@ -818,8 +814,8 @@ public org.python.Object pop(org.python.Object item) {
)
public org.python.Object remove(org.python.Object item) {
for (int i = 0; i < this.value.size(); i++) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_bool__(
item, this.value.get(i), org.python.types.Object.CMP_OP.EQ)).value) {
if (((org.python.types.Bool) org.python.types.Object.__cmp_eq__(
item, this.value.get(i))).value) {
this.value.remove(i);
return org.python.types.NoneType.NONE;
}
Expand Down
Loading

0 comments on commit edbfc47

Please sign in to comment.