@@ -601,6 +601,65 @@ where
601
601
self . map . get_key_value ( value) . map ( |( k, _) | k)
602
602
}
603
603
604
+ /// Inserts the given `value` into the set if it is not present, then
605
+ /// returns a reference to the value in the set.
606
+ ///
607
+ /// # Examples
608
+ ///
609
+ /// ```
610
+ /// use hashbrown::HashSet;
611
+ ///
612
+ /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
613
+ /// assert_eq!(set.len(), 3);
614
+ /// assert_eq!(set.get_or_insert(2), &2);
615
+ /// assert_eq!(set.get_or_insert(100), &100);
616
+ /// assert_eq!(set.len(), 4); // 100 was inserted
617
+ /// ```
618
+ #[ inline]
619
+ pub fn get_or_insert ( & mut self , value : T ) -> & T {
620
+ // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
621
+ // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
622
+ self . map
623
+ . raw_entry_mut ( )
624
+ . from_key ( & value)
625
+ . or_insert ( value, ( ) )
626
+ . 0
627
+ }
628
+
629
+ /// Inserts a value computed from `f` into the set if the given `value` is
630
+ /// not present, then returns a reference to the value in the set.
631
+ ///
632
+ /// # Examples
633
+ ///
634
+ /// ```
635
+ /// use hashbrown::HashSet;
636
+ ///
637
+ /// let mut set: HashSet<String> = ["cat", "dog", "horse"]
638
+ /// .iter().map(|&pet| pet.to_owned()).collect();
639
+ ///
640
+ /// assert_eq!(set.len(), 3);
641
+ /// for &pet in &["cat", "dog", "fish"] {
642
+ /// let value = set.get_or_insert_with(pet, str::to_owned);
643
+ /// assert_eq!(value, pet);
644
+ /// }
645
+ /// assert_eq!(set.len(), 4); // a new "fish" was inserted
646
+ /// ```
647
+ #[ inline]
648
+ pub fn get_or_insert_with < Q : ?Sized , F > ( & mut self , value : & Q , f : F ) -> & T
649
+ where
650
+ T : Borrow < Q > ,
651
+ Q : Hash + Eq ,
652
+ F : FnOnce ( & Q ) -> T ,
653
+ {
654
+ // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
655
+ // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
656
+ self . map
657
+ . raw_entry_mut ( )
658
+ . from_key ( value)
659
+ . or_insert_with ( || ( f ( value) , ( ) ) )
660
+ . 0
661
+ }
662
+
604
663
/// Returns `true` if `self` has no elements in common with `other`.
605
664
/// This is equivalent to checking for an empty intersection.
606
665
///
0 commit comments