mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
feat(data_structures): add SparseStack::last_mut method (#7528)
Add `SparseStack::last_mut` method, as companion to `SparseStack::last`.
This commit is contained in:
parent
d5aaee732e
commit
defaf4bf2b
1 changed files with 15 additions and 0 deletions
|
|
@ -128,6 +128,21 @@ impl<T> SparseStack<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get value of last entry on the stack.
|
||||||
|
#[inline]
|
||||||
|
pub fn last_mut(&mut self) -> Option<&mut T> {
|
||||||
|
let has_value = *self.has_values.last();
|
||||||
|
if has_value {
|
||||||
|
debug_assert!(!self.values.is_empty());
|
||||||
|
// SAFETY: Last `self.has_values` is only `true` if there's a corresponding value in `self.values`.
|
||||||
|
// This invariant is maintained in `push`, `pop`, `take_last`, `last_or_init`, and `last_mut_or_init`.
|
||||||
|
let value = unsafe { self.values.last_mut_unchecked() };
|
||||||
|
Some(value)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Take value from last entry on the stack, leaving last entry empty.
|
/// Take value from last entry on the stack, leaving last entry empty.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn take_last(&mut self) -> Option<T> {
|
pub fn take_last(&mut self) -> Option<T> {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue