@@ -858,20 +858,24 @@ pub trait ImmutableVector<'self, T> {
858
858
/// Returns a reversed iterator over a vector
859
859
fn rev_iter ( self ) -> RevIterator < ' self , T > ;
860
860
/// 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.
862
863
fn split ( self , pred: ' self |& T | -> bool) -> SplitIterator < ' self , T > ;
863
864
/// Returns an iterator over the subslices of the vector which are
864
865
/// 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.
866
868
fn splitn ( self , n : uint , pred: ' self |& T | -> bool) -> SplitIterator < ' self , T > ;
867
869
/// Returns an iterator over the subslices of the vector which are
868
870
/// 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.
870
873
fn rsplit ( self , pred: ' self |& T | -> bool) -> RSplitIterator < ' self , T > ;
871
874
/// Returns an iterator over the subslices of the vector which are
872
875
/// separated by elements that match `pred` limited to splitting
873
876
/// 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.
875
879
fn rsplitn ( self , n : uint , pred: ' self |& T | -> bool) -> RSplitIterator < ' self , T > ;
876
880
877
881
/**
@@ -1933,6 +1937,11 @@ pub trait MutableVector<'self, T> {
1933
1937
/// Returns a reversed iterator that allows modifying each value
1934
1938
fn mut_rev_iter ( self ) -> MutRevIterator < ' self , T > ;
1935
1939
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
+
1936
1945
/**
1937
1946
* Returns an iterator over `size` elements of the vector at a time.
1938
1947
* 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] {
2081
2090
self . mut_iter ( ) . invert ( )
2082
2091
}
2083
2092
2093
+ #[ inline]
2094
+ fn mut_split ( self , pred: ' self |& T | -> bool) -> MutSplitIterator < ' self , T > {
2095
+ MutSplitIterator { v : self , pred : pred, finished : false }
2096
+ }
2097
+
2084
2098
#[ inline]
2085
2099
fn mut_chunks ( self , chunk_size : uint ) -> MutChunkIter < ' self , T > {
2086
2100
assert ! ( chunk_size > 0 ) ;
@@ -2575,6 +2589,73 @@ impl<'self, T> Clone for VecIterator<'self, T> {
2575
2589
iterator ! { struct VecMutIterator -> * mut T , & ' self mut T }
2576
2590
pub type MutRevIterator < ' self , T > = Invert < VecMutIterator < ' self , T > > ;
2577
2591
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
+
2578
2659
/// An iterator over a vector in (non-overlapping) mutable chunks (`size` elements at a time). When
2579
2660
/// the vector len is not evenly divided by the chunk size, the last slice of the iteration will be
2580
2661
/// the remainder.
@@ -4038,6 +4119,31 @@ mod tests {
4038
4119
x. pop_ref ( ) ;
4039
4120
}
4040
4121
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
+
4041
4147
#[ test]
4042
4148
fn test_mut_chunks ( ) {
4043
4149
let mut v = [ 0u8 , 1 , 2 , 3 , 4 , 5 , 6 ] ;
0 commit comments