mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 17:18:32 +03:00
Implemented list::len() based on Container trait
This commit is contained in:
+12
-11
@@ -10,6 +10,8 @@
|
||||
|
||||
//! A standard, garbage-collected linked list.
|
||||
|
||||
use std::container::Container;
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
#[allow(missing_doc)]
|
||||
pub enum List<T> {
|
||||
@@ -53,6 +55,11 @@ pub fn iter<'a>(&'a self) -> Items<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Container for List<T> {
|
||||
/// Returns the length of a list
|
||||
fn len(&self) -> uint { self.iter().len() }
|
||||
}
|
||||
|
||||
/// Returns true if a list contains an element with the given value
|
||||
pub fn has<T:Eq>(list: @List<T>, element: T) -> bool {
|
||||
let mut found = false;
|
||||
@@ -70,13 +77,6 @@ pub fn is_empty<T>(list: @List<T>) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the length of a list
|
||||
pub fn len<T>(list: @List<T>) -> uint {
|
||||
let mut count = 0u;
|
||||
iter(list, |_e| count += 1u);
|
||||
count
|
||||
}
|
||||
|
||||
/// Returns all but the first element of a list
|
||||
pub fn tail<T>(list: @List<T>) -> @List<T> {
|
||||
match *list {
|
||||
@@ -252,10 +252,11 @@ fn test_has() {
|
||||
|
||||
#[test]
|
||||
fn test_len() {
|
||||
let list = @List::from_vec([0, 1, 2]);
|
||||
let empty = @list::Nil::<int>;
|
||||
assert_eq!(list::len(list), 3u);
|
||||
assert_eq!(list::len(empty), 0u);
|
||||
let empty = Nil::<int>;
|
||||
assert_eq!(empty.len(), 0u);
|
||||
|
||||
let list = List::from_vec([0, 1, 2]);
|
||||
assert_eq!(list.len(), 3u);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user