2012-07-04 16:53:12 -05:00
|
|
|
//! Sorting methods
|
2012-08-31 18:41:15 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use vec::{len, push};
|
|
|
|
use core::cmp::{Eq, Ord};
|
2012-09-25 17:53:24 -05:00
|
|
|
use dvec::DVec;
|
2011-08-12 00:48:08 -05:00
|
|
|
|
2012-09-04 16:37:47 -05:00
|
|
|
type Le<T> = pure fn(v1: &T, v2: &T) -> bool;
|
2010-12-21 02:44:06 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Merge sort. Returns a new vector containing the sorted list.
|
|
|
|
*
|
|
|
|
* Has worst case O(n log n) performance, best case O(n), but
|
|
|
|
* is not space efficient. This is a stable sort.
|
|
|
|
*/
|
2012-11-17 12:21:14 -06:00
|
|
|
pub pure fn merge_sort<T: Copy>(v: &[const T], le: Le<T>) -> ~[T] {
|
2012-09-04 16:37:47 -05:00
|
|
|
type Slice = (uint, uint);
|
2012-02-27 20:32:45 -06:00
|
|
|
|
2012-11-17 12:21:14 -06:00
|
|
|
unsafe {return merge_sort_(v, (0u, len(v)), le);}
|
2012-02-27 20:32:45 -06:00
|
|
|
|
2012-09-27 19:05:13 -05:00
|
|
|
fn merge_sort_<T: Copy>(v: &[const T], slice: Slice, le: Le<T>)
|
2012-06-29 18:26:56 -05:00
|
|
|
-> ~[T] {
|
2012-07-16 16:32:59 -05:00
|
|
|
let begin = slice.first();
|
|
|
|
let end = slice.second();
|
2012-02-27 20:32:45 -06:00
|
|
|
|
|
|
|
let v_len = end - begin;
|
2012-10-23 13:11:23 -05:00
|
|
|
if v_len == 0 { return ~[]; }
|
|
|
|
if v_len == 1 { return ~[v[begin]]; }
|
2012-02-27 20:32:45 -06:00
|
|
|
|
2012-10-23 13:11:23 -05:00
|
|
|
let mid = v_len / 2 + begin;
|
2012-02-27 20:32:45 -06:00
|
|
|
let a = (begin, mid);
|
|
|
|
let b = (mid, end);
|
2012-09-27 19:05:13 -05:00
|
|
|
return merge(le, merge_sort_(v, a, le), merge_sort_(v, b, le));
|
2012-02-27 20:32:45 -06:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn merge<T: Copy>(le: Le<T>, a: &[T], b: &[T]) -> ~[T] {
|
2012-09-21 20:43:30 -05:00
|
|
|
let mut rs = vec::with_capacity(len(a) + len(b));
|
2012-02-27 20:32:45 -06:00
|
|
|
let a_len = len(a);
|
2012-10-23 13:11:23 -05:00
|
|
|
let mut a_ix = 0;
|
2012-02-27 20:32:45 -06:00
|
|
|
let b_len = len(b);
|
2012-10-23 13:11:23 -05:00
|
|
|
let mut b_ix = 0;
|
2011-07-27 07:19:39 -05:00
|
|
|
while a_ix < a_len && b_ix < b_len {
|
2012-08-02 17:42:56 -05:00
|
|
|
if le(&a[a_ix], &b[b_ix]) {
|
2012-09-26 19:33:34 -05:00
|
|
|
rs.push(a[a_ix]);
|
2012-10-23 13:11:23 -05:00
|
|
|
a_ix += 1;
|
|
|
|
} else { rs.push(b[b_ix]); b_ix += 1; }
|
2011-03-16 16:58:02 -05:00
|
|
|
}
|
2012-11-17 21:43:02 -06:00
|
|
|
rs.push_all(vec::slice(a, a_ix, a_len));
|
|
|
|
rs.push_all(vec::slice(b, b_ix, b_len));
|
|
|
|
move rs
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-27 19:05:13 -05:00
|
|
|
fn part<T: Copy>(arr: &[mut T], left: uint,
|
|
|
|
right: uint, pivot: uint, compare_func: Le<T>) -> uint {
|
2011-08-19 17:16:48 -05:00
|
|
|
let pivot_value = arr[pivot];
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[pivot] <-> arr[right];
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut storage_index: uint = left;
|
|
|
|
let mut i: uint = left;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < right {
|
2012-08-02 17:42:56 -05:00
|
|
|
if compare_func(&arr[i], &pivot_value) {
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[storage_index];
|
2012-10-23 13:11:23 -05:00
|
|
|
storage_index += 1;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
2012-10-23 13:11:23 -05:00
|
|
|
i += 1;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[storage_index] <-> arr[right];
|
2012-08-01 19:30:05 -05:00
|
|
|
return storage_index;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
|
2012-09-27 19:05:13 -05:00
|
|
|
fn qsort<T: Copy>(arr: &[mut T], left: uint,
|
|
|
|
right: uint, compare_func: Le<T>) {
|
2011-07-27 07:19:39 -05:00
|
|
|
if right > left {
|
|
|
|
let pivot = (left + right) / 2u;
|
2012-09-27 19:05:13 -05:00
|
|
|
let new_pivot = part::<T>(arr, left, right, pivot, compare_func);
|
2011-07-27 07:19:39 -05:00
|
|
|
if new_pivot != 0u {
|
2011-07-18 17:01:47 -05:00
|
|
|
// Need to do this check before recursing due to overflow
|
2012-09-27 19:05:13 -05:00
|
|
|
qsort::<T>(arr, left, new_pivot - 1u, compare_func);
|
2011-07-18 17:01:47 -05:00
|
|
|
}
|
2012-09-27 19:05:13 -05:00
|
|
|
qsort::<T>(arr, new_pivot + 1u, right, compare_func);
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Quicksort. Sorts a mut vector in place.
|
|
|
|
*
|
|
|
|
* Has worst case O(n^2) performance, average case O(n log n).
|
|
|
|
* This is an unstable sort.
|
|
|
|
*/
|
2012-10-03 21:47:09 -05:00
|
|
|
pub fn quick_sort<T: Copy>(arr: &[mut T], compare_func: Le<T>) {
|
2012-08-01 19:30:05 -05:00
|
|
|
if len::<T>(arr) == 0u { return; }
|
2012-09-27 19:05:13 -05:00
|
|
|
qsort::<T>(arr, 0u, len::<T>(arr) - 1u, compare_func);
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn qsort3<T: Copy Ord Eq>(arr: &[mut T], left: int, right: int) {
|
2012-08-01 19:30:05 -05:00
|
|
|
if right <= left { return; }
|
2011-08-19 17:16:48 -05:00
|
|
|
let v: T = arr[right];
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i: int = left - 1;
|
|
|
|
let mut j: int = right;
|
|
|
|
let mut p: int = i;
|
|
|
|
let mut q: int = j;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2011-05-24 12:12:32 -05:00
|
|
|
i += 1;
|
2012-08-27 23:17:42 -05:00
|
|
|
while arr[i] < v { i += 1; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
2012-08-27 23:17:42 -05:00
|
|
|
while v < arr[j] {
|
2011-07-27 07:19:39 -05:00
|
|
|
if j == left { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
if i >= j { break; }
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[j];
|
2012-08-27 23:17:42 -05:00
|
|
|
if arr[i] == v {
|
2011-05-24 12:12:32 -05:00
|
|
|
p += 1;
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[p] <-> arr[i];
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
2012-08-27 23:17:42 -05:00
|
|
|
if v == arr[j] {
|
2011-05-24 12:12:32 -05:00
|
|
|
q -= 1;
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[j] <-> arr[q];
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[right];
|
2011-05-24 12:12:32 -05:00
|
|
|
j = i - 1;
|
|
|
|
i += 1;
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut k: int = left;
|
2011-07-27 07:19:39 -05:00
|
|
|
while k < p {
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[k] <-> arr[j];
|
2011-05-24 12:12:32 -05:00
|
|
|
k += 1;
|
|
|
|
j -= 1;
|
2011-08-12 12:56:57 -05:00
|
|
|
if k == len::<T>(arr) as int { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
k = right - 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
while k > q {
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[k];
|
2011-05-24 12:12:32 -05:00
|
|
|
k -= 1;
|
|
|
|
i += 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
if k == 0 { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
2012-08-27 23:17:42 -05:00
|
|
|
qsort3::<T>(arr, left, j);
|
|
|
|
qsort3::<T>(arr, i, right);
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Fancy quicksort. Sorts a mut vector in place.
|
|
|
|
*
|
2012-07-11 18:49:02 -05:00
|
|
|
* Based on algorithm presented by ~[Sedgewick and Bentley]
|
2012-07-04 16:53:12 -05:00
|
|
|
* (http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf).
|
|
|
|
* According to these slides this is the algorithm of choice for
|
|
|
|
* 'randomly ordered keys, abstract compare' & 'small number of key values'.
|
|
|
|
*
|
|
|
|
* This is an unstable sort.
|
|
|
|
*/
|
2012-09-27 20:21:32 -05:00
|
|
|
pub fn quick_sort3<T: Copy Ord Eq>(arr: &[mut T]) {
|
2012-08-02 17:42:56 -05:00
|
|
|
if arr.len() <= 1 { return; }
|
2012-08-27 23:17:42 -05:00
|
|
|
qsort3(arr, 0, (arr.len() - 1) as int);
|
2011-06-30 21:09:30 -05:00
|
|
|
}
|
|
|
|
|
2012-09-27 20:21:32 -05:00
|
|
|
pub trait Sort {
|
2012-09-03 17:58:01 -05:00
|
|
|
fn qsort(self);
|
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
impl<T: Copy Ord Eq> &[mut T] : Sort {
|
2012-09-03 17:58:01 -05:00
|
|
|
fn qsort(self) { quick_sort3(self); }
|
|
|
|
}
|
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
const MIN_MERGE: uint = 64;
|
|
|
|
const MIN_GALLOP: uint = 7;
|
|
|
|
const INITIAL_TMP_STORAGE: uint = 128;
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
pub fn tim_sort<T: Copy Ord>(array: &[mut T]) {
|
2012-09-25 17:53:24 -05:00
|
|
|
let size = array.len();
|
|
|
|
if size < 2 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if size < MIN_MERGE {
|
2012-10-01 11:34:04 -05:00
|
|
|
let init_run_len = count_run_ascending(array);
|
2012-10-24 20:42:41 -05:00
|
|
|
binarysort(array, init_run_len);
|
2012-09-25 17:53:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-24 20:42:41 -05:00
|
|
|
let ms = &MergeState();
|
|
|
|
let min_run = min_run_length(size);
|
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
let mut idx = 0;
|
|
|
|
let mut remaining = size;
|
|
|
|
loop {
|
|
|
|
let arr = vec::mut_view(array, idx, size);
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut run_len: uint = count_run_ascending(arr);
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
if run_len < min_run {
|
|
|
|
let force = if remaining <= min_run {remaining} else {min_run};
|
2012-09-25 17:53:24 -05:00
|
|
|
let slice = vec::mut_view(arr, 0, force);
|
2012-10-24 20:42:41 -05:00
|
|
|
binarysort(slice, run_len);
|
2012-10-01 11:34:04 -05:00
|
|
|
run_len = force;
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
ms.push_run(idx, run_len);
|
|
|
|
ms.merge_collapse(array);
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
idx += run_len;
|
|
|
|
remaining -= run_len;
|
2012-09-27 20:44:14 -05:00
|
|
|
if remaining == 0 { break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
ms.merge_force_collapse(array);
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
|
2012-10-24 20:42:41 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:42:09 -05:00
|
|
|
// Reverse the order of elements in a slice, in place
|
2012-10-01 11:34:04 -05:00
|
|
|
fn reverse_slice<T>(v: &[mut T], start: uint, end:uint) {
|
2012-09-25 17:53:24 -05:00
|
|
|
let mut i = start;
|
|
|
|
while i < end / 2 {
|
2012-10-24 19:15:11 -05:00
|
|
|
util::swap(&mut v[i], &mut v[end - i - 1]);
|
2012-09-25 17:53:24 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
pure fn min_run_length(n: uint) -> uint {
|
2012-09-25 17:53:24 -05:00
|
|
|
let mut n = n;
|
|
|
|
let mut r = 0; // becomes 1 if any 1 bits are shifted off
|
|
|
|
|
|
|
|
while n >= MIN_MERGE {
|
|
|
|
r |= n & 1;
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
return n + r;
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
fn count_run_ascending<T: Copy Ord>(array: &[mut T]) -> uint {
|
2012-09-25 17:53:24 -05:00
|
|
|
let size = array.len();
|
|
|
|
assert size > 0;
|
|
|
|
if size == 1 { return 1; }
|
|
|
|
|
|
|
|
let mut run = 2;
|
|
|
|
if array[1] < array[0] {
|
|
|
|
while run < size && array[run] < array[run-1] {
|
|
|
|
run += 1;
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
reverse_slice(array, 0, run);
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
|
|
|
while run < size && array[run] >= array[run-1] {
|
|
|
|
run += 1;
|
|
|
|
}
|
|
|
|
}
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
return run;
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
pure fn gallop_left<T: Copy Ord>(key: &const T, array: &[const T],
|
2012-09-27 20:44:14 -05:00
|
|
|
hint: uint) -> uint {
|
2012-09-25 17:53:24 -05:00
|
|
|
let size = array.len();
|
|
|
|
assert size != 0 && hint < size;
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut last_ofs = 0;
|
2012-09-25 17:53:24 -05:00
|
|
|
let mut ofs = 1;
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
if *key > array[hint] {
|
2012-10-01 11:34:04 -05:00
|
|
|
// Gallop right until array[hint+last_ofs] < key <= array[hint+ofs]
|
|
|
|
let max_ofs = size - hint;
|
|
|
|
while ofs < max_ofs && *key > array[hint+ofs] {
|
|
|
|
last_ofs = ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs = (ofs << 1) + 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs < last_ofs { ofs = max_ofs; } // uint overflow guard
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs > max_ofs { ofs = max_ofs; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
last_ofs += hint;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs += hint;
|
|
|
|
} else {
|
2012-10-01 11:34:04 -05:00
|
|
|
let max_ofs = hint + 1;
|
|
|
|
while ofs < max_ofs && *key <= array[hint-ofs] {
|
|
|
|
last_ofs = ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs = (ofs << 1) + 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs < last_ofs { ofs = max_ofs; } // uint overflow guard
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs > max_ofs { ofs = max_ofs; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let tmp = last_ofs;
|
|
|
|
last_ofs = hint - ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs = hint - tmp;
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
assert (last_ofs < ofs || last_ofs+1 < ofs+1) && ofs <= size;
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
last_ofs += 1;
|
|
|
|
while last_ofs < ofs {
|
|
|
|
let m = last_ofs + ((ofs - last_ofs) >> 1);
|
2012-09-25 17:53:24 -05:00
|
|
|
if *key > array[m] {
|
2012-10-01 11:34:04 -05:00
|
|
|
last_ofs = m+1;
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
|
|
|
ofs = m;
|
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
assert last_ofs == ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
return ofs;
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
pure fn gallop_right<T: Copy Ord>(key: &const T, array: &[const T],
|
2012-09-27 19:05:13 -05:00
|
|
|
hint: uint) -> uint {
|
2012-09-25 17:53:24 -05:00
|
|
|
let size = array.len();
|
|
|
|
assert size != 0 && hint < size;
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut last_ofs = 0;
|
2012-09-25 17:53:24 -05:00
|
|
|
let mut ofs = 1;
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
if *key >= array[hint] {
|
2012-10-01 11:34:04 -05:00
|
|
|
// Gallop right until array[hint+last_ofs] <= key < array[hint+ofs]
|
|
|
|
let max_ofs = size - hint;
|
|
|
|
while ofs < max_ofs && *key >= array[hint+ofs] {
|
|
|
|
last_ofs = ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs = (ofs << 1) + 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs < last_ofs { ofs = max_ofs; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs > max_ofs { ofs = max_ofs; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
last_ofs += hint;
|
2012-09-27 20:44:14 -05:00
|
|
|
ofs += hint;
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
2012-10-01 11:34:04 -05:00
|
|
|
// Gallop left until array[hint-ofs] <= key < array[hint-last_ofs]
|
|
|
|
let max_ofs = hint + 1;
|
|
|
|
while ofs < max_ofs && *key < array[hint-ofs] {
|
|
|
|
last_ofs = ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs = (ofs << 1) + 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs < last_ofs { ofs = max_ofs; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if ofs > max_ofs { ofs = max_ofs; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let tmp = last_ofs;
|
|
|
|
last_ofs = hint - ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
ofs = hint - tmp;
|
|
|
|
}
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
assert (last_ofs < ofs || last_ofs+1 < ofs+1) && ofs <= size;
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
last_ofs += 1;
|
|
|
|
while last_ofs < ofs {
|
|
|
|
let m = last_ofs + ((ofs - last_ofs) >> 1);
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
if *key >= array[m] {
|
2012-10-01 11:34:04 -05:00
|
|
|
last_ofs = m + 1;
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
|
|
|
ofs = m;
|
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
assert last_ofs == ofs;
|
2012-09-25 17:53:24 -05:00
|
|
|
return ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RunState {
|
|
|
|
base: uint,
|
|
|
|
len: uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MergeState<T> {
|
2012-10-01 11:34:04 -05:00
|
|
|
mut min_gallop: uint,
|
2012-09-25 17:53:24 -05:00
|
|
|
runs: DVec<RunState>,
|
|
|
|
}
|
|
|
|
|
2012-10-25 11:30:41 -05:00
|
|
|
// Fixme (#3853) Move into MergeState
|
2012-09-25 17:53:24 -05:00
|
|
|
fn MergeState<T>() -> MergeState<T> {
|
|
|
|
MergeState {
|
2012-10-01 11:34:04 -05:00
|
|
|
min_gallop: MIN_GALLOP,
|
2012-09-25 17:53:24 -05:00
|
|
|
runs: DVec(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
impl<T: Copy Ord> MergeState<T> {
|
2012-10-22 21:44:43 -05:00
|
|
|
fn push_run(&self, run_base: uint, run_len: uint) {
|
2012-10-01 11:34:04 -05:00
|
|
|
let tmp = RunState{base: run_base, len: run_len};
|
2012-09-25 17:53:24 -05:00
|
|
|
self.runs.push(tmp);
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:44:43 -05:00
|
|
|
fn merge_at(&self, n: uint, array: &[mut T]) {
|
2012-09-25 17:53:24 -05:00
|
|
|
let mut size = self.runs.len();
|
|
|
|
assert size >= 2;
|
|
|
|
assert n == size-2 || n == size-3;
|
|
|
|
|
|
|
|
do self.runs.borrow_mut |arr| {
|
|
|
|
|
|
|
|
let mut b1 = arr[n].base;
|
|
|
|
let mut l1 = arr[n].len;
|
|
|
|
let b2 = arr[n+1].base;
|
|
|
|
let l2 = arr[n+1].len;
|
|
|
|
|
|
|
|
assert l1 > 0 && l2 > 0;
|
|
|
|
assert b1 + l1 == b2;
|
|
|
|
|
|
|
|
arr[n].len = l1 + l2;
|
|
|
|
if n == size-3 {
|
|
|
|
arr[n+1].base = arr[n+2].base;
|
|
|
|
arr[n+1].len = arr[n+2].len;
|
|
|
|
}
|
|
|
|
|
|
|
|
let slice = vec::mut_view(array, b1, b1+l1);
|
2012-10-01 11:34:04 -05:00
|
|
|
let k = gallop_right(&const array[b2], slice, 0);
|
2012-09-25 17:53:24 -05:00
|
|
|
b1 += k;
|
|
|
|
l1 -= k;
|
|
|
|
if l1 != 0 {
|
|
|
|
let slice = vec::mut_view(array, b2, b2+l2);
|
2012-10-01 11:34:04 -05:00
|
|
|
let l2 = gallop_left(
|
2012-09-25 17:53:24 -05:00
|
|
|
&const array[b1+l1-1],slice,l2-1);
|
|
|
|
if l2 > 0 {
|
|
|
|
if l1 <= l2 {
|
2012-10-01 11:34:04 -05:00
|
|
|
self.merge_lo(array, b1, l1, b2, l2);
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
2012-10-01 11:34:04 -05:00
|
|
|
self.merge_hi(array, b1, l1, b2, l2);
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.runs.pop();
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:44:43 -05:00
|
|
|
fn merge_lo(&self, array: &[mut T], base1: uint, len1: uint,
|
2012-09-27 19:05:13 -05:00
|
|
|
base2: uint, len2: uint) {
|
2012-09-25 17:53:24 -05:00
|
|
|
assert len1 != 0 && len2 != 0 && base1+len1 == base2;
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
let tmp = vec::to_mut(vec::slice(array, base1, base1+len1));
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
let mut c1 = 0;
|
|
|
|
let mut c2 = base2;
|
|
|
|
let mut dest = base1;
|
|
|
|
let mut len1 = len1;
|
|
|
|
let mut len2 = len2;
|
|
|
|
|
|
|
|
array[dest] <-> array[c2];
|
|
|
|
dest += 1; c2 += 1; len2 -= 1;
|
|
|
|
|
|
|
|
if len2 == 0 {
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest, tmp, 0, len1);
|
2012-09-25 17:53:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if len1 == 1 {
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest, array, c2, len2);
|
|
|
|
array[dest+len2] <-> tmp[c1];
|
2012-09-25 17:53:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut min_gallop = self.min_gallop;
|
2012-09-25 17:53:24 -05:00
|
|
|
loop {
|
|
|
|
let mut count1 = 0;
|
|
|
|
let mut count2 = 0;
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut break_outer = false;
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
loop {
|
|
|
|
assert len1 > 1 && len2 != 0;
|
2012-10-24 19:15:11 -05:00
|
|
|
if array[c2] < tmp[c1] {
|
2012-09-25 17:53:24 -05:00
|
|
|
array[dest] <-> array[c2];
|
|
|
|
dest += 1; c2 += 1; len2 -= 1;
|
|
|
|
count2 += 1; count1 = 0;
|
|
|
|
if len2 == 0 {
|
2012-10-01 11:34:04 -05:00
|
|
|
break_outer = true;
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-24 19:15:11 -05:00
|
|
|
array[dest] <-> tmp[c1];
|
2012-09-25 17:53:24 -05:00
|
|
|
dest += 1; c1 += 1; len1 -= 1;
|
|
|
|
count1 += 1; count2 = 0;
|
|
|
|
if len1 == 1 {
|
2012-10-01 11:34:04 -05:00
|
|
|
break_outer = true;
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if break_outer || ((count1 | count2) >= min_gallop) {
|
2012-09-25 17:53:24 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if break_outer { break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
// Start to gallop
|
|
|
|
loop {
|
|
|
|
assert len1 > 1 && len2 != 0;
|
|
|
|
|
2012-10-25 11:30:41 -05:00
|
|
|
let tmp_view = vec::const_view(tmp, c1, c1+len1);
|
|
|
|
count1 = gallop_right(&const array[c2], tmp_view, 0);
|
2012-09-25 17:53:24 -05:00
|
|
|
if count1 != 0 {
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest, tmp, c1, count1);
|
2012-09-25 17:53:24 -05:00
|
|
|
dest += count1; c1 += count1; len1 -= count1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if len1 <= 1 { break_outer = true; break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
array[dest] <-> array[c2];
|
|
|
|
dest += 1; c2 += 1; len2 -= 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if len2 == 0 { break_outer = true; break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-04 11:24:55 -05:00
|
|
|
let tmp_view = vec::const_view(array, c2, c2+len2);
|
2012-10-24 19:15:11 -05:00
|
|
|
count2 = gallop_left(&const tmp[c1], tmp_view, 0);
|
2012-09-25 17:53:24 -05:00
|
|
|
if count2 != 0 {
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest, array, c2, count2);
|
2012-09-25 17:53:24 -05:00
|
|
|
dest += count2; c2 += count2; len2 -= count2;
|
2012-10-01 11:34:04 -05:00
|
|
|
if len2 == 0 { break_outer = true; break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-24 19:15:11 -05:00
|
|
|
array[dest] <-> tmp[c1];
|
2012-09-25 17:53:24 -05:00
|
|
|
dest += 1; c1 += 1; len1 -= 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if len1 == 1 { break_outer = true; break; }
|
|
|
|
min_gallop -= 1;
|
2012-09-27 19:05:13 -05:00
|
|
|
if !(count1 >= MIN_GALLOP || count2 >= MIN_GALLOP) {
|
|
|
|
break;
|
2012-09-27 20:44:14 -05:00
|
|
|
}
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if break_outer { break; }
|
|
|
|
if min_gallop < 0 { min_gallop = 0; }
|
|
|
|
min_gallop += 2; // Penalize for leaving gallop
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
self.min_gallop = if min_gallop < 1 { 1 } else { min_gallop };
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
if len1 == 1 {
|
|
|
|
assert len2 > 0;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest, array, c2, len2);
|
|
|
|
array[dest+len2] <-> tmp[c1];
|
2012-09-25 17:53:24 -05:00
|
|
|
} else if len1 == 0 {
|
2012-10-06 12:44:56 -05:00
|
|
|
fail ~"Comparison violates its contract!";
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
|
|
|
assert len2 == 0;
|
|
|
|
assert len1 > 1;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest, tmp, c1, len1);
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:44:43 -05:00
|
|
|
fn merge_hi(&self, array: &[mut T], base1: uint, len1: uint,
|
2012-09-27 19:05:13 -05:00
|
|
|
base2: uint, len2: uint) {
|
2012-09-25 17:53:24 -05:00
|
|
|
assert len1 != 1 && len2 != 0 && base1 + len1 == base2;
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
let tmp = vec::to_mut(vec::slice(array, base2, base2+len2));
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
let mut c1 = base1 + len1 - 1;
|
|
|
|
let mut c2 = len2 - 1;
|
|
|
|
let mut dest = base2 + len2 - 1;
|
|
|
|
let mut len1 = len1;
|
|
|
|
let mut len2 = len2;
|
|
|
|
|
|
|
|
array[dest] <-> array[c1];
|
|
|
|
dest -= 1; c1 -= 1; len1 -= 1;
|
|
|
|
|
|
|
|
if len1 == 0 {
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest-(len2-1), tmp, 0, len2);
|
2012-09-25 17:53:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if len2 == 1 {
|
|
|
|
dest -= len1;
|
|
|
|
c1 -= len1;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest+1, array, c1+1, len1);
|
|
|
|
array[dest] <-> tmp[c2];
|
2012-09-25 17:53:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut min_gallop = self.min_gallop;
|
2012-09-25 17:53:24 -05:00
|
|
|
loop {
|
|
|
|
let mut count1 = 0;
|
|
|
|
let mut count2 = 0;
|
2012-10-01 11:34:04 -05:00
|
|
|
let mut break_outer = false;
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
loop {
|
|
|
|
assert len1 != 0 && len2 > 1;
|
2012-10-24 19:15:11 -05:00
|
|
|
if tmp[c2] < array[c1] {
|
2012-09-25 17:53:24 -05:00
|
|
|
array[dest] <-> array[c1];
|
|
|
|
dest -= 1; c1 -= 1; len1 -= 1;
|
|
|
|
count1 += 1; count2 = 0;
|
|
|
|
if len1 == 0 {
|
2012-10-01 11:34:04 -05:00
|
|
|
break_outer = true;
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-24 19:15:11 -05:00
|
|
|
array[dest] <-> tmp[c2];
|
2012-09-25 17:53:24 -05:00
|
|
|
dest -= 1; c2 -= 1; len2 -= 1;
|
|
|
|
count2 += 1; count1 = 0;
|
|
|
|
if len2 == 1 {
|
2012-10-01 11:34:04 -05:00
|
|
|
break_outer = true;
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if break_outer || ((count1 | count2) >= min_gallop) {
|
2012-09-25 17:53:24 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
if break_outer { break; }
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
// Start to gallop
|
|
|
|
loop {
|
|
|
|
assert len2 > 1 && len1 != 0;
|
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
let tmp_view = vec::mut_view(array, base1, base1+len1);
|
|
|
|
count1 = len1 - gallop_right(
|
2012-10-24 19:15:11 -05:00
|
|
|
&const tmp[c2], tmp_view, len1-1);
|
2012-09-25 17:53:24 -05:00
|
|
|
|
|
|
|
if count1 != 0 {
|
|
|
|
dest -= count1; c1 -= count1; len1 -= count1;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest+1, array, c1+1, count1);
|
2012-10-01 11:34:04 -05:00
|
|
|
if len1 == 0 { break_outer = true; break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
array[dest] <-> tmp[c2];
|
2012-09-25 17:53:24 -05:00
|
|
|
dest -= 1; c2 -= 1; len2 -= 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if len2 == 1 { break_outer = true; break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
|
2012-10-25 11:30:41 -05:00
|
|
|
let tmp_view = vec::mut_view(tmp, 0, len2);
|
2012-10-04 11:24:55 -05:00
|
|
|
let count2 = len2 - gallop_left(&const array[c1],
|
2012-10-25 11:30:41 -05:00
|
|
|
tmp_view, len2-1);
|
2012-09-25 17:53:24 -05:00
|
|
|
if count2 != 0 {
|
|
|
|
dest -= count2; c2 -= count2; len2 -= count2;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest+1, tmp, c2+1, count2);
|
2012-10-01 11:34:04 -05:00
|
|
|
if len2 <= 1 { break_outer = true; break; }
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
array[dest] <-> array[c1];
|
|
|
|
dest -= 1; c1 -= 1; len1 -= 1;
|
2012-10-01 11:34:04 -05:00
|
|
|
if len1 == 0 { break_outer = true; break; }
|
|
|
|
min_gallop -= 1;
|
2012-09-27 19:05:13 -05:00
|
|
|
if !(count1 >= MIN_GALLOP || count2 >= MIN_GALLOP) {
|
|
|
|
break;
|
2012-09-27 20:44:14 -05:00
|
|
|
}
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-10-01 11:34:04 -05:00
|
|
|
if break_outer { break; }
|
|
|
|
if min_gallop < 0 { min_gallop = 0; }
|
|
|
|
min_gallop += 2; // Penalize for leaving gallop
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
self.min_gallop = if min_gallop < 1 { 1 } else { min_gallop };
|
2012-09-27 20:44:14 -05:00
|
|
|
|
2012-09-25 17:53:24 -05:00
|
|
|
if len2 == 1 {
|
|
|
|
assert len1 > 0;
|
|
|
|
dest -= len1;
|
|
|
|
c1 -= len1;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest+1, array, c1+1, len1);
|
|
|
|
array[dest] <-> tmp[c2];
|
2012-09-25 17:53:24 -05:00
|
|
|
} else if len2 == 0 {
|
2012-10-22 21:53:37 -05:00
|
|
|
fail ~"Comparison violates its contract!";
|
2012-09-25 17:53:24 -05:00
|
|
|
} else {
|
|
|
|
assert len1 == 0;
|
|
|
|
assert len2 != 0;
|
2012-10-24 19:15:11 -05:00
|
|
|
copy_vec(array, dest-(len2-1), tmp, 0, len2);
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:44:43 -05:00
|
|
|
fn merge_collapse(&self, array: &[mut T]) {
|
2012-09-25 17:53:24 -05:00
|
|
|
while self.runs.len() > 1 {
|
|
|
|
let mut n = self.runs.len()-2;
|
|
|
|
let chk = do self.runs.borrow |arr| {
|
|
|
|
if n > 0 && arr[n-1].len <= arr[n].len + arr[n+1].len {
|
|
|
|
if arr[n-1].len < arr[n+1].len { n -= 1; }
|
|
|
|
true
|
|
|
|
} else if arr[n].len <= arr[n+1].len {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !chk { break; }
|
2012-10-01 11:34:04 -05:00
|
|
|
self.merge_at(n, array);
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:44:43 -05:00
|
|
|
fn merge_force_collapse(&self, array: &[mut T]) {
|
2012-09-25 17:53:24 -05:00
|
|
|
while self.runs.len() > 1 {
|
|
|
|
let mut n = self.runs.len()-2;
|
|
|
|
if n > 0 {
|
|
|
|
do self.runs.borrow |arr| {
|
|
|
|
if arr[n-1].len < arr[n+1].len {
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-01 11:34:04 -05:00
|
|
|
self.merge_at(n, array);
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-10-24 19:15:11 -05:00
|
|
|
fn copy_vec<T: Copy>(dest: &[mut T], s1: uint,
|
2012-09-27 20:44:14 -05:00
|
|
|
from: &[const T], s2: uint, len: uint) {
|
2012-09-25 17:53:24 -05:00
|
|
|
assert s1+len <= dest.len() && s2+len <= from.len();
|
2012-10-24 19:21:39 -05:00
|
|
|
|
2012-10-24 19:15:11 -05:00
|
|
|
let slice = vec::slice(from, s2, s2+len);
|
|
|
|
for slice.eachi |i, v| {
|
|
|
|
dest[s1+i] = *v;
|
2012-09-25 17:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_qsort3 {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-08-31 18:41:15 -05:00
|
|
|
fn check_sort(v1: &[mut int], v2: &[mut int]) {
|
2012-01-17 21:05:07 -06:00
|
|
|
let len = vec::len::<int>(v1);
|
2012-06-06 16:19:52 -05:00
|
|
|
quick_sort3::<int>(v1);
|
2012-10-23 13:11:23 -05:00
|
|
|
let mut i = 0;
|
2012-01-17 21:05:07 -06:00
|
|
|
while i < len {
|
|
|
|
log(debug, v2[i]);
|
|
|
|
assert (v2[i] == v1[i]);
|
2012-10-23 13:11:23 -05:00
|
|
|
i += 1;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 3, 7, 4, 5, 2, 9, 5, 8];
|
|
|
|
let v2 = ~[mut 2, 3, 4, 5, 5, 7, 8, 9];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 1, 1, 1];
|
|
|
|
let v2 = ~[mut 1, 1, 1];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1: ~[mut int] = ~[mut];
|
|
|
|
let v2: ~[mut int] = ~[mut];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
|
2012-01-17 21:05:07 -06:00
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 9, 3, 3, 3, 9];
|
|
|
|
let v2 = ~[mut 3, 3, 3, 9, 9];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_qsort {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-08-31 18:41:15 -05:00
|
|
|
fn check_sort(v1: &[mut int], v2: &[mut int]) {
|
2012-01-17 21:05:07 -06:00
|
|
|
let len = vec::len::<int>(v1);
|
2012-08-02 17:42:56 -05:00
|
|
|
pure fn leual(a: &int, b: &int) -> bool { *a <= *b }
|
2012-09-27 20:44:14 -05:00
|
|
|
quick_sort::<int>(v1, leual);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0u;
|
2012-01-17 21:05:07 -06:00
|
|
|
while i < len {
|
|
|
|
log(debug, v2[i]);
|
|
|
|
assert (v2[i] == v1[i]);
|
2012-10-23 13:11:23 -05:00
|
|
|
i += 1;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 3, 7, 4, 5, 2, 9, 5, 8];
|
|
|
|
let v2 = ~[mut 2, 3, 4, 5, 5, 7, 8, 9];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 1, 1, 1];
|
|
|
|
let v2 = ~[mut 1, 1, 1];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1: ~[mut int] = ~[mut];
|
|
|
|
let v2: ~[mut int] = ~[mut];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
|
2012-01-17 21:05:07 -06:00
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 9, 3, 3, 3, 9];
|
|
|
|
let v2 = ~[mut 3, 3, 3, 9, 9];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test for #750
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let names = ~[mut 2, 1, 3];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let expected = ~[1, 2, 3];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-11-17 21:43:02 -06:00
|
|
|
do quick_sort(names) |x, y| { int::le(*x, *y) };
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-09-19 00:35:42 -05:00
|
|
|
let immut_names = vec::from_mut(move names);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-11-17 21:43:02 -06:00
|
|
|
let pairs = vec::zip_slice(expected, immut_names);
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(pairs) |p| {
|
2012-09-18 23:41:37 -05:00
|
|
|
let (a, b) = *p;
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("%d %d", a, b);
|
2012-03-27 08:14:12 -05:00
|
|
|
assert (a == b);
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-08-31 18:41:15 -05:00
|
|
|
fn check_sort(v1: &[int], v2: &[int]) {
|
2012-01-17 21:05:07 -06:00
|
|
|
let len = vec::len::<int>(v1);
|
2012-09-27 20:21:32 -05:00
|
|
|
pub pure fn le(a: &int, b: &int) -> bool { *a <= *b }
|
2012-02-03 04:53:00 -06:00
|
|
|
let f = le;
|
2012-09-27 20:44:14 -05:00
|
|
|
let v3 = merge_sort::<int>(v1, f);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0u;
|
2012-01-17 21:05:07 -06:00
|
|
|
while i < len {
|
|
|
|
log(debug, v3[i]);
|
|
|
|
assert (v3[i] == v2[i]);
|
2012-10-23 13:11:23 -05:00
|
|
|
i += 1;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[3, 7, 4, 5, 2, 9, 5, 8];
|
|
|
|
let v2 = ~[2, 3, 4, 5, 5, 7, 8, 9];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
{ let v1 = ~[1, 1, 1]; let v2 = ~[1, 1, 1]; check_sort(v1, v2); }
|
|
|
|
{ let v1:~[int] = ~[]; let v2:~[int] = ~[]; check_sort(v1, v2); }
|
|
|
|
{ let v1 = ~[9]; let v2 = ~[9]; check_sort(v1, v2); }
|
2012-01-17 21:05:07 -06:00
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[9, 3, 3, 3, 9];
|
|
|
|
let v2 = ~[3, 3, 3, 9, 9];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_sort_mutable() {
|
2012-09-27 20:21:32 -05:00
|
|
|
pub pure fn le(a: &int, b: &int) -> bool { *a <= *b }
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 3, 2, 1];
|
2012-09-27 20:44:14 -05:00
|
|
|
let v2 = merge_sort(v1, le);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert v2 == ~[1, 2, 3];
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-09-06 14:11:48 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_sort_stability()
|
|
|
|
{
|
|
|
|
// tjc: funny that we have to use parens
|
|
|
|
pure fn ile(x: &(&static/str), y: &(&static/str)) -> bool
|
|
|
|
{
|
2012-10-22 21:42:09 -05:00
|
|
|
unsafe // to_lower is not pure...
|
2012-09-06 14:11:48 -05:00
|
|
|
{
|
|
|
|
let x = x.to_lower();
|
|
|
|
let y = y.to_lower();
|
|
|
|
x <= y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let names1 = ~["joe bob", "Joe Bob", "Jack Brown", "JOE Bob",
|
|
|
|
"Sally Mae", "JOE BOB", "Alex Andy"];
|
|
|
|
let names2 = ~["Alex Andy", "Jack Brown", "joe bob", "Joe Bob",
|
|
|
|
"JOE Bob", "JOE BOB", "Sally Mae"];
|
2012-09-27 20:44:14 -05:00
|
|
|
let names3 = merge_sort(names1, ile);
|
2012-09-06 14:11:48 -05:00
|
|
|
assert names3 == names2;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-10-04 11:24:55 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_tim_sort {
|
2012-10-03 09:51:05 -05:00
|
|
|
struct CVal {
|
2012-11-17 21:43:02 -06:00
|
|
|
val: float,
|
2012-10-03 09:51:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CVal: Ord {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn lt(&self, other: &CVal) -> bool {
|
|
|
|
unsafe {
|
|
|
|
let rng = rand::Rng();
|
|
|
|
if rng.gen_float() > 0.995 { fail ~"It's happening!!!"; }
|
|
|
|
}
|
|
|
|
(*self).val < other.val
|
|
|
|
}
|
|
|
|
pure fn le(&self, other: &CVal) -> bool { (*self).val <= other.val }
|
|
|
|
pure fn gt(&self, other: &CVal) -> bool { (*self).val > other.val }
|
|
|
|
pure fn ge(&self, other: &CVal) -> bool { (*self).val >= other.val }
|
2012-10-03 09:51:05 -05:00
|
|
|
}
|
|
|
|
|
2012-09-27 21:01:43 -05:00
|
|
|
fn check_sort(v1: &[mut int], v2: &[mut int]) {
|
|
|
|
let len = vec::len::<int>(v1);
|
2012-10-03 21:41:53 -05:00
|
|
|
tim_sort::<int>(v1);
|
2012-09-27 21:01:43 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
while i < len {
|
|
|
|
log(debug, v2[i]);
|
|
|
|
assert (v2[i] == v1[i]);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
{
|
|
|
|
let v1 = ~[mut 3, 7, 4, 5, 2, 9, 5, 8];
|
|
|
|
let v2 = ~[mut 2, 3, 4, 5, 5, 7, 8, 9];
|
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v1 = ~[mut 1, 1, 1];
|
|
|
|
let v2 = ~[mut 1, 1, 1];
|
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v1: ~[mut int] = ~[mut];
|
|
|
|
let v2: ~[mut int] = ~[mut];
|
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
|
|
|
|
{
|
|
|
|
let v1 = ~[mut 9, 3, 3, 3, 9];
|
|
|
|
let v2 = ~[mut 3, 3, 3, 9, 9];
|
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 11:24:55 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2012-10-29 13:29:27 -05:00
|
|
|
#[cfg(unix)]
|
2012-10-03 09:51:05 -05:00
|
|
|
fn crash_test() {
|
2012-10-03 21:41:53 -05:00
|
|
|
let rng = rand::Rng();
|
|
|
|
let mut arr = do vec::from_fn(1000) |_i| {
|
2012-10-03 09:51:05 -05:00
|
|
|
let randVal = rng.gen_float();
|
2012-11-17 21:43:02 -06:00
|
|
|
CVal { val: randVal }
|
2012-10-03 09:51:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tim_sort(arr);
|
|
|
|
fail ~"Guarantee the fail";
|
|
|
|
}
|
2012-10-22 22:04:14 -05:00
|
|
|
|
2012-11-17 21:43:02 -06:00
|
|
|
struct DVal { val: uint }
|
2012-11-14 20:59:30 -06:00
|
|
|
|
|
|
|
impl DVal: Ord {
|
|
|
|
pure fn lt(&self, _x: &DVal) -> bool { true }
|
|
|
|
pure fn le(&self, _x: &DVal) -> bool { true }
|
|
|
|
pure fn gt(&self, _x: &DVal) -> bool { true }
|
|
|
|
pure fn ge(&self, _x: &DVal) -> bool { true }
|
|
|
|
}
|
|
|
|
|
2012-10-22 22:04:14 -05:00
|
|
|
#[test]
|
|
|
|
fn test_bad_Ord_impl() {
|
|
|
|
let rng = rand::Rng();
|
|
|
|
let mut arr = do vec::from_fn(500) |_i| {
|
|
|
|
let randVal = rng.gen_uint();
|
2012-11-17 21:43:02 -06:00
|
|
|
DVal { val: randVal }
|
2012-10-22 22:04:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tim_sort(arr);
|
|
|
|
}
|
2012-10-04 11:24:55 -05:00
|
|
|
}
|
2012-10-03 21:41:53 -05:00
|
|
|
|
2012-10-22 18:33:28 -05:00
|
|
|
#[cfg(test)]
|
2012-10-04 21:35:03 -05:00
|
|
|
mod big_tests {
|
2012-10-03 21:41:53 -05:00
|
|
|
|
2012-10-22 18:33:28 -05:00
|
|
|
#[test]
|
2012-10-22 21:42:09 -05:00
|
|
|
fn test_unique() {
|
2012-10-03 21:41:53 -05:00
|
|
|
let low = 5;
|
|
|
|
let high = 10;
|
2012-10-22 18:33:28 -05:00
|
|
|
tabulate_unique(low, high);
|
2012-10-22 21:42:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_managed() {
|
|
|
|
let low = 5;
|
|
|
|
let high = 10;
|
2012-10-22 18:33:28 -05:00
|
|
|
tabulate_managed(low, high);
|
2012-10-22 21:42:09 -05:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:41:53 -05:00
|
|
|
fn multiplyVec<T: Copy>(arr: &[const T], num: uint) -> ~[mut T] {
|
|
|
|
let size = arr.len();
|
|
|
|
let res = do vec::from_fn(num) |i| {
|
|
|
|
arr[i % size]
|
|
|
|
};
|
2012-10-22 21:42:09 -05:00
|
|
|
vec::to_mut(move res)
|
2012-10-03 21:41:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn makeRange(n: uint) -> ~[uint] {
|
|
|
|
let one = do vec::from_fn(n) |i| { i };
|
|
|
|
let mut two = copy one;
|
|
|
|
vec::reverse(two);
|
2012-10-22 21:42:09 -05:00
|
|
|
vec::append(move two, one)
|
2012-10-03 21:41:53 -05:00
|
|
|
}
|
|
|
|
|
2012-10-22 18:33:28 -05:00
|
|
|
fn tabulate_unique(lo: uint, hi: uint) {
|
2012-10-03 21:41:53 -05:00
|
|
|
fn isSorted<T: Ord>(arr: &[const T]) {
|
|
|
|
for uint::range(0, arr.len()-1) |i| {
|
|
|
|
if arr[i] > arr[i+1] {
|
|
|
|
fail ~"Array not sorted";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let rng = rand::Rng();
|
|
|
|
|
|
|
|
for uint::range(lo, hi) |i| {
|
|
|
|
let n = 1 << i;
|
|
|
|
let arr = do vec::from_fn(n) |_i| {
|
2012-11-17 21:43:02 -06:00
|
|
|
rng.gen_float()
|
2012-10-03 21:41:53 -05:00
|
|
|
};
|
2012-10-22 21:42:09 -05:00
|
|
|
let arr = vec::to_mut(move arr);
|
2012-10-03 21:41:53 -05:00
|
|
|
|
|
|
|
tim_sort(arr); // *sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
vec::reverse(arr);
|
|
|
|
tim_sort(arr); // \sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
tim_sort(arr); // /sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
for 3.times {
|
|
|
|
let i1 = rng.gen_uint_range(0, n);
|
|
|
|
let i2 = rng.gen_uint_range(0, n);
|
|
|
|
arr[i1] <-> arr[i2];
|
|
|
|
}
|
|
|
|
tim_sort(arr); // 3sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
if n >= 10 {
|
|
|
|
let size = arr.len();
|
|
|
|
let mut idx = 1;
|
|
|
|
while idx <= 10 {
|
2012-11-17 21:43:02 -06:00
|
|
|
arr[size-idx] = rng.gen_float();
|
2012-10-03 21:41:53 -05:00
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tim_sort(arr); // +sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
for (n/100).times {
|
|
|
|
let idx = rng.gen_uint_range(0, n);
|
2012-11-17 21:43:02 -06:00
|
|
|
arr[idx] = rng.gen_float();
|
2012-10-03 21:41:53 -05:00
|
|
|
}
|
|
|
|
tim_sort(arr);
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
let arr = if n > 4 {
|
|
|
|
let part = vec::view(arr, 0, 4);
|
|
|
|
multiplyVec(part, n)
|
2012-10-22 21:42:09 -05:00
|
|
|
} else { move arr };
|
2012-10-03 21:41:53 -05:00
|
|
|
tim_sort(arr); // ~sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
2012-11-17 21:43:02 -06:00
|
|
|
let mut arr = vec::from_elem(n, -0.5);
|
2012-10-03 21:41:53 -05:00
|
|
|
tim_sort(arr); // =sort
|
|
|
|
isSorted(arr);
|
|
|
|
|
|
|
|
let half = n / 2;
|
2012-11-17 21:43:02 -06:00
|
|
|
let mut arr = makeRange(half).map(|i| *i as float);
|
2012-10-03 21:41:53 -05:00
|
|
|
tim_sort(arr); // !sort
|
|
|
|
isSorted(arr);
|
|
|
|
}
|
2012-10-22 18:33:28 -05:00
|
|
|
}
|
2012-10-04 21:35:03 -05:00
|
|
|
|
2012-10-22 18:33:28 -05:00
|
|
|
fn tabulate_managed(lo: uint, hi: uint) {
|
2012-10-24 19:17:24 -05:00
|
|
|
fn isSorted<T: Ord>(arr: &[const @T]) {
|
2012-10-04 21:35:03 -05:00
|
|
|
for uint::range(0, arr.len()-1) |i| {
|
|
|
|
if arr[i] > arr[i+1] {
|
|
|
|
fail ~"Array not sorted";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let rng = rand::Rng();
|
|
|
|
|
|
|
|
for uint::range(lo, hi) |i| {
|
|
|
|
let n = 1 << i;
|
|
|
|
let arr = do vec::from_fn(n) |_i| {
|
|
|
|
@rng.gen_float()
|
|
|
|
};
|
2012-10-22 21:42:09 -05:00
|
|
|
let arr = vec::to_mut(move arr);
|
2012-10-03 21:41:53 -05:00
|
|
|
|
2012-10-04 21:35:03 -05:00
|
|
|
tim_sort(arr); // *sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
vec::reverse(arr);
|
|
|
|
tim_sort(arr); // \sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
tim_sort(arr); // /sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
for 3.times {
|
|
|
|
let i1 = rng.gen_uint_range(0, n);
|
|
|
|
let i2 = rng.gen_uint_range(0, n);
|
|
|
|
arr[i1] <-> arr[i2];
|
|
|
|
}
|
|
|
|
tim_sort(arr); // 3sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
if n >= 10 {
|
|
|
|
let size = arr.len();
|
|
|
|
let mut idx = 1;
|
|
|
|
while idx <= 10 {
|
|
|
|
arr[size-idx] = @rng.gen_float();
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tim_sort(arr); // +sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-03 21:41:53 -05:00
|
|
|
|
2012-10-04 21:35:03 -05:00
|
|
|
for (n/100).times {
|
|
|
|
let idx = rng.gen_uint_range(0, n);
|
|
|
|
arr[idx] = @rng.gen_float();
|
|
|
|
}
|
|
|
|
tim_sort(arr);
|
2012-10-24 20:36:10 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
let arr = if n > 4 {
|
|
|
|
let part = vec::view(arr, 0, 4);
|
|
|
|
multiplyVec(part, n)
|
2012-10-22 21:42:09 -05:00
|
|
|
} else { move arr };
|
2012-10-04 21:35:03 -05:00
|
|
|
tim_sort(arr); // ~sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
let mut arr = vec::from_elem(n, @(-0.5));
|
|
|
|
tim_sort(arr); // =sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
|
|
|
|
let half = n / 2;
|
|
|
|
let mut arr = makeRange(half).map(|i| @(*i as float));
|
|
|
|
tim_sort(arr); // !sort
|
2012-10-24 19:17:24 -05:00
|
|
|
isSorted(arr);
|
2012-10-04 21:35:03 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-22 18:33:28 -05:00
|
|
|
|
2012-10-04 21:35:03 -05:00
|
|
|
struct LVal {
|
|
|
|
val: uint,
|
|
|
|
key: fn(@uint),
|
|
|
|
|
2012-11-13 20:38:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LVal : Drop {
|
|
|
|
fn finalize() {
|
2012-10-04 21:35:03 -05:00
|
|
|
let x = unsafe { task::local_data::local_data_get(self.key) };
|
|
|
|
match x {
|
|
|
|
Some(@y) => {
|
|
|
|
unsafe {
|
|
|
|
task::local_data::local_data_set(self.key, @(y+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => fail ~"Expected key to work",
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 21:41:53 -05:00
|
|
|
}
|
|
|
|
|
2012-10-04 21:35:03 -05:00
|
|
|
impl LVal: Ord {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn lt(&self, other: &a/LVal/&self) -> bool {
|
|
|
|
(*self).val < other.val
|
|
|
|
}
|
|
|
|
pure fn le(&self, other: &a/LVal/&self) -> bool {
|
|
|
|
(*self).val <= other.val
|
|
|
|
}
|
|
|
|
pure fn gt(&self, other: &a/LVal/&self) -> bool {
|
|
|
|
(*self).val > other.val
|
|
|
|
}
|
|
|
|
pure fn ge(&self, other: &a/LVal/&self) -> bool {
|
|
|
|
(*self).val >= other.val
|
|
|
|
}
|
2012-10-04 21:35:03 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-27 21:01:43 -05:00
|
|
|
|
2010-12-21 02:44:06 -06:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|