15
15
use cast:: transmute;
16
16
use cast;
17
17
use container:: { Container , Mutable } ;
18
+ use cmp;
18
19
use cmp:: { Eq , Ord , TotalEq , TotalOrd , Ordering , Less , Equal , Greater } ;
19
20
use clone:: Clone ;
20
21
use iterator:: { FromIterator , Iterator , IteratorUtil } ;
@@ -2059,6 +2060,21 @@ pub trait MutableVector<'self, T> {
2059
2060
fn mut_iter ( self ) -> VecMutIterator < ' self , T > ;
2060
2061
fn mut_rev_iter ( self ) -> VecMutRevIterator < ' self , T > ;
2061
2062
2063
+ /**
2064
+ * Consumes `src` and moves as many elements as it can into `self`
2065
+ * from the range [start,end).
2066
+ *
2067
+ * Returns the number of elements copied (the shorter of self.len()
2068
+ * and end - start).
2069
+ *
2070
+ * # Arguments
2071
+ *
2072
+ * * src - A mutable vector of `T`
2073
+ * * start - The index into `src` to start copying from
2074
+ * * end - The index into `str` to stop copying from
2075
+ */
2076
+ fn move_from ( self , src : ~[ T ] , start : uint , end : uint ) -> uint ;
2077
+
2062
2078
unsafe fn unsafe_mut_ref ( & self , index : uint ) -> * mut T ;
2063
2079
unsafe fn unsafe_set ( & self , index : uint , val : T ) ;
2064
2080
}
@@ -2087,6 +2103,14 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
2087
2103
}
2088
2104
}
2089
2105
2106
+ #[ inline]
2107
+ fn move_from( self , mut src : ~[ T ] , start : uint , end : uint ) -> uint {
2108
+ for self . mut_iter( ) . zip( src. mut_slice( start, end) . mut_iter( ) ) . advance |( a, b) | {
2109
+ util:: swap( a, b) ;
2110
+ }
2111
+ cmp:: min( self . len( ) , end-start)
2112
+ }
2113
+
2090
2114
#[ inline]
2091
2115
unsafe fn unsafe_mut_ref( & self , index: uint) -> * mut T {
2092
2116
let pair_ptr : & ( * mut T , uint) = transmute( self ) ;
@@ -2100,6 +2124,23 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
2100
2124
}
2101
2125
}
2102
2126
2127
+ /// Trait for ~[T] where T is Cloneable
2128
+ pub trait MutableCloneableVector < T > {
2129
+ /// Copies as many elements from `src` as it can into `self`
2130
+ /// (the shorter of self.len() and src.len()). Returns the number of elements copied.
2131
+ fn copy_from( self , & [ T ] ) -> uint;
2132
+ }
2133
+
2134
+ impl <' self , T : Clone > MutableCloneableVector < T > for & ' self mut [ T ] {
2135
+ #[ inline]
2136
+ fn copy_from( self , src: & [ T ] ) -> uint {
2137
+ for self. mut_iter( ) . zip( src. iter( ) ) . advance |( a, b) | {
2138
+ * a = b. clone( ) ;
2139
+ }
2140
+ cmp:: min( self . len( ) , src. len( ) )
2141
+ }
2142
+ }
2143
+
2103
2144
/**
2104
2145
* Constructs a vector from an unsafe pointer to a buffer
2105
2146
*
@@ -3895,6 +3936,38 @@ mod tests {
3895
3936
assert_eq ! ( xs, [ 5 , 5 , 5 , 5 , 5 ] )
3896
3937
}
3897
3938
3939
+ #[ test]
3940
+ fn test_move_from( ) {
3941
+ let mut a = [ 1 , 2 , 3 , 4 , 5 ] ;
3942
+ let b = ~[ 6 , 7 , 8 ] ;
3943
+ assert_eq ! ( a. move_from( b, 0 , 3 ) , 3 ) ;
3944
+ assert_eq ! ( a, [ 6 , 7 , 8 , 4 , 5 ] ) ;
3945
+ let mut a = [ 7 , 2 , 8 , 1 ] ;
3946
+ let b = ~[ 3 , 1 , 4 , 1 , 5 , 9 ] ;
3947
+ assert_eq ! ( a. move_from( b, 0 , 6 ) , 4 ) ;
3948
+ assert_eq ! ( a, [ 3 , 1 , 4 , 1 ] ) ;
3949
+ let mut a = [ 1 , 2 , 3 , 4 ] ;
3950
+ let b = ~[ 5 , 6 , 7 , 8 , 9 , 0 ] ;
3951
+ assert_eq ! ( a. move_from( b, 2 , 3 ) , 1 ) ;
3952
+ assert_eq ! ( a, [ 7 , 2 , 3 , 4 ] ) ;
3953
+ let mut a = [ 1 , 2 , 3 , 4 , 5 ] ;
3954
+ let b = ~[ 5 , 6 , 7 , 8 , 9 , 0 ] ;
3955
+ assert_eq ! ( a. mut_slice( 2 , 4 ) . move_from( b, 1 , 6 ) , 2 ) ;
3956
+ assert_eq ! ( a, [ 1 , 2 , 6 , 7 , 5 ] ) ;
3957
+ }
3958
+
3959
+ #[ test]
3960
+ fn test_copy_from( ) {
3961
+ let mut a = [ 1 , 2 , 3 , 4 , 5 ] ;
3962
+ let b = [ 6 , 7 , 8 ] ;
3963
+ assert_eq ! ( a. copy_from( b) , 3 ) ;
3964
+ assert_eq ! ( a, [ 6 , 7 , 8 , 4 , 5 ] ) ;
3965
+ let mut c = [ 7 , 2 , 8 , 1 ] ;
3966
+ let d = [ 3 , 1 , 4 , 1 , 5 , 9 ] ;
3967
+ assert_eq ! ( c. copy_from( d) , 4 ) ;
3968
+ assert_eq ! ( c, [ 3 , 1 , 4 , 1 ] ) ;
3969
+ }
3970
+
3898
3971
#[ test]
3899
3972
fn test_reverse_part( ) {
3900
3973
let mut values = [ 1 , 2 , 3 , 4 , 5 ] ;
0 commit comments