@@ -2522,10 +2522,17 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2522
2522
self . len ( ) == 0
2523
2523
}
2524
2524
2525
- /// Returns a [`Cursor`] pointing to the first gap above the given bound.
2525
+ /// Returns a [`Cursor`] pointing at the gap before the smallest key
2526
+ /// greater than the given bound.
2526
2527
///
2527
- /// Passing [`Bound::Unbounded`] will return a cursor pointing to the start
2528
- /// of the map.
2528
+ /// Passing [`Bound::Included(x)`] will return a cursor pointing to the
2529
+ /// gap before the smallest key greater than or equal to `x`.
2530
+ ///
2531
+ /// Passing [`Bound::Excluded(x)`] will return a cursor pointing to the
2532
+ /// gap before the smallest key greater than `x`.
2533
+ ///
2534
+ /// Passing [`Bound::Unbounded`] will return a cursor pointing to the
2535
+ /// gap before the smallest key in the map.
2529
2536
///
2530
2537
/// # Examples
2531
2538
///
@@ -2535,17 +2542,24 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2535
2542
/// use std::collections::BTreeMap;
2536
2543
/// use std::ops::Bound;
2537
2544
///
2538
- /// let mut a = BTreeMap::new();
2539
- /// a.insert(1, "a");
2540
- /// a.insert(2, "b");
2541
- /// a.insert(3, "c");
2542
- /// a.insert(4, "d");
2543
- /// let cursor = a.lower_bound(Bound::Included(&2));
2545
+ /// let map = BTreeMap::from([
2546
+ /// (1, "a"),
2547
+ /// (2, "b"),
2548
+ /// (3, "c"),
2549
+ /// (4, "d"),
2550
+ /// ]);
2551
+ ///
2552
+ /// let cursor = map.lower_bound(Bound::Included(&2));
2544
2553
/// assert_eq!(cursor.peek_prev(), Some((&1, &"a")));
2545
2554
/// assert_eq!(cursor.peek_next(), Some((&2, &"b")));
2546
- /// let cursor = a.lower_bound(Bound::Excluded(&2));
2555
+ ///
2556
+ /// let cursor = map.lower_bound(Bound::Excluded(&2));
2547
2557
/// assert_eq!(cursor.peek_prev(), Some((&2, &"b")));
2548
2558
/// assert_eq!(cursor.peek_next(), Some((&3, &"c")));
2559
+ ///
2560
+ /// let cursor = map.lower_bound(Bound::Unbounded);
2561
+ /// assert_eq!(cursor.peek_prev(), None);
2562
+ /// assert_eq!(cursor.peek_next(), Some((&1, &"a")));
2549
2563
/// ```
2550
2564
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
2551
2565
pub fn lower_bound < Q : ?Sized > ( & self , bound : Bound < & Q > ) -> Cursor < ' _ , K , V >
@@ -2561,11 +2575,17 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2561
2575
Cursor { current : Some ( edge) , root : self . root . as_ref ( ) }
2562
2576
}
2563
2577
2564
- /// Returns a [`CursorMut`] pointing to the first gap above the given bound.
2578
+ /// Returns a [`CursorMut`] pointing at the gap before the smallest key
2579
+ /// greater than the given bound.
2565
2580
///
2581
+ /// Passing [`Bound::Included(x)`] will return a cursor pointing to the
2582
+ /// gap before the smallest key greater than or equal to `x`.
2566
2583
///
2567
- /// Passing [`Bound::Unbounded`] will return a cursor pointing to the start
2568
- /// of the map.
2584
+ /// Passing [`Bound::Excluded(x)`] will return a cursor pointing to the
2585
+ /// gap before the smallest key greater than `x`.
2586
+ ///
2587
+ /// Passing [`Bound::Unbounded`] will return a cursor pointing to the
2588
+ /// gap before the smallest key in the map.
2569
2589
///
2570
2590
/// # Examples
2571
2591
///
@@ -2575,17 +2595,24 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2575
2595
/// use std::collections::BTreeMap;
2576
2596
/// use std::ops::Bound;
2577
2597
///
2578
- /// let mut a = BTreeMap::new();
2579
- /// a.insert(1, "a");
2580
- /// a.insert(2, "b");
2581
- /// a.insert(3, "c");
2582
- /// a.insert(4, "d");
2583
- /// let mut cursor = a.lower_bound_mut(Bound::Included(&2));
2598
+ /// let mut map = BTreeMap::from([
2599
+ /// (1, "a"),
2600
+ /// (2, "b"),
2601
+ /// (3, "c"),
2602
+ /// (4, "d"),
2603
+ /// ]);
2604
+ ///
2605
+ /// let mut cursor = map.lower_bound_mut(Bound::Included(&2));
2584
2606
/// assert_eq!(cursor.peek_prev(), Some((&1, &mut "a")));
2585
2607
/// assert_eq!(cursor.peek_next(), Some((&2, &mut "b")));
2586
- /// let mut cursor = a.lower_bound_mut(Bound::Excluded(&2));
2608
+ ///
2609
+ /// let mut cursor = map.lower_bound_mut(Bound::Excluded(&2));
2587
2610
/// assert_eq!(cursor.peek_prev(), Some((&2, &mut "b")));
2588
2611
/// assert_eq!(cursor.peek_next(), Some((&3, &mut "c")));
2612
+ ///
2613
+ /// let mut cursor = map.lower_bound_mut(Bound::Unbounded);
2614
+ /// assert_eq!(cursor.peek_prev(), None);
2615
+ /// assert_eq!(cursor.peek_next(), Some((&1, &mut "a")));
2589
2616
/// ```
2590
2617
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
2591
2618
pub fn lower_bound_mut < Q : ?Sized > ( & mut self , bound : Bound < & Q > ) -> CursorMut < ' _ , K , V , A >
@@ -2618,10 +2645,17 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2618
2645
}
2619
2646
}
2620
2647
2621
- /// Returns a [`Cursor`] pointing at the last gap below the given bound.
2648
+ /// Returns a [`Cursor`] pointing at the gap after the greatest key
2649
+ /// smaller than the given bound.
2622
2650
///
2623
- /// Passing [`Bound::Unbounded`] will return a cursor pointing to the end
2624
- /// of the map.
2651
+ /// Passing [`Bound::Included(x)`] will return a cursor pointing to the
2652
+ /// gap after the greatest key smaller than or equal to `x`.
2653
+ ///
2654
+ /// Passing [`Bound::Excluded(x)`] will return a cursor pointing to the
2655
+ /// gap after the greatest key smaller than `x`.
2656
+ ///
2657
+ /// Passing [`Bound::Unbounded`] will return a cursor pointing to the
2658
+ /// gap after the greatest key in the map.
2625
2659
///
2626
2660
/// # Examples
2627
2661
///
@@ -2631,17 +2665,24 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2631
2665
/// use std::collections::BTreeMap;
2632
2666
/// use std::ops::Bound;
2633
2667
///
2634
- /// let mut a = BTreeMap::new();
2635
- /// a.insert(1, "a");
2636
- /// a.insert(2, "b");
2637
- /// a.insert(3, "c");
2638
- /// a.insert(4, "d");
2639
- /// let cursor = a.upper_bound(Bound::Included(&3));
2640
- /// assert_eq!(cursor.peek_prev(), Some((&3, &"c")));
2641
- /// assert_eq!(cursor.peek_next(), Some((&4, &"d")));
2642
- /// let cursor = a.upper_bound(Bound::Excluded(&3));
2668
+ /// let map = BTreeMap::from([
2669
+ /// (1, "a"),
2670
+ /// (2, "b"),
2671
+ /// (3, "c"),
2672
+ /// (4, "d"),
2673
+ /// ]);
2674
+ ///
2675
+ /// let cursor = map.upper_bound(Bound::Included(&3));
2676
+ /// assert_eq!(cursor.peek_prev(), Some((&3, &"d")));
2677
+ /// assert_eq!(cursor.peek_next(), Some((&4, &"c")));
2678
+ ///
2679
+ /// let cursor = map.upper_bound(Bound::Excluded(&3));
2643
2680
/// assert_eq!(cursor.peek_prev(), Some((&2, &"b")));
2644
2681
/// assert_eq!(cursor.peek_next(), Some((&3, &"c")));
2682
+ ///
2683
+ /// let cursor = map.upper_bound(Bound::Unbounded);
2684
+ /// assert_eq!(cursor.peek_prev(), Some((&4, &"d")));
2685
+ /// assert_eq!(cursor.peek_next(), None);
2645
2686
/// ```
2646
2687
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
2647
2688
pub fn upper_bound < Q : ?Sized > ( & self , bound : Bound < & Q > ) -> Cursor < ' _ , K , V >
@@ -2657,10 +2698,17 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2657
2698
Cursor { current : Some ( edge) , root : self . root . as_ref ( ) }
2658
2699
}
2659
2700
2660
- /// Returns a [`CursorMut`] pointing at the last gap below the given bound.
2701
+ /// Returns a [`CursorMut`] pointing at the gap after the greatest key
2702
+ /// smaller than the given bound.
2661
2703
///
2662
- /// Passing [`Bound::Unbounded`] will return a cursor pointing to the end
2663
- /// of the map.
2704
+ /// Passing [`Bound::Included(x)`] will return a cursor pointing to the
2705
+ /// gap after the greatest key smaller than or equal to `x`.
2706
+ ///
2707
+ /// Passing [`Bound::Excluded(x)`] will return a cursor pointing to the
2708
+ /// gap after the greatest key smaller than `x`.
2709
+ ///
2710
+ /// Passing [`Bound::Unbounded`] will return a cursor pointing to the
2711
+ /// gap after the greatest key in the map.
2664
2712
///
2665
2713
/// # Examples
2666
2714
///
@@ -2670,17 +2718,24 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
2670
2718
/// use std::collections::BTreeMap;
2671
2719
/// use std::ops::Bound;
2672
2720
///
2673
- /// let mut a = BTreeMap::new();
2674
- /// a.insert(1, "a");
2675
- /// a.insert(2, "b");
2676
- /// a.insert(3, "c");
2677
- /// a.insert(4, "d");
2678
- /// let mut cursor = a.upper_bound_mut(Bound::Included(&3));
2721
+ /// let mut map = BTreeMap::from([
2722
+ /// (1, "a"),
2723
+ /// (2, "b"),
2724
+ /// (3, "c"),
2725
+ /// (4, "d"),
2726
+ /// ]);
2727
+ ///
2728
+ /// let mut cursor = map.upper_bound_mut(Bound::Included(&3));
2679
2729
/// assert_eq!(cursor.peek_prev(), Some((&3, &mut "c")));
2680
2730
/// assert_eq!(cursor.peek_next(), Some((&4, &mut "d")));
2681
- /// let mut cursor = a.upper_bound_mut(Bound::Excluded(&3));
2731
+ ///
2732
+ /// let mut cursor = map.upper_bound_mut(Bound::Excluded(&3));
2682
2733
/// assert_eq!(cursor.peek_prev(), Some((&2, &mut "b")));
2683
2734
/// assert_eq!(cursor.peek_next(), Some((&3, &mut "c")));
2735
+ ///
2736
+ /// let mut cursor = map.upper_bound_mut(Bound::Unbounded);
2737
+ /// assert_eq!(cursor.peek_prev(), Some((&4, &mut "d")));
2738
+ /// assert_eq!(cursor.peek_next(), None);
2684
2739
/// ```
2685
2740
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
2686
2741
pub fn upper_bound_mut < Q : ?Sized > ( & mut self , bound : Bound < & Q > ) -> CursorMut < ' _ , K , V , A >
@@ -3040,8 +3095,8 @@ impl<'a, K, V, A> CursorMutKey<'a, K, V, A> {
3040
3095
3041
3096
// Now the tree editing operations
3042
3097
impl < ' a , K : Ord , V , A : Allocator + Clone > CursorMutKey < ' a , K , V , A > {
3043
- /// Inserts a new element into the `BTreeMap` in the gap that the
3044
- /// `CursorMutKey` is currently pointing to.
3098
+ /// Inserts a new key-value pair into the map in the gap that the
3099
+ /// cursor is currently pointing to.
3045
3100
///
3046
3101
/// After the insertion the cursor will be pointing at the gap before the
3047
3102
/// newly inserted element.
@@ -3083,8 +3138,8 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMutKey<'a, K, V, A> {
3083
3138
* self . length += 1 ;
3084
3139
}
3085
3140
3086
- /// Inserts a new element into the `BTreeMap` in the gap that the
3087
- /// `CursorMutKey` is currently pointing to.
3141
+ /// Inserts a new key-value pair into the map in the gap that the
3142
+ /// cursor is currently pointing to.
3088
3143
///
3089
3144
/// After the insertion the cursor will be pointing at the gap after the
3090
3145
/// newly inserted element.
@@ -3129,19 +3184,16 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMutKey<'a, K, V, A> {
3129
3184
* self . length += 1 ;
3130
3185
}
3131
3186
3132
- /// Inserts a new element into the `BTreeMap` in the gap that the
3133
- /// `CursorMutKey` is currently pointing to.
3187
+ /// Inserts a new key-value pair into the map in the gap that the
3188
+ /// cursor is currently pointing to.
3134
3189
///
3135
3190
/// After the insertion the cursor will be pointing at the gap before the
3136
3191
/// newly inserted element.
3137
3192
///
3138
- /// # Panics
3139
- ///
3140
- /// This function panics if:
3141
- /// - the given key compares less than or equal to the current element (if
3142
- /// any).
3143
- /// - the given key compares greater than or equal to the next element (if
3144
- /// any).
3193
+ /// If the inserted key is not greater than the key before the cursor
3194
+ /// (if any), or if it not less than the key after the cursor (if any),
3195
+ /// then an [`UnorderedKeyError`] is returned since this would
3196
+ /// invalidate the [`Ord`] invariant between the keys of the map.
3145
3197
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
3146
3198
pub fn insert_after ( & mut self , key : K , value : V ) -> Result < ( ) , UnorderedKeyError > {
3147
3199
if let Some ( ( prev, _) ) = self . peek_prev ( ) {
@@ -3160,19 +3212,16 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMutKey<'a, K, V, A> {
3160
3212
Ok ( ( ) )
3161
3213
}
3162
3214
3163
- /// Inserts a new element into the `BTreeMap` in the gap that the
3164
- /// `CursorMutKey` is currently pointing to.
3215
+ /// Inserts a new key-value pair into the map in the gap that the
3216
+ /// cursor is currently pointing to.
3165
3217
///
3166
3218
/// After the insertion the cursor will be pointing at the gap after the
3167
3219
/// newly inserted element.
3168
3220
///
3169
- /// # Panics
3170
- ///
3171
- /// This function panics if:
3172
- /// - the given key compares greater than or equal to the current element
3173
- /// (if any).
3174
- /// - the given key compares less than or equal to the previous element (if
3175
- /// any).
3221
+ /// If the inserted key is not greater than the key before the cursor
3222
+ /// (if any), or if it not less than the key after the cursor (if any),
3223
+ /// then an [`UnorderedKeyError`] is returned since this would
3224
+ /// invalidate the [`Ord`] invariant between the keys of the map.
3176
3225
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
3177
3226
pub fn insert_before ( & mut self , key : K , value : V ) -> Result < ( ) , UnorderedKeyError > {
3178
3227
if let Some ( ( prev, _) ) = self . peek_prev ( ) {
@@ -3239,10 +3288,10 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMutKey<'a, K, V, A> {
3239
3288
}
3240
3289
3241
3290
impl < ' a , K : Ord , V , A : Allocator + Clone > CursorMut < ' a , K , V , A > {
3242
- /// Inserts a new element into the `BTreeMap` in the gap that the
3243
- /// `CursorMut` is currently pointing to.
3291
+ /// Inserts a new key-value pair into the map in the gap that the
3292
+ /// cursor is currently pointing to.
3244
3293
///
3245
- /// After the insertion the cursor will be pointing at the gap before the
3294
+ /// After the insertion the cursor will be pointing at the gap after the
3246
3295
/// newly inserted element.
3247
3296
///
3248
3297
/// # Safety
@@ -3257,8 +3306,8 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
3257
3306
unsafe { self . inner . insert_after_unchecked ( key, value) }
3258
3307
}
3259
3308
3260
- /// Inserts a new element into the `BTreeMap` in the gap that the
3261
- /// `CursorMut` is currently pointing to.
3309
+ /// Inserts a new key-value pair into the map in the gap that the
3310
+ /// cursor is currently pointing to.
3262
3311
///
3263
3312
/// After the insertion the cursor will be pointing at the gap after the
3264
3313
/// newly inserted element.
@@ -3275,37 +3324,31 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
3275
3324
unsafe { self . inner . insert_before_unchecked ( key, value) }
3276
3325
}
3277
3326
3278
- /// Inserts a new element into the `BTreeMap` in the gap that the
3279
- /// `CursorMut` is currently pointing to.
3327
+ /// Inserts a new key-value pair into the map in the gap that the
3328
+ /// cursor is currently pointing to.
3280
3329
///
3281
3330
/// After the insertion the cursor will be pointing at the gap before the
3282
3331
/// newly inserted element.
3283
3332
///
3284
- /// # Panics
3285
- ///
3286
- /// This function panics if:
3287
- /// - the given key compares less than or equal to the current element (if
3288
- /// any).
3289
- /// - the given key compares greater than or equal to the next element (if
3290
- /// any).
3333
+ /// If the inserted key is not greater than the key before the cursor
3334
+ /// (if any), or if it not less than the key after the cursor (if any),
3335
+ /// then an [`UnorderedKeyError`] is returned since this would
3336
+ /// invalidate the [`Ord`] invariant between the keys of the map.
3291
3337
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
3292
3338
pub fn insert_after ( & mut self , key : K , value : V ) -> Result < ( ) , UnorderedKeyError > {
3293
3339
self . inner . insert_after ( key, value)
3294
3340
}
3295
3341
3296
- /// Inserts a new element into the `BTreeMap` in the gap that the
3297
- /// `CursorMut` is currently pointing to.
3342
+ /// Inserts a new key-value pair into the map in the gap that the
3343
+ /// cursor is currently pointing to.
3298
3344
///
3299
3345
/// After the insertion the cursor will be pointing at the gap after the
3300
3346
/// newly inserted element.
3301
3347
///
3302
- /// # Panics
3303
- ///
3304
- /// This function panics if:
3305
- /// - the given key compares greater than or equal to the current element
3306
- /// (if any).
3307
- /// - the given key compares less than or equal to the previous element (if
3308
- /// any).
3348
+ /// If the inserted key is not greater than the key before the cursor
3349
+ /// (if any), or if it not less than the key after the cursor (if any),
3350
+ /// then an [`UnorderedKeyError`] is returned since this would
3351
+ /// invalidate the [`Ord`] invariant between the keys of the map.
3309
3352
#[ unstable( feature = "btree_cursors" , issue = "107540" ) ]
3310
3353
pub fn insert_before ( & mut self , key : K , value : V ) -> Result < ( ) , UnorderedKeyError > {
3311
3354
self . inner . insert_before ( key, value)
0 commit comments