Skip to content
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

Positions based on Enumerate #816

Merged
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
42 changes: 17 additions & 25 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use self::multi_product::*;

use crate::size_hint::{self, SizeHint};
use std::fmt;
use std::iter::{FromIterator, Fuse, FusedIterator};
use std::iter::{Enumerate, FromIterator, Fuse, FusedIterator};
use std::marker::PhantomData;

/// An iterator adaptor that alternates elements from two iterators until both
Expand Down Expand Up @@ -1039,16 +1039,15 @@ where
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Positions<I, F> {
iter: I,
iter: Enumerate<I>,
f: F,
count: usize,
}

impl<I, F> fmt::Debug for Positions<I, F>
where
I: fmt::Debug,
{
debug_fmt_fields!(Positions, iter, count);
debug_fmt_fields!(Positions, iter);
}

/// Create a new `Positions` iterator.
Expand All @@ -1057,7 +1056,8 @@ where
I: Iterator,
F: FnMut(I::Item) -> bool,
{
Positions { iter, f, count: 0 }
let iter = iter.enumerate();
Positions { iter, f }
}

impl<I, F> Iterator for Positions<I, F>
Expand All @@ -1068,14 +1068,10 @@ where
type Item = usize;

fn next(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.next() {
let i = self.count;
self.count = i + 1;
if (self.f)(v) {
return Some(i);
}
}
None
let f = &mut self.f;
// TODO: once MSRV >= 1.62, use `then_some`.
self.iter
.find_map(|(count, val)| if f(val) { Some(count) } else { None })
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand All @@ -1086,13 +1082,11 @@ where
where
G: FnMut(B, Self::Item) -> B,
{
let mut count = self.count;
let mut f = self.f;
self.iter.fold(init, |mut acc, val| {
self.iter.fold(init, |mut acc, (count, val)| {
if f(val) {
acc = func(acc, count);
}
count += 1;
acc
})
}
Expand All @@ -1104,22 +1098,20 @@ where
F: FnMut(I::Item) -> bool,
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.next_back() {
if (self.f)(v) {
return Some(self.count + self.iter.len());
}
}
None
let f = &mut self.f;
// TODO: once MSRV >= 1.62, use `then_some`.
self.iter
.by_ref()
.rev()
.find_map(|(count, val)| if f(val) { Some(count) } else { None })
}

fn rfold<B, G>(self, init: B, mut func: G) -> B
where
G: FnMut(B, Self::Item) -> B,
{
let mut count = self.count + self.iter.len();
let mut f = self.f;
self.iter.rfold(init, |mut acc, val| {
count -= 1;
self.iter.rfold(init, |mut acc, (count, val)| {
if f(val) {
acc = func(acc, count);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ pub trait Itertools: Iterator {
/// Return an iterator adaptor that yields the indices of all elements
/// satisfying a predicate, counted from the start of the iterator.
///
/// Equivalent to `iter.enumerate().filter(|(_, v)| predicate(v)).map(|(i, _)| i)`.
/// Equivalent to `iter.enumerate().filter(|(_, v)| predicate(*v)).map(|(i, _)| i)`.
///
/// ```
/// use itertools::Itertools;
Expand Down
6 changes: 6 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,12 @@ quickcheck! {
let b = &b[..len];
itertools::equal(zip_eq(a, b), zip(a, b))
}
fn equal_positions(a: Vec<i32>) -> bool {
let with_pos = a.iter().positions(|v| v % 2 == 0);
let without = a.iter().enumerate().filter(|(_, v)| *v % 2 == 0).map(|(i, _)| i);
itertools::equal(with_pos.clone(), without.clone())
&& itertools::equal(with_pos.rev(), without.rev())
}
fn size_zip_longest(a: Iter<i16, Exact>, b: Iter<i16, Exact>) -> bool {
let filt = a.clone().dedup();
let filt2 = b.clone().dedup();
Expand Down