2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
import vec::len;
|
|
|
|
import vec::slice;
|
2011-06-30 21:09:30 -05:00
|
|
|
import ilen = ivec::len;
|
|
|
|
import islice = ivec::slice;
|
|
|
|
export ivector;
|
2011-05-20 21:12:16 -05:00
|
|
|
export lteq;
|
|
|
|
export merge_sort;
|
|
|
|
export quick_sort;
|
2011-05-24 12:12:32 -05:00
|
|
|
export quick_sort3;
|
2011-05-20 21:12:16 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
type lteq[T] = fn(&T, &T) -> bool ;
|
2010-12-21 02:44:06 -06:00
|
|
|
|
|
|
|
fn merge_sort[T](lteq[T] le, vec[T] v) -> vec[T] {
|
2011-03-16 16:58:02 -05:00
|
|
|
fn merge[T](lteq[T] le, vec[T] a, vec[T] b) -> vec[T] {
|
2011-06-24 10:55:02 -05:00
|
|
|
let vec[T] rs = [];
|
2011-03-16 16:58:02 -05:00
|
|
|
let uint a_len = len[T](a);
|
|
|
|
let uint a_ix = 0u;
|
|
|
|
let uint b_len = len[T](b);
|
|
|
|
let uint b_ix = 0u;
|
|
|
|
while (a_ix < a_len && b_ix < b_len) {
|
|
|
|
if (le(a.(a_ix), b.(b_ix))) {
|
2011-06-24 10:55:02 -05:00
|
|
|
rs += [a.(a_ix)];
|
2011-03-16 16:58:02 -05:00
|
|
|
a_ix += 1u;
|
2011-06-24 10:55:02 -05:00
|
|
|
} else { rs += [b.(b_ix)]; b_ix += 1u; }
|
2011-03-16 16:58:02 -05:00
|
|
|
}
|
2011-06-24 10:55:02 -05:00
|
|
|
rs += slice[T](a, a_ix, a_len);
|
|
|
|
rs += slice[T](b, b_ix, b_len);
|
|
|
|
ret rs;
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
2011-03-16 16:58:02 -05:00
|
|
|
let uint v_len = len[T](v);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (v_len <= 1u) { ret v; }
|
2011-03-16 16:58:02 -05:00
|
|
|
let uint mid = v_len / 2u;
|
|
|
|
let vec[T] a = slice[T](v, 0u, mid);
|
|
|
|
let vec[T] b = slice[T](v, mid, v_len);
|
2011-06-15 13:19:50 -05:00
|
|
|
ret merge[T](le, merge_sort[T](le, a), merge_sort[T](le, b));
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
|
|
|
|
2011-05-05 15:08:52 -05:00
|
|
|
fn swap[T](vec[mutable T] arr, uint x, uint y) {
|
|
|
|
auto a = arr.(x);
|
|
|
|
arr.(x) = arr.(y);
|
|
|
|
arr.(y) = a;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn part[T](lteq[T] compare_func, vec[mutable T] arr, uint left, uint right,
|
|
|
|
uint pivot) -> uint {
|
2011-05-05 15:08:52 -05:00
|
|
|
auto pivot_value = arr.(pivot);
|
|
|
|
swap[T](arr, pivot, right);
|
|
|
|
let uint storage_index = left;
|
|
|
|
let uint i = left;
|
2011-06-15 13:19:50 -05:00
|
|
|
while (i < right) {
|
|
|
|
if (compare_func({ arr.(i) }, pivot_value)) {
|
|
|
|
swap[T](arr, i, storage_index);
|
|
|
|
storage_index += 1u;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
swap[T](arr, storage_index, right);
|
|
|
|
ret storage_index;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn qsort[T](lteq[T] compare_func, vec[mutable T] arr, uint left, uint right) {
|
2011-05-05 15:08:52 -05:00
|
|
|
if (right > left) {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto pivot = (left + right) / 2u;
|
2011-05-05 15:08:52 -05:00
|
|
|
auto new_pivot = part[T](compare_func, arr, left, right, pivot);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (new_pivot == 0u) { ret; }
|
2011-05-05 15:08:52 -05:00
|
|
|
qsort[T](compare_func, arr, left, new_pivot - 1u);
|
|
|
|
qsort[T](compare_func, arr, new_pivot + 1u, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
fn quick_sort[T](lteq[T] compare_func, vec[mutable T] arr) {
|
2011-06-15 13:19:50 -05:00
|
|
|
if (len[T](arr) == 0u) { ret; }
|
|
|
|
qsort[T](compare_func, arr, 0u, len[T](arr) - 1u);
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
|
2011-05-24 12:12:32 -05:00
|
|
|
|
|
|
|
// Based on algorithm presented by Sedgewick and Bentley here:
|
|
|
|
// 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'
|
|
|
|
fn qsort3[T](lteq[T] compare_func_lt, lteq[T] compare_func_eq,
|
2011-06-09 02:30:46 -05:00
|
|
|
vec[mutable T] arr, int left, int right) {
|
2011-06-15 13:19:50 -05:00
|
|
|
if (right <= left) { ret; }
|
2011-05-24 12:12:32 -05:00
|
|
|
let T v = arr.(right);
|
|
|
|
let int i = left - 1;
|
|
|
|
let int j = right;
|
|
|
|
let int p = i;
|
|
|
|
let int q = j;
|
|
|
|
while (true) {
|
|
|
|
i += 1;
|
2011-06-15 13:19:50 -05:00
|
|
|
while (compare_func_lt({ arr.(i) }, v)) { i += 1; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
2011-06-15 13:19:50 -05:00
|
|
|
while (compare_func_lt(v, { arr.(j) })) {
|
|
|
|
if (j == left) { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
if (i >= j) { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
swap[T](arr, i as uint, j as uint);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (compare_func_eq({ arr.(i) }, v)) {
|
2011-05-24 12:12:32 -05:00
|
|
|
p += 1;
|
|
|
|
swap[T](arr, p as uint, i as uint);
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
if (compare_func_eq(v, { arr.(j) })) {
|
2011-05-24 12:12:32 -05:00
|
|
|
q -= 1;
|
|
|
|
swap[T](arr, j as uint, q as uint);
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-05-24 12:12:32 -05:00
|
|
|
swap[T](arr, i as uint, right as uint);
|
|
|
|
j = i - 1;
|
|
|
|
i += 1;
|
|
|
|
let int k = left;
|
|
|
|
while (k < p) {
|
|
|
|
swap[T](arr, k as uint, j as uint);
|
|
|
|
k += 1;
|
|
|
|
j -= 1;
|
2011-06-15 13:19:50 -05:00
|
|
|
if (k == vec::len[T](arr) as int) { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
k = right - 1;
|
|
|
|
while (k > q) {
|
|
|
|
swap[T](arr, i as uint, k as uint);
|
|
|
|
k -= 1;
|
|
|
|
i += 1;
|
2011-06-15 13:19:50 -05:00
|
|
|
if (k == 0) { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, left, j);
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quick_sort3[T](lteq[T] compare_func_lt, lteq[T] compare_func_eq,
|
2011-06-15 13:19:50 -05:00
|
|
|
vec[mutable T] arr) {
|
|
|
|
if (vec::len[T](arr) == 0u) { ret; }
|
2011-05-24 12:12:32 -05:00
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
|
|
|
|
(vec::len[T](arr) as int) - 1);
|
|
|
|
}
|
2011-06-30 21:09:30 -05:00
|
|
|
|
|
|
|
mod ivector {
|
|
|
|
export merge_sort;
|
|
|
|
export quick_sort;
|
|
|
|
export quick_sort3;
|
|
|
|
|
|
|
|
type lteq[T] = fn(&T, &T) -> bool;
|
|
|
|
|
|
|
|
fn merge_sort[T](lteq[T] le, &T[] v) -> T[] {
|
|
|
|
fn merge[T](lteq[T] le, &T[] a, &T[] b) -> T[] {
|
|
|
|
let T[] rs = ~[];
|
|
|
|
let uint a_len = ilen[T](a);
|
|
|
|
let uint a_ix = 0u;
|
|
|
|
let uint b_len = ilen[T](b);
|
|
|
|
let uint b_ix = 0u;
|
|
|
|
while (a_ix < a_len && b_ix < b_len) {
|
|
|
|
if (le(a.(a_ix), b.(b_ix))) {
|
|
|
|
rs += ~[a.(a_ix)];
|
|
|
|
a_ix += 1u;
|
|
|
|
} else { rs += ~[b.(b_ix)]; b_ix += 1u; }
|
|
|
|
}
|
|
|
|
rs += islice[T](a, a_ix, a_len);
|
|
|
|
rs += islice[T](b, b_ix, b_len);
|
|
|
|
ret rs;
|
|
|
|
}
|
|
|
|
let uint v_len = ilen[T](v);
|
|
|
|
if (v_len <= 1u) { ret v; }
|
|
|
|
let uint mid = v_len / 2u;
|
|
|
|
let T[] a = islice[T](v, 0u, mid);
|
|
|
|
let T[] b = islice[T](v, mid, v_len);
|
|
|
|
ret merge[T](le, merge_sort[T](le, a), merge_sort[T](le, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn swap[T](&T[mutable] arr, uint x, uint y) {
|
|
|
|
auto a = arr.(x);
|
|
|
|
arr.(x) = arr.(y);
|
|
|
|
arr.(y) = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn part[T](lteq[T] compare_func, &T[mutable] arr, uint left, uint right,
|
|
|
|
uint pivot) -> uint {
|
|
|
|
auto pivot_value = arr.(pivot);
|
|
|
|
swap[T](arr, pivot, right);
|
|
|
|
let uint storage_index = left;
|
|
|
|
let uint i = left;
|
|
|
|
while (i < right) {
|
|
|
|
if (compare_func({ arr.(i) }, pivot_value)) {
|
|
|
|
swap[T](arr, i, storage_index);
|
|
|
|
storage_index += 1u;
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
swap[T](arr, storage_index, right);
|
|
|
|
ret storage_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn qsort[T](lteq[T] compare_func, &T[mutable] arr, uint left,
|
|
|
|
uint right) {
|
|
|
|
if (right > left) {
|
|
|
|
auto pivot = (left + right) / 2u;
|
|
|
|
auto new_pivot = part[T](compare_func, arr, left, right, pivot);
|
|
|
|
if (new_pivot == 0u) { ret; }
|
|
|
|
qsort[T](compare_func, arr, left, new_pivot - 1u);
|
|
|
|
qsort[T](compare_func, arr, new_pivot + 1u, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quick_sort[T](lteq[T] compare_func, &T[mutable] arr) {
|
|
|
|
if (ilen[T](arr) == 0u) { ret; }
|
|
|
|
qsort[T](compare_func, arr, 0u, ilen[T](arr) - 1u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Based on algorithm presented by Sedgewick and Bentley here:
|
|
|
|
// http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf
|
|
|
|
// According to these slides this is the algorithm of choice for
|
2011-07-01 00:21:30 -05:00
|
|
|
// 'randomly ordered keys, abstract compare' & 'small number of key
|
|
|
|
// values'
|
2011-06-30 21:09:30 -05:00
|
|
|
fn qsort3[T](lteq[T] compare_func_lt, lteq[T] compare_func_eq,
|
|
|
|
&T[mutable] arr, int left, int right) {
|
|
|
|
if (right <= left) { ret; }
|
|
|
|
let T v = arr.(right);
|
|
|
|
let int i = left - 1;
|
|
|
|
let int j = right;
|
|
|
|
let int p = i;
|
|
|
|
let int q = j;
|
|
|
|
while (true) {
|
|
|
|
i += 1;
|
|
|
|
while (compare_func_lt({ arr.(i) }, v)) { i += 1; }
|
|
|
|
j -= 1;
|
|
|
|
while (compare_func_lt(v, { arr.(j) })) {
|
|
|
|
if (j == left) { break; }
|
|
|
|
j -= 1;
|
|
|
|
}
|
|
|
|
if (i >= j) { break; }
|
|
|
|
swap[T](arr, i as uint, j as uint);
|
|
|
|
if (compare_func_eq({ arr.(i) }, v)) {
|
|
|
|
p += 1;
|
|
|
|
swap[T](arr, p as uint, i as uint);
|
|
|
|
}
|
|
|
|
if (compare_func_eq(v, { arr.(j) })) {
|
|
|
|
q -= 1;
|
|
|
|
swap[T](arr, j as uint, q as uint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swap[T](arr, i as uint, right as uint);
|
|
|
|
j = i - 1;
|
|
|
|
i += 1;
|
|
|
|
let int k = left;
|
|
|
|
while (k < p) {
|
|
|
|
swap[T](arr, k as uint, j as uint);
|
|
|
|
k += 1;
|
|
|
|
j -= 1;
|
|
|
|
if (k == ilen[T](arr) as int) { break; }
|
|
|
|
}
|
|
|
|
k = right - 1;
|
|
|
|
while (k > q) {
|
|
|
|
swap[T](arr, i as uint, k as uint);
|
|
|
|
k -= 1;
|
|
|
|
i += 1;
|
|
|
|
if (k == 0) { break; }
|
|
|
|
}
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, left, j);
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quick_sort3[T](lteq[T] compare_func_lt, lteq[T] compare_func_eq,
|
|
|
|
&T[mutable] arr) {
|
|
|
|
if (ilen[T](arr) == 0u) { ret; }
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
|
|
|
|
(ilen[T](arr) as int) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-12-21 02:44:06 -06:00
|
|
|
// End:
|