@@ -975,6 +975,40 @@ pub trait ImmutableVector<'self, T> {
975
975
* foreign interop.
976
976
*/
977
977
fn as_imm_buf < U > ( & self , f : |* T , uint| -> U ) -> U ;
978
+
979
+ /**
980
+ * Returns a mutable reference to the first element in this slice
981
+ * and adjusts the slice in place so that it no longer contains
982
+ * that element. O(1).
983
+ *
984
+ * Equivalent to:
985
+ *
986
+ * ```
987
+ * let head = &self[0];
988
+ * *self = self.slice_from(1);
989
+ * head
990
+ * ```
991
+ *
992
+ * Fails if slice is empty.
993
+ */
994
+ fn shift_ref ( & mut self ) -> & ' self T ;
995
+
996
+ /**
997
+ * Returns a mutable reference to the last element in this slice
998
+ * and adjusts the slice in place so that it no longer contains
999
+ * that element. O(1).
1000
+ *
1001
+ * Equivalent to:
1002
+ *
1003
+ * ```
1004
+ * let tail = &self[self.len() - 1];
1005
+ * *self = self.slice_to(self.len() - 1);
1006
+ * tail
1007
+ * ```
1008
+ *
1009
+ * Fails if slice is empty.
1010
+ */
1011
+ fn pop_ref ( & mut self ) -> & ' self T ;
978
1012
}
979
1013
980
1014
impl < ' self , T > ImmutableVector < ' self , T > for & ' self [ T ] {
@@ -1141,6 +1175,20 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
1141
1175
let s = self . repr ( ) ;
1142
1176
f ( s. data , s. len )
1143
1177
}
1178
+
1179
+ fn shift_ref ( & mut self ) -> & ' self T {
1180
+ unsafe {
1181
+ let s: & mut Slice < T > = cast:: transmute ( self ) ;
1182
+ & * raw:: shift_ptr ( s)
1183
+ }
1184
+ }
1185
+
1186
+ fn pop_ref ( & mut self ) -> & ' self T {
1187
+ unsafe {
1188
+ let s: & mut Slice < T > = cast:: transmute ( self ) ;
1189
+ & * raw:: pop_ptr ( s)
1190
+ }
1191
+ }
1144
1192
}
1145
1193
1146
1194
/// Extension methods for vectors contain `Eq` elements.
@@ -1859,23 +1907,61 @@ impl<T:Eq> OwnedEqVector<T> for ~[T] {
1859
1907
pub trait MutableVector < ' self , T > {
1860
1908
/// Return a slice that points into another slice.
1861
1909
fn mut_slice ( self , start : uint , end : uint ) -> & ' self mut [ T ] ;
1910
+
1862
1911
/**
1863
1912
* Returns a slice of self from `start` to the end of the vec.
1864
1913
*
1865
1914
* Fails when `start` points outside the bounds of self.
1866
1915
*/
1867
1916
fn mut_slice_from ( self , start : uint ) -> & ' self mut [ T ] ;
1917
+
1868
1918
/**
1869
1919
* Returns a slice of self from the start of the vec to `end`.
1870
1920
*
1871
1921
* Fails when `end` points outside the bounds of self.
1872
1922
*/
1873
1923
fn mut_slice_to ( self , end : uint ) -> & ' self mut [ T ] ;
1924
+
1874
1925
/// Returns an iterator that allows modifying each value
1875
1926
fn mut_iter ( self ) -> VecMutIterator < ' self , T > ;
1927
+
1876
1928
/// Returns a reversed iterator that allows modifying each value
1877
1929
fn mut_rev_iter ( self ) -> MutRevIterator < ' self , T > ;
1878
1930
1931
+ /**
1932
+ * Returns a mutable reference to the first element in this slice
1933
+ * and adjusts the slice in place so that it no longer contains
1934
+ * that element. O(1).
1935
+ *
1936
+ * Equivalent to:
1937
+ *
1938
+ * ```
1939
+ * let head = &mut self[0];
1940
+ * *self = self.mut_slice_from(1);
1941
+ * head
1942
+ * ```
1943
+ *
1944
+ * Fails if slice is empty.
1945
+ */
1946
+ fn mut_shift_ref ( & mut self ) -> & ' self mut T ;
1947
+
1948
+ /**
1949
+ * Returns a mutable reference to the last element in this slice
1950
+ * and adjusts the slice in place so that it no longer contains
1951
+ * that element. O(1).
1952
+ *
1953
+ * Equivalent to:
1954
+ *
1955
+ * ```
1956
+ * let tail = &mut self[self.len() - 1];
1957
+ * *self = self.mut_slice_to(self.len() - 1);
1958
+ * tail
1959
+ * ```
1960
+ *
1961
+ * Fails if slice is empty.
1962
+ */
1963
+ fn mut_pop_ref ( & mut self ) -> & ' self mut T ;
1964
+
1879
1965
/**
1880
1966
* Swaps two elements in a vector
1881
1967
*
@@ -1978,6 +2064,20 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
1978
2064
self . mut_iter ( ) . invert ( )
1979
2065
}
1980
2066
2067
+ fn mut_shift_ref ( & mut self ) -> & ' self mut T {
2068
+ unsafe {
2069
+ let s: & mut Slice < T > = cast:: transmute ( self ) ;
2070
+ cast:: transmute_mut ( & * raw:: shift_ptr ( s) )
2071
+ }
2072
+ }
2073
+
2074
+ fn mut_pop_ref ( & mut self ) -> & ' self mut T {
2075
+ unsafe {
2076
+ let s: & mut Slice < T > = cast:: transmute ( self ) ;
2077
+ cast:: transmute_mut ( & * raw:: pop_ptr ( s) )
2078
+ }
2079
+ }
2080
+
1981
2081
fn swap ( self , a : uint , b : uint ) {
1982
2082
unsafe {
1983
2083
// Can't take two mutable loans from one vector, so instead just cast
@@ -2189,6 +2289,31 @@ pub mod raw {
2189
2289
}
2190
2290
}
2191
2291
}
2292
+
2293
+ /**
2294
+ * Returns a pointer to first element in slice and adjusts
2295
+ * slice so it no longer contains that element. Fails if
2296
+ * slice is empty. O(1).
2297
+ */
2298
+ pub unsafe fn shift_ptr < T > ( slice : & mut Slice < T > ) -> * T {
2299
+ if slice. len == 0 { fail ! ( "shift on empty slice" ) ; }
2300
+ let head: * T = slice. data ;
2301
+ slice. data = ptr:: offset ( slice. data , 1 ) ;
2302
+ slice. len -= 1 ;
2303
+ head
2304
+ }
2305
+
2306
+ /**
2307
+ * Returns a pointer to last element in slice and adjusts
2308
+ * slice so it no longer contains that element. Fails if
2309
+ * slice is empty. O(1).
2310
+ */
2311
+ pub unsafe fn pop_ptr < T > ( slice : & mut Slice < T > ) -> * T {
2312
+ if slice. len == 0 { fail ! ( "pop on empty slice" ) ; }
2313
+ let tail: * T = ptr:: offset ( slice. data , ( slice. len - 1 ) as int ) ;
2314
+ slice. len -= 1 ;
2315
+ tail
2316
+ }
2192
2317
}
2193
2318
2194
2319
/// Operations on `[u8]`
@@ -3822,6 +3947,75 @@ mod tests {
3822
3947
assert!(!empty.ends_with(bytes!(" foo")));
3823
3948
assert!(bytes!(" foobar") . ends_with( empty) ) ;
3824
3949
}
3950
+
3951
+ #[ test]
3952
+ fn test_shift_ref ( ) {
3953
+ let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
3954
+ let h = x. shift_ref ( ) ;
3955
+ assert_eq ! ( * h, 1 ) ;
3956
+ assert_eq ! ( x. len( ) , 4 ) ;
3957
+ assert_eq ! ( x[ 0 ] , 2 ) ;
3958
+ assert_eq ! ( x[ 3 ] , 5 ) ;
3959
+ }
3960
+
3961
+ #[ test]
3962
+ #[ should_fail]
3963
+ fn test_shift_ref_empty ( ) {
3964
+ let mut x: & [ int ] = [ ] ;
3965
+ x. shift_ref ( ) ;
3966
+ }
3967
+
3968
+ #[ test]
3969
+ fn test_pop_ref ( ) {
3970
+ let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
3971
+ let h = x. pop_ref ( ) ;
3972
+ assert_eq ! ( * h, 5 ) ;
3973
+ assert_eq ! ( x. len( ) , 4 ) ;
3974
+ assert_eq ! ( x[ 0 ] , 1 ) ;
3975
+ assert_eq ! ( x[ 3 ] , 4 ) ;
3976
+ }
3977
+
3978
+ #[ test]
3979
+ #[ should_fail]
3980
+ fn test_pop_ref_empty ( ) {
3981
+ let mut x: & [ int ] = [ ] ;
3982
+ x. pop_ref ( ) ;
3983
+ }
3984
+
3985
+
3986
+ #[ test]
3987
+ fn test_mut_shift_ref ( ) {
3988
+ let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
3989
+ let h = x. mut_shift_ref ( ) ;
3990
+ assert_eq ! ( * h, 1 ) ;
3991
+ assert_eq ! ( x. len( ) , 4 ) ;
3992
+ assert_eq ! ( x[ 0 ] , 2 ) ;
3993
+ assert_eq ! ( x[ 3 ] , 5 ) ;
3994
+ }
3995
+
3996
+ #[ test]
3997
+ #[ should_fail]
3998
+ fn test_mut_shift_ref_empty ( ) {
3999
+ let mut x: & mut [ int ] = [ ] ;
4000
+ x. mut_shift_ref ( ) ;
4001
+ }
4002
+
4003
+ #[ test]
4004
+ fn test_mut_pop_ref ( ) {
4005
+ let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4006
+ let h = x. mut_pop_ref ( ) ;
4007
+ assert_eq ! ( * h, 5 ) ;
4008
+ assert_eq ! ( x. len( ) , 4 ) ;
4009
+ assert_eq ! ( x[ 0 ] , 1 ) ;
4010
+ assert_eq ! ( x[ 3 ] , 4 ) ;
4011
+ }
4012
+
4013
+ #[ test]
4014
+ #[ should_fail]
4015
+ fn test_mut_pop_ref_empty ( ) {
4016
+ let mut x: & mut [ int ] = [ ] ;
4017
+ x. mut_pop_ref ( ) ;
4018
+ }
3825
4019
}
3826
4020
3827
4021
#[ cfg( test) ]
0 commit comments