2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
//! A standard, garbage-collected linked list.
|
2011-10-26 13:28:23 -05:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2013-07-11 14:05:17 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-10-02 01:26:45 -05:00
|
|
|
#[allow(missing_doc)]
|
2012-10-02 14:02:59 -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-12-23 16:38:01 -06:00
|
|
|
/// Create a list from a vector
|
2013-07-18 19:12:46 -05:00
|
|
|
pub fn from_vec<T:Clone + 'static>(v: &[T]) -> @List<T> {
|
2013-07-02 14:47:32 -05:00
|
|
|
v.rev_iter().fold(@Nil::<T>, |t, h| @Cons((*h).clone(), 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
|
|
|
|
*/
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn foldl<T:Clone,U>(z: T, ls: @List<U>, f: |&T, &U| -> T) -> T {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut accum: T = z;
|
2013-11-20 17:46:49 -06:00
|
|
|
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
|
|
|
|
*
|
2014-02-09 10:45:28 -06:00
|
|
|
* Apply function `f` to each element of `ls`, starting from the first.
|
2012-07-04 16:53:12 -05:00
|
|
|
* When function `f` returns true then an option containing the element
|
|
|
|
* is returned. If `f` matches no elements then none is returned.
|
|
|
|
*/
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn find<T:Clone>(ls: @List<T>, f: |&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-09-28 02:22:18 -05:00
|
|
|
Cons(ref hd, tl) => {
|
2013-07-02 14:47:32 -05:00
|
|
|
if f(hd) { return Some((*hd).clone()); }
|
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
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
|
2014-02-09 11:03:47 -06:00
|
|
|
/**
|
|
|
|
* Returns true if a list contains an element that matches a given predicate
|
|
|
|
*
|
|
|
|
* Apply function `f` to each element of `ls`, starting from the first.
|
|
|
|
* When function `f` returns true then it also returns true. If `f` matches no
|
|
|
|
* elements then false is returned.
|
|
|
|
*/
|
|
|
|
pub fn any<T>(ls: @List<T>, f: |&T| -> bool) -> bool {
|
|
|
|
let mut ls = ls;
|
|
|
|
loop {
|
|
|
|
ls = match *ls {
|
|
|
|
Cons(ref hd, tl) => {
|
|
|
|
if f(hd) { return true; }
|
|
|
|
tl
|
|
|
|
}
|
|
|
|
Nil => return false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if a list contains an element with the given value
|
2013-07-02 14:47:32 -05:00
|
|
|
pub fn has<T:Eq>(ls: @List<T>, elt: T) -> bool {
|
2013-07-31 14:07:44 -05:00
|
|
|
let mut found = false;
|
2013-11-20 17:46:49 -06:00
|
|
|
each(ls, |e| {
|
2013-07-31 14:07:44 -05:00
|
|
|
if *e == elt { found = true; false } else { true }
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-07-31 14:07:44 -05:00
|
|
|
return found;
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the list is empty
|
2013-07-02 14:47:32 -05:00
|
|
|
pub fn is_empty<T>(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
|
2011-12-29 14:24:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns the length of a list
|
2013-03-21 23:34:30 -05:00
|
|
|
pub 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
|
2013-07-02 14:47:32 -05:00
|
|
|
pub fn tail<T>(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,
|
2013-10-21 15:08:31 -05:00
|
|
|
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
|
2013-07-02 14:47:32 -05:00
|
|
|
pub fn head<T:Clone>(ls: @List<T>) -> T {
|
2012-08-15 13:55:17 -05:00
|
|
|
match *ls {
|
2013-07-02 14:47:32 -05:00
|
|
|
Cons(ref hd, _) => (*hd).clone(),
|
2012-08-15 13:55:17 -05:00
|
|
|
// makes me sad
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!("head invoked on empty list")
|
2012-08-15 13:55:17 -05:00
|
|
|
}
|
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
|
2013-07-18 19:12:46 -05:00
|
|
|
pub fn append<T:Clone + 'static>(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,
|
2013-05-29 18:59:33 -05:00
|
|
|
Cons(ref x, xs) => {
|
2012-08-03 21:59:04 -05:00
|
|
|
let rest = append(xs, m);
|
2013-07-02 14:47:32 -05:00
|
|
|
return @Cons((*x).clone(), rest);
|
2012-08-03 21:59:04 -05:00
|
|
|
}
|
2011-05-26 21:05:23 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
|
2012-08-29 18:28:04 -05:00
|
|
|
/*
|
|
|
|
/// Push one element into the front of a list, returning a new list
|
|
|
|
/// THIS VERSION DOESN'T ACTUALLY WORK
|
2013-07-02 14:47:32 -05:00
|
|
|
fn push<T:Clone>(ll: &mut @list<T>, vv: T) {
|
2012-08-29 18:28:04 -05:00
|
|
|
ll = &mut @cons(vv, *ll)
|
2012-05-22 12:54:12 -05:00
|
|
|
}
|
2012-08-29 18:28:04 -05:00
|
|
|
*/
|
2012-05-22 12:54:12 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Iterate over a list
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn iter<T>(l: @List<T>, f: |&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 {
|
2012-09-26 17:59:37 -05:00
|
|
|
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
|
2011-11-17 09:42:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Iterate over a list
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn each<T>(l: @List<T>, f: |&T| -> bool) -> bool {
|
2013-05-02 17:33:27 -05:00
|
|
|
let mut cur = l;
|
|
|
|
loop {
|
|
|
|
cur = match *cur {
|
|
|
|
Cons(ref hd, tl) => {
|
|
|
|
if !f(hd) { return false; }
|
|
|
|
tl
|
|
|
|
}
|
|
|
|
Nil => { return true; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 05:45:42 -05:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-02-02 23:56:49 -06:00
|
|
|
use list::{List, Nil, from_vec, head, is_empty, tail};
|
2012-12-27 20:24:18 -06:00
|
|
|
use list;
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::option;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_is_empty() {
|
2013-05-23 11:39:00 -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
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(is_empty(empty));
|
|
|
|
assert!(!is_empty(full1));
|
|
|
|
assert!(!is_empty(full2));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_from_vec() {
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([0, 1, 2]);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(head(l), 0);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
let tail_l = tail(l);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(head(tail_l), 1);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
let tail_tail_l = tail(tail_l);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(head(tail_tail_l), 2);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_from_vec_empty() {
|
2013-05-23 11:39:00 -05:00
|
|
|
let empty : @list::List<int> = from_vec([]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(empty, @list::Nil::<int>);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_foldl() {
|
2012-08-29 18:28:04 -05:00
|
|
|
fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([0, 1, 2, 3, 4]);
|
2012-09-04 16:12:14 -05:00
|
|
|
let empty = @list::Nil::<int>;
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(list::foldl(0u, l, add), 10u);
|
|
|
|
assert_eq!(list::foldl(0u, empty, add), 0u);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_foldl2() {
|
2012-08-29 18:28:04 -05:00
|
|
|
fn sub(a: &int, b: &int) -> int {
|
|
|
|
*a - *b
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([1, 2, 3, 4]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(list::foldl(0, l, sub), -10);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_find_success() {
|
2012-08-29 18:28:04 -05:00
|
|
|
fn match_(i: &int) -> bool { return *i == 2; }
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([0, 1, 2]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(list::find(l, match_), option::Some(2));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_find_fail() {
|
2012-08-29 18:28:04 -05:00
|
|
|
fn match_(_i: &int) -> bool { return false; }
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([0, 1, 2]);
|
2012-09-04 16:12:14 -05:00
|
|
|
let empty = @list::Nil::<int>;
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(list::find(l, match_), option::None::<int>);
|
|
|
|
assert_eq!(list::find(empty, match_), option::None::<int>);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2014-02-09 11:03:47 -06:00
|
|
|
#[test]
|
|
|
|
fn test_any() {
|
|
|
|
fn match_(i: &int) -> bool { return *i == 2; }
|
|
|
|
let l = from_vec([0, 1, 2]);
|
|
|
|
let empty = @list::Nil::<int>;
|
|
|
|
assert_eq!(list::any(l, match_), true);
|
|
|
|
assert_eq!(list::any(empty, match_), false);
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_has() {
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([5, 8, 6]);
|
2012-09-04 16:12:14 -05:00
|
|
|
let empty = @list::Nil::<int>;
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((list::has(l, 5)));
|
|
|
|
assert!((!list::has(l, 7)));
|
|
|
|
assert!((list::has(l, 8)));
|
|
|
|
assert!((!list::has(empty, 5)));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_len() {
|
2013-05-23 11:39:00 -05:00
|
|
|
let l = from_vec([0, 1, 2]);
|
2012-09-04 16:12:14 -05:00
|
|
|
let empty = @list::Nil::<int>;
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(list::len(l), 3u);
|
|
|
|
assert_eq!(list::len(empty), 0u);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 18:28:04 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_append() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert!(from_vec([1,2,3,4])
|
|
|
|
== list::append(list::from_vec([1,2]), list::from_vec([3,4])));
|
2012-08-29 18:28:04 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|