@@ -2403,6 +2403,12 @@ impl<T> VecDeque<T> {
24032403 /// [`Result::Err`] is returned, containing the index where a matching
24042404 /// element could be inserted while maintaining sorted order.
24052405 ///
2406+ /// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
2407+ ///
2408+ /// [`binary_search_by`]: VecDeque::binary_search_by
2409+ /// [`binary_search_by_key`]: VecDeque::binary_search_by_key
2410+ /// [`partition_point`]: VecDeque::partition_point
2411+ ///
24062412 /// # Examples
24072413 ///
24082414 /// Looks up a series of four elements. The first is found, with a
@@ -2457,6 +2463,12 @@ impl<T> VecDeque<T> {
24572463 /// [`Result::Err`] is returned, containing the index where a matching
24582464 /// element could be inserted while maintaining sorted order.
24592465 ///
2466+ /// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
2467+ ///
2468+ /// [`binary_search`]: VecDeque::binary_search
2469+ /// [`binary_search_by_key`]: VecDeque::binary_search_by_key
2470+ /// [`partition_point`]: VecDeque::partition_point
2471+ ///
24602472 /// # Examples
24612473 ///
24622474 /// Looks up a series of four elements. The first is found, with a
@@ -2481,8 +2493,11 @@ impl<T> VecDeque<T> {
24812493 F : FnMut ( & ' a T ) -> Ordering ,
24822494 {
24832495 let ( front, back) = self . as_slices ( ) ;
2496+ let cmp_back = back. first ( ) . map ( |elem| f ( elem) ) ;
24842497
2485- if let Some ( Ordering :: Less | Ordering :: Equal ) = back. first ( ) . map ( |elem| f ( elem) ) {
2498+ if let Some ( Ordering :: Equal ) = cmp_back {
2499+ Ok ( front. len ( ) )
2500+ } else if let Some ( Ordering :: Less ) = cmp_back {
24862501 back. binary_search_by ( f) . map ( |idx| idx + front. len ( ) ) . map_err ( |idx| idx + front. len ( ) )
24872502 } else {
24882503 front. binary_search_by ( f)
@@ -2492,15 +2507,21 @@ impl<T> VecDeque<T> {
24922507 /// Binary searches this sorted `VecDeque` with a key extraction function.
24932508 ///
24942509 /// Assumes that the `VecDeque` is sorted by the key, for instance with
2495- /// [`make_contiguous().sort_by_key()`](#method.make_contiguous) using the same
2496- /// key extraction function.
2510+ /// [`make_contiguous().sort_by_key()`] using the same key extraction function.
24972511 ///
24982512 /// If the value is found then [`Result::Ok`] is returned, containing the
24992513 /// index of the matching element. If there are multiple matches, then any
25002514 /// one of the matches could be returned. If the value is not found then
25012515 /// [`Result::Err`] is returned, containing the index where a matching
25022516 /// element could be inserted while maintaining sorted order.
25032517 ///
2518+ /// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
2519+ ///
2520+ /// [`make_contiguous().sort_by_key()`]: VecDeque::make_contiguous
2521+ /// [`binary_search`]: VecDeque::binary_search
2522+ /// [`binary_search_by`]: VecDeque::binary_search_by
2523+ /// [`partition_point`]: VecDeque::partition_point
2524+ ///
25042525 /// # Examples
25052526 ///
25062527 /// Looks up a series of four elements in a slice of pairs sorted by
@@ -2531,6 +2552,51 @@ impl<T> VecDeque<T> {
25312552 {
25322553 self . binary_search_by ( |k| f ( k) . cmp ( b) )
25332554 }
2555+
2556+ /// Returns the index of the partition point according to the given predicate
2557+ /// (the index of the first element of the second partition).
2558+ ///
2559+ /// The deque is assumed to be partitioned according to the given predicate.
2560+ /// This means that all elements for which the predicate returns true are at the start of the deque
2561+ /// and all elements for which the predicate returns false are at the end.
2562+ /// For example, [7, 15, 3, 5, 4, 12, 6] is a partitioned under the predicate x % 2 != 0
2563+ /// (all odd numbers are at the start, all even at the end).
2564+ ///
2565+ /// If this deque is not partitioned, the returned result is unspecified and meaningless,
2566+ /// as this method performs a kind of binary search.
2567+ ///
2568+ /// See also [`binary_search`], [`binary_search_by`], and [`binary_search_by_key`].
2569+ ///
2570+ /// [`binary_search`]: VecDeque::binary_search
2571+ /// [`binary_search_by`]: VecDeque::binary_search_by
2572+ /// [`binary_search_by_key`]: VecDeque::binary_search_by_key
2573+ ///
2574+ /// # Examples
2575+ ///
2576+ /// ```
2577+ /// #![feature(vecdeque_binary_search)]
2578+ /// use std::collections::VecDeque;
2579+ ///
2580+ /// let deque: VecDeque<_> = vec![1, 2, 3, 3, 5, 6, 7].into();
2581+ /// let i = deque.partition_point(|&x| x < 5);
2582+ ///
2583+ /// assert_eq!(i, 4);
2584+ /// assert!(deque.iter().take(i).all(|&x| x < 5));
2585+ /// assert!(deque.iter().skip(i).all(|&x| !(x < 5)));
2586+ /// ```
2587+ #[ unstable( feature = "vecdeque_binary_search" , issue = "78021" ) ]
2588+ pub fn partition_point < P > ( & self , mut pred : P ) -> usize
2589+ where
2590+ P : FnMut ( & T ) -> bool ,
2591+ {
2592+ let ( front, back) = self . as_slices ( ) ;
2593+
2594+ if let Some ( true ) = back. first ( ) . map ( |v| pred ( v) ) {
2595+ back. partition_point ( pred) + front. len ( )
2596+ } else {
2597+ front. partition_point ( pred)
2598+ }
2599+ }
25342600}
25352601
25362602impl < T : Clone > VecDeque < T > {
0 commit comments