8000 Add __length_hint__ support for iterators by coolreader18 · Pull Request #1666 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Add __length_hint__ support for iterators #1666

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 5 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add __length_hint__ to a bunch of iterators
  • Loading branch information
coolreader18 committed Jan 10, 2020
commit 098393d4d6e4db91d31868800ef7431535825971
14 changes: 14 additions & 0 deletions vm/src/obj/objiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ impl PySequenceIterator {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}

#[pymethod(name = "__length_hint__")]
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<isize> {
let pos = self.position.get();
let hint = if self.reversed {
pos + 1
} else {
let len = objsequence::opt_len(&self.obj, vm).unwrap_or_else(|| {
Err(vm.new_type_error("sequence has no __len__ method".to_string()))
})?;
len as isize - pos
};
Ok(hint)
}
}

pub fn seq_iter_method(obj: PyObjectRef, _vm: &VirtualMachine) -> PySequenceIterator {
Expand Down
10 changes: 10 additions & 0 deletions vm/src/obj/objlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,11 @@ impl PyListIterator {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}

#[pymethod(name = "__length_hint__")]
fn length_hint(&self, _vm: &VirtualMachine) -> usize {
self.list.elements.borrow().len() - self.position.get()
}
}

#[pyclass]
Expand Down Expand Up @@ -906,6 +911,11 @@ impl PyListReverseIterator {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}

#[pymethod(name = "__length_hint__")]
fn length_hint(&self, _vm: &VirtualMachine) -> usize {
self.position.get()
}
}

pub fn init(context: &PyContext) {
Expand Down
9 changes: 9 additions & 0 deletions vm/src/obj/objmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ impl PyMap {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}

#[pymethod(name = "__length_hint__")]
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<usize> {
self.iterators.iter().try_fold(0, |prev, cur| {
let cur = objiter::length_hint(vm, cur.clone())?.unwrap_or(0);
let max = std::cmp::max(prev, cur);
Ok(max)
})
}
}

pub fn init(context: &PyContext) {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/obj/objrange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ impl PyRange {
}

#[pymethod(name = "__len__")]
fn len(&self, _vm: &VirtualMachine) -> PyInt {
PyInt::new(self.length())
fn len(&self, _vm: &VirtualMachine) -> BigInt {
self.length()
}

#[pymethod(name = "__repr__")]
Expand Down
34 changes: 17 additions & 17 deletions vm/src/stdlib/itertools.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::cell::{Cell, RefCell};
use std::cmp::Ordering;
use std::iter;
use std::ops::{AddAssign, SubAssign};
use std::rc::Rc;

use num_bigint::BigInt;
use num_traits::sign::Signed;
use num_traits::ToPrimitive;
use num_traits::{One, Signed, ToPrimitive, Zero};

use crate::function::{Args, OptionalArg, OptionalOption, PyFuncArgs};
use crate::obj::objbool;
Expand Down Expand Up @@ -166,11 +163,11 @@ impl PyItertoolsCount {
) -> PyResult<PyRef<Self>> {
let start = match start.into_option() {
Some(int) => int.as_bigint().clone(),
None => BigInt::from(0),
None => BigInt::zero(),
};
let step = match step.into_option() {
Some(int) => int.as_bigint().clone(),
None => BigInt::from(1),
None => BigInt::one(),
};

PyItertoolsCount {
Expand All @@ -183,7 +180,7 @@ impl PyItertoolsCount {
#[pymethod(name = "__next__")]
fn next(&self, _vm: &VirtualMachine) -> PyResult<PyInt> {
let result = self.cur.borrow().clone();
AddAssign::add_assign(&mut self.cur.borrow_mut() as &mut BigInt, &self.step);
*self.cur.borrow_mut() += &self.step;
Ok(PyInt::new(result))
}

Expand Down Expand Up @@ -296,16 +293,11 @@ impl PyItertoolsRepeat {

#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
if self.times.is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

match self.times.as_ref().unwrap().borrow().cmp(&BigInt::from(0)) {
Ordering::Less | Ordering::Equal => return Err(new_stop_iteration(vm)),
_ => (),
};

SubAssign::sub_assign(
&mut self.times.as_ref().unwrap().borrow_mut() as &mut BigInt,
&BigInt::from(1),
);
if let Some(ref times) = self.times {
if *times.borrow() <= BigInt::zero() {
return Err(new_stop_iteration(vm));
}
*times.borrow_mut() -= 1;
}

Ok(self.object.clone())
Expand All @@ -315,6 +307,14 @@ impl PyItertoolsRepeat {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}

#[pymethod(name = "__length_hint__")]
fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef {
match self.times {
Some(ref times) => vm.new_int(times.borrow().clone()),
None => vm.new_int(0),
}
}
}

#[pyclass(name = "starmap")]
Expand Down
0