BTreeMap first/last: simplify implementations

This commit is contained in:
Stein Somers 2020-04-04 20:32:07 +02:00
parent af89eb5e5b
commit bdbe56ecb8
1 changed files with 16 additions and 38 deletions

View File

@ -653,11 +653,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// assert_eq!(map.first_key_value(), Some((&1, &"b"))); /// assert_eq!(map.first_key_value(), Some((&1, &"b")));
/// ``` /// ```
#[unstable(feature = "map_first_last", issue = "62924")] #[unstable(feature = "map_first_last", issue = "62924")]
pub fn first_key_value<T: ?Sized>(&self) -> Option<(&K, &V)> pub fn first_key_value(&self) -> Option<(&K, &V)> {
where
T: Ord,
K: Borrow<T>,
{
let front = self.root.as_ref()?.as_ref().first_leaf_edge(); let front = self.root.as_ref()?.as_ref().first_leaf_edge();
front.right_kv().ok().map(Handle::into_kv) front.right_kv().ok().map(Handle::into_kv)
} }
@ -682,21 +678,14 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// } /// }
/// ``` /// ```
#[unstable(feature = "map_first_last", issue = "62924")] #[unstable(feature = "map_first_last", issue = "62924")]
pub fn first_entry<T: ?Sized>(&mut self) -> Option<OccupiedEntry<'_, K, V>> pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
where
T: Ord,
K: Borrow<T>,
{
let front = self.root.as_mut()?.as_mut().first_leaf_edge(); let front = self.root.as_mut()?.as_mut().first_leaf_edge();
if let Ok(kv) = front.right_kv() { let kv = front.right_kv().ok()?;
Some(OccupiedEntry { Some(OccupiedEntry {
handle: kv.forget_node_type(), handle: kv.forget_node_type(),
length: &mut self.length, length: &mut self.length,
_marker: PhantomData, _marker: PhantomData,
}) })
} else {
None
}
} }
/// Returns the last key-value pair in the map. /// Returns the last key-value pair in the map.
@ -716,11 +705,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// assert_eq!(map.last_key_value(), Some((&2, &"a"))); /// assert_eq!(map.last_key_value(), Some((&2, &"a")));
/// ``` /// ```
#[unstable(feature = "map_first_last", issue = "62924")] #[unstable(feature = "map_first_last", issue = "62924")]
pub fn last_key_value<T: ?Sized>(&self) -> Option<(&K, &V)> pub fn last_key_value(&self) -> Option<(&K, &V)> {
where
T: Ord,
K: Borrow<T>,
{
let back = self.root.as_ref()?.as_ref().last_leaf_edge(); let back = self.root.as_ref()?.as_ref().last_leaf_edge();
back.left_kv().ok().map(Handle::into_kv) back.left_kv().ok().map(Handle::into_kv)
} }
@ -745,21 +730,14 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// } /// }
/// ``` /// ```
#[unstable(feature = "map_first_last", issue = "62924")] #[unstable(feature = "map_first_last", issue = "62924")]
pub fn last_entry<T: ?Sized>(&mut self) -> Option<OccupiedEntry<'_, K, V>> pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
where
T: Ord,
K: Borrow<T>,
{
let back = self.root.as_mut()?.as_mut().last_leaf_edge(); let back = self.root.as_mut()?.as_mut().last_leaf_edge();
if let Ok(kv) = back.left_kv() { let kv = back.left_kv().ok()?;
Some(OccupiedEntry { Some(OccupiedEntry {
handle: kv.forget_node_type(), handle: kv.forget_node_type(),
length: &mut self.length, length: &mut self.length,
_marker: PhantomData, _marker: PhantomData,
}) })
} else {
None
}
} }
/// Returns `true` if the map contains a value for the specified key. /// Returns `true` if the map contains a value for the specified key.