pub struct LinkedListView<'a, T> { /* private fields */ }
Expand description

An immutable slice of a linked list.

Note that this is not a slice in the std::slice sense in terms of the memory layout. Nevertheless, it corresponds to a subset of elements of an original linked list, which itself is a linked list, due to the recursiveness of the data structure.

Implementations§

source§

impl<'a, T> LinkedListView<'a, T>

source

pub fn empty() -> Self

Creates a new empty linked list slice.

source

pub fn len(&self) -> usize

Returns the length of the linked list.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();
assert_eq!(0, list.len());

list.push_back('a');
list.push_front('b');
assert_eq!(2, list.len());

_ = list.pop_back();
assert_eq!(1, list.len());
source

pub fn is_empty(&self) -> bool

Returns true if the LinkedList is empty.

This operation should compute in O(1) time.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();
assert!(list.is_empty());

list.push_front('a');
list.push_front('b');
assert!(!list.is_empty());

_ = list.pop_back();
assert!(!list.is_empty());

list.clear();
assert!(list.is_empty());
source

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

Provides a reference to the back element, or None if the list is empty.

This operation should compute in O(1) time.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();
assert_eq!(list.back(), None);

list.push_back(42);
assert_eq!(list.back(), Some(&42));

list.push_front(1);
assert_eq!(list.back(), Some(&42));

list.push_back(7);
assert_eq!(list.back(), Some(&7));
source

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

Provides a reference to the front element, or None if the list is empty.

This operation should compute in O(1) time.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();
assert_eq!(list.front(), None);

list.push_front(42);
assert_eq!(list.front(), Some(&42));

list.push_back(1);
assert_eq!(list.front(), Some(&42));

list.push_front(7);
assert_eq!(list.front(), Some(&7));
source

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

Provides a forward iterator; which starts from the front-most element to the back.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();

list.push_back(1);
list.push_back(2);
list.push_front(0);
list.push_back(3);

let mut iter = list.iter();
assert_eq!(iter.next(), Some(&0));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), None);
source

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

Provides a backward iterator; which starts from the back-most element to the front.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();

list.push_back(1);
list.push_back(2);
list.push_front(0);
list.push_back(3);

let mut iter = list.iter_from_back();
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&0));
assert_eq!(iter.next(), None);
source

pub fn get_at(&self, at: usize) -> Option<&T>

Returns a reference to element at the at position starting from the front; None when at is out of bounds.

This operation requires O(n) time.

Examples
use orx_linked_list::*;

let mut list = LinkedList::new();

// build linked list: a <-> b <-> c
list.push_back('b');
list.push_front('a');
list.push_back('c');

assert_eq!(Some(&'a'), list.get_at(0));
assert_eq!(Some(&'b'), list.get_at(1));
assert_eq!(Some(&'c'), list.get_at(2));
assert_eq!(None, list.get_at(3));
source

pub fn collect(&self) -> LinkedList<'a, T>
where T: Clone,

Collects the LinkedListSlice into a new LinkedList.

Example
use orx_linked_list::*;

let list: LinkedList<_> = (0..6).collect();
assert_eq!(list, &[0, 1, 2, 3, 4, 5]);

let (_, b) = list.split_after(&1).unwrap();
assert_eq!(b, &[2, 3, 4, 5]); // b: LinkedListSlice is a view of the original list

let mut new_list = b.collect(); // new_list: LinkedList is a new list built from b
assert_eq!(new_list, &[2, 3, 4, 5]);

new_list.pop_back();
assert_eq!(new_list, &[2, 3, 4]);

assert_eq!(list, &[0, 1, 2, 3, 4, 5]);
assert_eq!(b, &[2, 3, 4, 5]);
source§

impl<'a, T: PartialEq> LinkedListView<'a, T>

source

pub fn contains(&self, value: &T) -> bool

Returns whether or not the given value is in the list.

Example
use orx_linked_list::*;

let mut list = LinkedList::new();

assert!(!list.contains(&'a'));

list.push_back('a');
assert!(list.contains(&'a'));
source

pub fn index_of(&self, value: &T) -> Option<usize>

Returns the index of the given value from the front of the list if it exists; None otherwise.

Example
use orx_linked_list::*;

let mut list = LinkedList::new();

assert!(list.index_of(&'a').is_none());

list.push_back('a');
list.push_back('b');
list.push_front('c');
list.push_front('d');
assert_eq!(Some(2), list.index_of(&'a'));
source

pub fn from_back_index_of(&self, value: &T) -> Option<usize>

Returns the index of the given value from the back of the list if it exists; None otherwise.

Example
use orx_linked_list::*;

let mut list = LinkedList::new();

assert!(list.from_back_index_of(&'a').is_none());

list.push_back('a');
list.push_back('b');
list.push_front('c');
list.push_front('d');
assert_eq!(Some(1), list.from_back_index_of(&'a'));

Trait Implementations§

source§

impl<'a, T: Debug> Debug for LinkedListView<'a, T>

source§

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

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

impl<'a, T> Default for LinkedListView<'a, T>

source§

fn default() -> Self

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

impl<'a, T: PartialEq> PartialEq<[T]> for LinkedListView<'a, T>

source§

fn eq(&self, other: &[T]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T: PartialEq> PartialEq<LinkedList<'a, T>> for LinkedListView<'a, T>

source§

fn eq(&self, other: &LinkedList<'a, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for [T]

source§

fn eq(&self, other: &LinkedListView<'a, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for LinkedList<'a, T>

source§

fn eq(&self, other: &LinkedListView<'a, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T: PartialEq, S: AsRef<[T]>> PartialEq<S> for LinkedListView<'a, T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T: PartialEq> PartialEq for LinkedListView<'a, T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'a, T> RefUnwindSafe for LinkedListView<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for LinkedListView<'a, T>
where T: Sync,

§

impl<'a, T> Sync for LinkedListView<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for LinkedListView<'a, T>

§

impl<'a, T> UnwindSafe for LinkedListView<'a, T>
where T: RefUnwindSafe,

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

§

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

§

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.