|
10 | 10 | use core::array::LengthAtMost32;
|
11 | 11 | use core::cmp::{self, Ordering};
|
12 | 12 | use core::fmt;
|
13 |
| -use core::iter::{repeat_with, FromIterator, FusedIterator}; |
14 |
| -use core::mem; |
| 13 | +use core::iter::{once, repeat_with, FromIterator, FusedIterator}; |
| 14 | +use core::mem::{self, replace}; |
15 | 15 | use core::ops::Bound::{Excluded, Included, Unbounded};
|
16 | 16 | use core::ops::{Index, IndexMut, RangeBounds, Try};
|
17 | 17 | use core::ptr::{self, NonNull};
|
@@ -57,11 +57,88 @@ pub struct VecDeque<T> {
|
57 | 57 | buf: RawVec<T>,
|
58 | 58 | }
|
59 | 59 |
|
| 60 | +/// PairSlices pairs up equal length slice parts of two deques |
| 61 | +/// |
| 62 | +/// For example, given deques "A" and "B" with the following division into slices: |
| 63 | +/// |
| 64 | +/// A: [0 1 2] [3 4 5] |
| 65 | +/// B: [a b] [c d e] |
| 66 | +/// |
| 67 | +/// It produces the following sequence of matching slices: |
| 68 | +/// |
| 69 | +/// ([0 1], [a b]) |
| 70 | +/// ([2], [c]) |
| 71 | +/// ([3 4], [d e]) |
| 72 | +/// |
| 73 | +/// and the uneven remainder of either A or B is skipped. |
| 74 | +struct PairSlices<'a, 'b, T> { |
| 75 | + a0: &'a mut [T], |
| 76 | + a1: &'a mut [T], |
| 77 | + b0: &'b [T], |
| 78 | + b1: &'b [T], |
| 79 | +} |
| 80 | + |
| 81 | +impl<'a, 'b, T> PairSlices<'a, 'b, T> { |
| 82 | + fn from(to: &'a mut VecDeque<T>, from: &'b VecDeque<T>) -> Self { |
| 83 | + let (a0, a1) = to.as_mut_slices(); |
| 84 | + let (b0, b1) = from.as_slices(); |
| 85 | + PairSlices { a0, a1, b0, b1 } |
| 86 | + } |
| 87 | + |
| 88 | + fn has_remainder(&self) -> bool { |
| 89 | + !self.b0.is_empty() |
| 90 | + } |
| 91 | + |
| 92 | + fn remainder(self) -> impl Iterator<Item=&'b [T]> { |
| 93 | + once(self.b0).chain(once(self.b1)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl<'a, 'b, T> Iterator for PairSlices<'a, 'b, T> |
| 98 | +{ |
| 99 | + type Item = (&'a mut [T], &'b [T]); |
| 100 | + fn next(&mut self) -> Option<Self::Item> { |
| 101 | + // Get next part length |
| 102 | + let part = cmp::min(self.a0.len(), self.b0.len()); |
| 103 | + if part == 0 { |
| 104 | + return None; |
| 105 | + } |
| 106 | + let (p0, p1) = replace(&mut self.a0, &mut []).split_at_mut(part); |
| 107 | + let (q0, q1) = self.b0.split_at(part); |
| 108 | + |
| 109 | + // Move a1 into a0, if it's empty (and b1, b0 the same way). |
| 110 | + self.a0 = p1; |
| 111 | + self.b0 = q1; |
| 112 | + if self.a0.is_empty() { |
| 113 | + self.a0 = replace(&mut self.a1, &mut []); |
| 114 | + } |
| 115 | + if self.b0.is_empty() { |
| 116 | + self.b0 = replace(&mut self.b1, &[]); |
| 117 | + } |
| 118 | + Some((p0, q0)) |
| 119 | + } |
| 120 | +} |
| 121 | + |
60 | 122 | #[stable(feature = "rust1", since = "1.0.0")]
|
61 | 123 | impl<T: Clone> Clone for VecDeque<T> {
|
62 | 124 | fn clone(&self) -> VecDeque<T> {
|
63 | 125 | self.iter().cloned().collect()
|
64 | 126 | }
|
| 127 | + |
| 128 | + fn clone_from(&mut self, other: &Self) { |
| 129 | + self.truncate(other.len()); |
| 130 | + |
| 131 | + let mut iter = PairSlices::from(self, other); |
| 132 | + while let Some((dst, src)) = iter.next() { |
| 133 | + dst.clone_from_slice(&src); |
| 134 | + } |
| 135 | + |
| 136 | + if iter.has_remainder() { |
| 137 | + for remainder in iter.remainder() { |
| 138 | + self.extend(remainder.iter().cloned()); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
65 | 142 | }
|
66 | 143 |
|
67 | 144 | #[stable(feature = "rust1", since = "1.0.0")]
|
@@ -2209,6 +2286,16 @@ impl<'a, T> Iterator for Iter<'a, T> {
|
2209 | 2286 | final_res
|
2210 | 2287 | }
|
2211 | 2288 |
|
| 2289 | + fn nth(&mut self, n: usize) -> Option<Self::Item> { |
| 2290 | + if n >= count(self.tail, self.head, self.ring.len()) { |
| 2291 | + self.tail = self.head; |
| 2292 | + None |
| 2293 | + } else { |
| 2294 | + self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len()); |
| 2295 | + self.next() |
| 2296 | + } |
| 2297 | + } |
| 2298 | + |
2212 | 2299 | #[inline]
|
2213 | 2300 | fn last(mut self) -> Option<&'a T> {
|
2214 | 2301 | self.next_back()
|
@@ -2327,6 +2414,16 @@ impl<'a, T> Iterator for IterMut<'a, T> {
|
2327 | 2414 | back.iter_mut().fold(accum, &mut f)
|
2328 | 2415 | }
|
2329 | 2416 |
|
| 2417 | + fn nth(&mut self, n: usize) -> Option<Self::Item> { |
| 2418 | + if n >= count(self.tail, self.head, self.ring.len()) { |
| 2419 | + self.tail = self.head; |
| 2420 | + None |
| 2421 | + } else { |
| 2422 | + self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len()); |
| 2423 | + self.next() |
| 2424 | + } |
| 2425 | + } |
| 2426 | + |
2330 | 2427 | #[inline]
|
2331 | 2428 | fn last(mut self) -> Option<&'a mut T> {
|
2332 | 2429 | self.next_back()
|
|
0 commit comments