Skip to content

Commit ae53f8b

Browse files
committed
Auto merge of #98 - Amanieu:set_entry, r=Amanieu
Add entry-like methods to HashSet This is ported from rust-lang/rust#60894
2 parents e1f6b0e + b3167df commit ae53f8b

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/map.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1257,11 +1257,13 @@ pub struct RawEntryBuilderMut<'a, K, V, S> {
12571257
///
12581258
/// This is a lower-level version of [`Entry`].
12591259
///
1260-
/// This `enum` is constructed from the [`raw_entry`] method on [`HashMap`].
1260+
/// This `enum` is constructed through the [`raw_entry_mut`] method on [`HashMap`],
1261+
/// then calling one of the methods of that [`RawEntryBuilderMut`].
12611262
///
12621263
/// [`HashMap`]: struct.HashMap.html
12631264
/// [`Entry`]: enum.Entry.html
1264-
/// [`raw_entry`]: struct.HashMap.html#method.raw_entry
1265+
/// [`raw_entry_mut`]: struct.HashMap.html#method.raw_entry_mut
1266+
/// [`RawEntryBuilderMut`]: struct.RawEntryBuilderMut.html
12651267
pub enum RawEntryMut<'a, K, V, S> {
12661268
/// An occupied entry.
12671269
Occupied(RawOccupiedEntryMut<'a, K, V>),

src/set.rs

+59
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,65 @@ where
601601
self.map.get_key_value(value).map(|(k, _)| k)
602602
}
603603

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+
604663
/// Returns `true` if `self` has no elements in common with `other`.
605664
/// This is equivalent to checking for an empty intersection.
606665
///

0 commit comments

Comments
 (0)