Skip to content

Commit 44fc3c6

Browse files
committed
add MutableVector::mut_split(self, pred) -> DoubleEndedIterator<&mut [T]>
This method is the mutable version of ImmutableVector::split. It is a DoubleEndedIterator, making mut_rsplit irrelevent. The size_hint method is not optimal because of #9629. At the same time, clarify *split* iterator doc.
1 parent 25bb1a4 commit 44fc3c6

File tree

1 file changed

+110
-4
lines changed

1 file changed

+110
-4
lines changed

src/libstd/vec.rs

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,20 +858,24 @@ pub trait ImmutableVector<'self, T> {
858858
/// Returns a reversed iterator over a vector
859859
fn rev_iter(self) -> RevIterator<'self, T>;
860860
/// Returns an iterator over the subslices of the vector which are
861-
/// separated by elements that match `pred`.
861+
/// separated by elements that match `pred`. The matched element
862+
/// is not contained in the subslices.
862863
fn split(self, pred: 'self |&T| -> bool) -> SplitIterator<'self, T>;
863864
/// Returns an iterator over the subslices of the vector which are
864865
/// separated by elements that match `pred`, limited to splitting
865-
/// at most `n` times.
866+
/// at most `n` times. The matched element is not contained in
867+
/// the subslices.
866868
fn splitn(self, n: uint, pred: 'self |&T| -> bool) -> SplitIterator<'self, T>;
867869
/// Returns an iterator over the subslices of the vector which are
868870
/// separated by elements that match `pred`. This starts at the
869-
/// end of the vector and works backwards.
871+
/// end of the vector and works backwards. The matched element is
872+
/// not contained in the subslices.
870873
fn rsplit(self, pred: 'self |&T| -> bool) -> RSplitIterator<'self, T>;
871874
/// Returns an iterator over the subslices of the vector which are
872875
/// separated by elements that match `pred` limited to splitting
873876
/// at most `n` times. This starts at the end of the vector and
874-
/// works backwards.
877+
/// works backwards. The matched element is not contained in the
878+
/// subslices.
875879
fn rsplitn(self, n: uint, pred: 'self |&T| -> bool) -> RSplitIterator<'self, T>;
876880

877881
/**
@@ -1933,6 +1937,11 @@ pub trait MutableVector<'self, T> {
19331937
/// Returns a reversed iterator that allows modifying each value
19341938
fn mut_rev_iter(self) -> MutRevIterator<'self, T>;
19351939

1940+
/// Returns an iterator over the mutable subslices of the vector
1941+
/// which are separated by elements that match `pred`. The
1942+
/// matched element is not contained in the subslices.
1943+
fn mut_split(self, pred: 'self |&T| -> bool) -> MutSplitIterator<'self, T>;
1944+
19361945
/**
19371946
* Returns an iterator over `size` elements of the vector at a time.
19381947
* The chunks are mutable and do not overlap. If `size` does not divide the
@@ -2081,6 +2090,11 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
20812090
self.mut_iter().invert()
20822091
}
20832092

2093+
#[inline]
2094+
fn mut_split(self, pred: 'self |&T| -> bool) -> MutSplitIterator<'self, T> {
2095+
MutSplitIterator { v: self, pred: pred, finished: false }
2096+
}
2097+
20842098
#[inline]
20852099
fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'self, T> {
20862100
assert!(chunk_size > 0);
@@ -2575,6 +2589,73 @@ impl<'self, T> Clone for VecIterator<'self, T> {
25752589
iterator!{struct VecMutIterator -> *mut T, &'self mut T}
25762590
pub type MutRevIterator<'self, T> = Invert<VecMutIterator<'self, T>>;
25772591

2592+
/// An iterator over the subslices of the vector which are separated
2593+
/// by elements that match `pred`.
2594+
pub struct MutSplitIterator<'self, T> {
2595+
priv v: &'self mut [T],
2596+
priv pred: 'self |t: &T| -> bool,
2597+
priv finished: bool
2598+
}
2599+
2600+
impl<'self, T> Iterator<&'self mut [T]> for MutSplitIterator<'self, T> {
2601+
#[inline]
2602+
fn next(&mut self) -> Option<&'self mut [T]> {
2603+
if self.finished { return None; }
2604+
2605+
match self.v.iter().position(|x| (self.pred)(x)) {
2606+
None => {
2607+
self.finished = true;
2608+
let tmp = util::replace(&mut self.v, &mut []);
2609+
let len = tmp.len();
2610+
let (head, tail) = tmp.mut_split_at(len);
2611+
self.v = tail;
2612+
Some(head)
2613+
}
2614+
Some(idx) => {
2615+
let tmp = util::replace(&mut self.v, &mut []);
2616+
let (head, tail) = tmp.mut_split_at(idx);
2617+
self.v = tail.mut_slice_from(1);
2618+
Some(head)
2619+
}
2620+
}
2621+
}
2622+
2623+
#[inline]
2624+
fn size_hint(&self) -> (uint, Option<uint>) {
2625+
if self.finished { return (0, Some(0)) }
2626+
2627+
// if the predicate doesn't match anything, we yield one slice
2628+
// if it matches every element, we yield len+1 empty slices.
2629+
// FIXME #9629
2630+
//(1, Some(self.v.len() + 1))
2631+
(1, None)
2632+
}
2633+
}
2634+
2635+
impl<'self, T> DoubleEndedIterator<&'self mut [T]> for MutSplitIterator<'self, T> {
2636+
#[inline]
2637+
fn next_back(&mut self) -> Option<&'self mut [T]> {
2638+
if self.finished { return None; }
2639+
2640+
match self.v.iter().rposition(|x| (self.pred)(x)) {
2641+
None => {
2642+
self.finished = true;
2643+
let tmp = util::replace(&mut self.v, &mut []);
2644+
let len = tmp.len();
2645+
let (head, tail) = tmp.mut_split_at(len);
2646+
self.v = tail;
2647+
Some(head)
2648+
}
2649+
Some(idx) => {
2650+
let tmp = util::replace(&mut self.v, &mut []);
2651+
let (head, tail) = tmp.mut_split_at(idx);
2652+
self.v = head;
2653+
Some(tail.mut_slice_from(1))
2654+
}
2655+
}
2656+
}
2657+
}
2658+
25782659
/// An iterator over a vector in (non-overlapping) mutable chunks (`size` elements at a time). When
25792660
/// the vector len is not evenly divided by the chunk size, the last slice of the iteration will be
25802661
/// the remainder.
@@ -4038,6 +4119,31 @@ mod tests {
40384119
x.pop_ref();
40394120
}
40404121

4122+
#[test]
4123+
fn test_mut_splitator() {
4124+
let mut xs = [0,1,0,2,3,0,0,4,5,0];
4125+
assert_eq!(xs.mut_split(|x| *x == 0).len(), 6);
4126+
for slice in xs.mut_split(|x| *x == 0) {
4127+
slice.reverse();
4128+
}
4129+
assert_eq!(xs, [0,1,0,3,2,0,0,5,4,0]);
4130+
4131+
let mut xs = [0,1,0,2,3,0,0,4,5,0,6,7];
4132+
for slice in xs.mut_split(|x| *x == 0).take(5) {
4133+
slice.reverse();
4134+
}
4135+
assert_eq!(xs, [0,1,0,3,2,0,0,5,4,0,6,7]);
4136+
}
4137+
4138+
#[test]
4139+
fn test_mut_splitator_invert() {
4140+
let mut xs = [1,2,0,3,4,0,0,5,6,0];
4141+
for slice in xs.mut_split(|x| *x == 0).invert().take(4) {
4142+
slice.reverse();
4143+
}
4144+
assert_eq!(xs, [1,2,0,4,3,0,0,6,5,0]);
4145+
}
4146+
40414147
#[test]
40424148
fn test_mut_chunks() {
40434149
let mut v = [0u8, 1, 2, 3, 4, 5, 6];

0 commit comments

Comments
 (0)