Skip to content

treemap: remove .each in favor of .iter().advance #7405

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 1 commit 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
4 changes: 2 additions & 2 deletions src/libextra/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ impl<
fn encode(&self, e: &mut E) {
do e.emit_map(self.len()) |e| {
let mut i = 0;
for self.each |key, val| {
for self.iter().advance |(key, val)| {
e.emit_map_elt_key(i, |e| key.encode(e));
e.emit_map_elt_val(i, |e| val.encode(e));
i += 1;
Expand Down Expand Up @@ -866,7 +866,7 @@ impl<
fn encode(&self, s: &mut S) {
do s.emit_seq(self.len()) |s| {
let mut i = 0;
for self.each |e| {
for self.iter().advance |e| {
s.emit_seq_elt(i, |s| e.encode(s));
i += 1;
}
Expand Down
23 changes: 7 additions & 16 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,14 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
/// Create an empty TreeMap
pub fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }

/// Visit all key-value pairs in order
pub fn each<'a>(&'a self, f: &fn(&'a K, &'a V) -> bool) -> bool {
each(&self.root, f)
}

/// Visit all keys in order
pub fn each_key(&self, f: &fn(&K) -> bool) -> bool {
self.each(|k, _| f(k))
self.iter().advance(|(k, _)| f(k))
}

/// Visit all values in order
pub fn each_value<'a>(&'a self, f: &fn(&'a V) -> bool) -> bool {
self.each(|_, v| f(v))
self.iter().advance(|(_, v)| f(v))
}

/// Iterate over the map and mutate the contained values
Expand Down Expand Up @@ -484,10 +479,6 @@ impl<T: TotalOrd> TreeSet<T> {
TreeSetIterator{iter: self.map.iter()}
}

/// Visit all values in order
#[inline]
pub fn each(&self, f: &fn(&T) -> bool) -> bool { self.map.each_key(f) }

/// Visit all values in reverse order
#[inline]
pub fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
Expand Down Expand Up @@ -779,7 +770,7 @@ mod test_treemap {
let &(k, v) = x;
assert!(map.find(&k).unwrap() == &v)
}
for map.each |map_k, map_v| {
for map.iter().advance |(map_k, map_v)| {
let mut found = false;
for ctrl.iter().advance |x| {
let &(ctrl_k, ctrl_v) = x;
Expand Down Expand Up @@ -885,7 +876,7 @@ mod test_treemap {
}

#[test]
fn test_each() {
fn test_iterator() {
let mut m = TreeMap::new();

assert!(m.insert(3, 6));
Expand All @@ -895,7 +886,7 @@ mod test_treemap {
assert!(m.insert(1, 2));

let mut n = 0;
for m.each |k, v| {
for m.iter().advance |(k, v)| {
assert_eq!(*k, n);
assert_eq!(*v, n * 2);
n += 1;
Expand Down Expand Up @@ -1090,7 +1081,7 @@ mod test_set {
}

#[test]
fn test_each() {
fn test_iterator() {
let mut m = TreeSet::new();

assert!(m.insert(3));
Expand All @@ -1100,7 +1091,7 @@ mod test_set {
assert!(m.insert(1));

let mut n = 0;
for m.each |x| {
for m.iter().advance |x| {
println(fmt!("%?", x));
assert_eq!(*x, n);
n += 1
Expand Down