Struct SkipLinkedList

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

§SkipLinkedList

SkipLinkedList is a skiplist-backed linked-list that supports fast random access. The (amortized) time complexity is O(log n) for both reads and writes, regardless of the position. It is more efficient than Vec and Linkedlist for large list that requires lots of random access.

§Examples

let mut list = skip_linked_list::SkipLinkedList::new();

list.push_front(1);
list.push_back(2);
list.insert(1, 3);
list.insert(1, 4);
list.insert(1, 5);
// current list is: [1, 5, 4, 3, 2]

assert_eq!(list.get(1), Some(&5));
assert_eq!(list.get(3), Some(&3));
assert_eq!(list.remove(2), 4);

Implementations§

Source§

impl<T> SkipLinkedList<T>

Source

pub fn new() -> Self

Creates a new list.

Source

pub fn insert(&mut self, i: usize, elem: T)

Inserts an element at position index within the list, shifting all elements after it to the right.

§Examples
let mut list = skip_linked_list::SkipLinkedList::new();
list.insert(0, 10);
list.insert(1, 30);
list.insert(1, 20);
assert_eq!(list.into_iter().collect::<Vec<i32>>(), vec![10, 20, 30]);
§Panics

Panics if i > len.

Source

pub fn get(&self, i: usize) -> Option<&T>

Gets the element at position index within the list.

§Examples
let mut list = skip_linked_list::SkipLinkedList::new();
list.insert(0, 10);
assert_eq!(list.get(0), Some(&10));
assert_eq!(list.get(1), None);
Source

pub fn remove(&mut self, i: usize) -> T

Removes an element at position index within the list, shifting all elements after it to the left.

§Examples
let mut list = skip_linked_list::SkipLinkedList::new();
list.insert(0, 10);
list.insert(1, 20);
assert_eq!(list.remove(0), 10);
assert_eq!(list.remove(0), 20);
§Panics

Panics if i >= len.

Source

pub fn len(&self) -> usize

Returns the length of the list.

Source

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

Inserts an element at the start of the list.

Source

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

Inserts an element at the end of the list.

Source

pub fn pop_front(&mut self) -> T

Removes an element at the start of the list.

§Panics

Panics if list is empty.

Source

pub fn pop_back(&mut self) -> T

Removes an element at the end of the list.

§Panics

Panics if list is empty.

Source

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

Returns an iterator over the list.

Source

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

Returns an mut iterator over the list.

Source

pub fn into_iter(self) -> IntoIter<T>

Consumes the list into an iterator.

Source§

impl<T> SkipLinkedList<T>
where T: Display,

Source

pub fn visualize(&self)

Prints the internals of the list.

Trait Implementations§

Source§

impl<T> Drop for SkipLinkedList<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SkipLinkedList<T>

§

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

§

impl<T> !Send for SkipLinkedList<T>

§

impl<T> !Sync for SkipLinkedList<T>

§

impl<T> Unpin for SkipLinkedList<T>

§

impl<T> UnwindSafe for SkipLinkedList<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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V