@@ -2141,6 +2141,55 @@ impl<T> VecDeque<T> {
2141
2141
}
2142
2142
}
2143
2143
2144
+ /// Sorts the deque with a comparator function.
2145
+ ///
2146
+ /// This sort is stable (i.e., does not reorder equal elements) and `O(n log n)` worst-case.
2147
+ ///
2148
+ /// The comparator function must define a total ordering for the elements in the deque. If
2149
+ /// the ordering is not total, the order of the elements is unspecified. An order is a
2150
+ /// total order if it is (for all `a`, `b` and `c`):
2151
+ ///
2152
+ /// * total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true, and
2153
+ /// * transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
2154
+ ///
2155
+ /// When applicable, unstable sorting is preferred because it is generally faster than stable
2156
+ /// sorting and it doesn't allocate auxiliary memory.
2157
+ /// See [`sort_unstable_by`](#method.sort_unstable_by).
2158
+ ///
2159
+ /// # Current implementation
2160
+ ///
2161
+ /// The current implementation moves all elements of the internal buffer into one continous slice without
2162
+ /// allocating, which is then sorted using [`slice::sort_by`] (which may allocate).
2163
+ ///
2164
+ /// # Examples
2165
+ ///
2166
+ /// ```
2167
+ /// #![feature(vecdeque_sort)]
2168
+ /// use std::collections::VecDeque;
2169
+ ///
2170
+ /// let mut v: VecDeque<_> = [5, 4, 1, 3, 2].iter().collect();
2171
+ ///
2172
+ /// v.sort_by(|a, b| a.cmp(b));
2173
+ /// let expected: VecDeque<_> = [1, 2, 3, 4, 5].iter().collect();
2174
+ /// assert_eq!(v, expected);
2175
+ ///
2176
+ /// // reverse sorting
2177
+ /// v.sort_by(|a, b| b.cmp(a));
2178
+ /// let expected: VecDeque<_> = [5, 4, 3, 2, 1].iter().collect();
2179
+ /// assert_eq!(v, expected);
2180
+ /// ```
2181
+ ///
2182
+ /// [`slice::sort_by`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by
2183
+ #[ unstable( feature = "vecdeque_sort" , issue = "none" ) ]
2184
+ pub fn sort_by < F > ( & mut self , compare : F )
2185
+ where
2186
+ F : FnMut ( & T , & T ) -> Ordering ,
2187
+ {
2188
+ self . make_continuous ( ) ;
2189
+
2190
+ self . as_mut_slices ( ) . 0 . sort_by ( compare) ;
2191
+ }
2192
+
2144
2193
/// Rotates the double-ended queue `k` places to the right.
2145
2194
///
2146
2195
/// Equivalently,
@@ -2971,7 +3020,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
2971
3020
let buf = other. buf . ptr ( ) ;
2972
3021
let len = other. len ( ) ;
2973
3022
let cap = other. cap ( ) ;
2974
-
3023
+
2975
3024
if other. head != 0 {
2976
3025
ptr:: copy ( buf. add ( other. tail ) , buf, len) ;
2977
3026
}
0 commit comments