diff options
| author | bors <bors@rust-lang.org> | 2013-05-10 20:35:00 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-05-10 20:35:00 -0700 |
| commit | 9f106a643e6cdf2f3c8d62bcec61da087ed24c5b (patch) | |
| tree | b5593b086685c556b08f9772daff3e1391b1356f /src/libstd/priority_queue.rs | |
| parent | c49cf8b3300a97201058debd63b0f7aef34d3c35 (diff) | |
| parent | 60803e5fc81ed7065c91c0b1725dbbd4da0af3f7 (diff) | |
| download | rust-9f106a643e6cdf2f3c8d62bcec61da087ed24c5b.tar.gz rust-9f106a643e6cdf2f3c8d62bcec61da087ed24c5b.zip | |
auto merge of #6260 : alexcrichton/rust/issue-3466-no-swap, r=pcwalton
There may be a more efficient implementation of `core::util::swap_ptr`. The issue mentioned using `move_val_init`, but I couldn't figure out what that did, so I just used `copy_memory` a few times instead. I'm not exactly the best at reading LLVM generated by rust, but this does appear to be optimized away just as expected (when possible).
Diffstat (limited to 'src/libstd/priority_queue.rs')
| -rw-r--r-- | src/libstd/priority_queue.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/libstd/priority_queue.rs b/src/libstd/priority_queue.rs index bdb93142472..ded632b29d9 100644 --- a/src/libstd/priority_queue.rs +++ b/src/libstd/priority_queue.rs @@ -11,6 +11,7 @@ //! A priority queue implemented with a binary heap use core::old_iter::BaseIter; +use core::util::{replace, swap}; #[abi = "rust-intrinsic"] extern "rust-intrinsic" mod rusti { @@ -73,7 +74,10 @@ pub impl <T:Ord> PriorityQueue<T> { /// Pop the greatest item from the queue - fails if empty fn pop(&mut self) -> T { let mut item = self.data.pop(); - if !self.is_empty() { item <-> self.data[0]; self.siftdown(0); } + if !self.is_empty() { + swap(&mut item, &mut self.data[0]); + self.siftdown(0); + } item } @@ -92,7 +96,7 @@ pub impl <T:Ord> PriorityQueue<T> { /// Optimized version of a push followed by a pop fn push_pop(&mut self, mut item: T) -> T { if !self.is_empty() && self.data[0] > item { - item <-> self.data[0]; + swap(&mut item, &mut self.data[0]); self.siftdown(0); } item @@ -100,7 +104,7 @@ pub impl <T:Ord> PriorityQueue<T> { /// Optimized version of a pop followed by a push - fails if empty fn replace(&mut self, mut item: T) -> T { - item <-> self.data[0]; + swap(&mut item, &mut self.data[0]); self.siftdown(0); item } @@ -115,7 +119,7 @@ pub impl <T:Ord> PriorityQueue<T> { let mut end = q.len(); while end > 1 { end -= 1; - q.data[end] <-> q.data[0]; + vec::swap(q.data, 0, end); q.siftdown_range(0, end) } q.to_vec() @@ -149,8 +153,7 @@ pub impl <T:Ord> PriorityQueue<T> { while pos > start { let parent = (pos - 1) >> 1; if new > self.data[parent] { - let mut x = rusti::uninit(); - x <-> self.data[parent]; + let x = replace(&mut self.data[parent], rusti::uninit()); rusti::move_val_init(&mut self.data[pos], x); pos = parent; loop @@ -169,8 +172,7 @@ pub impl <T:Ord> PriorityQueue<T> { while pos > start { let parent = (pos - 1) >> 1; if new > self.data[parent] { - let mut x = rusti::init(); - x <-> self.data[parent]; + let x = replace(&mut self.data[parent], rusti::init()); rusti::move_val_init(&mut self.data[pos], x); pos = parent; loop @@ -194,8 +196,7 @@ pub impl <T:Ord> PriorityQueue<T> { if right < end && !(self.data[child] > self.data[right]) { child = right; } - let mut x = rusti::uninit(); - x <-> self.data[child]; + let x = replace(&mut self.data[child], rusti::uninit()); rusti::move_val_init(&mut self.data[pos], x); pos = child; child = 2 * pos + 1; @@ -218,8 +219,7 @@ pub impl <T:Ord> PriorityQueue<T> { if right < end && !(self.data[child] > self.data[right]) { child = right; } - let mut x = rusti::init(); - x <-> self.data[child]; + let x = replace(&mut self.data[child], rusti::init()); rusti::move_val_init(&mut self.data[pos], x); pos = child; child = 2 * pos + 1; |
