Skip to content

Commit 53295bc

Browse files
authored
Rollup merge of rust-lang#56802 - clarcharr:nth_back, r=alexcrichton
Add DoubleEndedIterator::nth_back As suggested by rust-lang#54054. This doesn't fix that issue, as this doesn't add enough implementations to optimise that specific use case, but it adds the method and a few (relatively) trivial overrides to work as an initial implementation. It's probably going to be a lot of work adding `nth_back` implementations everywhere, and I don't have the time to include it all in this commit. But, it's a start. :)
2 parents 6d34ec1 + fb18dda commit 53295bc

File tree

4 files changed

+107
-6
lines changed

4 files changed

+107
-6
lines changed

src/libcore/iter/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
429429
#[inline]
430430
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
431431

432+
#[inline]
433+
fn nth(&mut self, n: usize) -> Option<<I as Iterator>::Item> { self.iter.nth_back(n) }
434+
432435
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where
433436
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
434437
{
@@ -461,6 +464,9 @@ impl<I> DoubleEndedIterator for Rev<I> where I: DoubleEndedIterator {
461464
#[inline]
462465
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }
463466

467+
#[inline]
468+
fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> { self.iter.nth(n) }
469+
464470
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where
465471
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
466472
{

src/libcore/iter/traits.rs

+73-6
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,62 @@ pub trait DoubleEndedIterator: Iterator {
427427
#[stable(feature = "rust1", since = "1.0.0")]
428428
fn next_back(&mut self) -> Option<Self::Item>;
429429

430+
/// Returns the `n`th element from the end of the iterator.
431+
///
432+
/// This is essentially the reversed version of [`nth`]. Although like most indexing
433+
/// operations, the count starts from zero, so `nth_back(0)` returns the first value fro
434+
/// the end, `nth_back(1)` the second, and so on.
435+
///
436+
/// Note that all elements between the end and the returned element will be
437+
/// consumed, including the returned element. This also means that calling
438+
/// `nth_back(0)` multiple times on the same iterator will return different
439+
/// elements.
440+
///
441+
/// `nth_back()` will return [`None`] if `n` is greater than or equal to the length of the
442+
/// iterator.
443+
///
444+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
445+
/// [`nth`]: ../../std/iter/trait.Iterator.html#method.nth
446+
///
447+
/// # Examples
448+
///
449+
/// Basic usage:
450+
///
451+
/// ```
452+
/// #![feature(iter_nth_back)]
453+
/// let a = [1, 2, 3];
454+
/// assert_eq!(a.iter().nth_back(2), Some(&1));
455+
/// ```
456+
///
457+
/// Calling `nth_back()` multiple times doesn't rewind the iterator:
458+
///
459+
/// ```
460+
/// #![feature(iter_nth_back)]
461+
/// let a = [1, 2, 3];
462+
///
463+
/// let mut iter = a.iter();
464+
///
465+
/// assert_eq!(iter.nth_back(1), Some(&2));
466+
/// assert_eq!(iter.nth_back(1), None);
467+
/// ```
468+
///
469+
/// Returning `None` if there are less than `n + 1` elements:
470+
///
471+
/// ```
472+
/// #![feature(iter_nth_back)]
473+
/// let a = [1, 2, 3];
474+
/// assert_eq!(a.iter().nth_back(10), None);
475+
/// ```
476+
#[inline]
477+
#[unstable(feature = "iter_nth_back", issue = "56995")]
478+
fn nth_back(&mut self, mut n: usize) -> Option<Self::Item> {
479+
for x in self.rev() {
480+
if n == 0 { return Some(x) }
481+
n -= 1;
482+
}
483+
None
484+
}
485+
430486
/// This is the reverse version of [`try_fold()`]: it takes elements
431487
/// starting from the back of the iterator.
432488
///
@@ -461,8 +517,11 @@ pub trait DoubleEndedIterator: Iterator {
461517
/// ```
462518
#[inline]
463519
#[stable(feature = "iterator_try_fold", since = "1.27.0")]
464-
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
465-
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
520+
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
521+
where
522+
Self: Sized,
523+
F: FnMut(B, Self::Item) -> R,
524+
R: Try<Ok=B>
466525
{
467526
let mut accum = init;
468527
while let Some(x) = self.next_back() {
@@ -524,8 +583,10 @@ pub trait DoubleEndedIterator: Iterator {
524583
/// ```
525584
#[inline]
526585
#[stable(feature = "iter_rfold", since = "1.27.0")]
527-
fn rfold<B, F>(mut self, accum: B, mut f: F) -> B where
528-
Self: Sized, F: FnMut(B, Self::Item) -> B,
586+
fn rfold<B, F>(mut self, accum: B, mut f: F) -> B
587+
where
588+
Self: Sized,
589+
F: FnMut(B, Self::Item) -> B,
529590
{
530591
self.try_rfold(accum, move |acc, x| Ok::<B, !>(f(acc, x))).unwrap()
531592
}
@@ -574,7 +635,8 @@ pub trait DoubleEndedIterator: Iterator {
574635
/// ```
575636
#[inline]
576637
#[stable(feature = "iter_rfind", since = "1.27.0")]
577-
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
638+
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
639+
where
578640
Self: Sized,
579641
P: FnMut(&Self::Item) -> bool
580642
{
@@ -587,7 +649,12 @@ pub trait DoubleEndedIterator: Iterator {
587649

588650
#[stable(feature = "rust1", since = "1.0.0")]
589651
impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
590-
fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
652+
fn next_back(&mut self) -> Option<I::Item> {
653+
(**self).next_back()
654+
}
655+
fn nth_back(&mut self, n: usize) -> Option<I::Item> {
656+
(**self).nth_back(n)
657+
}
591658
}
592659

593660
/// An iterator that knows its exact length.

src/libcore/tests/iter.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,33 @@ fn test_iterator_nth() {
10161016
assert_eq!(v.iter().nth(v.len()), None);
10171017
}
10181018

1019+
#[test]
1020+
fn test_iterator_nth_back() {
1021+
let v: &[_] = &[0, 1, 2, 3, 4];
1022+
for i in 0..v.len() {
1023+
assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - 1 - i]);
1024+
}
1025+
assert_eq!(v.iter().nth_back(v.len()), None);
1026+
}
1027+
1028+
#[test]
1029+
fn test_iterator_rev_nth_back() {
1030+
let v: &[_] = &[0, 1, 2, 3, 4];
1031+
for i in 0..v.len() {
1032+
assert_eq!(v.iter().rev().nth_back(i).unwrap(), &v[i]);
1033+
}
1034+
assert_eq!(v.iter().rev().nth_back(v.len()), None);
1035+
}
1036+
1037+
#[test]
1038+
fn test_iterator_rev_nth() {
1039+
let v: &[_] = &[0, 1, 2, 3, 4];
1040+
for i in 0..v.len() {
1041+
assert_eq!(v.iter().rev().nth(i).unwrap(), &v[v.len() - 1 - i]);
1042+
}
1043+
assert_eq!(v.iter().rev().nth(v.len()), None);
1044+
}
1045+
10191046
#[test]
10201047
fn test_iterator_last() {
10211048
let v: &[_] = &[0, 1, 2, 3, 4];

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(flt2dec)]
2020
#![feature(fmt_internals)]
2121
#![feature(hashmap_internals)]
22+
#![feature(iter_nth_back)]
2223
#![feature(iter_unfold)]
2324
#![feature(pattern)]
2425
#![feature(range_is_empty)]

0 commit comments

Comments
 (0)