Skip to content

revert PriorityQueue to using init() #6461

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
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
60 changes: 4 additions & 56 deletions src/libstd/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@

use core::old_iter::BaseIter;
use core::util::{replace, swap};

#[abi = "rust-intrinsic"]
extern "rust-intrinsic" {
fn move_val_init<T>(dst: &mut T, src: T);
fn init<T>() -> T;
#[cfg(not(stage0))]
fn uninit<T>() -> T;
}
use core::unstable::intrinsics::{init, move_val_init};

pub struct PriorityQueue<T> {
priv data: ~[T],
Expand Down Expand Up @@ -141,33 +134,13 @@ pub impl <T:Ord> PriorityQueue<T> {

// The implementations of siftup and siftdown use unsafe blocks in
// order to move an element out of the vector (leaving behind a
// junk element), shift along the others and move it back into the
// zeroed element), shift along the others and move it back into the
// vector over the junk element. This reduces the constant factor
// compared to using swaps, which involves twice as many moves.

#[cfg(not(stage0))]
priv fn siftup(&mut self, start: uint, mut pos: uint) {
unsafe {
let new = *ptr::to_unsafe_ptr(&self.data[pos]);

while pos > start {
let parent = (pos - 1) >> 1;
if new > self.data[parent] {
let x = replace(&mut self.data[parent], uninit());
move_val_init(&mut self.data[pos], x);
pos = parent;
loop
}
break
}
move_val_init(&mut self.data[pos], new);
}
}

#[cfg(stage0)]
priv fn siftup(&mut self, start: uint, mut pos: uint) {
unsafe {
let new = *ptr::to_unsafe_ptr(&self.data[pos]);
let new = replace(&mut self.data[pos], init());

while pos > start {
let parent = (pos - 1) >> 1;
Expand All @@ -183,35 +156,10 @@ pub impl <T:Ord> PriorityQueue<T> {
}
}


#[cfg(not(stage0))]
priv fn siftdown_range(&mut self, mut pos: uint, end: uint) {
unsafe {
let start = pos;
let new = *ptr::to_unsafe_ptr(&self.data[pos]);

let mut child = 2 * pos + 1;
while child < end {
let right = child + 1;
if right < end && !(self.data[child] > self.data[right]) {
child = right;
}
let x = replace(&mut self.data[child], uninit());
move_val_init(&mut self.data[pos], x);
pos = child;
child = 2 * pos + 1;
}

move_val_init(&mut self.data[pos], new);
self.siftup(start, pos);
}
}

#[cfg(stage0)]
priv fn siftdown_range(&mut self, mut pos: uint, end: uint) {
unsafe {
let start = pos;
let new = *ptr::to_unsafe_ptr(&self.data[pos]);
let new = replace(&mut self.data[pos], init());

let mut child = 2 * pos + 1;
while child < end {
Expand Down