Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashMap: #or_default_entry and #or_insert_entry #601

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,38 @@ impl<'a, K, V, S, A: Allocator> Entry<'a, K, V, S, A> {
}
}

/// Ensures a value is in the entry by inserting the default if empty,
/// and returns an [`OccupiedEntry`].
///
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
///
/// // nonexistent key
/// let entry = map.entry("poneyland").or_insert_entry(3);
/// assert_eq!(entry.key(), &"poneyland");
/// assert_eq!(entry.get(), &3);
///
/// // existing key
/// let mut entry = map.entry("poneyland").or_insert_entry(10);
/// assert_eq!(entry.key(), &"poneyland");
/// assert_eq!(entry.get(), &3);
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn or_insert_entry(self, default: V) -> OccupiedEntry<'a, K, V, S, A>
where
K: Hash,
S: BuildHasher,
{
match self {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => entry.insert_entry(default),
}
}

/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
///
Expand Down Expand Up @@ -4328,6 +4360,39 @@ impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V
EntryRef::Vacant(entry) => entry.insert(Default::default()),
}
}

/// Ensures a value is in the entry by inserting the default value if empty,
/// and returns an [`OccupiedEntry`].
///
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
///
/// let mut map: HashMap<String, Option<u32>> = HashMap::new();
///
/// // nonexistent key
/// let entry = map.entry_ref("poneyland").or_default_entry();
/// assert_eq!(entry.key(), &"poneyland");
/// assert_eq!(entry.get(), &None);
///
/// // existing key
/// map.insert("horseland".to_string(), Some(3));
/// let entry = map.entry_ref("horseland").or_default_entry();
/// assert_eq!(entry.key(), &"horseland");
/// assert_eq!(entry.get(), &Some(3));
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn or_default_entry(self) -> OccupiedEntry<'a, K, V, S, A>
where
K: Hash + From<&'b Q>,
S: BuildHasher,
{
match self {
EntryRef::Occupied(entry) => entry,
EntryRef::Vacant(entry) => entry.insert_entry(Default::default()),
}
}
}

impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S, A> {
Expand Down
Loading