Struct indexlist::IndexList [−][src]
pub struct IndexList<T> { /* fields omitted */ }
A doubly linked list, backed by a vector.
See the crate documentation for more.
Methods
impl<T> IndexList<T> where
T: PartialEq,
T: Debug,
[src]
impl<T> IndexList<T> where
T: PartialEq,
T: Debug,
pub fn new() -> IndexList<T>
[src]
pub fn new() -> IndexList<T>
Creates a new IndexList<T>
.
Examples
Making a new list:
extern crate indexlist; use indexlist::IndexList; let list: IndexList<i32> = IndexList::new();
pub fn with_capacity(size: usize) -> IndexList<T>
[src]
pub fn with_capacity(size: usize) -> IndexList<T>
Creates a new IndexList<T>
with a given capacity.
If you know roughly how many elements will be stored in the list, creating one with that capacity can reduce allocations, increasing performance.
Examples
Making a new list:
extern crate indexlist; use indexlist::IndexList; let list: IndexList<i32> = IndexList::with_capacity(100);
pub fn head(&self) -> Option<&T>
[src]
pub fn head(&self) -> Option<&T>
Returns a reference to the first item in the list.
Will return None
if the list is empty.
Examples
The first item is often the first one that's pushed on:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_back(5); assert_eq!(list.head(), Some(&5));
But of course, not always!
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_back(5); // this will append to the front, so it's now the head list.push_front(10); assert_eq!(list.head(), Some(&10));
pub fn push_back(&mut self, item: T) -> Index
[src]
pub fn push_back(&mut self, item: T) -> Index
Adds this item to the tail of the list.
Examples
Pushing several numbers into a list:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_back(5); list.push_back(10); list.push_back(15); // This prints 5, 10, and then 15, each on its own line for element in list.iter() { println!("{}", element); }
pub fn push_front(&mut self, item: T) -> Index
[src]
pub fn push_front(&mut self, item: T) -> Index
Adds this item to the head of the list.
Examples
Pushing several numbers into a list:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_front(5); list.push_front(10); list.push_front(15); // This prints 15, 10, and then 5, each on its own line for element in list.iter() { println!("{}", element); }
pub fn contains(&self, value: &T) -> bool
[src]
pub fn contains(&self, value: &T) -> bool
Does this list contain this element?
Returns true if it does, and false if it does not.
Examples
Checking both possibilities:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_back(5); // our list does contain five assert!(list.contains(&5)); // our list does not contain ten assert!(!list.contains(&10));
pub fn get(&self, index: Index) -> Option<&T>
[src]
pub fn get(&self, index: Index) -> Option<&T>
Returns the item at this index if it exists.
If there's an item at this index, then this will return a reference to
it. If not, returns None
.
Indexes are generational, and so this method will use the generation to
determine if this element exists. For more, see Index
's documentation.
Examples
Getting an element at an index:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); assert_eq!(list.get(five), Some(&5));
An element that doesn't exist returns None
:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); list.remove(five); assert!(list.get(five).is_none());
Generational indexes ensure that we don't access incorrect items:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); list.push_back(10); list.remove(five); // since we have a free spot, this will go where 5 was list.push_back(15); // our index is out of date, and so will not return 15 here assert!(list.get(five).is_none());
pub fn remove(&mut self, index: Index) -> Option<T>
[src]
pub fn remove(&mut self, index: Index) -> Option<T>
Removes the item at this index, and returns the removed item.
If there's an item at this index, then this will remove it from the list
and return it. If there isn't, it will return None
.
Indexes are generational, and so this method will use the generation to
determine if this element exists. For more, see Index
's documentation.
Examples
Removing an element from an index:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); let five = list.remove(five); assert_eq!(five, Some(5)); assert!(!list.contains(&5));
An element that doesn't exist returns None
:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); list.remove(five); assert!(list.remove(five).is_none());
Generational indexes ensure that we don't access incorrect items:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); list.push_back(10); list.remove(five); // since we have a free spot, this will go where 5 was list.push_back(15); // our index is out of date, and so will not return 15 here assert!(list.remove(five).is_none());
pub fn iter(
&self
) -> impl Iterator<Item = &T>
[src]
pub fn iter(
&self
) -> impl Iterator<Item = &T>
Returns an iterator of references to the items in the list.
Examples
Using an iterator to print out all of the items in a list:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_back(5); list.push_back(10); list.push_back(15); // This prints 5, 10, and then 15, each on its own line for element in list.iter() { println!("{}", element); }
pub fn index_of(&self, item: &T) -> Option<Index>
[src]
pub fn index_of(&self, item: &T) -> Option<Index>
Returns an Index
to this item.
If this item is not in the list, returns None
.
Examples
Finding an item:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); let five = list.push_back(5); let index = list.index_of(&5); assert_eq!(Some(five), index);
pub fn pop_front(&mut self) -> Option<T>
[src]
pub fn pop_front(&mut self) -> Option<T>
Removes the head of the list.
If an item was removed, this will also return it.
If this list is empty, returns None
.
Examples
Removing the head:
extern crate indexlist; use indexlist::IndexList; let mut list = IndexList::new(); list.push_back(5); assert_eq!(list.pop_front(), Some(5)); assert_eq!(list.iter().count(), 0);
Trait Implementations
impl<T: Debug> Debug for IndexList<T>
[src]
impl<T: Debug> Debug for IndexList<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<T: PartialEq> PartialEq for IndexList<T>
[src]
impl<T: PartialEq> PartialEq for IndexList<T>