Implemented list::len() based on Container trait
This commit is contained in:
parent
197116d7ce
commit
e589fcffcc
1 changed files with 12 additions and 11 deletions
|
|
@ -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 @@ impl<T> List<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 @@ mod tests {
|
|||
|
||||
#[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]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue