rust/src/libstd/list.rs

297 lines
6.9 KiB
Rust
Raw Normal View History

//! A standard linked list
#[forbid(deprecated_mode)];
2011-10-26 13:28:23 -05:00
2012-09-04 13:23:53 -05:00
use core::cmp::Eq;
use core::option;
use option::*;
use option::{Some, None};
2010-10-18 16:35:44 -05:00
pub enum List<T> {
2012-09-04 16:12:14 -05:00
Cons(T, @List<T>),
Nil,
2011-10-26 13:28:23 -05:00
}
2012-09-04 16:12:14 -05:00
/// Cregate a list from a vector
pub fn from_vec<T: Copy>(v: &[T]) -> @List<T> {
2012-09-28 00:20:47 -05:00
vec::foldr(v, @Nil::<T>, |h, t| @Cons(*h, t))
2011-05-21 23:11:28 -05:00
}
/**
* Left fold
*
* Applies `f` to `u` and the first element in the list, then applies `f` to
* the result of the previous call and the second element, and so on,
* returning the accumulated result.
*
* # Arguments
*
* * ls - The list to fold
* * z - The initial value
* * f - The function to apply
*/
pub fn foldl<T: Copy, U>(z: T, ls: @List<U>, f: fn((&T), (&U)) -> T) -> T {
let mut accum: T = z;
do iter(ls) |elt| { accum = f(&accum, elt);}
accum
2010-10-18 16:35:44 -05:00
}
/**
* Search for an element that matches a given predicate
*
* Apply function `f` to each element of `v`, starting from the first.
* When function `f` returns true then an option containing the element
* is returned. If `f` matches no elements then none is returned.
*/
pub fn find<T: Copy>(ls: @List<T>, f: fn((&T)) -> bool) -> Option<T> {
let mut ls = ls;
loop {
2012-08-06 14:34:08 -05:00
ls = match *ls {
2012-09-28 02:22:18 -05:00
Cons(ref hd, tl) => {
if f(hd) { return Some(*hd); }
2012-05-21 08:15:58 -05:00
tl
2011-07-27 07:19:39 -05:00
}
2012-09-04 16:12:14 -05:00
Nil => return None
2010-10-18 16:35:44 -05:00
}
};
2010-10-18 16:35:44 -05:00
}
/// Returns true if a list contains an element with the given value
pub fn has<T: Copy Eq>(ls: @List<T>, elt: T) -> bool {
2012-06-30 18:19:07 -05:00
for each(ls) |e| {
2012-09-26 20:12:07 -05:00
if *e == elt { return true; }
}
2012-08-01 19:30:05 -05:00
return false;
2011-05-25 11:54:43 -05:00
}
/// Returns true if the list is empty
pub pure fn is_empty<T: Copy>(ls: @List<T>) -> bool {
2012-08-06 14:34:08 -05:00
match *ls {
2012-09-04 16:12:14 -05:00
Nil => true,
2012-08-03 21:59:04 -05:00
_ => false
}
}
/// Returns true if the list is not empty
pub pure fn is_not_empty<T: Copy>(ls: @List<T>) -> bool {
2012-08-01 19:30:05 -05:00
return !is_empty(ls);
}
/// Returns the length of a list
pub fn len<T>(ls: @List<T>) -> uint {
let mut count = 0u;
2012-06-30 18:19:07 -05:00
iter(ls, |_e| count += 1u);
count
2010-10-18 16:35:44 -05:00
}
/// Returns all but the first element of a list
pub pure fn tail<T: Copy>(ls: @List<T>) -> @List<T> {
2012-08-06 14:34:08 -05:00
match *ls {
2012-09-04 16:12:14 -05:00
Cons(_, tl) => return tl,
Nil => fail ~"list empty"
}
2011-08-09 19:36:07 -05:00
}
/// Returns the first element of a list
pub pure fn head<T: Copy>(ls: @List<T>) -> T {
2012-08-15 13:55:17 -05:00
match *ls {
2012-09-28 02:22:18 -05:00
Cons(copy hd, _) => hd,
2012-08-15 13:55:17 -05:00
// makes me sad
_ => fail ~"head invoked on empty list"
}
2011-08-09 19:36:07 -05:00
}
/// Appends one list to another
pub pure fn append<T: Copy>(l: @List<T>, m: @List<T>) -> @List<T> {
2012-08-06 14:34:08 -05:00
match *l {
2012-09-04 16:12:14 -05:00
Nil => return m,
2012-09-28 02:22:18 -05:00
Cons(copy x, xs) => {
2012-08-03 21:59:04 -05:00
let rest = append(xs, m);
2012-09-04 16:12:14 -05:00
return @Cons(x, rest);
2012-08-03 21:59:04 -05:00
}
}
}
2011-08-09 19:36:07 -05:00
/*
/// Push one element into the front of a list, returning a new list
/// THIS VERSION DOESN'T ACTUALLY WORK
pure fn push<T: Copy>(ll: &mut @list<T>, vv: T) {
ll = &mut @cons(vv, *ll)
}
*/
/// Iterate over a list
pub fn iter<T>(l: @List<T>, f: fn((&T))) {
2012-05-21 08:15:58 -05:00
let mut cur = l;
loop {
2012-08-06 14:34:08 -05:00
cur = match *cur {
Cons(ref hd, tl) => {
2012-05-21 08:15:58 -05:00
f(hd);
tl
}
2012-09-04 16:12:14 -05:00
Nil => break
}
}
}
/// Iterate over a list
pub fn each<T>(l: @List<T>, f: fn((&T)) -> bool) {
2012-05-21 08:15:58 -05:00
let mut cur = l;
loop {
2012-08-06 14:34:08 -05:00
cur = match *cur {
2012-09-26 20:12:07 -05:00
Cons(ref hd, tl) => {
2012-08-01 19:30:05 -05:00
if !f(hd) { return; }
tl
2012-05-21 08:15:58 -05:00
}
2012-09-04 16:12:14 -05:00
Nil => break
}
}
}
impl<T:Eq> List<T> : Eq {
#[cfg(stage0)]
pure fn eq(other: &List<T>) -> bool {
match self {
2012-09-28 02:22:18 -05:00
Cons(ref e0a, e1a) => {
match (*other) {
2012-09-28 02:22:18 -05:00
Cons(ref e0b, e1b) => e0a == e0b && e1a == e1b,
_ => false
}
}
Nil => {
match (*other) {
Nil => true,
_ => false
}
}
}
}
#[cfg(stage1)]
#[cfg(stage2)]
pure fn eq(&self, other: &List<T>) -> bool {
match (*self) {
Cons(ref e0a, e1a) => {
match (*other) {
Cons(ref e0b, e1b) => e0a == e0b && e1a == e1b,
_ => false
}
}
Nil => {
match (*other) {
Nil => true,
_ => false
}
}
}
}
#[cfg(stage0)]
pure fn ne(other: &List<T>) -> bool { !self.eq(other) }
#[cfg(stage1)]
#[cfg(stage2)]
pure fn ne(&self, other: &List<T>) -> bool { !(*self).eq(other) }
}
2012-08-27 18:26:35 -05:00
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
#[legacy_exports];
2012-01-17 21:05:07 -06:00
#[test]
fn test_is_empty() {
2012-09-04 16:12:14 -05:00
let empty : @list::List<int> = from_vec(~[]);
let full1 = from_vec(~[1]);
let full2 = from_vec(~['r', 'u']);
2012-01-17 21:05:07 -06:00
assert is_empty(empty);
assert !is_empty(full1);
assert !is_empty(full2);
assert !is_not_empty(empty);
assert is_not_empty(full1);
assert is_not_empty(full2);
}
#[test]
fn test_from_vec() {
let l = from_vec(~[0, 1, 2]);
2012-01-17 21:05:07 -06:00
assert (head(l) == 0);
let tail_l = tail(l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
assert (head(tail_tail_l) == 2);
}
#[test]
fn test_from_vec_empty() {
2012-09-04 16:12:14 -05:00
let empty : @list::List<int> = from_vec(~[]);
assert (empty == @list::Nil::<int>);
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_foldl() {
fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
let l = from_vec(~[0, 1, 2, 3, 4]);
2012-09-04 16:12:14 -05:00
let empty = @list::Nil::<int>;
assert (list::foldl(0u, l, add) == 10u);
assert (list::foldl(0u, empty, add) == 0u);
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_foldl2() {
fn sub(a: &int, b: &int) -> int {
*a - *b
2012-01-17 21:05:07 -06:00
}
let l = from_vec(~[1, 2, 3, 4]);
assert (list::foldl(0, l, sub) == -10);
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_find_success() {
fn match_(i: &int) -> bool { return *i == 2; }
let l = from_vec(~[0, 1, 2]);
2012-08-20 14:23:37 -05:00
assert (list::find(l, match_) == option::Some(2));
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_find_fail() {
fn match_(_i: &int) -> bool { return false; }
let l = from_vec(~[0, 1, 2]);
2012-09-04 16:12:14 -05:00
let empty = @list::Nil::<int>;
2012-08-20 14:23:37 -05:00
assert (list::find(l, match_) == option::None::<int>);
assert (list::find(empty, match_) == option::None::<int>);
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_has() {
let l = from_vec(~[5, 8, 6]);
2012-09-04 16:12:14 -05:00
let empty = @list::Nil::<int>;
2012-01-17 21:05:07 -06:00
assert (list::has(l, 5));
assert (!list::has(l, 7));
assert (list::has(l, 8));
assert (!list::has(empty, 5));
}
#[test]
fn test_len() {
let l = from_vec(~[0, 1, 2]);
2012-09-04 16:12:14 -05:00
let empty = @list::Nil::<int>;
2012-01-17 21:05:07 -06:00
assert (list::len(l) == 3u);
assert (list::len(empty) == 0u);
}
#[test]
fn test_append() {
assert from_vec(~[1,2,3,4])
== list::append(list::from_vec(~[1,2]), list::from_vec(~[3,4]));
}
2012-01-17 21:05:07 -06:00
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: