@@ -2181,7 +2181,7 @@ impl<T> VecDeque<T> {
2181
2181
///
2182
2182
/// This method does not allocate and does not change the order of the
2183
2183
/// inserted elements. As it returns a mutable slice, this can be used to
2184
- /// sort or binary search a deque.
2184
+ /// sort a deque.
2185
2185
///
2186
2186
/// Once the internal storage is contiguous, the [`as_slices`] and
2187
2187
/// [`as_mut_slices`] methods will return the entire contents of the
@@ -2430,6 +2430,143 @@ impl<T> VecDeque<T> {
2430
2430
self . wrap_copy ( self . tail , self . head , k) ;
2431
2431
}
2432
2432
}
2433
+
2434
+ /// Binary searches this sorted `VecDeque` for a given element.
2435
+ ///
2436
+ /// If the value is found then [`Result::Ok`] is returned, containing the
2437
+ /// index of the matching element. If there are multiple matches, then any
2438
+ /// one of the matches could be returned. If the value is not found then
2439
+ /// [`Result::Err`] is returned, containing the index where a matching
2440
+ /// element could be inserted while maintaining sorted order.
2441
+ ///
2442
+ /// # Examples
2443
+ ///
2444
+ /// Looks up a series of four elements. The first is found, with a
2445
+ /// uniquely determined position; the second and third are not
2446
+ /// found; the fourth could match any position in `[1, 4]`.
2447
+ ///
2448
+ /// ```
2449
+ /// #![feature(vecdeque_binary_search)]
2450
+ /// use std::collections::VecDeque;
2451
+ ///
2452
+ /// let deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
2453
+ ///
2454
+ /// assert_eq!(deque.binary_search(&13), Ok(9));
2455
+ /// assert_eq!(deque.binary_search(&4), Err(7));
2456
+ /// assert_eq!(deque.binary_search(&100), Err(13));
2457
+ /// let r = deque.binary_search(&1);
2458
+ /// assert!(matches!(r, Ok(1..=4)));
2459
+ /// ```
2460
+ ///
2461
+ /// If you want to insert an item to a sorted `VecDeque`, while maintaining
2462
+ /// sort order:
2463
+ ///
2464
+ /// ```
2465
+ /// #![feature(vecdeque_binary_search)]
2466
+ /// use std::collections::VecDeque;
2467
+ ///
2468
+ /// let mut deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
2469
+ /// let num = 42;
2470
+ /// let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
2471
+ /// deque.insert(idx, num);
2472
+ /// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
2473
+ /// ```
2474
+ #[ unstable( feature = "vecdeque_binary_search" , issue = "78021" ) ]
2475
+ #[ inline]
2476
+ pub fn binary_search ( & self , x : & T ) -> Result < usize , usize >
2477
+ where
2478
+ T : Ord ,
2479
+ {
2480
+ self . binary_search_by ( |e| e. cmp ( x) )
2481
+ }
2482
+
2483
+ /// Binary searches this sorted `VecDeque` with a comparator function.
2484
+ ///
2485
+ /// The comparator function should implement an order consistent
2486
+ /// with the sort order of the underlying `VecDeque`, returning an
2487
+ /// order code that indicates whether its argument is `Less`,
2488
+ /// `Equal` or `Greater` than the desired target.
2489
+ ///
2490
+ /// If the value is found then [`Result::Ok`] is returned, containing the
2491
+ /// index of the matching element. If there are multiple matches, then any
2492
+ /// one of the matches could be returned. If the value is not found then
2493
+ /// [`Result::Err`] is returned, containing the index where a matching
2494
+ /// element could be inserted while maintaining sorted order.
2495
+ ///
2496
+ /// # Examples
2497
+ ///
2498
+ /// Looks up a series of four elements. The first is found, with a
2499
+ /// uniquely determined position; the second and third are not
2500
+ /// found; the fourth could match any position in `[1, 4]`.
2501
+ ///
2502
+ /// ```
2503
+ /// #![feature(vecdeque_binary_search)]
2504
+ /// use std::collections::VecDeque;
2505
+ ///
2506
+ /// let deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
2507
+ ///
2508
+ /// assert_eq!(deque.binary_search_by(|x| x.cmp(&13)), Ok(9));
2509
+ /// assert_eq!(deque.binary_search_by(|x| x.cmp(&4)), Err(7));
2510
+ /// assert_eq!(deque.binary_search_by(|x| x.cmp(&100)), Err(13));
2511
+ /// let r = deque.binary_search_by(|x| x.cmp(&1));
2512
+ /// assert!(matches!(r, Ok(1..=4)));
2513
+ /// ```
2514
+ #[ unstable( feature = "vecdeque_binary_search" , issue = "78021" ) ]
2515
+ pub fn binary_search_by < ' a , F > ( & ' a self , mut f : F ) -> Result < usize , usize >
2516
+ where
2517
+ F : FnMut ( & ' a T ) -> Ordering ,
2518
+ {
2519
+ let ( front, back) = self . as_slices ( ) ;
2520
+
2521
+ if let Some ( Ordering :: Less | Ordering :: Equal ) = back. first ( ) . map ( |elem| f ( elem) ) {
2522
+ back. binary_search_by ( f) . map ( |idx| idx + front. len ( ) ) . map_err ( |idx| idx + front. len ( ) )
2523
+ } else {
2524
+ front. binary_search_by ( f)
2525
+ }
2526
+ }
2527
+
2528
+ /// Binary searches this sorted `VecDeque` with a key extraction function.
2529
+ ///
2530
+ /// Assumes that the `VecDeque` is sorted by the key, for instance with
2531
+ /// [`make_contiguous().sort_by_key()`](#method.make_contiguous) using the same
2532
+ /// key extraction function.
2533
+ ///
2534
+ /// If the value is found then [`Result::Ok`] is returned, containing the
2535
+ /// index of the matching element. If there are multiple matches, then any
2536
+ /// one of the matches could be returned. If the value is not found then
2537
+ /// [`Result::Err`] is returned, containing the index where a matching
2538
+ /// element could be inserted while maintaining sorted order.
2539
+ ///
2540
+ /// # Examples
2541
+ ///
2542
+ /// Looks up a series of four elements in a slice of pairs sorted by
2543
+ /// their second elements. The first is found, with a uniquely
2544
+ /// determined position; the second and third are not found; the
2545
+ /// fourth could match any position in `[1, 4]`.
2546
+ ///
2547
+ /// ```
2548
+ /// #![feature(vecdeque_binary_search)]
2549
+ /// use std::collections::VecDeque;
2550
+ ///
2551
+ /// let deque: VecDeque<_> = vec![(0, 0), (2, 1), (4, 1), (5, 1),
2552
+ /// (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
2553
+ /// (1, 21), (2, 34), (4, 55)].into();
2554
+ ///
2555
+ /// assert_eq!(deque.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
2556
+ /// assert_eq!(deque.binary_search_by_key(&4, |&(a,b)| b), Err(7));
2557
+ /// assert_eq!(deque.binary_search_by_key(&100, |&(a,b)| b), Err(13));
2558
+ /// let r = deque.binary_search_by_key(&1, |&(a,b)| b);
2559
+ /// assert!(matches!(r, Ok(1..=4)));
2560
+ /// ```
2561
+ #[ unstable( feature = "vecdeque_binary_search" , issue = "78021" ) ]
2562
+ #[ inline]
2563
+ pub fn binary_search_by_key < ' a , B , F > ( & ' a self , b : & B , mut f : F ) -> Result < usize , usize >
2564
+ where
2565
+ F : FnMut ( & ' a T ) -> B ,
2566
+ B : Ord ,
2567
+ {
2568
+ self . binary_search_by ( |k| f ( k) . cmp ( b) )
2569
+ }
2433
2570
}
2434
2571
2435
2572
impl < T : Clone > VecDeque < T > {
0 commit comments