Skip to content

Refactor sequence item comparison #1500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 22 additions & 66 deletions vm/src/obj/objsequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::vm::VirtualMachine;
use num_bigint::{BigInt, ToBigInt};
use num_traits::{One, Signed, ToPrimitive, Zero};

use super::objbool;
use super::objint::{PyInt, PyIntRef};
use super::objlist::PyList;
use super::objslice::{PySlice, PySliceRef};
Expand Down Expand Up @@ -285,15 +284,14 @@ pub fn seq_equal(
vm: &VirtualMachine,
zelf: &dyn SimpleSeq,
other: &dyn SimpleSeq,
) -> Result<bool, PyObjectRef> {
) -> PyResult<bool> {
if zelf.len() == other.len() {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if !a.is(b) {
let eq = vm._eq(a.clone(), b.clone())?;
let value = objbool::boolval(vm, eq)?;
if !value {
return Ok(false);
}
if a.is(b) {
continue;
}
if !vm.bool_eq(a.clone(), b.clone())? {
return Ok(false);
}
}
Ok(true)
Expand All @@ -302,84 +300,42 @@ pub fn seq_equal(
}
}

pub fn seq_lt(
vm: &VirtualMachine,
zelf: &dyn SimpleSeq,
other: &dyn SimpleSeq,
) -> Result<bool, PyObjectRef> {
pub fn seq_lt(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if vm.bool_lt(a.clone(), b.clone())? {
return Ok(true);
} else if !vm.bool_eq(a.clone(), b.clone())? {
return Ok(false);
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
return Ok(v);
}
}

if zelf.len() == other.len() {
Ok(false)
} else {
Ok(zelf.len() < other.len())
}
Ok(zelf.len() < other.len())
}

pub fn seq_gt(
vm: &VirtualMachine,
zelf: &dyn SimpleSeq,
other: &dyn SimpleSeq,
) -> Result<bool, PyObjectRef> {
pub fn seq_gt(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if vm.bool_gt(a.clone(), b.clone())? {
return Ok(true);
} else if !vm.bool_eq(a.clone(), b.clone())? {
return Ok(false);
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
return Ok(v);
}
}

if zelf.len() == other.len() {
Ok(false)
} else {
Ok(zelf.len() > other.len())
}
Ok(zelf.len() > other.len())
}

pub fn seq_ge(
vm: &VirtualMachine,
zelf: &dyn SimpleSeq,
other: &dyn SimpleSeq,
) -> Result<bool, PyObjectRef> {
pub fn seq_ge(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if vm.bool_gt(a.clone(), b.clone())? {
return Ok(true);
} else if !vm.bool_eq(a.clone(), b.clone())? {
return Ok(false);
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
return Ok(v);
}
}

if zelf.len() == other.len() {
Ok(true)
} else {
Ok(zelf.len() > other.len())
}
Ok(zelf.len() >= other.len())
}

pub fn seq_le(
vm: &VirtualMachine,
zelf: &dyn SimpleSeq,
other: &dyn SimpleSeq,
) -> Result<bool, PyObjectRef> {
pub fn seq_le(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if vm.bool_lt(a.clone(), b.clone())? {
return Ok(true);
} else if !vm.bool_eq(a.clone(), b.clone())? {
return Ok(false);
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
return Ok(v);
}
}

if zelf.len() == other.len() {
Ok(true)
} else {
Ok(zelf.len() < other.len())
}
Ok(zelf.len() <= other.len())
}

pub struct SeqMul<'a> {
Expand Down
42 changes: 14 additions & 28 deletions vm/src/obj/objslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,44 +129,30 @@ impl PySlice {
Ok(true)
}

#[inline]
fn inner_lte(&self, other: &PySlice, eq: bool, vm: &VirtualMachine) -> PyResult<bool> {
if vm.bool_lt(self.start(vm), other.start(vm))? {
return Ok(true);
} else if !vm.bool_eq(self.start(vm), other.start(vm))? {
return Ok(false);
if let Some(v) = vm.bool_seq_lt(self.start(vm), other.start(vm))? {
return Ok(v);
}

if vm.bool_lt(self.stop(vm), other.stop(vm))? {
return Ok(true);
} else if !vm.bool_eq(self.stop(vm), other.stop(vm))? {
return Ok(false);
if let Some(v) = vm.bool_seq_lt(self.stop(vm), other.stop(vm))? {
return Ok(v);
}

if vm.bool_lt(self.step(vm), other.step(vm))? {
return Ok(true);
} else if !vm.bool_eq(self.step(vm), other.step(vm))? {
return Ok(false);
if let Some(v) = vm.bool_seq_lt(self.step(vm), other.step(vm))? {
return Ok(v);
}
Ok(eq)
}

#[inline]
fn inner_gte(&self, other: &PySlice, eq: bool, vm: &VirtualMachine) -> PyResult<bool> {
if vm.bool_gt(self.start(vm), other.start(vm))? {
return Ok(true);
} else if !vm.bool_eq(self.start(vm), other.start(vm))? {
return Ok(false);
if let Some(v) = vm.bool_seq_gt(self.start(vm), other.start(vm))? {
return Ok(v);
}

if vm.bool_gt(self.stop(vm), other.stop(vm))? {
return Ok(true);
} else if !vm.bool_eq(self.stop(vm), other.stop(vm))? {
return Ok(false);
if let Some(v) = vm.bool_seq_gt(self.stop(vm), other.stop(vm))? {
return Ok(v);
}

if vm.bool_gt(self.step(vm), other.step(vm))? {
return Ok(true);
} else if !vm.bool_eq(self.step(vm), other.step(vm))? {
return Ok(false);
if let Some(v) = vm.bool_seq_gt(self.step(vm), other.step(vm))? {
return Ok(v);
}
Ok(eq)
}
Expand Down
22 changes: 16 additions & 6 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,15 +1272,25 @@ impl VirtualMachine {
Ok(value)
}

pub fn bool_lt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<bool> {
let lt = self._lt(a.clone(), b.clone())?;
let value = objbool::boolval(self, lt)?;
pub fn bool_seq_lt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<Option<bool>> {
let value = if objbool::boolval(self, self._lt(a.clone(), b.clone())?)? {
Some(true)
} else if !objbool::boolval(self, self._eq(a.clone(), b.clone())?)? {
Some(false)
} else {
None
};
Ok(value)
}

pub fn bool_gt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<bool> {
let gt = self._gt(a.clone(), b.clone())?;
let value = objbool::boolval(self, gt)?;
pub fn bool_seq_gt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<Option<bool>> {
let value = if objbool::boolval(self, self._gt(a.clone(), b.clone())?)? {
Some(true)
} else if !objbool::boolval(self, self._eq(a.clone(), b.clone())?)? {
Some(false)
} else {
None
};
Ok(value)
}
}
Expand Down