Struct orx_linked_list::LinkedListView
source · 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>
impl<'a, T> LinkedListView<'a, T>
sourcepub fn len(&self) -> usize
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());sourcepub fn is_empty(&self) -> bool
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());sourcepub fn back(&self) -> Option<&T>
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));sourcepub fn front(&self) -> Option<&T>
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));sourcepub fn iter(&self) -> Iter<'a, T, IterFromFront>
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);sourcepub fn iter_from_back(&self) -> Iter<'_, T, IterFromBack>
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);sourcepub fn get_at(&self, at: usize) -> Option<&T>
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));sourcepub fn collect(&self) -> LinkedList<'a, T>where
T: Clone,
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>
impl<'a, T: PartialEq> LinkedListView<'a, T>
sourcepub fn contains(&self, value: &T) -> bool
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'));sourcepub fn index_of(&self, value: &T) -> Option<usize>
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'));sourcepub fn from_back_index_of(&self, value: &T) -> Option<usize>
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>
impl<'a, T: Debug> Debug for LinkedListView<'a, T>
source§impl<'a, T> Default for LinkedListView<'a, T>
impl<'a, T> Default for LinkedListView<'a, T>
source§impl<'a, T: PartialEq> PartialEq<LinkedList<'a, T>> for LinkedListView<'a, T>
impl<'a, T: PartialEq> PartialEq<LinkedList<'a, T>> for LinkedListView<'a, T>
source§fn eq(&self, other: &LinkedList<'a, T>) -> bool
fn eq(&self, other: &LinkedList<'a, T>) -> bool
self and other values to be equal, and is used
by ==.source§impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for [T]
impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for [T]
source§fn eq(&self, other: &LinkedListView<'a, T>) -> bool
fn eq(&self, other: &LinkedListView<'a, T>) -> bool
self and other values to be equal, and is used
by ==.source§impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for LinkedList<'a, T>
impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for LinkedList<'a, T>
source§fn eq(&self, other: &LinkedListView<'a, T>) -> bool
fn eq(&self, other: &LinkedListView<'a, T>) -> bool
self and other values to be equal, and is used
by ==.