Rollup merge of #60555 - timvermeulen:rchunks_nth_back, r=scottmcm
Implement nth_back for RChunks(Exact)(Mut) Part of #54054. These implementations may not be optimal because of the use of `self.len()`, but it's quite cheap and simplifies the code a lot. There's quite some duplication going on here, I wouldn't mind cleaning this up later. A good next step would probably be to add private `split_off_up_to`/`split_off_from` helper methods for slices since their behavior is commonly useful throughout the `Chunks` types. r? @scottmcm
This commit is contained in:
commit
23b9b8320b
2 changed files with 124 additions and 0 deletions
|
@ -4649,6 +4649,23 @@ impl<'a, T> DoubleEndedIterator for RChunks<'a, T> {
|
||||||
Some(fst)
|
Some(fst)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||||
|
let len = self.len();
|
||||||
|
if n >= len {
|
||||||
|
self.v = &[];
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
// can't underflow because `n < len`
|
||||||
|
let offset_from_end = (len - 1 - n) * self.chunk_size;
|
||||||
|
let end = self.v.len() - offset_from_end;
|
||||||
|
let start = end.saturating_sub(self.chunk_size);
|
||||||
|
let nth_back = &self.v[start..end];
|
||||||
|
self.v = &self.v[end..];
|
||||||
|
Some(nth_back)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rchunks", since = "1.31.0")]
|
#[stable(feature = "rchunks", since = "1.31.0")]
|
||||||
|
@ -4774,6 +4791,24 @@ impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T> {
|
||||||
Some(head)
|
Some(head)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||||
|
let len = self.len();
|
||||||
|
if n >= len {
|
||||||
|
self.v = &mut [];
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
// can't underflow because `n < len`
|
||||||
|
let offset_from_end = (len - 1 - n) * self.chunk_size;
|
||||||
|
let end = self.v.len() - offset_from_end;
|
||||||
|
let start = end.saturating_sub(self.chunk_size);
|
||||||
|
let (tmp, tail) = mem::replace(&mut self.v, &mut []).split_at_mut(end);
|
||||||
|
let (_, nth_back) = tmp.split_at_mut(start);
|
||||||
|
self.v = tail;
|
||||||
|
Some(nth_back)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rchunks", since = "1.31.0")]
|
#[stable(feature = "rchunks", since = "1.31.0")]
|
||||||
|
@ -4898,6 +4933,24 @@ impl<'a, T> DoubleEndedIterator for RChunksExact<'a, T> {
|
||||||
Some(fst)
|
Some(fst)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||||
|
let len = self.len();
|
||||||
|
if n >= len {
|
||||||
|
self.v = &[];
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
// now that we know that `n` corresponds to a chunk,
|
||||||
|
// none of these operations can underflow/overflow
|
||||||
|
let offset = (len - n) * self.chunk_size;
|
||||||
|
let start = self.v.len() - offset;
|
||||||
|
let end = start + self.chunk_size;
|
||||||
|
let nth_back = &self.v[start..end];
|
||||||
|
self.v = &self.v[end..];
|
||||||
|
Some(nth_back)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rchunks", since = "1.31.0")]
|
#[stable(feature = "rchunks", since = "1.31.0")]
|
||||||
|
@ -5016,6 +5069,25 @@ impl<'a, T> DoubleEndedIterator for RChunksExactMut<'a, T> {
|
||||||
Some(head)
|
Some(head)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||||
|
let len = self.len();
|
||||||
|
if n >= len {
|
||||||
|
self.v = &mut [];
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
// now that we know that `n` corresponds to a chunk,
|
||||||
|
// none of these operations can underflow/overflow
|
||||||
|
let offset = (len - n) * self.chunk_size;
|
||||||
|
let start = self.v.len() - offset;
|
||||||
|
let end = start + self.chunk_size;
|
||||||
|
let (tmp, tail) = mem::replace(&mut self.v, &mut []).split_at_mut(end);
|
||||||
|
let (_, nth_back) = tmp.split_at_mut(start);
|
||||||
|
self.v = tail;
|
||||||
|
Some(nth_back)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rchunks", since = "1.31.0")]
|
#[stable(feature = "rchunks", since = "1.31.0")]
|
||||||
|
|
|
@ -356,6 +356,19 @@ fn test_rchunks_nth() {
|
||||||
assert_eq!(c2.next(), None);
|
assert_eq!(c2.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rchunks_nth_back() {
|
||||||
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||||
|
let mut c = v.rchunks(2);
|
||||||
|
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
|
||||||
|
assert_eq!(c.next_back().unwrap(), &[4, 5]);
|
||||||
|
|
||||||
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
||||||
|
let mut c2 = v2.rchunks(3);
|
||||||
|
assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
|
||||||
|
assert_eq!(c2.next_back(), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rchunks_last() {
|
fn test_rchunks_last() {
|
||||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||||
|
@ -407,6 +420,19 @@ fn test_rchunks_mut_nth() {
|
||||||
assert_eq!(c2.next(), None);
|
assert_eq!(c2.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rchunks_mut_nth_back() {
|
||||||
|
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||||
|
let mut c = v.rchunks_mut(2);
|
||||||
|
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
|
||||||
|
assert_eq!(c.next_back().unwrap(), &[4, 5]);
|
||||||
|
|
||||||
|
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||||
|
let mut c2 = v2.rchunks_mut(3);
|
||||||
|
assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
|
||||||
|
assert_eq!(c2.next_back(), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rchunks_mut_last() {
|
fn test_rchunks_mut_last() {
|
||||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||||
|
@ -460,6 +486,19 @@ fn test_rchunks_exact_nth() {
|
||||||
assert_eq!(c2.next(), None);
|
assert_eq!(c2.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rchunks_exact_nth_back() {
|
||||||
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||||
|
let mut c = v.rchunks_exact(2);
|
||||||
|
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
|
||||||
|
assert_eq!(c.next_back().unwrap(), &[4, 5]);
|
||||||
|
|
||||||
|
let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
|
||||||
|
let mut c2 = v2.rchunks_exact(3);
|
||||||
|
assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
|
||||||
|
assert_eq!(c2.next(), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rchunks_exact_last() {
|
fn test_rchunks_exact_last() {
|
||||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||||
|
@ -518,6 +557,19 @@ fn test_rchunks_exact_mut_nth() {
|
||||||
assert_eq!(c2.next(), None);
|
assert_eq!(c2.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rchunks_exact_mut_nth_back() {
|
||||||
|
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||||
|
let mut c = v.rchunks_exact_mut(2);
|
||||||
|
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
|
||||||
|
assert_eq!(c.next_back().unwrap(), &[4, 5]);
|
||||||
|
|
||||||
|
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
|
||||||
|
let mut c2 = v2.rchunks_exact_mut(3);
|
||||||
|
assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
|
||||||
|
assert_eq!(c2.next(), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rchunks_exact_mut_last() {
|
fn test_rchunks_exact_mut_last() {
|
||||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||||
|
|
Loading…
Reference in a new issue