extra: Implement iterator::Extendable
This commit is contained in:
parent
f68621326e
commit
f8ae526f70
@ -25,7 +25,7 @@
|
||||
use std::cast;
|
||||
use std::ptr;
|
||||
use std::util;
|
||||
use std::iterator::{FromIterator, Invert};
|
||||
use std::iterator::{FromIterator, Extendable, Invert};
|
||||
|
||||
use container::Deque;
|
||||
|
||||
@ -541,11 +541,17 @@ impl<A> DoubleEndedIterator<A> for ConsumeIterator<A> {
|
||||
impl<A, T: Iterator<A>> FromIterator<A, T> for DList<A> {
|
||||
fn from_iterator(iterator: &mut T) -> DList<A> {
|
||||
let mut ret = DList::new();
|
||||
for iterator.advance |elt| { ret.push_back(elt); }
|
||||
ret.extend(iterator);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, T: Iterator<A>> Extendable<A, T> for DList<A> {
|
||||
fn extend(&mut self, iterator: &mut T) {
|
||||
for iterator.advance |elt| { self.push_back(elt); }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Eq> Eq for DList<A> {
|
||||
fn eq(&self, other: &DList<A>) -> bool {
|
||||
self.len() == other.len() &&
|
||||
|
@ -16,7 +16,7 @@ use std::clone::Clone;
|
||||
use std::unstable::intrinsics::{move_val_init, init};
|
||||
use std::util::{replace, swap};
|
||||
use std::vec;
|
||||
use std::iterator::FromIterator;
|
||||
use std::iterator::{FromIterator, Extendable};
|
||||
|
||||
/// A priority queue implemented with a binary heap
|
||||
#[deriving(Clone)]
|
||||
@ -191,20 +191,27 @@ impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> {
|
||||
}
|
||||
|
||||
impl<T: Ord, Iter: Iterator<T>> FromIterator<T, Iter> for PriorityQueue<T> {
|
||||
pub fn from_iterator(iter: &mut Iter) -> PriorityQueue<T> {
|
||||
let (lower, _) = iter.size_hint();
|
||||
|
||||
fn from_iterator(iter: &mut Iter) -> PriorityQueue<T> {
|
||||
let mut q = PriorityQueue::new();
|
||||
q.reserve_at_least(lower);
|
||||
|
||||
for iter.advance |elem| {
|
||||
q.push(elem);
|
||||
}
|
||||
q.extend(iter);
|
||||
|
||||
q
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord, Iter: Iterator<T>> Extendable<T, Iter> for PriorityQueue<T> {
|
||||
fn extend(&mut self, iter: &mut Iter) {
|
||||
let (lower, _) = iter.size_hint();
|
||||
|
||||
let len = self.capacity();
|
||||
self.reserve_at_least(len + lower);
|
||||
|
||||
for iter.advance |elem| {
|
||||
self.push(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sort::merge_sort;
|
||||
|
@ -16,7 +16,7 @@
|
||||
use std::num;
|
||||
use std::uint;
|
||||
use std::vec;
|
||||
use std::iterator::{FromIterator, Invert, RandomAccessIterator};
|
||||
use std::iterator::{FromIterator, Invert, RandomAccessIterator, Extendable};
|
||||
|
||||
use container::Deque;
|
||||
|
||||
@ -325,14 +325,21 @@ impl<A: Eq> Eq for RingBuf<A> {
|
||||
|
||||
impl<A, T: Iterator<A>> FromIterator<A, T> for RingBuf<A> {
|
||||
fn from_iterator(iterator: &mut T) -> RingBuf<A> {
|
||||
let mut deq = RingBuf::new();
|
||||
for iterator.advance |elt| {
|
||||
deq.push_back(elt);
|
||||
}
|
||||
let (lower, _) = iterator.size_hint();
|
||||
let mut deq = RingBuf::with_capacity(lower);
|
||||
deq.extend(iterator);
|
||||
deq
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, T: Iterator<A>> Extendable<A, T> for RingBuf<A> {
|
||||
fn extend(&mut self, iterator: &mut T) {
|
||||
for iterator.advance |elt| {
|
||||
self.push_back(elt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
use std::num;
|
||||
use std::util::{swap, replace};
|
||||
use std::iterator::FromIterator;
|
||||
use std::iterator::{FromIterator, Extendable};
|
||||
|
||||
// This is implemented as an AA tree, which is a simplified variation of
|
||||
// a red-black tree where red (horizontal) nodes can only be added
|
||||
@ -753,29 +753,39 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
|
||||
}
|
||||
|
||||
impl<K: TotalOrd, V, T: Iterator<(K, V)>> FromIterator<(K, V), T> for TreeMap<K, V> {
|
||||
pub fn from_iterator(iter: &mut T) -> TreeMap<K, V> {
|
||||
fn from_iterator(iter: &mut T) -> TreeMap<K, V> {
|
||||
let mut map = TreeMap::new();
|
||||
|
||||
for iter.advance |(k, v)| {
|
||||
map.insert(k, v);
|
||||
}
|
||||
|
||||
map.extend(iter);
|
||||
map
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: TotalOrd, V, T: Iterator<(K, V)>> Extendable<(K, V), T> for TreeMap<K, V> {
|
||||
#[inline]
|
||||
fn extend(&mut self, iter: &mut T) {
|
||||
for iter.advance |(k, v)| {
|
||||
self.insert(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: TotalOrd, Iter: Iterator<T>> FromIterator<T, Iter> for TreeSet<T> {
|
||||
pub fn from_iterator(iter: &mut Iter) -> TreeSet<T> {
|
||||
let mut set = TreeSet::new();
|
||||
|
||||
for iter.advance |elem| {
|
||||
set.insert(elem);
|
||||
}
|
||||
|
||||
set.extend(iter);
|
||||
set
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: TotalOrd, Iter: Iterator<T>> Extendable<T, Iter> for TreeSet<T> {
|
||||
#[inline]
|
||||
fn extend(&mut self, iter: &mut Iter) {
|
||||
for iter.advance |elem| {
|
||||
self.insert(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_treemap {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user