@@ -1897,8 +1897,6 @@ impl<T> VecDeque<T> {
1897
1897
/// # Examples
1898
1898
///
1899
1899
/// ```
1900
- /// #![feature(vec_resize_with)]
1901
- ///
1902
1900
/// use std::collections::VecDeque;
1903
1901
///
1904
1902
/// let mut buf = VecDeque::new();
@@ -1917,7 +1915,7 @@ impl<T> VecDeque<T> {
1917
1915
/// buf.resize_with(5, || { state += 1; state });
1918
1916
/// assert_eq!(buf, [5, 10, 101, 102, 103]);
1919
1917
/// ```
1920
- #[ unstable ( feature = "vec_resize_with" , issue = "41758 " ) ]
1918
+ #[ stable ( feature = "vec_resize_with" , since = "1.33.0 " ) ]
1921
1919
pub fn resize_with ( & mut self , new_len : usize , generator : impl FnMut ( ) ->T ) {
1922
1920
let len = self . len ( ) ;
1923
1921
@@ -1927,6 +1925,118 @@ impl<T> VecDeque<T> {
1927
1925
self . truncate ( new_len) ;
1928
1926
}
1929
1927
}
1928
+
1929
+ /// Rotates the double-ended queue `mid` places to the left.
1930
+ ///
1931
+ /// Equivalently,
1932
+ /// - Rotates item `mid` into the first position.
1933
+ /// - Pops the first `mid` items and pushes them to the end.
1934
+ /// - Rotates `len() - mid` places to the right.
1935
+ ///
1936
+ /// # Panics
1937
+ ///
1938
+ /// If `mid` is greater than `len()`. Note that `mid == len()`
1939
+ /// does _not_ panic and is a no-op rotation.
1940
+ ///
1941
+ /// # Complexity
1942
+ ///
1943
+ /// Takes `O(min(mid, len() - mid))` time and no extra space.
1944
+ ///
1945
+ /// # Examples
1946
+ ///
1947
+ /// ```
1948
+ /// #![feature(vecdeque_rotate)]
1949
+ ///
1950
+ /// use std::collections::VecDeque;
1951
+ ///
1952
+ /// let mut buf: VecDeque<_> = (0..10).collect();
1953
+ ///
1954
+ /// buf.rotate_left(3);
1955
+ /// assert_eq!(buf, [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]);
1956
+ ///
1957
+ /// for i in 1..10 {
1958
+ /// assert_eq!(i * 3 % 10, buf[0]);
1959
+ /// buf.rotate_left(3);
1960
+ /// }
1961
+ /// assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
1962
+ /// ```
1963
+ #[ unstable( feature = "vecdeque_rotate" , issue = "56686" ) ]
1964
+ pub fn rotate_left ( & mut self , mid : usize ) {
1965
+ assert ! ( mid <= self . len( ) ) ;
1966
+ let k = self . len ( ) - mid;
1967
+ if mid <= k {
1968
+ unsafe { self . rotate_left_inner ( mid) }
1969
+ } else {
1970
+ unsafe { self . rotate_right_inner ( k) }
1971
+ }
1972
+ }
1973
+
1974
+ /// Rotates the double-ended queue `k` places to the right.
1975
+ ///
1976
+ /// Equivalently,
1977
+ /// - Rotates the first item into position `k`.
1978
+ /// - Pops the last `k` items and pushes them to the front.
1979
+ /// - Rotates `len() - k` places to the left.
1980
+ ///
1981
+ /// # Panics
1982
+ ///
1983
+ /// If `k` is greater than `len()`. Note that `k == len()`
1984
+ /// does _not_ panic and is a no-op rotation.
1985
+ ///
1986
+ /// # Complexity
1987
+ ///
1988
+ /// Takes `O(min(k, len() - k))` time and no extra space.
1989
+ ///
1990
+ /// # Examples
1991
+ ///
1992
+ /// ```
1993
+ /// #![feature(vecdeque_rotate)]
1994
+ ///
1995
+ /// use std::collections::VecDeque;
1996
+ ///
1997
+ /// let mut buf: VecDeque<_> = (0..10).collect();
1998
+ ///
1999
+ /// buf.rotate_right(3);
2000
+ /// assert_eq!(buf, [7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);
2001
+ ///
2002
+ /// for i in 1..10 {
2003
+ /// assert_eq!(0, buf[i * 3 % 10]);
2004
+ /// buf.rotate_right(3);
2005
+ /// }
2006
+ /// assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
2007
+ /// ```
2008
+ #[ unstable( feature = "vecdeque_rotate" , issue = "56686" ) ]
2009
+ pub fn rotate_right ( & mut self , k : usize ) {
2010
+ assert ! ( k <= self . len( ) ) ;
2011
+ let mid = self . len ( ) - k;
2012
+ if k <= mid {
2013
+ unsafe { self . rotate_right_inner ( k) }
2014
+ } else {
2015
+ unsafe { self . rotate_left_inner ( mid) }
2016
+ }
2017
+ }
2018
+
2019
+ // Safety: the following two methods require that the rotation amount
2020
+ // be less than half the length of the deque.
2021
+ //
2022
+ // `wrap_copy` requres that `min(x, cap() - x) + copy_len <= cap()`,
2023
+ // but than `min` is never more than half the capacity, regardless of x,
2024
+ // so it's sound to call here because we're calling with something
2025
+ // less than half the length, which is never above half the capacity.
2026
+
2027
+ unsafe fn rotate_left_inner ( & mut self , mid : usize ) {
2028
+ debug_assert ! ( mid * 2 <= self . len( ) ) ;
2029
+ self . wrap_copy ( self . head , self . tail , mid) ;
2030
+ self . head = self . wrap_add ( self . head , mid) ;
2031
+ self . tail = self . wrap_add ( self . tail , mid) ;
2032
+ }
2033
+
2034
+ unsafe fn rotate_right_inner ( & mut self , k : usize ) {
2035
+ debug_assert ! ( k * 2 <= self . len( ) ) ;
2036
+ self . head = self . wrap_sub ( self . head , k) ;
2037
+ self . tail = self . wrap_sub ( self . tail , k) ;
2038
+ self . wrap_copy ( self . tail , self . head , k) ;
2039
+ }
1930
2040
}
1931
2041
1932
2042
impl < T : Clone > VecDeque < T > {
0 commit comments