Skip to content

Implemented FromIterator for other containers. Fixes #7626 #7788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use std::unstable::intrinsics::{move_val_init, init};
use std::util::{replace, swap};
use std::vec;
use std::iterator::FromIterator;

/// A priority queue implemented with a binary heap
pub struct PriorityQueue<T> {
Expand Down Expand Up @@ -191,6 +192,21 @@ impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> {
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}

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();

let mut q = PriorityQueue::new();
q.reserve_at_least(lower);

for iter.advance |elem| {
q.push(elem);
}

q
}
}

#[cfg(test)]
mod tests {
use sort::merge_sort;
Expand Down Expand Up @@ -341,4 +357,15 @@ mod tests {
#[should_fail]
#[ignore(cfg(windows))]
fn test_empty_replace() { let mut heap = PriorityQueue::new(); heap.replace(5); }

#[test]
fn test_from_iter() {
let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1];

let mut q: PriorityQueue<uint> = xs.rev_iter().transform(|&x| x).collect();

for xs.iter().advance |&x| {
assert_eq!(q.pop(), x);
}
}
}
47 changes: 47 additions & 0 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use std::num;
use std::util::{swap, replace};
use std::iterator::FromIterator;

// 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
Expand Down Expand Up @@ -695,6 +696,30 @@ 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> {
let mut map = TreeMap::new();

for iter.advance |(k, v)| {
map.insert(k, v);
}

map
}
}

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
}
}

#[cfg(test)]
mod test_treemap {

Expand Down Expand Up @@ -1013,6 +1038,17 @@ mod test_treemap {
i += 1;
}
}

#[test]
fn test_from_iter() {
let xs = ~[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];

let map: TreeMap<int, int> = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |&(k, v)| {
assert_eq!(map.find(&k), Some(&v));
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1240,4 +1276,15 @@ mod test_set {
assert_eq!(m.pop(&1), Some(2));
assert_eq!(m.pop(&1), None);
}

#[test]
fn test_from_iter() {
let xs = ~[1, 2, 3, 4, 5, 6, 7, 8, 9];

let set: TreeSet<int> = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |x: &int| {
assert!(set.contains(x));
}
}
}
50 changes: 49 additions & 1 deletion src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use container::{Container, Mutable, Map, Set};
use cmp::{Eq, Equiv};
use hash::Hash;
use iterator::{Iterator, IteratorUtil};
use iterator::{Iterator, IteratorUtil, FromIterator};
use num;
use option::{None, Option, Some};
use rand::RngUtil;
Expand Down Expand Up @@ -610,6 +610,18 @@ impl<'self, K> Iterator<&'self K> for HashSetIterator<'self, K> {
}
}

impl<K: Eq + Hash, V, T: Iterator<(K, V)>> FromIterator<(K, V), T> for HashMap<K, V> {
pub fn from_iterator(iter: &mut T) -> HashMap<K, V> {
let (lower, _) = iter.size_hint();
let mut map = HashMap::with_capacity(lower);

for iter.advance |(k, v)| {
map.insert(k, v);
}

map
}
}

/// An implementation of a hash set using the underlying representation of a
/// HashMap where the value is (). As with the `HashMap` type, a `HashSet`
Expand Down Expand Up @@ -723,6 +735,20 @@ impl<T:Hash + Eq> HashSet<T> {
}
}

impl<K: Eq + Hash, T: Iterator<K>> FromIterator<K, T> for HashSet<K> {
pub fn from_iterator(iter: &mut T) -> HashSet<K> {
let (lower, _) = iter.size_hint();
let mut set = HashSet::with_capacity(lower);

for iter.advance |k| {
set.insert(k);
}

set
}
}


#[cfg(test)]
mod test_map {
use container::{Container, Map, Set};
Expand Down Expand Up @@ -935,6 +961,17 @@ mod test_map {

assert_eq!(m.find_equiv(&("qux")), None);
}

#[test]
fn test_from_iter() {
let xs = ~[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];

let map: HashMap<int, int> = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |&(k, v)| {
assert_eq!(map.find(&k), Some(&v));
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1116,4 +1153,15 @@ mod test_set {
}
assert_eq!(i, expected.len());
}

#[test]
fn test_from_iter() {
let xs = ~[1, 2, 3, 4, 5, 6, 7, 8, 9];

let set: HashSet<int> = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |x: &int| {
assert!(set.contains(x));
}
}
}
80 changes: 66 additions & 14 deletions src/libstd/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! An ordered map and set for integer keys implemented as a radix trie

use prelude::*;
use iterator::IteratorUtil;
use iterator::{IteratorUtil, FromIterator};
use uint;
use util::{swap, replace};

Expand Down Expand Up @@ -171,6 +171,18 @@ impl<T> TrieMap<T> {
}
}

impl<T, Iter: Iterator<(uint, T)>> FromIterator<(uint, T), Iter> for TrieMap<T> {
pub fn from_iterator(iter: &mut Iter) -> TrieMap<T> {
let mut map = TrieMap::new();

for iter.advance |(k, v)| {
map.insert(k, v);
}

map
}
}

#[allow(missing_doc)]
pub struct TrieSet {
priv map: TrieMap<()>
Expand Down Expand Up @@ -230,6 +242,18 @@ impl TrieSet {
}
}

impl<Iter: Iterator<uint>> FromIterator<uint, Iter> for TrieSet {
pub fn from_iterator(iter: &mut Iter) -> TrieSet {
let mut set = TrieSet::new();

for iter.advance |elem| {
set.insert(elem);
}

set
}
}

struct TrieNode<T> {
count: uint,
children: [Child<T>, ..SIZE]
Expand Down Expand Up @@ -382,7 +406,7 @@ pub fn check_integrity<T>(trie: &TrieNode<T>) {
}

#[cfg(test)]
mod tests {
mod test_map {
use super::*;
use core::option::{Some, None};
use uint;
Expand Down Expand Up @@ -510,6 +534,39 @@ mod tests {
}
}

#[test]
fn test_swap() {
let mut m = TrieMap::new();
assert_eq!(m.swap(1, 2), None);
assert_eq!(m.swap(1, 3), Some(2));
assert_eq!(m.swap(1, 4), Some(3));
}

#[test]
fn test_pop() {
let mut m = TrieMap::new();
m.insert(1, 2);
assert_eq!(m.pop(&1), Some(2));
assert_eq!(m.pop(&1), None);
}

#[test]
fn test_from_iter() {
let xs = ~[(1u, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];

let map: TrieMap<int> = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |&(k, v)| {
assert_eq!(map.find(&k), Some(&v));
}
}
}

#[cfg(test)]
mod test_set {
use super::*;
use uint;

#[test]
fn test_sane_chunk() {
let x = 1;
Expand All @@ -533,18 +590,13 @@ mod tests {
}

#[test]
fn test_swap() {
let mut m = TrieMap::new();
assert_eq!(m.swap(1, 2), None);
assert_eq!(m.swap(1, 3), Some(2));
assert_eq!(m.swap(1, 4), Some(3));
}
fn test_from_iter() {
let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1];

#[test]
fn test_pop() {
let mut m = TrieMap::new();
m.insert(1, 2);
assert_eq!(m.pop(&1), Some(2));
assert_eq!(m.pop(&1), None);
let set: TrieSet = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |x| {
assert!(set.contains(x));
}
}
}