#[repr(C)]pub struct BSSimpleList<T>where
T: Zeroable,{ /* private fields */ }
Expand description
Single-directional linked list (stack-like).
This linked list behaves like a stack, where the most recently pushed value is always placed at the head. The previous head is stored on the heap as the “next” value, forming a chain of nodes.
Diagram of the linked list structure (Head -> Next -> Next…):
head -> [value] -> [next] -> [next] -> ... -> None
↑
latest push
§Note
This linked list assumes that a value is considered uninitialized when it is zero-initialized. Using it without zero initialization is dangerous.
Ideally, an Option
should be used to indicate whether a value is initialized, but this risk persists due to the strict memory layout requirements imposed by the FFI (Foreign Function Interface) type.
§Example
let mut list = BSSimpleList::new();
list.push_front(10);
list.push_front(20);
list.push_front(30);
list.push_front(40);
list.print_tree();
assert_eq!(list.len(), 4);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&40));
assert_eq!(iter.next(), Some(&30));
assert_eq!(iter.next(), Some(&20));
assert_eq!(iter.next(), Some(&10));
list.pop_front();
assert_eq!(list.len(), 3);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&30));
assert_eq!(iter.next(), Some(&20));
assert_eq!(iter.next(), Some(&10));
Implementations§
Source§impl<T> BSSimpleList<T>where
T: Zeroable,
impl<T> BSSimpleList<T>where
T: Zeroable,
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty list.
§Example
let list = BSSimpleList::<i32>::new();
assert!(list.is_empty());
Sourcepub fn push_front(&mut self, value: T)
pub fn push_front(&mut self, value: T)
Pushes a new value to the front of the list.
§Example
let mut list = BSSimpleList::new();
list.push_front(10);
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if the list is empty.
§Example
let mut list = BSSimpleList::<i32>::new();
assert!(list.is_empty());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the list.
Note that the computation cost is O(n)
since it is a linked list.
§Example
let mut list = BSSimpleList::new();
list.push_front(1); // 2
list.push_front(2); // 1
list.push_front(3); // 0
assert_eq!(list.len(), 3);
Sourcepub fn insert_after(
&mut self,
pos: &mut Node<T>,
value: T,
) -> Option<&mut Node<T>>
pub fn insert_after( &mut self, pos: &mut Node<T>, value: T, ) -> Option<&mut Node<T>>
Sourcepub const fn erase_after(&mut self, pos: &mut Node<T>)
pub const fn erase_after(&mut self, pos: &mut Node<T>)
Removes the node after the given position.
§Example
use commonlibsse_ng::re::BSTList::{BSSimpleList, Node};
let mut list = BSSimpleList::new();
let mut first = Node::new(1, None);
list.insert_after(&mut first, 2);
list.erase_after(&mut first);
Sourcepub fn get(&self, pos: usize) -> Option<&T>
pub fn get(&self, pos: usize) -> Option<&T>
Returns a reference to the node at the given position.
§Example
let mut list = BSSimpleList::new();
list.push_front(1); // 3
list.push_front(2); // 2
list.push_front(3); // 1
list.push_front(4); // 0
assert_eq!(list.get(1), Some(&3));
Sourcepub fn get_mut(&mut self, pos: usize) -> Option<&mut T>
pub fn get_mut(&mut self, pos: usize) -> Option<&mut T>
Returns a mutable reference to the node at the given position.
§Example
let mut list = BSSimpleList::new();
list.push_front(1); // 3
list.push_front(2); // 2
list.push_front(3); // 1
list.push_front(4); // 0
if let Some(value) = list.get_mut(2) {
*value = 5;
}
assert_eq!(list.get(2), Some(&5));
Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Consumes the SimpleList
and returns a Vec<T>
containing all elements in order.
This method traverses the linked list and collects the values into a Vec<T>
.
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 list to the given length.
- If the list is shorter, it will be extended with the given value.
- If the list is longer, it will be truncated.
§Example
- If length is 0, nothing is done.
let mut list = BSSimpleList::<i32>::new();
list.resize(3, 0);
assert_eq!(list.len(), 0);
- If the list is longer, it will be truncated.
let mut list = BSSimpleList::<i32>::new();
list.push_front(1);
list.push_front(2);
list.push_front(3);
list.push_front(4);
list.resize(3, 0);
assert_eq!(list.len(), 3);
- If the list is shorter, it will be extended with the given value.
let mut list = BSSimpleList::<i32>::new();
list.push_front(1);
list.push_front(2);
list.resize(4, 5);
assert_eq!(list.len(), 4);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&5));
assert_eq!(iter.next(), Some(&5));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
Source§impl<T> BSSimpleList<T>
impl<T> BSSimpleList<T>
Sourcepub fn print_tree(&self)
pub fn print_tree(&self)
Prints the list as a tree-like structure
Trait Implementations§
Source§impl<T> Clone for BSSimpleList<T>
impl<T> Clone for BSSimpleList<T>
Source§impl<T> Debug for BSSimpleList<T>
impl<T> Debug for BSSimpleList<T>
Source§impl<T> Default for BSSimpleList<T>where
T: Zeroable,
impl<T> Default for BSSimpleList<T>where
T: Zeroable,
Source§impl<T> Drop for BSSimpleList<T>where
T: Zeroable,
impl<T> Drop for BSSimpleList<T>where
T: Zeroable,
Source§impl<T> Extend<T> for &mut BSSimpleList<T>where
T: Zeroable,
impl<T> Extend<T> for &mut BSSimpleList<T>where
T: Zeroable,
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
)Source§impl<T> Extend<T> for BSSimpleList<T>where
T: Zeroable,
impl<T> Extend<T> for BSSimpleList<T>where
T: Zeroable,
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
)