rust/src/libextra/sort.rs

1222 lines
33 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Sorting methods
2012-08-31 18:41:15 -05:00
2012-09-04 13:23:53 -05:00
use core::cmp::{Eq, Ord};
2013-03-26 15:38:07 -05:00
use core::vec::len;
use core::vec;
2013-05-05 23:42:54 -05:00
use core::util::swap;
2011-08-12 00:48:08 -05:00
type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool;
2010-12-21 02:44:06 -06: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.
*/
pub fn merge_sort<T:Copy>(v: &[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
return merge_sort_(v, (0u, len(v)), le);
2012-02-27 20:32:45 -06:00
fn merge_sort_<T:Copy>(v: &[T], slice: Slice, le: Le<T>)
-> ~[T] {
let begin = slice.first();
let end = slice.second();
2012-02-27 20:32:45 -06:00
let v_len = end - begin;
if v_len == 0 { return ~[]; }
if v_len == 1 { return ~[v[begin]]; }
2012-02-27 20:32:45 -06:00
let mid = v_len / 2 + begin;
2012-02-27 20:32:45 -06:00
let a = (begin, mid);
let b = (mid, end);
return merge(le, merge_sort_(v, a, le), merge_sort_(v, b, le));
2012-02-27 20:32:45 -06:00
}
fn merge<T:Copy>(le: Le<T>, a: &[T], b: &[T]) -> ~[T] {
let mut rs = vec::with_capacity(len(a) + len(b));
2012-02-27 20:32:45 -06:00
let a_len = len(a);
let mut a_ix = 0;
2012-02-27 20:32:45 -06:00
let b_len = len(b);
let mut b_ix = 0;
2011-07-27 07:19:39 -05:00
while a_ix < a_len && b_ix < b_len {
if le(&a[a_ix], &b[b_ix]) {
rs.push(a[a_ix]);
a_ix += 1;
} else { rs.push(b[b_ix]); b_ix += 1; }
}
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));
2013-02-15 01:30:30 -06:00
rs
2010-12-21 02:44:06 -06:00
}
}
2013-03-15 14:24:24 -05:00
fn part<T>(arr: &mut [T], left: uint,
right: uint, pivot: uint, compare_func: Le<T>) -> uint {
2013-05-05 23:42:54 -05:00
vec::swap(arr, pivot, right);
2013-03-15 14:24:24 -05:00
let mut storage_index: uint = left;
let mut i: uint = left;
while i < right {
if compare_func(&arr[i], &arr[right]) {
2013-05-05 23:42:54 -05:00
vec::swap(arr, i, storage_index);
2013-03-15 14:24:24 -05:00
storage_index += 1;
}
i += 1;
}
2013-05-05 23:42:54 -05:00
vec::swap(arr, storage_index, right);
2013-03-15 14:24:24 -05:00
return storage_index;
}
fn qsort<T>(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;
let new_pivot = part::<T>(arr, left, right, pivot, compare_func);
2011-07-27 07:19:39 -05:00
if new_pivot != 0u {
// Need to do this check before recursing due to overflow
qsort::<T>(arr, left, new_pivot - 1u, compare_func);
}
qsort::<T>(arr, new_pivot + 1u, right, compare_func);
}
}
/**
* 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.
*/
pub fn quick_sort<T>(arr: &mut [T], compare_func: Le<T>) {
2012-08-01 19:30:05 -05:00
if len::<T>(arr) == 0u { return; }
qsort::<T>(arr, 0u, len::<T>(arr) - 1u, compare_func);
}
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; }
let v: T = arr[right];
let mut i: int = left - 1;
let mut j: int = right;
let mut p: int = i;
let mut q: int = j;
loop {
i += 1;
while arr[i] < v { i += 1; }
j -= 1;
while v < arr[j] {
2011-07-27 07:19:39 -05:00
if j == left { break; }
j -= 1;
}
2011-07-27 07:19:39 -05:00
if i >= j { break; }
2013-05-05 23:42:54 -05:00
vec::swap(arr, i as uint, j as uint);
if arr[i] == v {
p += 1;
2013-05-05 23:42:54 -05:00
vec::swap(arr, p as uint, i as uint);
}
if v == arr[j] {
q -= 1;
2013-05-05 23:42:54 -05:00
vec::swap(arr, j as uint, q as uint);
}
}
2013-05-05 23:42:54 -05:00
vec::swap(arr, i as uint, right as uint);
j = i - 1;
i += 1;
let mut k: int = left;
2011-07-27 07:19:39 -05:00
while k < p {
2013-05-05 23:42:54 -05:00
vec::swap(arr, k as uint, j as uint);
k += 1;
j -= 1;
if k == len::<T>(arr) as int { break; }
}
k = right - 1;
2011-07-27 07:19:39 -05:00
while k > q {
2013-05-05 23:42:54 -05:00
vec::swap(arr, i as uint, k as uint);
k -= 1;
i += 1;
2011-07-27 07:19:39 -05:00
if k == 0 { break; }
}
qsort3::<T>(arr, left, j);
qsort3::<T>(arr, i, right);
}
/**
* Fancy quicksort. Sorts a mut vector in place.
*
* Based on algorithm presented by ~[Sedgewick and Bentley]
* (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.
*/
pub fn quick_sort3<T:Copy + Ord + Eq>(arr: &mut [T]) {
if arr.len() <= 1 { return; }
let len = arr.len(); // FIXME(#5074) nested calls
2013-03-15 14:24:24 -05:00
qsort3(arr, 0, (len - 1) as int);
}
pub trait Sort {
fn qsort(self);
}
impl<'self, T:Copy + Ord + Eq> Sort for &'self mut [T] {
fn qsort(self) { quick_sort3(self); }
}
static MIN_MERGE: uint = 64;
static MIN_GALLOP: uint = 7;
static INITIAL_TMP_STORAGE: uint = 128;
2012-09-25 17:53:24 -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 {
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;
}
2013-03-07 17:37:05 -06:00
let mut ms = MergeState();
2012-10-24 20:42:41 -05:00
let min_run = min_run_length(size);
2012-09-25 17:53:24 -05:00
let mut idx = 0;
let mut remaining = size;
loop {
2013-03-15 14:24:24 -05:00
let run_len: uint = {
// This scope contains the slice `arr` here:
let arr = vec::mut_slice(array, idx, size);
let mut run_len: uint = count_run_ascending(arr);
if run_len < min_run {
let force = if remaining <= min_run {remaining} else {min_run};
let slice = vec::mut_slice(arr, 0, force);
binarysort(slice, run_len);
run_len = force;
}
run_len
};
2012-09-25 17:53:24 -05:00
ms.push_run(idx, run_len);
ms.merge_collapse(array);
2012-09-25 17:53:24 -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
ms.merge_force_collapse(array);
2012-09-25 17:53:24 -05:00
}
fn binarysort<T:Copy + Ord>(array: &mut [T], start: uint) {
2012-10-24 20:42:41 -05:00
let size = array.len();
let mut start = start;
2013-03-28 20:39:09 -05:00
assert!(start <= size);
2012-10-24 20:42:41 -05:00
if start == 0 { start += 1; }
while start < size {
let pivot = array[start];
let mut left = 0;
let mut right = start;
2013-03-28 20:39:09 -05:00
assert!(left <= right);
2012-10-24 20:42:41 -05:00
while left < right {
let mid = (left + right) >> 1;
if pivot < array[mid] {
right = mid;
} else {
left = mid+1;
}
}
assert_eq!(left, right);
2013-04-12 00:10:12 -05:00
let n = start-left;
2012-10-24 20:42:41 -05:00
shift_vec(array, left+1, left, n);
2013-02-15 01:30:30 -06:00
array[left] = pivot;
2012-10-24 20:42:41 -05:00
start += 1;
}
}
// Reverse the order of elements in a slice, in place
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 {
2013-05-05 23:42:54 -05:00
vec::swap(v, i, end - i - 1);
2012-09-25 17:53:24 -05:00
i += 1;
}
}
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;
}
fn count_run_ascending<T:Copy + Ord>(array: &mut [T]) -> uint {
2012-09-25 17:53:24 -05:00
let size = array.len();
2013-03-28 20:39:09 -05:00
assert!(size > 0);
2012-09-25 17:53:24 -05:00
if size == 1 { return 1; }
let mut run = 2;
if array[1] < array[0] {
while run < size && array[run] < array[run-1] {
run += 1;
}
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;
}
fn gallop_left<T:Copy + Ord>(key: &T,
array: &[T],
hint: uint)
-> uint {
2012-09-25 17:53:24 -05:00
let size = array.len();
2013-03-28 20:39:09 -05:00
assert!(size != 0 && hint < size);
2012-09-25 17:53:24 -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] {
// 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;
if ofs < last_ofs { ofs = max_ofs; } // uint overflow guard
2012-09-25 17:53:24 -05:00
}
if ofs > max_ofs { ofs = max_ofs; }
2012-09-25 17:53:24 -05:00
last_ofs += hint;
2012-09-25 17:53:24 -05:00
ofs += hint;
} else {
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;
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
if ofs > max_ofs { ofs = max_ofs; }
2012-09-25 17:53:24 -05:00
let tmp = last_ofs;
last_ofs = hint - ofs;
2012-09-25 17:53:24 -05:00
ofs = hint - tmp;
}
2013-03-28 20:39:09 -05:00
assert!((last_ofs < ofs || last_ofs+1 < ofs+1) && ofs <= size);
2012-09-25 17:53:24 -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] {
last_ofs = m+1;
2012-09-25 17:53:24 -05:00
} else {
ofs = m;
}
}
assert_eq!(last_ofs, ofs);
2012-09-25 17:53:24 -05:00
return ofs;
}
fn gallop_right<T:Copy + Ord>(key: &T,
array: &[T],
hint: uint)
-> uint {
2012-09-25 17:53:24 -05:00
let size = array.len();
2013-03-28 20:39:09 -05:00
assert!(size != 0 && hint < size);
2012-09-25 17:53:24 -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] {
// 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;
if ofs < last_ofs { ofs = max_ofs; }
2012-09-25 17:53:24 -05:00
}
if ofs > max_ofs { ofs = max_ofs; }
2012-09-25 17:53:24 -05:00
last_ofs += hint;
2012-09-27 20:44:14 -05:00
ofs += hint;
2012-09-25 17:53:24 -05:00
} else {
// 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;
if ofs < last_ofs { ofs = max_ofs; }
2012-09-25 17:53:24 -05:00
}
if ofs > max_ofs { ofs = max_ofs; }
2012-09-25 17:53:24 -05:00
let tmp = last_ofs;
last_ofs = hint - ofs;
2012-09-25 17:53:24 -05:00
ofs = hint - tmp;
}
2013-03-28 20:39:09 -05:00
assert!((last_ofs < ofs || last_ofs+1 < ofs+1) && ofs <= size);
2012-09-25 17:53:24 -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] {
last_ofs = m + 1;
2012-09-25 17:53:24 -05:00
} else {
ofs = m;
}
}
assert_eq!(last_ofs, ofs);
2012-09-25 17:53:24 -05:00
return ofs;
}
struct RunState {
base: uint,
len: uint,
}
struct MergeState<T> {
2013-03-07 17:37:05 -06:00
min_gallop: uint,
runs: ~[RunState],
2012-09-25 17:53:24 -05:00
}
// Fixme (#3853) Move into MergeState
2012-09-25 17:53:24 -05:00
fn MergeState<T>() -> MergeState<T> {
MergeState {
min_gallop: MIN_GALLOP,
2013-03-07 17:37:05 -06:00
runs: ~[],
2012-09-25 17:53:24 -05:00
}
}
2013-03-07 17:37:05 -06:00
impl<T:Copy + Ord> MergeState<T> {
fn push_run(&mut self, run_base: uint, run_len: uint) {
let tmp = RunState{base: run_base, len: run_len};
2012-09-25 17:53:24 -05:00
self.runs.push(tmp);
}
2013-03-07 17:37:05 -06:00
fn merge_at(&mut self, n: uint, array: &mut [T]) {
2013-04-12 00:10:12 -05:00
let size = self.runs.len();
2013-03-28 20:39:09 -05:00
assert!(size >= 2);
assert!(n == size-2 || n == size-3);
2012-09-25 17:53:24 -05:00
2013-03-07 17:37:05 -06:00
let mut b1 = self.runs[n].base;
let mut l1 = self.runs[n].len;
let b2 = self.runs[n+1].base;
let l2 = self.runs[n+1].len;
2012-09-25 17:53:24 -05:00
2013-03-28 20:39:09 -05:00
assert!(l1 > 0 && l2 > 0);
assert_eq!(b1 + l1, b2);
2012-09-25 17:53:24 -05:00
2013-03-07 17:37:05 -06:00
self.runs[n].len = l1 + l2;
if n == size-3 {
self.runs[n+1].base = self.runs[n+2].base;
self.runs[n+1].len = self.runs[n+2].len;
}
2012-09-25 17:53:24 -05:00
2013-03-15 14:24:24 -05:00
let k = { // constrain lifetime of slice below
let slice = vec::slice(array, b1, b1+l1);
gallop_right(&array[b2], slice, 0)
2013-03-15 14:24:24 -05:00
};
2013-03-07 17:37:05 -06:00
b1 += k;
l1 -= k;
if l1 != 0 {
2013-03-15 14:24:24 -05:00
let l2 = { // constrain lifetime of slice below
let slice = vec::slice(array, b2, b2+l2);
gallop_left(&array[b1+l1-1],slice,l2-1)
2013-03-15 14:24:24 -05:00
};
2013-03-07 17:37:05 -06:00
if l2 > 0 {
if l1 <= l2 {
self.merge_lo(array, b1, l1, b2, l2);
} else {
self.merge_hi(array, b1, l1, b2, l2);
2012-09-25 17:53:24 -05:00
}
}
}
self.runs.pop();
}
2013-03-07 17:37:05 -06:00
fn merge_lo(&mut self, array: &mut [T], base1: uint, len1: uint,
base2: uint, len2: uint) {
2013-03-28 20:39:09 -05:00
assert!(len1 != 0 && len2 != 0 && base1+len1 == base2);
2012-09-27 20:44:14 -05:00
let mut tmp = ~[];
for uint::range(base1, base1+len1) |i| {
tmp.push(array[i]);
}
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;
2013-05-05 23:42:54 -05:00
vec::swap(array, dest, c2);
2012-09-25 17:53:24 -05:00
dest += 1; c2 += 1; len2 -= 1;
if len2 == 0 {
copy_vec(array, dest, tmp.slice(0, len1));
2012-09-25 17:53:24 -05:00
return;
}
if len1 == 1 {
shift_vec(array, dest, c2, len2);
2013-05-05 23:42:54 -05:00
swap(&mut tmp[c1], &mut array[dest+len2]);
2012-09-25 17:53:24 -05:00
return;
}
let mut min_gallop = self.min_gallop;
2012-09-25 17:53:24 -05:00
loop {
let mut count1 = 0;
let mut count2 = 0;
let mut break_outer = false;
2012-09-25 17:53:24 -05:00
loop {
2013-03-28 20:39:09 -05:00
assert!(len1 > 1 && len2 != 0);
2012-10-24 19:15:11 -05:00
if array[c2] < tmp[c1] {
2013-05-05 23:42:54 -05:00
vec::swap(array, dest, c2);
2012-09-25 17:53:24 -05:00
dest += 1; c2 += 1; len2 -= 1;
count2 += 1; count1 = 0;
if len2 == 0 {
break_outer = true;
2012-09-25 17:53:24 -05:00
}
} else {
2013-05-05 23:42:54 -05:00
swap(&mut array[dest], &mut tmp[c1]);
2012-09-25 17:53:24 -05:00
dest += 1; c1 += 1; len1 -= 1;
count1 += 1; count2 = 0;
if len1 == 1 {
break_outer = true;
2012-09-25 17:53:24 -05:00
}
}
if break_outer || ((count1 | count2) >= min_gallop) {
2012-09-25 17:53:24 -05:00
break;
}
}
if break_outer { break; }
2012-09-25 17:53:24 -05:00
// Start to gallop
loop {
2013-03-28 20:39:09 -05:00
assert!(len1 > 1 && len2 != 0);
2012-09-25 17:53:24 -05:00
count1 = {
let tmp_view = vec::slice(tmp, c1, c1+len1);
gallop_right(&array[c2], tmp_view, 0)
};
2012-09-25 17:53:24 -05:00
if count1 != 0 {
copy_vec(array, dest, tmp.slice(c1, c1+count1));
2012-09-25 17:53:24 -05:00
dest += count1; c1 += count1; len1 -= count1;
if len1 <= 1 { break_outer = true; break; }
2012-09-25 17:53:24 -05:00
}
2013-05-05 23:42:54 -05:00
vec::swap(array, dest, c2);
2012-09-25 17:53:24 -05:00
dest += 1; c2 += 1; len2 -= 1;
if len2 == 0 { break_outer = true; break; }
2012-09-25 17:53:24 -05:00
count2 = {
let tmp_view = vec::slice(array, c2, c2+len2);
gallop_left(&tmp[c1], tmp_view, 0)
};
2012-09-25 17:53:24 -05:00
if count2 != 0 {
shift_vec(array, dest, c2, count2);
2012-09-25 17:53:24 -05:00
dest += count2; c2 += count2; len2 -= count2;
if len2 == 0 { break_outer = true; break; }
2012-09-25 17:53:24 -05:00
}
2013-05-05 23:42:54 -05:00
swap(&mut array[dest], &mut tmp[c1]);
2012-09-25 17:53:24 -05:00
dest += 1; c1 += 1; len1 -= 1;
if len1 == 1 { break_outer = true; break; }
min_gallop -= 1;
if !(count1 >= MIN_GALLOP || count2 >= MIN_GALLOP) {
break;
2012-09-27 20:44:14 -05:00
}
2012-09-25 17:53:24 -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
}
self.min_gallop = if min_gallop < 1 { 1 } else { min_gallop };
2012-09-25 17:53:24 -05:00
if len1 == 1 {
2013-03-28 20:39:09 -05:00
assert!(len2 > 0);
shift_vec(array, dest, c2, len2);
2013-05-05 23:42:54 -05:00
swap(&mut array[dest+len2], &mut tmp[c1]);
2012-09-25 17:53:24 -05:00
} else if len1 == 0 {
fail!("Comparison violates its contract!");
2012-09-25 17:53:24 -05:00
} else {
assert_eq!(len2, 0);
2013-03-28 20:39:09 -05:00
assert!(len1 > 1);
copy_vec(array, dest, tmp.slice(c1, c1+len1));
2012-09-25 17:53:24 -05:00
}
}
2013-03-07 17:37:05 -06:00
fn merge_hi(&mut self, array: &mut [T], base1: uint, len1: uint,
base2: uint, len2: uint) {
2013-03-28 20:39:09 -05:00
assert!(len1 != 1 && len2 != 0 && base1 + len1 == base2);
2012-09-25 17:53:24 -05:00
let mut tmp = ~[];
for uint::range(base2, base2+len2) |i| {
tmp.push(array[i]);
}
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;
2013-05-05 23:42:54 -05:00
vec::swap(array, dest, c1);
2012-09-25 17:53:24 -05:00
dest -= 1; c1 -= 1; len1 -= 1;
if len1 == 0 {
copy_vec(array, dest-(len2-1), tmp.slice(0, len2));
2012-09-25 17:53:24 -05:00
return;
}
if len2 == 1 {
dest -= len1;
c1 -= len1;
shift_vec(array, dest+1, c1+1, len1);
2013-05-05 23:42:54 -05:00
swap(&mut array[dest], &mut tmp[c2]);
2012-09-25 17:53:24 -05:00
return;
}
let mut min_gallop = self.min_gallop;
2012-09-25 17:53:24 -05:00
loop {
let mut count1 = 0;
let mut count2 = 0;
let mut break_outer = false;
2012-09-25 17:53:24 -05:00
loop {
2013-03-28 20:39:09 -05:00
assert!(len1 != 0 && len2 > 1);
2012-10-24 19:15:11 -05:00
if tmp[c2] < array[c1] {
2013-05-05 23:42:54 -05:00
vec::swap(array, dest, c1);
2012-09-25 17:53:24 -05:00
dest -= 1; c1 -= 1; len1 -= 1;
count1 += 1; count2 = 0;
if len1 == 0 {
break_outer = true;
2012-09-25 17:53:24 -05:00
}
} else {
2013-05-05 23:42:54 -05:00
swap(&mut array[dest], &mut tmp[c2]);
2012-09-25 17:53:24 -05:00
dest -= 1; c2 -= 1; len2 -= 1;
count2 += 1; count1 = 0;
if len2 == 1 {
break_outer = true;
2012-09-25 17:53:24 -05:00
}
}
if break_outer || ((count1 | count2) >= min_gallop) {
2012-09-25 17:53:24 -05:00
break;
}
}
if break_outer { break; }
2012-09-27 20:44:14 -05:00
2012-09-25 17:53:24 -05:00
// Start to gallop
loop {
2013-03-28 20:39:09 -05:00
assert!(len2 > 1 && len1 != 0);
2012-09-25 17:53:24 -05:00
2013-03-15 14:24:24 -05:00
{ // constrain scope of tmp_view:
let tmp_view = vec::mut_slice (array, base1, base1+len1);
count1 = len1 - gallop_right(
&tmp[c2], tmp_view, len1-1);
2013-03-15 14:24:24 -05:00
}
2012-09-25 17:53:24 -05:00
if count1 != 0 {
dest -= count1; c1 -= count1; len1 -= count1;
shift_vec(array, dest+1, c1+1, count1);
if len1 == 0 { break_outer = true; break; }
2012-09-25 17:53:24 -05:00
}
2013-05-05 23:42:54 -05:00
swap(&mut array[dest], &mut tmp[c2]);
2012-09-25 17:53:24 -05:00
dest -= 1; c2 -= 1; len2 -= 1;
if len2 == 1 { break_outer = true; break; }
2012-09-25 17:53:24 -05:00
let count2;
2013-03-15 14:24:24 -05:00
{ // constrain scope of tmp_view
let tmp_view = vec::mut_slice(tmp, 0, len2);
count2 = len2 - gallop_left(&array[c1],
tmp_view,
len2-1);
}
2012-09-25 17:53:24 -05:00
if count2 != 0 {
dest -= count2; c2 -= count2; len2 -= count2;
copy_vec(array, dest+1, tmp.slice(c2+1, c2+1+count2));
if len2 <= 1 { break_outer = true; break; }
2012-09-25 17:53:24 -05:00
}
2013-05-05 23:42:54 -05:00
vec::swap(array, dest, c1);
2012-09-25 17:53:24 -05:00
dest -= 1; c1 -= 1; len1 -= 1;
if len1 == 0 { break_outer = true; break; }
min_gallop -= 1;
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
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
}
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 {
2013-03-28 20:39:09 -05:00
assert!(len1 > 0);
2012-09-25 17:53:24 -05:00
dest -= len1;
c1 -= len1;
shift_vec(array, dest+1, c1+1, len1);
2013-05-05 23:42:54 -05:00
swap(&mut array[dest], &mut tmp[c2]);
2012-09-25 17:53:24 -05:00
} else if len2 == 0 {
fail!("Comparison violates its contract!");
2012-09-25 17:53:24 -05:00
} else {
assert_eq!(len1, 0);
2013-03-28 20:39:09 -05:00
assert!(len2 != 0);
copy_vec(array, dest-(len2-1), tmp.slice(0, len2));
2012-09-25 17:53:24 -05:00
}
}
2013-03-07 17:37:05 -06:00
fn merge_collapse(&mut self, array: &mut [T]) {
2012-09-25 17:53:24 -05:00
while self.runs.len() > 1 {
let mut n = self.runs.len()-2;
2013-03-07 17:37:05 -06:00
if n > 0 &&
self.runs[n-1].len <= self.runs[n].len + self.runs[n+1].len
{
if self.runs[n-1].len < self.runs[n+1].len { n -= 1; }
} else if self.runs[n].len <= self.runs[n+1].len {
/* keep going */
} else {
break;
}
self.merge_at(n, array);
2012-09-25 17:53:24 -05:00
}
}
2013-03-07 17:37:05 -06:00
fn merge_force_collapse(&mut 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 {
2013-03-07 17:37:05 -06:00
if self.runs[n-1].len < self.runs[n+1].len {
n -= 1;
2012-09-25 17:53:24 -05:00
}
}
self.merge_at(n, array);
2012-09-25 17:53:24 -05:00
}
}
}
#[inline(always)]
fn copy_vec<T:Copy>(dest: &mut [T],
s1: uint,
from: &[T]) {
assert!(s1+from.len() <= dest.len());
for from.eachi |i, v| {
2012-10-24 19:15:11 -05:00
dest[s1+i] = *v;
2012-09-25 17:53:24 -05:00
}
}
#[inline(always)]
fn shift_vec<T:Copy>(dest: &mut [T],
s1: uint,
s2: uint,
len: uint) {
assert!(s1+len <= dest.len());
let tmp = dest.slice(s2, s2+len).to_vec();
copy_vec(dest, s1, tmp);
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod test_qsort3 {
use sort::*;
2012-12-28 14:46:08 -06:00
use core::vec;
fn check_sort(v1: &mut [int], v2: &mut [int]) {
2012-01-17 21:05:07 -06:00
let len = vec::len::<int>(v1);
quick_sort3::<int>(v1);
let mut i = 0;
2012-01-17 21:05:07 -06:00
while i < len {
assert_eq!(v2[i], v1[i]);
i += 1;
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test() {
2012-01-17 21:05:07 -06:00
{
let mut v1 = ~[3, 7, 4, 5, 2, 9, 5, 8];
let mut v2 = ~[2, 3, 4, 5, 5, 7, 8, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let mut v1 = ~[1, 1, 1];
let mut v2 = ~[1, 1, 1];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let mut v1: ~[int] = ~[];
let mut v2: ~[int] = ~[];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
2013-01-30 20:07:11 -06:00
{ let mut v1 = ~[9]; let mut v2 = ~[9]; check_sort(v1, v2); }
2012-01-17 21:05:07 -06:00
{
let mut v1 = ~[9, 3, 3, 3, 9];
let mut v2 = ~[3, 3, 3, 9, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
}
}
#[cfg(test)]
mod test_qsort {
use sort::*;
2012-12-28 14:46:08 -06:00
use core::int;
use core::vec;
fn check_sort(v1: &mut [int], v2: &mut [int]) {
2012-01-17 21:05:07 -06:00
let len = vec::len::<int>(v1);
fn leual(a: &int, b: &int) -> bool { *a <= *b }
2012-09-27 20:44:14 -05:00
quick_sort::<int>(v1, leual);
let mut i = 0u;
2012-01-17 21:05:07 -06:00
while i < len {
// debug!(v2[i]);
assert_eq!(v2[i], v1[i]);
i += 1;
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test() {
2012-01-17 21:05:07 -06:00
{
let mut v1 = ~[3, 7, 4, 5, 2, 9, 5, 8];
let mut v2 = ~[2, 3, 4, 5, 5, 7, 8, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let mut v1 = ~[1, 1, 1];
let mut v2 = ~[1, 1, 1];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let mut v1: ~[int] = ~[];
let mut v2: ~[int] = ~[];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
2013-01-30 20:07:11 -06:00
{ let mut v1 = ~[9]; let mut v2 = ~[9]; check_sort(v1, v2); }
2012-01-17 21:05:07 -06:00
{
let mut v1 = ~[9, 3, 3, 3, 9];
let mut v2 = ~[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() {
let mut names = ~[2, 1, 3];
2012-01-17 21:05:07 -06: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
2013-02-15 01:30:30 -06:00
let immut_names = 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);
for pairs.each |p| {
let (a, b) = *p;
2012-08-22 19:24:52 -05:00
debug!("%d %d", a, b);
assert_eq!(a, b);
}
2012-01-17 21:05:07 -06:00
}
}
#[cfg(test)]
mod tests {
use sort::*;
2012-12-28 14:46:08 -06:00
use core::vec;
fn check_sort(v1: &[int], v2: &[int]) {
2012-01-17 21:05:07 -06:00
let len = vec::len::<int>(v1);
pub 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);
let mut i = 0u;
2012-01-17 21:05:07 -06:00
while i < len {
2013-03-08 14:39:42 -06:00
debug!(v3[i]);
assert_eq!(v3[i], v2[i]);
i += 1;
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test() {
2012-01-17 21:05:07 -06: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);
}
{ 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
{
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() {
pub fn le(a: &int, b: &int) -> bool { *a <= *b }
let mut v1 = ~[3, 2, 1];
2012-09-27 20:44:14 -05:00
let v2 = merge_sort(v1, le);
assert_eq!(v2, ~[1, 2, 3]);
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_merge_sort_stability() {
// tjc: funny that we have to use parens
fn ile(x: &(&'static str), y: &(&'static str)) -> bool
{
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
// (Actually, could just remove the to_str_* call, but needs an deriving(Ord) on
// Ascii)
let x = x.to_ascii().to_lower().to_str_ascii();
let y = y.to_ascii().to_lower().to_str_ascii();
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);
assert_eq!(names3, names2);
}
2012-01-17 21:05:07 -06:00
}
#[cfg(test)]
mod test_tim_sort {
use sort::tim_sort;
use core::rand::RngUtil;
2012-12-28 14:46:08 -06:00
struct CVal {
2012-11-17 21:43:02 -06:00
val: float,
}
impl Ord for CVal {
fn lt(&self, other: &CVal) -> bool {
2013-05-07 19:57:58 -05:00
let mut rng = rand::rng();
if rng.gen::<float>() > 0.995 {
fail!("It's happening!!!");
2013-05-07 19:57:58 -05:00
}
(*self).val < other.val
}
fn le(&self, other: &CVal) -> bool { (*self).val <= other.val }
fn gt(&self, other: &CVal) -> bool { (*self).val > other.val }
fn ge(&self, other: &CVal) -> bool { (*self).val >= other.val }
}
fn check_sort(v1: &mut [int], v2: &mut [int]) {
2012-09-27 21:01:43 -05:00
let len = vec::len::<int>(v1);
tim_sort::<int>(v1);
2012-09-27 21:01:43 -05:00
let mut i = 0u;
while i < len {
// debug!(v2[i]);
assert_eq!(v2[i], v1[i]);
2012-09-27 21:01:43 -05:00
i += 1u;
}
}
#[test]
fn test() {
{
let mut v1 = ~[3, 7, 4, 5, 2, 9, 5, 8];
let mut v2 = ~[2, 3, 4, 5, 5, 7, 8, 9];
2012-09-27 21:01:43 -05:00
check_sort(v1, v2);
}
{
let mut v1 = ~[1, 1, 1];
let mut v2 = ~[1, 1, 1];
2012-09-27 21:01:43 -05:00
check_sort(v1, v2);
}
{
let mut v1: ~[int] = ~[];
let mut v2: ~[int] = ~[];
2012-09-27 21:01:43 -05:00
check_sort(v1, v2);
}
2013-01-30 20:07:11 -06:00
{ let mut v1 = ~[9]; let mut v2 = ~[9]; check_sort(v1, v2); }
2012-09-27 21:01:43 -05:00
{
let mut v1 = ~[9, 3, 3, 3, 9];
let mut v2 = ~[3, 3, 3, 9, 9];
2012-09-27 21:01:43 -05:00
check_sort(v1, v2);
}
}
#[test]
#[should_fail]
#[cfg(unix)]
fn crash_test() {
2013-05-07 19:57:58 -05:00
let mut rng = rand::rng();
let mut arr = do vec::from_fn(1000) |_i| {
CVal { val: rng.gen() }
};
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 }
impl Ord for DVal {
fn lt(&self, _x: &DVal) -> bool { true }
fn le(&self, _x: &DVal) -> bool { true }
fn gt(&self, _x: &DVal) -> bool { true }
fn ge(&self, _x: &DVal) -> bool { true }
}
2012-10-22 22:04:14 -05:00
#[test]
fn test_bad_Ord_impl() {
2013-05-07 19:57:58 -05:00
let mut rng = rand::rng();
2012-10-22 22:04:14 -05:00
let mut arr = do vec::from_fn(500) |_i| {
DVal { val: rng.gen() }
2012-10-22 22:04:14 -05:00
};
tim_sort(arr);
}
}
2012-10-22 18:33:28 -05:00
#[cfg(test)]
mod big_tests {
use sort::*;
use core::rand::RngUtil;
2012-10-22 18:33:28 -05:00
#[test]
fn test_unique() {
let low = 5;
let high = 10;
2012-10-22 18:33:28 -05:00
tabulate_unique(low, high);
}
#[test]
fn test_managed() {
let low = 5;
let high = 10;
2012-10-22 18:33:28 -05:00
tabulate_managed(low, high);
}
fn multiplyVec<T:Copy>(arr: &[T], num: uint) -> ~[T] {
let size = arr.len();
let res = do vec::from_fn(num) |i| {
arr[i % size]
};
2013-02-15 01:30:30 -06:00
res
}
fn makeRange(n: uint) -> ~[uint] {
let one = do vec::from_fn(n) |i| { i };
let mut two = copy one;
vec::reverse(two);
2013-02-15 01:30:30 -06:00
vec::append(two, one)
}
2012-10-22 18:33:28 -05:00
fn tabulate_unique(lo: uint, hi: uint) {
fn isSorted<T:Ord>(arr: &[T]) {
for uint::range(0, arr.len()-1) |i| {
if arr[i] > arr[i+1] {
fail!("Array not sorted");
}
}
}
2013-05-07 19:57:58 -05:00
let mut rng = rand::rng();
for uint::range(lo, hi) |i| {
let n = 1 << i;
let mut arr: ~[float] = do vec::from_fn(n) |_i| {
rng.gen()
};
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);
2013-05-05 23:42:54 -05:00
vec::swap(arr, i1, i2);
}
tim_sort(arr); // 3sort
isSorted(arr);
if n >= 10 {
let size = arr.len();
let mut idx = 1;
while idx <= 10 {
arr[size-idx] = rng.gen();
idx += 1;
}
}
tim_sort(arr); // +sort
isSorted(arr);
for (n/100).times {
let idx = rng.gen_uint_range(0, n);
arr[idx] = rng.gen();
}
tim_sort(arr);
isSorted(arr);
2013-01-30 20:07:11 -06:00
let mut arr = if n > 4 {
let part = vec::slice(arr, 0, 4);
multiplyVec(part, n)
2013-02-15 01:30:30 -06:00
} else { arr };
tim_sort(arr); // ~sort
isSorted(arr);
2012-11-17 21:43:02 -06:00
let mut arr = vec::from_elem(n, -0.5);
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);
tim_sort(arr); // !sort
isSorted(arr);
}
2012-10-22 18:33:28 -05:00
}
2012-10-22 18:33:28 -05:00
fn tabulate_managed(lo: uint, hi: uint) {
fn isSorted<T:Ord>(arr: &[@T]) {
for uint::range(0, arr.len()-1) |i| {
if arr[i] > arr[i+1] {
fail!("Array not sorted");
}
}
}
2013-05-07 19:57:58 -05:00
let mut rng = rand::rng();
for uint::range(lo, hi) |i| {
let n = 1 << i;
let arr: ~[@float] = do vec::from_fn(n) |_i| {
@rng.gen()
};
2013-02-15 01:30:30 -06:00
let mut arr = arr;
tim_sort(arr); // *sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
vec::reverse(arr);
tim_sort(arr); // \sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
tim_sort(arr); // /sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
for 3.times {
let i1 = rng.gen_uint_range(0, n);
let i2 = rng.gen_uint_range(0, n);
2013-05-05 23:42:54 -05:00
vec::swap(arr, i1, i2);
}
tim_sort(arr); // 3sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
if n >= 10 {
let size = arr.len();
let mut idx = 1;
while idx <= 10 {
arr[size-idx] = @rng.gen();
idx += 1;
}
}
tim_sort(arr); // +sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
for (n/100).times {
let idx = rng.gen_uint_range(0, n);
arr[idx] = @rng.gen();
}
tim_sort(arr);
isSorted(arr);
2013-01-30 20:07:11 -06:00
let mut arr = if n > 4 {
let part = vec::slice(arr, 0, 4);
multiplyVec(part, n)
2013-02-15 01:30:30 -06:00
} else { arr };
tim_sort(arr); // ~sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
let mut arr = vec::from_elem(n, @(-0.5));
tim_sort(arr); // =sort
2012-10-24 19:17:24 -05:00
isSorted(arr);
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-22 18:33:28 -05:00
struct LVal<'self> {
val: uint,
key: &'self fn(@uint),
2012-11-13 20:38:18 -06:00
}
#[unsafe_destructor]
impl<'self> Drop for LVal<'self> {
fn finalize(&self) {
let x = unsafe { local_data::local_data_get(self.key) };
match x {
Some(@y) => {
unsafe {
local_data::local_data_set(self.key, @(y+1));
}
}
_ => fail!("Expected key to work"),
}
}
}
impl<'self> Ord for LVal<'self> {
fn lt<'a>(&self, other: &'a LVal<'self>) -> bool {
(*self).val < other.val
}
fn le<'a>(&self, other: &'a LVal<'self>) -> bool {
(*self).val <= other.val
}
fn gt<'a>(&self, other: &'a LVal<'self>) -> bool {
(*self).val > other.val
}
fn ge<'a>(&self, other: &'a LVal<'self>) -> bool {
(*self).val >= other.val
}
}
}