Enum seax_util::list::List
[−]
[src]
pub enum List<T> { Cons(T, Box<List<T>>), Nil, }
Singly-linked cons
list.
This is used internally to represent list primitives in the Seax virtual machine.
Variants
Cons(T, Box<List<T>>)
Cons cell containing a T
and a link to the tail
Nil
The empty list.
Methods
impl<T> List<T>
[src]
Public implementation for List.
fn new() -> List<T>
Creates a new empty list
fn prepend(self, it: T) -> List<T>
Prepends the given item to the list.
This is an O(1) operation.
Arguments
item
- the item to prepend
Return Value
- The list with the new head item prepended
Examples
let mut a_list: List<isize> = List::new(); a_list = a_list.prepend(1); assert_eq!(a_list, list![1]); a_list = a_list.prepend(2); assert_eq!(a_list, list![2,1]);
fn append(&mut self, it: T)
Appends an item to the end of the list.
This is an O(n) operation.
Arguments
item
- the item to append
Examples
let mut a_list: List<isize> = List::new(); a_list.append(1); assert_eq!(a_list, list![1]); a_list.append(2); assert_eq!(a_list, list![1,2]);
fn append_chain(&mut self, it: T) -> &mut List<T>
Appends an item to the end of the list.
Returns the last element of the list to support 'append chaining'
of a large number of items; this allows you to omit a complete traversal
of the list for every append and should be used in situations
such as fold
s.
The first append is still O(n), but long as you hang on to your
pointer to the tail, subsequent appends should all be O(1). However,
this requires you to keep a &mut
pointer to the list, so use it
sparingly, especially in situations of concurrent access.
Arguments
item
- the item to append
Examples
let mut a_list: List<isize> = List::new(); // this is a function so that the `&mut` borrow is released. fn append_two_items<T>(l: &mut List<T>, first: T, second: T) { l.append_chain(first).append_chain(second); } append_two_items(&mut a_list, 1, 2); assert_eq!(a_list, list![1,2]);
fn length(&self) -> usize
Returns the length of the list.
Examples
let a_list = list!(1,2,3,4); assert_eq!(a_list.length(), 4)
fn is_empty(&self) -> bool
Returns true if the list is empty, false otherwise.
Examples
let list = list!(1,2,3,4); assert_eq!(list.is_empty(), false)
let list: List<isize> = Nil; assert_eq!(list.is_empty(), true)
fn tail<'a>(&'a self) -> &'a Self
Returns the tail of the list from this element.
Examples
let list = list!(1,2,3,4); assert_eq!(list.tail(), &list!(2,3,4))
fn iter<'a>(&'a self) -> ListIterator<'a, T>
Provide a forward iterator
fn last(&self) -> &T
Returns the last element of the list
Examples
let a_list = list!(1,2,3,4); assert_eq!(a_list.last(), &4)
fn get<'a>(&'a self, index: u64) -> Option<&'a T>
Optionally index the list.
Unlike list indexing syntax (list[i]
), this returns None
if the index is out of bound rather than panicking.
Lists are zero-indexed, so just as when using list indexing syntax, the head of the list is index 0 and the last element of the list is index (length - 1).
Arguments
idx
- the index to attempt to access
Return Value
Some(&T)
if the index exists within the list,None
otherwise.
Examples
let a_list = list!(1,2,3,4); assert_eq!(a_list.get(1), Some(&2)); assert_eq!(a_list.get(3), Some(&4)); assert_eq!(a_list.get(10), None);
Trait Implementations
impl<T> Stack<T> for List<T>
[src]
Stack implementation using a cons
list
fn push(self, item: T) -> List<T>
Push an item to the top of the stack, returning a new stack.
Examples:
let mut s: List<isize> = Stack::empty(); assert_eq!(s.peek(), None); s = s.push(1); assert_eq!(s.peek(), Some(&1)); s = s.push(6); assert_eq!(s.peek(), Some(&6));
fn pop(self) -> Option<(T, List<T>)>
Pop the top element of the stack.
Pop the top element of the stack. Returns an
Option<(T,List<T>)>
containing the top element and a new
List<T>
with that item removed, or None
if the stack is
empty.
Examples:
let mut s: List<isize> = Stack::empty(); s = s.push(2); s = s.push(1); let pop_result = s.pop().unwrap(); s = pop_result.1; assert_eq!(s.peek(), Some(&2)); assert_eq!(pop_result.0, 1);
fn empty() -> List<T>
Returns an empty stack.
fn peek(&self) -> Option<&T>
Peek at the top element of the stack.
Peek at the top element of the stack. Returns an Option<&T>
with a borrowed pointer to the top element, or None
if the
stack is empty.
Examples:
let mut s: List<isize> = Stack::empty(); s = s.push(2); s = s.push(1); let pop_result = s.pop().unwrap(); s = pop_result.1; assert_eq!(s.peek(), Some(&2)); assert_eq!(pop_result.0, 1);
impl<T: PartialEq> PartialEq for List<T>
[src]
fn eq(&self, __arg_0: &List<T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &List<T>) -> bool
This method tests for !=
.
impl<T: Eq> Eq for List<T>
[src]
impl<T: Hash> Hash for List<T>
[src]
fn hash<__HT: Hasher>(&self, __arg_0: &mut __HT)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T: Clone> Clone for List<T>
[src]
fn clone(&self) -> List<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<'a, T> Display for List<T> where
T: Display,
[src]
T: Display,
impl<'a, T> Debug for List<T> where
T: Debug,
[src]
T: Debug,
impl<T> Default for List<T>
[src]
impl<T> FromIterator<T> for List<T>
[src]
fn from_iter<I>(iterable: I) -> List<T> where
I: IntoIterator<Item = T>,
I: IntoIterator<Item = T>,
Build a List<T>
from a structure implementing IntoIterator<T>
.
This takes advantage of the List.append_chain()
method under the
hood to provide roughly O(n) performance.
Examples
let mut a_vec = vec![1,2,3,4]; let another_vec = a_vec.clone(); let a_list = List::from_iter(a_vec); for i in 0..a_list.length() { assert_eq!(a_list[i], another_vec[i]) }
impl<T> Index<usize> for List<T>
[src]
Implementation of indexing for List<T>
.
Examples:
let list: List<isize> = list!(1,2,3,4,5,6); assert_eq!(list[0usize], 1);
type Output = T
The returned type after indexing
fn index<'a>(&'a self, _index: usize) -> &'a T
The method for the indexing (container[index]
) operation
impl<T> Index<u64> for List<T>
[src]
Implementation of indexing for List<T>
.
Examples:
let list = list!(1,2,3,4,5,6); assert_eq!(list[0usize], 1);
type Output = T
The returned type after indexing
fn index<'a>(&'a self, _index: u64) -> &'a T
The method for the indexing (container[index]
) operation
impl<T> Index<i64> for List<T>
[src]
Implementation of indexing for List<T>
.
Examples:
let list = list!(1,2,3,4,5,6); assert_eq!(list[0isize], 1);
type Output = T
The returned type after indexing
fn index<'a>(&'a self, _index: i64) -> &'a T
The method for the indexing (container[index]
) operation
impl<T> Index<isize> for List<T>
[src]
Implementation of indexing for List<T>
.
Examples:
let list = list!(1,2,3,4,5,6); assert_eq!(list[0isize], 1);
type Output = T
The returned type after indexing
fn index<'a>(&'a self, _index: isize) -> &'a T
The method for the indexing (container[index]
) operation
impl<T> Encode for List<T> where
T: Encode,
[src]
T: Encode,