Skip to content

Commit

Permalink
Stripped trailing spaces; Implemented FromIterator for TreeMap and Pr…
Browse files Browse the repository at this point in the history
…iorityQueue
  • Loading branch information
MarkJr94 committed Jul 14, 2013
1 parent bb6615d commit bbe03da
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
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));
}
}
}
8 changes: 4 additions & 4 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ impl<K: Eq + Hash, V, T: Iterator<(K, V)>> FromIterator<(K, V), T> for HashMap<K

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 @@ -747,7 +747,7 @@ impl<K: Eq + Hash, T: Iterator<K>> FromIterator<K, T> for HashSet<K> {
set
}
}


#[cfg(test)]
mod test_map {
Expand Down Expand Up @@ -965,7 +965,7 @@ mod test_map {
#[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)| {
Expand Down Expand Up @@ -1157,7 +1157,7 @@ mod test_set {
#[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| {
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ mod test_map {
#[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)| {
Expand Down Expand Up @@ -592,7 +592,7 @@ mod test_set {
#[test]
fn test_from_iter() {
let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1];

let set: TrieSet = xs.iter().transform(|&x| x).collect();

for xs.iter().advance |x| {
Expand Down

5 comments on commit bbe03da

@bors
Copy link
Contributor

@bors bors commented on bbe03da Jul 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on bbe03da Jul 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging MarkJr94/rust/from_iter = bbe03da into auto

@bors
Copy link
Contributor

@bors bors commented on bbe03da Jul 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MarkJr94/rust/from_iter = bbe03da merged ok, testing candidate = 0cb1ac0

@bors
Copy link
Contributor

@bors bors commented on bbe03da Jul 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 0cb1ac0

Please sign in to comment.