@@ -2403,6 +2403,12 @@ impl<T> VecDeque<T> {
2403
2403
/// [`Result::Err`] is returned, containing the index where a matching
2404
2404
/// element could be inserted while maintaining sorted order.
2405
2405
///
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
+ ///
2406
2412
/// # Examples
2407
2413
///
2408
2414
/// Looks up a series of four elements. The first is found, with a
@@ -2457,6 +2463,12 @@ impl<T> VecDeque<T> {
2457
2463
/// [`Result::Err`] is returned, containing the index where a matching
2458
2464
/// element could be inserted while maintaining sorted order.
2459
2465
///
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
+ ///
2460
2472
/// # Examples
2461
2473
///
2462
2474
/// Looks up a series of four elements. The first is found, with a
@@ -2481,8 +2493,11 @@ impl<T> VecDeque<T> {
2481
2493
F : FnMut ( & ' a T ) -> Ordering ,
2482
2494
{
2483
2495
let ( front, back) = self . as_slices ( ) ;
2496
+ let cmp_back = back. first ( ) . map ( |elem| f ( elem) ) ;
2484
2497
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 {
2486
2501
back. binary_search_by ( f) . map ( |idx| idx + front. len ( ) ) . map_err ( |idx| idx + front. len ( ) )
2487
2502
} else {
2488
2503
front. binary_search_by ( f)
@@ -2492,15 +2507,21 @@ impl<T> VecDeque<T> {
2492
2507
/// Binary searches this sorted `VecDeque` with a key extraction function.
2493
2508
///
2494
2509
/// 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.
2497
2511
///
2498
2512
/// If the value is found then [`Result::Ok`] is returned, containing the
2499
2513
/// index of the matching element. If there are multiple matches, then any
2500
2514
/// one of the matches could be returned. If the value is not found then
2501
2515
/// [`Result::Err`] is returned, containing the index where a matching
2502
2516
/// element could be inserted while maintaining sorted order.
2503
2517
///
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
+ ///
2504
2525
/// # Examples
2505
2526
///
2506
2527
/// Looks up a series of four elements in a slice of pairs sorted by
@@ -2531,6 +2552,51 @@ impl<T> VecDeque<T> {
2531
2552
{
2532
2553
self . binary_search_by ( |k| f ( k) . cmp ( b) )
2533
2554
}
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
+ }
2534
2600
}
2535
2601
2536
2602
impl < T : Clone > VecDeque < T > {
0 commit comments