@@ -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,154 @@ 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 = "1" ) ]
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 = "1" ) ]
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
+ if self . is_empty ( ) {
2520
+ return Err ( 0 ) ;
2521
+ }
2522
+
2523
+ let ( front, back) = self . as_slices ( ) ;
2524
+
2525
+ match back. first ( ) . map ( |elem| f ( elem) ) {
2526
+ Some ( Ordering :: Equal ) => return Ok ( front. len ( ) ) ,
2527
+ Some ( Ordering :: Less ) => {
2528
+ return back[ 1 ..]
2529
+ . binary_search_by ( f)
2530
+ . map ( |idx| idx + front. len ( ) + 1 )
2531
+ . map_err ( |idx| idx + front. len ( ) + 1 ) ;
2532
+ }
2533
+ _ => { }
2534
+ }
2535
+
2536
+ front. binary_search_by ( f)
2537
+ }
2538
+
2539
+ /// Binary searches this sorted `VecDeque` with a key extraction function.
2540
+ ///
2541
+ /// Assumes that the `VecDeque` is sorted by the key, for instance with
2542
+ /// [`make_contiguous().sort_by_key()`](#method.make_contiguous) using the same
2543
+ /// key extraction function.
2544
+ ///
2545
+ /// If the value is found then [`Result::Ok`] is returned, containing the
2546
+ /// index of the matching element. If there are multiple matches, then any
2547
+ /// one of the matches could be returned. If the value is not found then
2548
+ /// [`Result::Err`] is returned, containing the index where a matching
2549
+ /// element could be inserted while maintaining sorted order.
2550
+ ///
2551
+ /// # Examples
2552
+ ///
2553
+ /// Looks up a series of four elements in a slice of pairs sorted by
2554
+ /// their second elements. The first is found, with a uniquely
2555
+ /// determined position; the second and third are not found; the
2556
+ /// fourth could match any position in `[1, 4]`.
2557
+ ///
2558
+ /// ```
2559
+ /// #![feature(vecdeque_binary_search)]
2560
+ /// use std::collections::VecDeque;
2561
+ ///
2562
+ /// let deque: VecDeque<_> = vec![(0, 0), (2, 1), (4, 1), (5, 1),
2563
+ /// (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
2564
+ /// (1, 21), (2, 34), (4, 55)].into();
2565
+ ///
2566
+ /// assert_eq!(deque.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
2567
+ /// assert_eq!(deque.binary_search_by_key(&4, |&(a,b)| b), Err(7));
2568
+ /// assert_eq!(deque.binary_search_by_key(&100, |&(a,b)| b), Err(13));
2569
+ /// let r = deque.binary_search_by_key(&1, |&(a,b)| b);
2570
+ /// assert!(matches!(r, Ok(1..=4)));
2571
+ /// ```
2572
+ #[ unstable( feature = "vecdeque_binary_search" , issue = "1" ) ]
2573
+ #[ inline]
2574
+ pub fn binary_search_by_key < ' a , B , F > ( & ' a self , b : & B , mut f : F ) -> Result < usize , usize >
2575
+ where
2576
+ F : FnMut ( & ' a T ) -> B ,
2577
+ B : Ord ,
2578
+ {
2579
+ self . binary_search_by ( |k| f ( k) . cmp ( b) )
2580
+ }
2433
2581
}
2434
2582
2435
2583
impl < T : Clone > VecDeque < T > {
0 commit comments