Skip to content

Commit

Permalink
Move binarysort out of MergeState
Browse files Browse the repository at this point in the history
  • Loading branch information
14427 committed Oct 25, 2012
1 parent 98c8a40 commit f2216ec
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/libstd/sort.rs
Expand Up @@ -172,16 +172,16 @@ pub fn tim_sort<T: Copy Ord>(array: &[mut T]) {
return;
}

let ms = &MergeState();
ms.array = array;
let min_run = min_run_length(size);

if size < MIN_MERGE {
let init_run_len = count_run_ascending(array);
ms.binarysort(array, init_run_len);
binarysort(array, init_run_len);
return;
}

let ms = &MergeState();
ms.array = array;
let min_run = min_run_length(size);

let mut idx = 0;
let mut remaining = size;
loop {
Expand All @@ -191,7 +191,7 @@ pub fn tim_sort<T: Copy Ord>(array: &[mut T]) {
if run_len < min_run {
let force = if remaining <= min_run {remaining} else {min_run};
let slice = vec::mut_view(arr, 0, force);
ms.binarysort(slice, run_len);
binarysort(slice, run_len);
run_len = force;
}

Expand All @@ -206,6 +206,36 @@ pub fn tim_sort<T: Copy Ord>(array: &[mut T]) {
ms.merge_force_collapse(array);
}

fn binarysort<T: Copy Ord>(array: &[mut T], start: uint) {
let size = array.len();
let mut start = start;
assert start <= size;

if start == 0 { start += 1; }

while start < size {
let pivot = array[start];
let mut left = 0;
let mut right = start;
assert left <= right;

while left < right {
let mid = (left + right) >> 1;
if pivot < array[mid] {
right = mid;
} else {
left = mid+1;
}
}
assert left == right;
let mut n = start-left;

copy_vec(array, left+1, array, left, n);
array[left] = move pivot;
start += 1;
}
}

// Reverse the order of elements in a slice, in place
fn reverse_slice<T>(v: &[mut T], start: uint, end:uint) {
let mut i = start;
Expand Down Expand Up @@ -366,36 +396,6 @@ fn MergeState<T>() -> MergeState<T> {
}

impl<T: Copy Ord> MergeState<T> {
fn binarysort(&self, array: &[mut T], start: uint) {
let size = array.len();
let mut start = start;
assert start <= size;

if start == 0 { start += 1; }

while start < size {
let pivot = array[start];
let mut left = 0;
let mut right = start;
assert left <= right;

while left < right {
let mid = (left + right) >> 1;
if pivot < array[mid] {
right = mid;
} else {
left = mid+1;
}
}
assert left == right;
let mut n = start-left;

copy_vec(array, left+1, array, left, n);
array[left] = move pivot;
start += 1;
}
}

fn push_run(&self, run_base: uint, run_len: uint) {
let tmp = RunState{base: run_base, len: run_len};
self.runs.push(tmp);
Expand Down

0 comments on commit f2216ec

Please sign in to comment.