new implementation for nth_back for chunks
This commit is contained in:
parent
aff83c80dd
commit
16223e4bea
2 changed files with 27 additions and 6 deletions
|
@ -4155,15 +4155,32 @@ impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
|
|||
|
||||
#[inline]
|
||||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||
let (end, overflow) = self.v.len().overflowing_sub(n * self.chunk_size);
|
||||
let remainder = match self.v.len().checked_rem(self.chunk_size) {
|
||||
Some(res) => res,
|
||||
None => 0,
|
||||
};
|
||||
|
||||
let sub_chunk_size = if remainder != 0 { remainder } else { self.chunk_size };
|
||||
|
||||
let safe_sub = match n.checked_mul(sub_chunk_size) {
|
||||
Some(res) => res,
|
||||
None => 0,
|
||||
};
|
||||
|
||||
let (end, overflow) = self.v.len().overflowing_sub(safe_sub);
|
||||
if overflow {
|
||||
self.v = &mut [];
|
||||
self.v= &[];
|
||||
None
|
||||
} else {
|
||||
let start = match end.checked_sub(self.chunk_size) {
|
||||
Some(res) => cmp::min(self.v.len(), res),
|
||||
None => 0,
|
||||
let start = if n == 0 {
|
||||
self.v.len() - sub_chunk_size
|
||||
} else {
|
||||
match end.checked_sub(self.chunk_size) {
|
||||
Some(res) => res,
|
||||
None => 0,
|
||||
}
|
||||
};
|
||||
|
||||
let nth_back = &self.v[start..end];
|
||||
self.v = &self.v[..start];
|
||||
Some(nth_back)
|
||||
|
|
|
@ -144,7 +144,7 @@ fn test_chunks_nth_back() {
|
|||
|
||||
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let mut c2 = v2.chunks(3);
|
||||
assert_eq!(c2.nth_back(1).unwrap(), &[0, 1]);
|
||||
assert_eq!(c2.nth_back(1).unwrap(), &[0, 1, 2]);
|
||||
assert_eq!(c2.next(), None);
|
||||
assert_eq!(c2.next_back(), None);
|
||||
|
||||
|
@ -152,6 +152,10 @@ fn test_chunks_nth_back() {
|
|||
let mut c3 = v3.chunks(10);
|
||||
assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
|
||||
assert_eq!(c3.next(), None);
|
||||
|
||||
let v4: &[i32] = &[0, 1, 2];
|
||||
let mut c4 = v4.chunks(10);
|
||||
assert_eq!(c4.nth_back(1_000_000_000usize), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue