rust/src/libstd/list.rs

259 lines
5.8 KiB
Rust
Raw Normal View History

2012-03-07 20:17:30 -06:00
#[doc = "A standard linked list"];
2011-10-26 13:28:23 -05:00
import core::option;
import option::*;
2011-09-12 18:13:28 -05:00
import option::{some, none};
2010-10-18 16:35:44 -05:00
2012-01-19 17:20:57 -06:00
enum list<T> {
2012-01-19 19:55:34 -06:00
cons(T, @list<T>),
nil,
2011-10-26 13:28:23 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "Create a list from a vector"]
fn from_vec<T: copy>(v: [const T]) -> list<T> {
*vec::foldr(v, @nil::<T>, { |h, t| @cons(h, t) })
2011-05-21 23:11:28 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "
2011-10-26 13:28:23 -05:00
Left fold
2012-03-07 20:17:30 -06:00
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.
2011-10-26 13:28:23 -05:00
2012-03-07 20:17:30 -06:00
# Arguments
2011-10-26 13:28:23 -05:00
2012-03-07 20:17:30 -06:00
* ls - The list to fold
* z - The initial value
* f - The function to apply
"]
fn foldl<T: copy, U>(z: T, ls: list<U>, f: fn(T, U) -> T) -> T {
let mut accum: T = z;
iter(ls) {|elt| accum = f(accum, elt);}
accum
2010-10-18 16:35:44 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "
2011-10-26 13:28:23 -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.
2012-03-07 20:17:30 -06:00
"]
fn find<T: copy>(ls: list<T>, f: fn(T) -> bool) -> option<T> {
let mut ls = ls;
loop {
2011-07-27 07:19:39 -05:00
alt ls {
cons(hd, tl) {
if f(hd) { ret some(hd); }
ls = *tl;
2011-07-27 07:19:39 -05:00
}
nil { ret none; }
2010-10-18 16:35:44 -05:00
}
};
2010-10-18 16:35:44 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "Returns true if a list contains an element with the given value"]
fn has<T: copy>(ls: list<T>, elt: T) -> bool {
let mut ls = ls;
loop {
2011-07-27 07:19:39 -05:00
alt ls {
cons(hd, tl) { if elt == hd { ret true; } else { ls = *tl; } }
nil { ret false; }
2011-05-25 11:54:43 -05:00
}
};
2011-05-25 11:54:43 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "Returns true if the list is empty"]
pure fn is_empty<T: copy>(ls: list<T>) -> bool {
alt ls {
nil { true }
_ { false }
}
}
2012-03-07 20:17:30 -06:00
#[doc = "Returns true if the list is not empty"]
pure fn is_not_empty<T: copy>(ls: list<T>) -> bool {
ret !is_empty(ls);
}
2012-03-07 20:17:30 -06:00
#[doc = "Returns the length of a list"]
fn len<T>(ls: list<T>) -> uint {
let mut count = 0u;
iter(ls) {|_e| count += 1u;}
count
2010-10-18 16:35:44 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "Returns all but the first element of a list"]
pure fn tail<T: copy>(ls: list<T>) -> list<T> {
alt ls {
cons(_, tl) { ret *tl; }
nil { fail "list empty" }
}
2011-08-09 19:36:07 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "Returns the first element of a list"]
pure fn head<T: copy>(ls: list<T>) -> T {
alt check ls { cons(hd, _) { hd } }
2011-08-09 19:36:07 -05:00
}
2012-03-07 20:17:30 -06:00
#[doc = "Appends one list to another"]
pure fn append<T: copy>(l: list<T>, m: list<T>) -> list<T> {
2011-07-27 07:19:39 -05:00
alt l {
nil { ret m; }
cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); }
}
}
2011-08-09 19:36:07 -05:00
2012-03-07 20:17:30 -06:00
#[doc = "Iterate over a list"]
2012-01-23 16:59:00 -06:00
fn iter<T>(l: list<T>, f: fn(T)) {
alt l {
cons(hd, tl) {
f(hd);
let mut cur = tl;
loop {
alt *cur {
cons(hd, tl) {
f(hd);
cur = tl;
}
nil { break; }
}
}
}
nil {}
}
}
#[doc = "Iterate over a list"]
fn each<T>(l: list<T>, f: fn(T) -> bool) {
alt l {
cons(hd, tl) {
if !f(hd) { ret; }
let mut cur = tl;
loop {
alt *cur {
cons(hd, tl) {
if !f(hd) { ret; }
cur = tl;
}
nil { break; }
}
}
}
nil {}
}
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
#[test]
fn test_is_empty() {
let empty : list::list<int> = from_vec([]);
let full1 = from_vec([1]);
let full2 = from_vec(['r', 'u']);
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]);
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() {
let empty : list::list<int> = from_vec([]);
assert (empty == list::nil::<int>);
}
#[test]
fn test_from_vec_mut() {
2012-03-26 20:35:18 -05:00
let l = from_vec([mut 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_foldl() {
fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); }
let l = from_vec([0, 1, 2, 3, 4]);
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
}
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 { ret i == 2; }
2012-01-17 21:05:07 -06:00
let l = from_vec([0, 1, 2]);
assert (list::find(l, match) == option::some(2));
}
#[test]
fn test_find_fail() {
fn match(&&_i: int) -> bool { ret false; }
2012-01-17 21:05:07 -06:00
let l = from_vec([0, 1, 2]);
let empty = list::nil::<int>;
assert (list::find(l, match) == option::none::<int>);
assert (list::find(empty, match) == option::none::<int>);
}
#[test]
fn test_has() {
let l = from_vec([5, 8, 6]);
let empty = list::nil::<int>;
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]);
let empty = list::nil::<int>;
assert (list::len(l) == 3u);
assert (list::len(empty) == 0u);
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: