get rid of implicit vec copies in treemap iterator
Each call to next() was doing a copy rather than a move. There's currently no way for this to be a method that uses &mut self, so it has to be a free function. Closes #4763.
This commit is contained in:
parent
5b6c26b4e4
commit
37e998696f
@ -49,8 +49,8 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
|
|||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
for self.len().times {
|
for self.len().times {
|
||||||
unsafe { // unsafe as a purity workaround
|
unsafe { // unsafe as a purity workaround
|
||||||
x = x.next();
|
map_next(&mut x);
|
||||||
y = y.next();
|
map_next(&mut y);
|
||||||
// FIXME: #4492 (ICE), x.get() == y.get()
|
// FIXME: #4492 (ICE), x.get() == y.get()
|
||||||
let (x1, x2) = x.get().unwrap();
|
let (x1, x2) = x.get().unwrap();
|
||||||
let (y1, y2) = y.get().unwrap();
|
let (y1, y2) = y.get().unwrap();
|
||||||
@ -74,8 +74,8 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
|
|||||||
let (a_len, b_len) = (a.len(), b.len());
|
let (a_len, b_len) = (a.len(), b.len());
|
||||||
for uint::min(a_len, b_len).times {
|
for uint::min(a_len, b_len).times {
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
map_next(&mut x);
|
||||||
y = y.next();
|
map_next(&mut y);
|
||||||
let (key_a,_) = x.get().unwrap();
|
let (key_a,_) = x.get().unwrap();
|
||||||
let (key_b,_) = y.get().unwrap();
|
let (key_b,_) = y.get().unwrap();
|
||||||
if *key_a < *key_b { return true; }
|
if *key_a < *key_b { return true; }
|
||||||
@ -210,32 +210,30 @@ impl <K: Ord, V> TreeMapIterator<K, V> {
|
|||||||
// Returns the current node, or None if this iterator is at the end.
|
// Returns the current node, or None if this iterator is at the end.
|
||||||
fn get(&const self) -> Option<(&self/K, &self/V)> {
|
fn get(&const self) -> Option<(&self/K, &self/V)> {
|
||||||
match self.current {
|
match self.current {
|
||||||
Some(res) => Some((&res.key, &res.value)),
|
Some(res) => Some((&res.key, &res.value)),
|
||||||
None => None
|
None => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Advance the iterator to the next node (in order). If this iterator
|
/// Advance the iterator to the next node (in order). If this iterator
|
||||||
/// is finished, does nothing.
|
/// is finished, does nothing.
|
||||||
fn next(self) -> TreeMapIterator/&self<K, V> {
|
fn map_next<K: Ord, V>(iter: &mut TreeMapIterator/&a<K, V>) {
|
||||||
let mut this = self;
|
while !iter.stack.is_empty() || iter.node.is_some() {
|
||||||
while !this.stack.is_empty() || this.node.is_some() {
|
match *iter.node {
|
||||||
match *this.node {
|
Some(ref x) => {
|
||||||
Some(ref x) => {
|
iter.stack.push(x);
|
||||||
this.stack.push(x);
|
iter.node = &x.left;
|
||||||
this.node = &x.left;
|
}
|
||||||
}
|
None => {
|
||||||
None => {
|
let res = iter.stack.pop();
|
||||||
let res = this.stack.pop();
|
iter.node = &res.right;
|
||||||
this.node = &res.right;
|
iter.current = Some(res);
|
||||||
this.current = Some(res);
|
return;
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.current = None;
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
iter.current = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TreeSet<T> {
|
pub struct TreeSet<T> {
|
||||||
@ -297,18 +295,18 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let mut x = self.iter();
|
let mut x = self.iter();
|
||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
let mut a = x.get();
|
let mut a = x.get();
|
||||||
let mut b = y.get();
|
let mut b = y.get();
|
||||||
while a.is_some() && b.is_some() {
|
while a.is_some() && b.is_some() {
|
||||||
let a1 = a.unwrap();
|
let a1 = a.unwrap();
|
||||||
let b1 = b.unwrap();
|
let b1 = b.unwrap();
|
||||||
if a1 < b1 {
|
if a1 < b1 {
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
} else if b1 < a1 {
|
} else if b1 < a1 {
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get();
|
b = y.get();
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
@ -328,8 +326,8 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let mut x = self.iter();
|
let mut x = self.iter();
|
||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
let mut a = x.get();
|
let mut a = x.get();
|
||||||
let mut b = y.get();
|
let mut b = y.get();
|
||||||
while b.is_some() {
|
while b.is_some() {
|
||||||
@ -345,10 +343,10 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !(a1 < b1) {
|
if !(a1 < b1) {
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get();
|
b = y.get();
|
||||||
}
|
}
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -361,15 +359,15 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
|
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
let mut a = x.get();
|
let mut a = x.get();
|
||||||
let mut b = y.get();
|
let mut b = y.get();
|
||||||
|
|
||||||
while a.is_some() {
|
while a.is_some() {
|
||||||
if b.is_none() {
|
if b.is_none() {
|
||||||
return do a.while_some() |a1| {
|
return do a.while_some() |a1| {
|
||||||
if f(a1) { x = x.next(); x.get() } else { None }
|
if f(a1) { set_next(&mut x); x.get() } else { None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,11 +376,11 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
|
|
||||||
if a1 < b1 {
|
if a1 < b1 {
|
||||||
if !f(a1) { return }
|
if !f(a1) { return }
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
} else {
|
} else {
|
||||||
if !(b1 < a1) { x = x.next(); a = x.get() }
|
if !(b1 < a1) { set_next(&mut x); a = x.get() }
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get();
|
b = y.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -396,15 +394,15 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
|
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
let mut a = x.get();
|
let mut a = x.get();
|
||||||
let mut b = y.get();
|
let mut b = y.get();
|
||||||
|
|
||||||
while a.is_some() {
|
while a.is_some() {
|
||||||
if b.is_none() {
|
if b.is_none() {
|
||||||
return do a.while_some() |a1| {
|
return do a.while_some() |a1| {
|
||||||
if f(a1) { x.next(); x.get() } else { None }
|
if f(a1) { set_next(&mut x); x.get() } else { None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,21 +411,21 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
|
|
||||||
if a1 < b1 {
|
if a1 < b1 {
|
||||||
if !f(a1) { return }
|
if !f(a1) { return }
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
} else {
|
} else {
|
||||||
if b1 < a1 {
|
if b1 < a1 {
|
||||||
if !f(b1) { return }
|
if !f(b1) { return }
|
||||||
} else {
|
} else {
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
}
|
}
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get();
|
b = y.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
do b.while_some |b1| {
|
do b.while_some |b1| {
|
||||||
if f(b1) { y = y.next(); y.get() } else { None }
|
if f(b1) { set_next(&mut y); y.get() } else { None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -438,8 +436,8 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
|
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
let mut a = x.get();
|
let mut a = x.get();
|
||||||
let mut b = y.get();
|
let mut b = y.get();
|
||||||
|
|
||||||
@ -447,13 +445,13 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let a1 = a.unwrap();
|
let a1 = a.unwrap();
|
||||||
let b1 = b.unwrap();
|
let b1 = b.unwrap();
|
||||||
if a1 < b1 {
|
if a1 < b1 {
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
} else {
|
} else {
|
||||||
if !(b1 < a1) {
|
if !(b1 < a1) {
|
||||||
if !f(a1) { return }
|
if !f(a1) { return }
|
||||||
}
|
}
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get();
|
b = y.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -466,15 +464,15 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
let mut y = other.iter();
|
let mut y = other.iter();
|
||||||
|
|
||||||
unsafe { // purity workaround
|
unsafe { // purity workaround
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
let mut a = x.get();
|
let mut a = x.get();
|
||||||
let mut b = y.get();
|
let mut b = y.get();
|
||||||
|
|
||||||
while a.is_some() {
|
while a.is_some() {
|
||||||
if b.is_none() {
|
if b.is_none() {
|
||||||
return do a.while_some() |a1| {
|
return do a.while_some() |a1| {
|
||||||
if f(a1) { x = x.next(); x.get() } else { None }
|
if f(a1) { set_next(&mut x); x.get() } else { None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -483,15 +481,15 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
|||||||
|
|
||||||
if b1 < a1 {
|
if b1 < a1 {
|
||||||
if !f(b1) { return }
|
if !f(b1) { return }
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get();
|
b = y.get();
|
||||||
} else {
|
} else {
|
||||||
if !f(a1) { return }
|
if !f(a1) { return }
|
||||||
if !(a1 < b1) {
|
if !(a1 < b1) {
|
||||||
y = y.next();
|
set_next(&mut y);
|
||||||
b = y.get()
|
b = y.get()
|
||||||
}
|
}
|
||||||
x = x.next();
|
set_next(&mut x);
|
||||||
a = x.get();
|
a = x.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -524,16 +522,16 @@ impl <T: Ord> TreeSetIterator<T> {
|
|||||||
/// Returns the current node, or None if this iterator is at the end.
|
/// Returns the current node, or None if this iterator is at the end.
|
||||||
fn get(&const self) -> Option<&self/T> {
|
fn get(&const self) -> Option<&self/T> {
|
||||||
match self.iter.get() {
|
match self.iter.get() {
|
||||||
None => None,
|
None => None,
|
||||||
Some((k, _)) => Some(k)
|
Some((k, _)) => Some(k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Advance the iterator to the next node (in order). If this iterator is
|
/// Advance the iterator to the next node (in order). If this iterator is
|
||||||
/// finished, does nothing.
|
/// finished, does nothing.
|
||||||
fn next(self) -> TreeSetIterator/&self<T> {
|
fn set_next<T: Ord>(iter: &mut TreeSetIterator/&a<T>) {
|
||||||
TreeSetIterator { iter: self.iter.next() }
|
map_next(&mut iter.iter);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nodes keep track of their level in the tree, starting at 1 in the
|
// Nodes keep track of their level in the tree, starting at 1 in the
|
||||||
@ -967,18 +965,18 @@ fn test_lazy_iterator() {
|
|||||||
|
|
||||||
// FIXME: #4492 (ICE): iter.next() == Some((&x1, &y1))
|
// FIXME: #4492 (ICE): iter.next() == Some((&x1, &y1))
|
||||||
|
|
||||||
iter = iter.next();
|
map_next(&mut iter);
|
||||||
assert iter.get().unwrap() == (&x1, &y1);
|
assert iter.get().unwrap() == (&x1, &y1);
|
||||||
iter = iter.next();
|
map_next(&mut iter);
|
||||||
assert iter.get().unwrap() == (&x2, &y2);
|
assert iter.get().unwrap() == (&x2, &y2);
|
||||||
iter = iter.next();
|
map_next(&mut iter);
|
||||||
assert iter.get().unwrap() == (&x3, &y3);
|
assert iter.get().unwrap() == (&x3, &y3);
|
||||||
iter = iter.next();
|
map_next(&mut iter);
|
||||||
assert iter.get().unwrap() == (&x4, &y4);
|
assert iter.get().unwrap() == (&x4, &y4);
|
||||||
iter = iter.next();
|
map_next(&mut iter);
|
||||||
assert iter.get().unwrap() == (&x5, &y5);
|
assert iter.get().unwrap() == (&x5, &y5);
|
||||||
|
|
||||||
iter = iter.next();
|
map_next(&mut iter);
|
||||||
assert iter.get().is_none();
|
assert iter.get().is_none();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user