Skip to content

Commit

Permalink
Add LinkedHashMap::to_front and LinkedHashMap::to_back entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyren committed Apr 22, 2021
1 parent c59f5c1 commit 9b0ee5d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/linked_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,40 @@ where
}
}
}

/// If an entry with this key exists, move it to the front of the list and return a reference to
/// the value.
#[inline]
pub fn to_front<Q>(&mut self, k: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.raw_entry_mut().from_key(k) {
RawEntryMut::Occupied(mut occupied) => {
occupied.to_front();
Some(occupied.into_mut())
}
RawEntryMut::Vacant(_) => None,
}
}

/// If an entry with this key exists, move it to the back of the list and return a reference to
/// the value.
#[inline]
pub fn to_back<Q>(&mut self, k: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.raw_entry_mut().from_key(k) {
RawEntryMut::Occupied(mut occupied) => {
occupied.to_back();
Some(occupied.into_mut())
}
RawEntryMut::Vacant(_) => None,
}
}
}

impl<K, V, S> LinkedHashMap<K, V, S>
Expand Down

0 comments on commit 9b0ee5d

Please sign in to comment.