2012-07-04 16:53:12 -05:00
|
|
|
//! A standard linked list
|
2011-10-26 13:28:23 -05:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::option;
|
|
|
|
import option::*;
|
2012-08-20 14:23:37 -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-07-04 16:53:12 -05:00
|
|
|
/// Create a list from a vector
|
2012-06-29 23:19:51 -05:00
|
|
|
fn from_vec<T: copy>(v: &[T]) -> @list<T> {
|
2012-06-30 18:19:07 -05:00
|
|
|
vec::foldr(v, @nil::<T>, |h, t| @cons(h, t))
|
2011-05-21 23:11:28 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -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
|
|
|
|
*/
|
2012-05-21 08:15:58 -05:00
|
|
|
fn foldl<T: copy, U>(z: T, ls: @list<U>, f: fn(T, U) -> T) -> T {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut accum: T = z;
|
2012-06-30 18:19:07 -05:00
|
|
|
do iter(ls) |elt| { accum = f(accum, elt);}
|
2012-01-05 02:18:19 -06:00
|
|
|
accum
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -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-08-20 14:23:37 -05:00
|
|
|
fn find<T: copy>(ls: @list<T>, f: fn(T) -> bool) -> Option<T> {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut ls = ls;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
ls = match *ls {
|
2012-08-03 21:59:04 -05:00
|
|
|
cons(hd, tl) => {
|
2012-08-20 14:23:37 -05:00
|
|
|
if f(hd) { return Some(hd); }
|
2012-05-21 08:15:58 -05:00
|
|
|
tl
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
nil => return None
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if a list contains an element with the given value
|
2012-05-21 08:15:58 -05:00
|
|
|
fn has<T: copy>(ls: @list<T>, elt: T) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for each(ls) |e| {
|
2012-08-01 19:30:05 -05:00
|
|
|
if e == elt { return true; }
|
2012-05-12 21:31:07 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the list is empty
|
2012-05-21 08:15:58 -05:00
|
|
|
pure fn is_empty<T: copy>(ls: @list<T>) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *ls {
|
2012-08-03 21:59:04 -05:00
|
|
|
nil => true,
|
|
|
|
_ => false
|
2011-12-29 14:24:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the list is not empty
|
2012-05-21 08:15:58 -05:00
|
|
|
pure fn is_not_empty<T: copy>(ls: @list<T>) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return !is_empty(ls);
|
2011-12-29 14:24:03 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns the length of a list
|
2012-05-21 08:15:58 -05:00
|
|
|
fn len<T>(ls: @list<T>) -> uint {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut count = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
iter(ls, |_e| count += 1u);
|
2012-01-05 02:18:19 -06:00
|
|
|
count
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
2010-10-16 00:09:09 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns all but the first element of a list
|
2012-05-21 11:37:34 -05:00
|
|
|
pure fn tail<T: copy>(ls: @list<T>) -> @list<T> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *ls {
|
2012-08-03 21:59:04 -05:00
|
|
|
cons(_, tl) => return tl,
|
|
|
|
nil => fail ~"list empty"
|
2011-12-29 14:24:03 -06:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
}
|
2011-05-09 05:40:09 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns the first element of a list
|
2012-05-21 08:15:58 -05:00
|
|
|
pure fn head<T: copy>(ls: @list<T>) -> T {
|
2012-08-15 13:55:17 -05:00
|
|
|
match *ls {
|
|
|
|
cons(hd, _) => hd,
|
|
|
|
// makes me sad
|
|
|
|
_ => fail ~"head invoked on empty list"
|
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
}
|
2011-05-26 21:05:23 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Appends one list to another
|
2012-05-21 08:15:58 -05:00
|
|
|
pure fn append<T: copy>(l: @list<T>, m: @list<T>) -> @list<T> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *l {
|
2012-08-03 21:59:04 -05:00
|
|
|
nil => return m,
|
|
|
|
cons(x, xs) => {
|
|
|
|
let rest = append(xs, m);
|
|
|
|
return @cons(x, rest);
|
|
|
|
}
|
2011-05-26 21:05:23 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Push an element to the front of a list
|
2012-05-22 12:54:12 -05:00
|
|
|
fn push<T: copy>(&l: list<T>, v: T) {
|
|
|
|
l = cons(v, @l);
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Iterate over a list
|
2012-05-21 08:15:58 -05:00
|
|
|
fn iter<T>(l: @list<T>, f: fn(T)) {
|
|
|
|
let mut cur = l;
|
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
cur = match *cur {
|
2012-08-03 21:59:04 -05:00
|
|
|
cons(hd, tl) => {
|
2012-05-21 08:15:58 -05:00
|
|
|
f(hd);
|
|
|
|
tl
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
nil => break
|
2011-11-17 09:42:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Iterate over a list
|
2012-05-21 11:37:34 -05:00
|
|
|
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-08-03 21:59:04 -05:00
|
|
|
cons(hd, tl) => {
|
2012-08-01 19:30:05 -05:00
|
|
|
if !f(hd) { return; }
|
2012-05-21 11:37:34 -05:00
|
|
|
tl
|
2012-05-21 08:15:58 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
nil => break
|
2012-03-27 05:45:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_empty() {
|
2012-06-29 18:26:56 -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() {
|
2012-06-29 18:26:56 -05:00
|
|
|
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-06-29 18:26:56 -05:00
|
|
|
let empty : @list::list<int> = from_vec(~[]);
|
2012-05-21 11:37:34 -05:00
|
|
|
assert (empty == @list::nil::<int>);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_foldl() {
|
2012-08-01 19:30:05 -05:00
|
|
|
fn add(&&a: uint, &&b: int) -> uint { return a + (b as uint); }
|
2012-06-29 18:26:56 -05:00
|
|
|
let l = from_vec(~[0, 1, 2, 3, 4]);
|
2012-05-21 08:15:58 -05:00
|
|
|
let empty = @list::nil::<int>;
|
2012-03-15 16:15:49 -05:00
|
|
|
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-06-29 18:26:56 -05:00
|
|
|
let l = from_vec(~[1, 2, 3, 4]);
|
2012-03-15 16:15:49 -05:00
|
|
|
assert (list::foldl(0, l, sub) == -10);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_success() {
|
2012-08-01 19:30:05 -05:00
|
|
|
fn match_(&&i: int) -> bool { return i == 2; }
|
2012-06-29 18:26:56 -05:00
|
|
|
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() {
|
2012-08-01 19:30:05 -05:00
|
|
|
fn match_(&&_i: int) -> bool { return false; }
|
2012-06-29 18:26:56 -05:00
|
|
|
let l = from_vec(~[0, 1, 2]);
|
2012-05-21 11:37:34 -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() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let l = from_vec(~[5, 8, 6]);
|
2012-05-21 08:15:58 -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() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let l = from_vec(~[0, 1, 2]);
|
2012-05-21 08:15:58 -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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-16 00:09:09 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|