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]

Creates a new IndexList<T>.

Examples

Making a new list:

extern crate indexlist;
 
use indexlist::IndexList;
 
let list: IndexList<i32> = IndexList::new();

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);

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));

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);
}

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);
}

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));

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());

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());

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);
}

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);

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]

Formats the value using the given formatter. Read more

impl<T: PartialEq> PartialEq for IndexList<T>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

impl<T> Send for IndexList<T> where
    T: Send

impl<T> Sync for IndexList<T> where
    T: Sync