List

Struct List 

Source
pub struct List<T> { /* private fields */ }
Expand description

List is a doubly linked list stored in one contiguous allocation.

§Features

  • O(1) insert and remove both at front and back.
  • O(1) insert anywhere if you have a cursor to that position.
  • Only use of unsafe is an unavoidable use for IterMut.

§Implementation

It is similar to a linked list in a language like C, except instead of pointers we use indices into a backing vector.

The list is just a vector, and indices to the head and tail:

struct List<T> {
    /// Head, Tail
    link: [usize; 2],
    nodes: Vec<Node<T>>,
}

The list node is represented like this:

struct Node<T> {
    /// Prev, Next.
    link: [usize; 2],
    value: T,
}

The link arrays contain the vector indices of the previous and next node. We use an array so that symmetries in front/back or prev/next can be used easily in the code — it’s nice if we can write just one push and one pop method instead of two.

There is a constant to denote a “null” index, and that’s usize’s max value. We don’t always have to check for this case, we can just access the nodes vector using .get() or .get_mut(); a “null” link is the None case.

§To do

List could be generic over the index type, so that internal prev/node links can use less space than a regular pointer (can be u16 or u32 index).

With some cleanup we can use unchecked indexing — but it’s not guaranteed to make any difference.

Implementations§

Source§

impl<T> List<T>

Source

pub fn new() -> Self

Create a new List.

Source

pub fn with_capacity(cap: usize) -> Self

Create a new List with specified capacity.

Source

pub fn len(&self) -> usize

Return the number of elements in the List.

Source

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

Return an iterator.

Source

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

Return an iterator.

Source

pub fn cursor(&mut self) -> Cursor<'_, T>

Return a new cursor, focused before the head of the List.

Source

pub fn push_front(&mut self, value: T)

Insert an element at the beginning of the List.

Source

pub fn push_back(&mut self, value: T)

Insert an element at the end of the List.

Source

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

Remove the element at the beginning of the List and return it, or return None if the List is empty.

Source

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

Remove the element at the end of the List and return it, or return None if the List is empty.

Source

pub fn linearize(&mut self)

Reorder internal datastructure into traversal order.

Trait Implementations§

Source§

impl<T: Clone> Clone for List<T>

Source§

fn clone(&self) -> List<T>

Returns a duplicate 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> Debug for List<T>

Source§

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

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

impl<'a, T> Extend<T> for List<T>

Source§

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

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<'a, T> FromIterator<T> for List<T>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for List<T>

§

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

§

impl<T> Send for List<T>
where T: Send,

§

impl<T> Sync for List<T>
where T: Sync,

§

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

§

impl<T> UnwindSafe for List<T>
where T: UnwindSafe,

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, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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