Struct intrusive_containers::linked_list::LinkedList
[−]
[src]
pub struct LinkedList<P, T, S, L> where T: OwningPointer<Target=S>, L: Linkable<Container=T> {
// some fields omitted
}
An intrusive doubly-linked list
Methods
impl<P, T, S, L> LinkedList<P, T, S, L> where T: OwningPointer<Target=S>, S: Node<P, L>, L: Linkable<Container=T>
fn new() -> LinkedList<P, T, S, L>
Creates an empty LinkedList
fn append(&mut self, other: &mut LinkedList<P, T, S, L>)
Moves all elements from other
to the end of the list.
This reuses all the nodes from other
and moves them into self
. After
this operation, other
becomes empty.
This operation should compute in O(1) time and O(1) memory.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut a = LinkedList::new(); let mut b = LinkedList::new(); a.push_back(Box::new(MyI32::new(1))); a.push_back(Box::new(MyI32::new(2))); b.push_back(Box::new(MyI32::new(3))); b.push_back(Box::new(MyI32::new(4))); a.append(&mut b); for e in a.iter() { println!("{}", e); // prints 1, then 2, then 3, then 4 } println!("{}", b.len()); // prints 0
fn iter<'a>(&'a self) -> Iter<'a, P, S, L>
Provides a forward iterator.
fn into_iter(self) -> IntoIter<P, T, S, L>
Consumes the list into an iterator yielding elements by value.
fn is_empty(&self) -> bool
Returns true
if the LinkedList
is empty
This operation should compute in O(1) time
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); assert!(dl.is_empty()); dl.push_front(Box::new(MyI32::new(1))); assert!(!dl.is_empty());
fn len(&self) -> usize
Returns the length of the LinkedList
.
This operation should compute in O(1) time.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); dl.push_front(Box::new(MyI32::new(2))); assert_eq!(dl.len(), 1); dl.push_front(Box::new(MyI32::new(1))); assert_eq!(dl.len(), 2); dl.push_back(Box::new(MyI32::new(3))); assert_eq!(dl.len(), 3);
fn clear(&mut self)
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); dl.push_front(Box::new(MyI32::new(2))); dl.push_front(Box::new(MyI32::new(1))); assert_eq!(dl.len(), 2); assert_eq!(dl.front(), Some(&1)); dl.clear(); assert_eq!(dl.len(), 0); assert_eq!(dl.front(), None);
fn front(&self) -> Option<&P>
Provides a reference to the front element, or None
if the list is
empty.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); assert_eq!(dl.front(), None); dl.push_front(Box::new(MyI32::new(1))); assert_eq!(dl.front(), Some(&1));
fn front_mut(&mut self) -> Option<&mut P>
Provides a mutable reference to the front element, or None
if the list
is empty.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); assert_eq!(dl.front(), None); dl.push_front(Box::new(MyI32::new(1))); assert_eq!(dl.front(), Some(&1)); match dl.front_mut() { None => {}, Some(x) => *x = 5, } assert_eq!(dl.front(), Some(&5));
fn back(&self) -> Option<&P>
Provides a reference to the back element, or None
if the list is
empty.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); assert_eq!(dl.back(), None); dl.push_back(Box::new(MyI32::new(1))); assert_eq!(dl.back(), Some(&1));
fn back_mut(&mut self) -> Option<&mut P>
Provides a mutable reference to the back element, or None
if the list
is empty.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut dl = LinkedList::new(); assert_eq!(dl.back(), None); dl.push_back(Box::new(MyI32::new(1))); assert_eq!(dl.back(), Some(&1)); match unsafe {dl.back_mut()} { None => {}, Some(x) => *x = 5, } assert_eq!(dl.back(), Some(&5));
fn push_front(&mut self, elt: T)
Adds an element first in the list.
This operation should compute in O(1) time.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut d = LinkedList::new(); assert_eq!(d.pop_front(), None); d.push_front(Box::new(MyI32::new(1))); d.push_front(Box::new(MyI32::new(3))); assert_eq!(d.pop_front(), Some(Box::new(MyI32::new(3)))); assert_eq!(d.pop_front(), Some(Box::new(MyI32::new(1)))); assert_eq!(d.pop_front(), None);
fn pop_front(&mut self) -> Option<T>
Removes the first element and returns it, or None
if the list is
empty.
This operation should compute in O(1) time.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut d = LinkedList::new(); assert_eq!(d.pop_front(), None); d.push_front(Box::new(MyI32::new(1))); d.push_front(Box::new(MyI32::new(3))); assert_eq!(d.pop_front(), Some(Box::new(MyI32::new(3)))); assert_eq!(d.pop_front(), Some(Box::new(MyI32::new(1)))); assert_eq!(d.pop_front(), None);
fn push_back(&mut self, elt: T)
Appends an element to the back of a list
This operation should compute in O(1) time.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut d = LinkedList::new(); d.push_back(Box::new(MyI32::new(1))); d.push_back(Box::new(MyI32::new(3))); assert_eq!(&3, d.back().unwrap());
fn pop_back(&mut self) -> Option<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
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut d = LinkedList::new(); assert_eq!(d.pop_back(), None); d.push_back(Box::new(MyI32::new(1))); d.push_back(Box::new(MyI32::new(3))); assert_eq!(d.pop_back(), Some(Box::new(MyI32::new(3))));
fn split_off(&mut self, at: usize) -> LinkedList<P, T, S, L>
Splits the list into two at the given index. Returns everything after the given index, including the index.
Panics
Panics if at > len
.
This operation should compute in O(n) time.
Examples
use intrusive_containers::LinkedList; define_list_element!(MyI32 = i32 : MyLink); let mut d = LinkedList::new(); d.push_front(Box::new(MyI32::new(1))); d.push_front(Box::new(MyI32::new(2))); d.push_front(Box::new(MyI32::new(3))); let mut splitted = d.split_off(2); assert_eq!(splitted.pop_front(), Some(Box::new(MyI32::new(1)))); assert_eq!(splitted.pop_front(), None);
impl<'a, P, T, S, L> LinkedList<P, T, S, L> where T: OwningPointer<Target=S> + 'a, S: Node<P, L> + 'a, L: Linkable<Container=T> + 'a
fn iter_mut(&'a mut self) -> IterMut<'a, P, T, S, L>
Provides a forward iterator with mutable references
This operation is marked unsafe because it would be possible to use
mem::replace
which would invalidate the links