Skip to content

Implement get_pair for HashMap #46992

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions src/liballoc/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,35 @@ impl<K: Ord, V> BTreeMap<K, V> {
}
}

/// Returns a references to the key-value pair corresponding to the supplied
/// key.
///
/// The supplied key may be any borrowed form of the map's key type, but the ordering
/// on the borrowed form *must* match the ordering on the key type.
///
/// # Examples
///
/// ```
/// #![feature(map_get_pair)]
/// use std::collections::BTreeMap;
///
/// let mut map = BTreeMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get_pair(&1), Some((&1, &"a")));
/// assert_eq!(map.get_pair(&2), None);
/// ```
#[unstable(feature = "map_get_pair", issue = "43143")]
#[inline]
pub fn get_pair<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>,
Q: Ord
{
match search::search_tree(self.root.as_ref(), key) {
Found(handle) => Some(handle.into_kv()),
GoDown(_) => None,
}
}

/// Returns `true` if the map contains a value for the specified key.
///
/// The key may be any borrowed form of the map's key type, but the ordering
Expand Down
30 changes: 30 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,36 @@ impl<K, V, S> HashMap<K, V, S>
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1)
}

/// Returns a references to the key-value pair corresponding to the supplied
/// key.
///
/// The supplied key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the key type.
///
/// [`Eq`]: ../../std/cmp/trait.Eq.html
/// [`Hash`]: ../../std/hash/trait.Hash.html
///
/// # Examples
///
/// ```
/// #![feature(map_get_pair)]
/// use std::collections::HashMap;
///
/// let mut map = HashMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get_pair(&1), Some((&1, &"a")));
/// assert_eq!(map.get_pair(&2), None);
/// ```
#[unstable(feature = "map_get_pair", issue = "43143")]
#[inline]
pub fn get_pair<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>,
Q: Hash + Eq
{
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs())
}

/// Returns true if the map contains a value for the specified key.
///
/// The key may be any borrowed form of the map's key type, but
Expand Down