deque: Implement FromIterator
So that deque can be used with IteratorUtil::collect()
This commit is contained in:
parent
40ce0b7d76
commit
08dc72f5d5
@ -13,6 +13,7 @@
|
||||
use std::uint;
|
||||
use std::vec;
|
||||
use std::cast::transmute;
|
||||
use std::iterator::FromIterator;
|
||||
|
||||
static INITIAL_CAPACITY: uint = 32u; // 2^5
|
||||
|
||||
@ -253,6 +254,16 @@ fn get<'r, T>(elts: &'r [Option<T>], i: uint) -> &'r T {
|
||||
match elts[i] { Some(ref t) => t, _ => fail!() }
|
||||
}
|
||||
|
||||
impl<A, T: Iterator<A>> FromIterator<A, T> for Deque<A> {
|
||||
fn from_iterator(iterator: &mut T) -> Deque<A> {
|
||||
let mut deq = Deque::new();
|
||||
for iterator.advance |elt| {
|
||||
deq.add_back(elt);
|
||||
}
|
||||
deq
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -545,4 +556,20 @@ mod tests {
|
||||
}
|
||||
assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0,&6,&7,&8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iterator() {
|
||||
use std::iterator;
|
||||
let v = ~[1,2,3,4,5,6,7];
|
||||
let deq: Deque<int> = v.iter().transform(|&x| x).collect();
|
||||
let u: ~[int] = deq.iter().transform(|&x| x).collect();
|
||||
assert_eq!(u, v);
|
||||
|
||||
let mut seq = iterator::Counter::new(0u, 2).take_(256);
|
||||
let deq: Deque<uint> = seq.collect();
|
||||
for deq.iter().enumerate().advance |(i, &x)| {
|
||||
assert_eq!(2*i, x);
|
||||
}
|
||||
assert_eq!(deq.len(), 256);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user