Struct BSSimpleList

Source
#[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,

Source

pub const fn new() -> Self

Creates a new, empty list.

§Example
let list = BSSimpleList::<i32>::new();
assert!(list.is_empty());
Source

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);
Source

pub fn pop_front(&mut self) -> Option<T>

Removes and returns the first element in the list.

§Returns

Some(TESBox<Node<T>>) if the list is not empty. None if the list is empty.

§Example
let mut list = BSSimpleList::new();
list.push_front(1);
assert!(list.pop_front().is_some());
Source

pub fn front(&self) -> Option<&T>

Returns a reference to the front element.

§Returns

None if the list is empty.

§Example
let list = BSSimpleList::<i32>::new();
assert!(list.front().is_none());
Source

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());
Source

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);
Source

pub fn insert_after( &mut self, pos: &mut Node<T>, value: T, ) -> Option<&mut Node<T>>

Inserts a new value after the given node.

§Returns

A mutable reference to the inserted node, or None if insertion failed.

§Example
let mut list = BSSimpleList::new();
let mut first = Node::new(1, None);
list.insert_after(&mut first, 2);
Source

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);
Source

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));
Source

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));
Source

pub fn clear(&mut self)

Clears the entire list.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the list.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns a mutable iterator over the list.

Source

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>.

Source

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>
where T: Display + Zeroable,

Source

pub fn print_tree(&self)

Prints the list as a tree-like structure

Trait Implementations§

Source§

impl<T> Clone for BSSimpleList<T>
where T: Clone + Zeroable,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for BSSimpleList<T>
where T: Debug + Zeroable,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for BSSimpleList<T>
where T: Zeroable,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for BSSimpleList<T>
where T: Zeroable,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Extend<T> for &mut BSSimpleList<T>
where T: Zeroable,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> Extend<T> for BSSimpleList<T>
where T: Zeroable,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> Hash for BSSimpleList<T>
where T: Hash + Zeroable,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T> IntoIterator for &'a BSSimpleList<T>
where T: Zeroable,

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut BSSimpleList<T>
where T: Zeroable,

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for BSSimpleList<T>
where T: Zeroable,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Ord for BSSimpleList<T>
where T: Ord + Zeroable,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for BSSimpleList<T>
where T: PartialEq + Zeroable,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialOrd for BSSimpleList<T>
where T: PartialOrd + Zeroable,

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Eq for BSSimpleList<T>
where T: Eq + Zeroable,

Auto Trait Implementations§

§

impl<T> Freeze for BSSimpleList<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BSSimpleList<T>
where T: RefUnwindSafe,

§

impl<T> !Send for BSSimpleList<T>

§

impl<T> !Sync for BSSimpleList<T>

§

impl<T> Unpin for BSSimpleList<T>
where T: Unpin,

§

impl<T> UnwindSafe for BSSimpleList<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more