mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(transformer): add SparseStack::with_capacity method (#6094)
Add `SparseStack::with_capacity` method.
This commit is contained in:
parent
85aff19514
commit
e60ce506ea
3 changed files with 56 additions and 4 deletions
|
|
@ -93,7 +93,6 @@ impl<T> NonEmptyStack<T> {
|
|||
/// * `capacity` must not be 0.
|
||||
/// * `capacity` must not exceed [`Self::MAX_CAPACITY`].
|
||||
#[inline]
|
||||
#[cfg_attr(not(test), expect(dead_code))]
|
||||
pub fn with_capacity(capacity: usize, initial_value: T) -> Self {
|
||||
assert!(capacity > 0, "`capacity` cannot be zero");
|
||||
assert!(capacity <= Self::MAX_CAPACITY, "`capacity` must not exceed `Self::MAX_CAPACITY`");
|
||||
|
|
@ -352,7 +351,6 @@ impl<T> NonEmptyStack<T> {
|
|||
|
||||
/// Get capacity.
|
||||
#[inline]
|
||||
#[cfg_attr(not(test), expect(dead_code))]
|
||||
pub fn capacity(&self) -> usize {
|
||||
// SAFETY: `self.start` and `self.end` are both derived from same pointer
|
||||
// (in `new_with_capacity_bytes_unchecked` and `push_slow`).
|
||||
|
|
|
|||
|
|
@ -31,6 +31,22 @@ pub struct SparseStack<T> {
|
|||
}
|
||||
|
||||
impl<T> SparseStack<T> {
|
||||
/// Maximum capacity for entries (either `Some` or `None`).
|
||||
///
|
||||
/// Effectively unlimited on 64-bit systems.
|
||||
#[expect(dead_code)]
|
||||
pub const MAX_TOTAL_CAPACITY: usize = NonEmptyStack::<bool>::MAX_CAPACITY;
|
||||
|
||||
/// Maximum capacity for filled entries (`Some`).
|
||||
///
|
||||
/// Unless `size_of::<T>() == 1`, `MAX_FILLED_CAPACITY` is lower than [`MAX_TOTAL_CAPACITY`].
|
||||
///
|
||||
/// Both are effectively unlimited on 64-bit systems.
|
||||
///
|
||||
/// [`MAX_TOTAL_CAPACITY`]: Self::MAX_TOTAL_CAPACITY
|
||||
#[expect(dead_code)]
|
||||
pub const MAX_FILLED_CAPACITY: usize = Stack::<T>::MAX_CAPACITY;
|
||||
|
||||
/// Create new `SparseStack`.
|
||||
///
|
||||
/// # Panics
|
||||
|
|
@ -42,6 +58,26 @@ impl<T> SparseStack<T> {
|
|||
Self { has_values: NonEmptyStack::new(false), values: Stack::new() }
|
||||
}
|
||||
|
||||
/// Create new `SparseStack` with pre-allocated capacity.
|
||||
///
|
||||
/// * `total_capacity` is capacity for any entries (either `Some` or `None`). Cannot be 0.
|
||||
/// * `filled_capacity` is capacity for full entries (`Some`).
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if any of these requirements are not satisfied:
|
||||
/// * `T` must not be a zero-sized type.
|
||||
/// * `total_capacity` must not be 0.
|
||||
/// * `total_capacity` must not exceed `Self::MAX_TOTAL_CAPACITY`.
|
||||
/// * `filled_capacity` must not exceed `Self::MAX_FILLED_CAPACITY`.
|
||||
#[inline]
|
||||
#[expect(dead_code)]
|
||||
pub fn with_capacity(total_capacity: usize, filled_capacity: usize) -> Self {
|
||||
Self {
|
||||
has_values: NonEmptyStack::with_capacity(total_capacity, false),
|
||||
values: Stack::with_capacity(filled_capacity),
|
||||
}
|
||||
}
|
||||
|
||||
/// Push an entry to the stack.
|
||||
#[inline]
|
||||
pub fn push(&mut self, value: Option<T>) {
|
||||
|
|
@ -150,4 +186,24 @@ impl<T> SparseStack<T> {
|
|||
pub fn len(&self) -> usize {
|
||||
self.has_values.len()
|
||||
}
|
||||
|
||||
/// Get capacity of stack for any entries (either `Some` or `None`).
|
||||
///
|
||||
/// Capacity is always at least 1. Stack is never empty.
|
||||
#[inline]
|
||||
#[expect(dead_code)]
|
||||
pub fn total_capacity(&self) -> usize {
|
||||
self.has_values.capacity()
|
||||
}
|
||||
|
||||
/// Get capacity of stack for filled entries (`Some`).
|
||||
///
|
||||
/// The capacity can be zero (unlike [`total_capacity`]).
|
||||
///
|
||||
/// [`total_capacity`]: Self::total_capacity
|
||||
#[inline]
|
||||
#[expect(dead_code)]
|
||||
pub fn filled_capacity(&self) -> usize {
|
||||
self.values.capacity()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,7 +73,6 @@ impl<T> Stack<T> {
|
|||
/// * `T` must not be a zero-sized type.
|
||||
/// * `capacity` must not exceed [`Self::MAX_CAPACITY`].
|
||||
#[inline]
|
||||
#[cfg_attr(not(test), expect(dead_code))]
|
||||
pub fn with_capacity(capacity: usize) -> Self {
|
||||
if capacity == 0 {
|
||||
Self::new()
|
||||
|
|
@ -374,7 +373,6 @@ impl<T> Stack<T> {
|
|||
|
||||
/// Get capacity.
|
||||
#[inline]
|
||||
#[cfg_attr(not(test), expect(dead_code))]
|
||||
pub fn capacity(&self) -> usize {
|
||||
// SAFETY: `self.start` and `self.end` are both derived from same pointer
|
||||
// (in `new`, `new_with_capacity_bytes_unchecked` and `push_slow`).
|
||||
|
|
|
|||
Loading…
Reference in a new issue