LinkedList

Struct LinkedList 

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

A doubly-linked list in which the liftime of iterator is independent from self.

The LinkedList allows pushing and popping elements at either end in constant time.

Implementations§

Source§

impl<T> LinkedList<T>

Source

pub fn new() -> Self

Creates an empty LinkedList.

§Examples
use atlist_rs::LinkedList;

let list: LinkedList<u32> = LinkedList::new();
Source

pub fn len(&self) -> usize

Returns the length of the LinkedList.

This operation should compute in O(1) time.

§Examples
use atlist_rs::LinkedList;

let mut dl = LinkedList::new();

let _ = dl.push_front(2);
assert_eq!(dl.len(), 1);

let _ = dl.push_front(1);
assert_eq!(dl.len(), 2);

let _ = dl.push_back(3);
assert_eq!(dl.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the LinkedList is empty.

This operation should compute in O(1) time.

§Examples
use atlist_rs::LinkedList;

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

let _ = dl.push_front("foo");
assert!(!dl.is_empty());
Source

pub fn clear(&mut self)

Removes all elements from the LinkedList.

This operation should compute in O(n) time.

§Examples
use atlist_rs::{LinkedList, LinkedListError};

let mut dl = LinkedList::new();

dl.push_front(2);
dl.push_front(1);
assert_eq!(dl.len(), 2);
assert_eq!(*dl.front().unwrap(), 1);

dl.clear();
assert_eq!(dl.len(), 0);
assert_eq!(dl.front(), Err(LinkedListError::Empty));
Source

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

Provides a forward iterator.

§Examples
use atlist_rs::LinkedList;

let mut list: LinkedList<u32> = LinkedList::new();

let _ = list.push_back(0);
let _ = list.push_back(1);
let _ = list.push_back(2);

let mut iter = list.iter();
assert_eq!(*iter.next().unwrap(), 0);
assert_eq!(*iter.next().unwrap(), 1);
assert_eq!(*iter.next().unwrap(), 2);
assert_eq!(iter.next(), None);
Source

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

Provides a forward iterator.

§Examples
use atlist_rs::LinkedList;
use std::cell::RefCell;

let mut list: LinkedList<RefCell<u32>> = LinkedList::new();

let _ = list.push_back(RefCell::new(0));
let _ = list.push_back(RefCell::new(1));
let _ = list.push_back(RefCell::new(2));

for element in list.iter_mut() {
    *(*element).borrow_mut() += 10;
}

let mut iter = list.iter_mut();
assert_eq!(*(*iter.next().unwrap()).borrow(), 10);
assert_eq!(*(*iter.next().unwrap()).borrow(), 11);
assert_eq!(*(*iter.next().unwrap()).borrow(), 12);
assert_eq!(iter.next(), None);
Source

pub fn iter_front(&self) -> Iter<T>

Provides a iterator at the front element.

The iterator is pointing to the “ghost” non-element if the list is empty.

Source

pub fn iter_back(&self) -> Iter<T>

Provides a iterator at the back element.

The iterator is pointing to the “ghost” non-element if the list is empty.

Source

pub fn iter_mut_front(&mut self) -> IterMut<T>

Provides a iterator with editing operations at the front element.

The iterator is pointing to the “ghost” non-element if the list is empty.

Source

pub fn iter_mut_back(&mut self) -> IterMut<T>

Provides a iterator with editing operations at the back element.

The iterator is pointing to the “ghost” non-element if the list is empty.

Source

pub fn push_front(&mut self, elt: T) -> LinkedListResult<()>

Adds an element first in the list.

This operation should compute in O(1) time.

Source

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

Removes the first element and returns it, or None if the list is empty.

This operation should compute in O(1) time.

Source

pub fn push_back(&mut self, elt: T) -> LinkedListResult<()>

Appends an element to the back of a list.

This operation should compute in O(1) time.

Source

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

Removes the last element from a list and returns it, or None if it is empty.

This operation should compute in O(1) time.

Source

pub fn front(&self) -> LinkedListResult<LinkedListItem<T>>

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

§Examples
use atlist_rs::{LinkedList, LinkedListError};

let mut dl = LinkedList::new();
assert_eq!(dl.front(), Err(LinkedListError::Empty));

let _ = dl.push_front(1);
assert_eq!(*dl.front().unwrap(), 1);
Source

pub fn back(&self) -> LinkedListResult<LinkedListItem<T>>

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

§Examples
use atlist_rs::{LinkedList, LinkedListError};

let mut dl = LinkedList::new();
assert_eq!(dl.back(), Err(LinkedListError::Empty));

let _ = dl.push_back(1);
assert_eq!(*dl.back().unwrap(), 1);
Source

pub fn contains_iter(&self, x: &Iter<T>) -> LinkedListResult<()>

Returns true if the LinkedList contains an element equal to the given value.

§Examples
use atlist_rs::{LinkedList, LinkedListError};

let mut list: LinkedList<u32> = LinkedList::new();
let mut another_list: LinkedList<u32> = LinkedList::new();

let _ = list.push_back(0);
let _ = list.push_back(1);
let _ = another_list.push_back(2);

assert_eq!(list.contains_iter(&list.iter()), Ok(()));
assert_eq!(list.contains_iter(&another_list.iter()), Err(LinkedListError::IteratorNotInList));
Source

pub fn contains_iter_mut(&self, x: &IterMut<T>) -> LinkedListResult<()>

Returns true if the LinkedList contains an element equal to the given value.

§Examples
use atlist_rs::{LinkedList, LinkedListError};

let mut list: LinkedList<u32> = LinkedList::new();
let mut another_list: LinkedList<u32> = LinkedList::new();

let _ = list.push_back(0);
let _ = list.push_back(1);
let _ = another_list.push_back(2);

let iter = list.iter_mut();
let another_iter = another_list.iter_mut();
assert_eq!(list.contains_iter_mut(&iter), Ok(()));
assert_eq!(list.contains_iter_mut(&another_iter), Err(LinkedListError::IteratorNotInList));
Source

pub fn insert_before( &mut self, iter: &IterMut<T>, elt: T, ) -> LinkedListResult<IterMut<T>>

Inserts a new element into the LinkedList before the current one.

If the cursor is pointing at the “ghost” non-element then the new element is inserted at the end of the LinkedList.

Returns the iterator of inserted value if success, or error if failed

Source

pub fn insert_after( &mut self, iter: &IterMut<T>, elt: T, ) -> LinkedListResult<IterMut<T>>

Inserts a new element into the LinkedList after the current one.

If the cursor is pointing at the “ghost” non-element then the new element is inserted at the front of the LinkedList.

Returns the iterator of inserted value if success, or error if failed

Source

pub fn remove_iter_mut( &mut self, iter: &mut IterMut<T>, ) -> LinkedListResult<LinkedListItem<T>>

Removes the current iterator from the LinkedList.

The element that was removed is returned, the iterator will point to the “ghost” non-element.

If the iterator is currently pointing to the “ghost” non-element then no element is removed and Err(LinkedListError::IteratorNotInList) is returned.

Trait Implementations§

Source§

impl<T: Clone> Clone for LinkedList<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for LinkedList<T>

Source§

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

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

impl<T> Default for LinkedList<T>

Source§

fn default() -> Self

Creates an empty LinkedList<T>.

Source§

impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T>

Source§

fn extend<I: IntoIterator<Item = &'a 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 LinkedList<T>

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> FromIterator<T> for LinkedList<T>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for LinkedList<T>

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<T> IntoIterator for &LinkedList<T>

Source§

type Item = Arc<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<T>

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

fn into_iter(self) -> Iter<T>

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for &mut LinkedList<T>

Source§

type Item = Arc<T>

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<T>

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

fn into_iter(self) -> IterMut<T>

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for LinkedList<T>

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> PartialEq for LinkedList<T>

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> PartialOrd for LinkedList<T>

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> Eq for LinkedList<T>

Auto Trait Implementations§

§

impl<T> Freeze for LinkedList<T>

§

impl<T> RefUnwindSafe for LinkedList<T>

§

impl<T> Send for LinkedList<T>
where T: Sync + Send,

§

impl<T> Sync for LinkedList<T>
where T: Sync + Send,

§

impl<T> Unpin for LinkedList<T>

§

impl<T> UnwindSafe for LinkedList<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, 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.