@@ -194,16 +194,16 @@ pub struct Difference<'a, T: 'a> {
194
194
#[ derive( Debug ) ]
195
195
enum DifferenceInner < ' a , T : ' a > {
196
196
Stitch {
197
- // iterate all of self and some of other, spotting matches along the way
197
+ // iterate all of ` self` and some of ` other` , spotting matches along the way
198
198
self_iter : Iter < ' a , T > ,
199
199
other_iter : Peekable < Iter < ' a , T > > ,
200
200
} ,
201
201
Search {
202
- // iterate a small set , look up in the large set
202
+ // iterate `self` , look up in `other`
203
203
self_iter : Iter < ' a , T > ,
204
204
other_set : & ' a BTreeSet < T > ,
205
205
} ,
206
- Iterate ( Iter < ' a , T > ) , // simply stream self's elements
206
+ Iterate ( Iter < ' a , T > ) , // simply produce all values in `self`
207
207
}
208
208
209
209
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
@@ -356,7 +356,7 @@ impl<T: Ord> BTreeSet<T> {
356
356
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
357
357
pub fn difference < ' a > ( & ' a self , other : & ' a BTreeSet < T > ) -> Difference < ' a , T > {
358
358
let ( self_min, self_max) = if let ( Some ( self_min) , Some ( self_max) ) =
359
- ( self . iter ( ) . next ( ) , self . iter ( ) . next_back ( ) )
359
+ ( self . first ( ) , self . last ( ) )
360
360
{
361
361
( self_min, self_max)
362
362
} else {
@@ -365,7 +365,7 @@ impl<T: Ord> BTreeSet<T> {
365
365
} ;
366
366
} ;
367
367
let ( other_min, other_max) = if let ( Some ( other_min) , Some ( other_max) ) =
368
- ( other. iter ( ) . next ( ) , other. iter ( ) . next_back ( ) )
368
+ ( other. first ( ) , other. last ( ) )
369
369
{
370
370
( other_min, other_max)
371
371
} else {
@@ -450,7 +450,7 @@ impl<T: Ord> BTreeSet<T> {
450
450
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
451
451
pub fn intersection < ' a > ( & ' a self , other : & ' a BTreeSet < T > ) -> Intersection < ' a , T > {
452
452
let ( self_min, self_max) = if let ( Some ( self_min) , Some ( self_max) ) =
453
- ( self . iter ( ) . next ( ) , self . iter ( ) . next_back ( ) )
453
+ ( self . first ( ) , self . last ( ) )
454
454
{
455
455
( self_min, self_max)
456
456
} else {
@@ -459,7 +459,7 @@ impl<T: Ord> BTreeSet<T> {
459
459
} ;
460
460
} ;
461
461
let ( other_min, other_max) = if let ( Some ( other_min) , Some ( other_max) ) =
462
- ( other. iter ( ) . next ( ) , other. iter ( ) . next_back ( ) )
462
+ ( other. first ( ) , other. last ( ) )
463
463
{
464
464
( other_min, other_max)
465
465
} else {
@@ -625,14 +625,14 @@ impl<T: Ord> BTreeSet<T> {
625
625
return false ;
626
626
}
627
627
let ( self_min, self_max) = if let ( Some ( self_min) , Some ( self_max) ) =
628
- ( self . iter ( ) . next ( ) , self . iter ( ) . next_back ( ) )
628
+ ( self . first ( ) , self . last ( ) )
629
629
{
630
630
( self_min, self_max)
631
631
} else {
632
632
return true ; // self is empty
633
633
} ;
634
634
let ( other_min, other_max) = if let ( Some ( other_min) , Some ( other_max) ) =
635
- ( other. iter ( ) . next ( ) , other. iter ( ) . next_back ( ) )
635
+ ( other. first ( ) , other. last ( ) )
636
636
{
637
637
( other_min, other_max)
638
638
} else {
@@ -654,14 +654,12 @@ impl<T: Ord> BTreeSet<T> {
654
654
Less => ( ) ,
655
655
}
656
656
if self_iter. len ( ) <= other. len ( ) / ITER_PERFORMANCE_TIPPING_SIZE_DIFF {
657
- // Big difference in number of elements.
658
657
for next in self_iter {
659
658
if !other. contains ( next) {
660
659
return false ;
661
660
}
662
661
}
663
662
} else {
664
- // Self is not much smaller than other set.
665
663
let mut other_iter = other. iter ( ) ;
666
664
other_iter. next ( ) ;
667
665
other_iter. next_back ( ) ;
@@ -702,6 +700,96 @@ impl<T: Ord> BTreeSet<T> {
702
700
other. is_subset ( self )
703
701
}
704
702
703
+ /// Returns a reference to the first value in the set, if any.
704
+ /// This value is always the minimum of all values in the set.
705
+ ///
706
+ /// # Examples
707
+ ///
708
+ /// Basic usage:
709
+ ///
710
+ /// ```
711
+ /// #![feature(map_first_last)]
712
+ /// use std::collections::BTreeSet;
713
+ ///
714
+ /// let mut map = BTreeSet::new();
715
+ /// assert_eq!(map.first(), None);
716
+ /// map.insert(1);
717
+ /// assert_eq!(map.first(), Some(&1));
718
+ /// map.insert(2);
719
+ /// assert_eq!(map.first(), Some(&1));
720
+ /// ```
721
+ #[ unstable( feature = "map_first_last" , issue = "62924" ) ]
722
+ pub fn first ( & self ) -> Option < & T > {
723
+ self . map . first_key_value ( ) . map ( |( k, _) | k)
724
+ }
725
+
726
+ /// Returns a reference to the last value in the set, if any.
727
+ /// This value is always the maximum of all values in the set.
728
+ ///
729
+ /// # Examples
730
+ ///
731
+ /// Basic usage:
732
+ ///
733
+ /// ```
734
+ /// #![feature(map_first_last)]
735
+ /// use std::collections::BTreeSet;
736
+ ///
737
+ /// let mut map = BTreeSet::new();
738
+ /// assert_eq!(map.first(), None);
739
+ /// map.insert(1);
740
+ /// assert_eq!(map.last(), Some(&1));
741
+ /// map.insert(2);
742
+ /// assert_eq!(map.last(), Some(&2));
743
+ /// ```
744
+ #[ unstable( feature = "map_first_last" , issue = "62924" ) ]
745
+ pub fn last ( & self ) -> Option < & T > {
746
+ self . map . last_key_value ( ) . map ( |( k, _) | k)
747
+ }
748
+
749
+ /// Removes the first value from the set and returns it, if any.
750
+ /// The first value is always the minimum value in the set.
751
+ ///
752
+ /// # Examples
753
+ ///
754
+ /// ```
755
+ /// #![feature(map_first_last)]
756
+ /// use std::collections::BTreeSet;
757
+ ///
758
+ /// let mut set = BTreeSet::new();
759
+ ///
760
+ /// set.insert(1);
761
+ /// while let Some(n) = set.pop_first() {
762
+ /// assert_eq!(n, 1);
763
+ /// }
764
+ /// assert!(set.is_empty());
765
+ /// ```
766
+ #[ unstable( feature = "map_first_last" , issue = "62924" ) ]
767
+ pub fn pop_first ( & mut self ) -> Option < T > {
768
+ self . map . first_entry ( ) . map ( |entry| entry. remove_entry ( ) . 0 )
769
+ }
770
+
771
+ /// Removes the last value from the set and returns it, if any.
772
+ /// The last value is always the maximum value in the set.
773
+ ///
774
+ /// # Examples
775
+ ///
776
+ /// ```
777
+ /// #![feature(map_first_last)]
778
+ /// use std::collections::BTreeSet;
779
+ ///
780
+ /// let mut set = BTreeSet::new();
781
+ ///
782
+ /// set.insert(1);
783
+ /// while let Some(n) = set.pop_last() {
784
+ /// assert_eq!(n, 1);
785
+ /// }
786
+ /// assert!(set.is_empty());
787
+ /// ```
788
+ #[ unstable( feature = "map_first_last" , issue = "62924" ) ]
789
+ pub fn pop_last ( & mut self ) -> Option < T > {
790
+ self . map . last_entry ( ) . map ( |entry| entry. remove_entry ( ) . 0 )
791
+ }
792
+
705
793
/// Adds a value to the set.
706
794
///
707
795
/// If the set did not have this value present, `true` is returned.
0 commit comments