#[repr(C)]pub struct BSTSmallArray<T, const N: usize = 1, A = TESGlobalAlloc>where
A: SelflessAllocator,{ /* private fields */ }
Expand description
Use stack while within the specified size, and use heap if it is larger.
This is the same purpose as smallvec
crate and other optimizations, except that the memory layout is for TES.
It is effective when most of the memory can be fit in the stack, except for some exceptions, but it slows down the process if there are frequent fallbacks to the heap.
-
N
: element length (not bytes size)
§Examples
let mut array = BSTSmallArray::<i32, 1>::new();
array.push(1); // push on stack.
array.push(2); // change to heap.
assert_eq!(array[0], 1);
assert_eq!(array[1], 2);
assert_eq!(array.len(), 2);
array.clear(); // reuse heap. grow to capacity 4.
assert_eq!(array.len(), 0);
assert_eq!(array.capacity(), 4); // Capacity is preserved
Implementations§
Source§impl<T, const N: usize, A> BSTSmallArray<T, N, A>where
A: SelflessAllocator,
impl<T, const N: usize, A> BSTSmallArray<T, N, A>where
A: SelflessAllocator,
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty BSTSmallArray<T, N, A>
with the specified allocator.
The array will not allocate until elements are pushed.
§Example
let array = BSTSmallArray::<i32, 10>::new();
assert!(array.is_empty());
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the array.
This is also referred to as the array’s “length”.
§Example
let array = BSTSmallArray::<i32, 10>::new();
assert_eq!(array.len(), 0);
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if the array contains no elements.
§Example
let array = BSTSmallArray::<i32, 10>::new();
assert!(array.is_empty());
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the total number of elements the array can hold without reallocating.
This is the allocated capacity, which may be larger than the current length.
§Example
let mut array = BSTSmallArray::<i32, 10>::new();
assert!(array.capacity() == 10); // stack size
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the array as much as possible.
It will drop any excess capacity not used by the current elements.
§Examples
let mut array = BSTSmallArray::<_, 4>::new(); // auto i32
array.push(1);
array.push(2);
array.push(3);
array.push(4);
array.push(5); // change to heap
assert_eq!(array.len(), 5);
array.shrink_to_fit();
assert!(array.capacity() == array.len());
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from the array and returns it, or None
if it’s empty.
§Example
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(1);
assert_eq!(array.pop(), Some(1));
assert_eq!(array.pop(), None);
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at the given index, if it exists.
§Example
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(42);
assert_eq!(array.get(0), Some(&42));
assert_eq!(array.get(1), None);
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index, if it exists.
§Example
let mut array = BSTSmallArray::<i32, 1>::new();
array.push(10);
if let Some(x) = array.get_mut(0) {
*x += 1;
}
assert_eq!(array[0], 11);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the array, removing all elements but preserving the capacity.
§Examples
let mut array = BSTSmallArray::<i32, 1>::new();
array.push(1); // push on stack.
array.push(2); // change to heap.
assert_eq!(array.len(), 2);
array.clear(); // reuse heap.
assert_eq!(array.len(), 0);
assert_eq!(array.capacity(), 4); // Capacity is preserved
Sourcepub fn as_non_null_ptr(&mut self) -> Option<NonNull<T>>
pub fn as_non_null_ptr(&mut self) -> Option<NonNull<T>>
Returns a non null pointer of the array’s buffer.
Sourcepub fn as_const_non_null_ptr(&self) -> Option<ConstNonNull<T>>
pub fn as_const_non_null_ptr(&self) -> Option<ConstNonNull<T>>
Returns a non null pointer of the array’s buffer.
Sourcepub fn contains(&self, value: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, value: &T) -> boolwhere
T: PartialEq,
Checks if the array contains the given element.
Returns true
if the element is present in the array, and false
otherwise.
§Examples
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(1);
array.push(2);
assert!(array.contains(&1));
assert!(!array.contains(&3));
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements that satisfy the predicate.
This method takes a closure that accepts an element of the array and returns a boolean.
Elements for which the closure returns true
will be kept, while elements for which
it returns false
will be removed.
§Examples
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(1);
array.push(2);
array.push(3);
array.retain(|&x| x > 1);
assert_eq!(array.len(), 2);
assert!(array.contains(&2));
assert!(array.contains(&3));
§Panics
array ptr is null
Sourcepub fn resize(&mut self, new_size: usize, value: T)where
T: Clone,
pub fn resize(&mut self, new_size: usize, value: T)where
T: Clone,
Resizes the array to the specified length.
If the array is resized to a larger length, the new elements will be initialized
using the default constructor for T
. If the array is resized to a smaller length,
elements at the end will be dropped.
§Examples
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(1);
array.push(2);
array.resize(5, 0);
assert_eq!(array.len(), 5);
assert_eq!(array[3], 0);
Sourcepub fn drain<R>(&mut self, range: R) -> BSTDrain<'_, T, N, A>where
R: RangeBounds<usize>,
pub fn drain<R>(&mut self, range: R) -> BSTDrain<'_, T, N, A>where
R: RangeBounds<usize>,
Removes a range of elements from the array, returning them as a vector.
This method removes the elements within the specified range and returns them as
a Vec<T>
. The range must be within the bounds of the array.
§Examples
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(1);
array.push(2);
array.push(3);
array.push(4);
array.push(5);
let drained = array.drain(0..2);
assert_eq!(drained.collect::<Vec<_>>(), vec![1, 2]);
assert_eq!(array.len(), 3);
assert_eq!(array[0], 3);
assert_eq!(array[1], 4);
assert_eq!(array[2], 5);
§Panics
Panics if the range is out of bounds.
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice of all elements in the array.
Sourcepub const fn iter(&self) -> BSTSmallArrayIterator<'_, T, N, A>
pub const fn iter(&self) -> BSTSmallArrayIterator<'_, T, N, A>
Returns an iterator over the elements of the array.
This iterator yields references to the elements in the array.
§Examples
let mut array = BSTSmallArray::<i32, 10>::new();
array.push(1);
array.push(2);
let sum: i32 = array.iter().sum();
assert_eq!(sum, 3);
Trait Implementations§
Source§impl<T: Clone, const N: usize, A> Clone for BSTSmallArray<T, N, A>where
A: SelflessAllocator + Clone,
impl<T: Clone, const N: usize, A> Clone for BSTSmallArray<T, N, A>where
A: SelflessAllocator + Clone,
Source§impl<T, const N: usize, A> Debug for BSTSmallArray<T, N, A>where
T: Debug,
A: SelflessAllocator,
impl<T, const N: usize, A> Debug for BSTSmallArray<T, N, A>where
T: Debug,
A: SelflessAllocator,
Source§impl<T, const N: usize> Default for BSTSmallArray<T, N>
impl<T, const N: usize> Default for BSTSmallArray<T, N>
Source§impl<T, const N: usize, A> Drop for BSTSmallArray<T, N, A>where
A: SelflessAllocator,
impl<T, const N: usize, A> Drop for BSTSmallArray<T, N, A>where
A: SelflessAllocator,
Source§impl<T, const N: usize, A> Extend<T> for BSTSmallArray<T, N, A>where
A: SelflessAllocator,
impl<T, const N: usize, A> Extend<T> for BSTSmallArray<T, N, A>where
A: SelflessAllocator,
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)