stdlib: Introduce ord and eq interfaces. Make std::sort::quick_sort3 use them. i=#2348
This commit is contained in:
10
src/libcore/cmp.rs
Normal file
10
src/libcore/cmp.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#[doc="Interfaces used for comparison."]
|
||||||
|
|
||||||
|
iface ord {
|
||||||
|
fn lt(&&other: self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
iface eq {
|
||||||
|
fn eq(&&other: self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -44,6 +44,7 @@ export extfmt;
|
|||||||
export tuple;
|
export tuple;
|
||||||
export to_str;
|
export to_str;
|
||||||
export dvec, dvec_iter;
|
export dvec, dvec_iter;
|
||||||
|
export cmp;
|
||||||
|
|
||||||
// NDM seems to be necessary for resolve to work
|
// NDM seems to be necessary for resolve to work
|
||||||
export option_iter;
|
export option_iter;
|
||||||
@@ -152,6 +153,7 @@ mod tuple;
|
|||||||
|
|
||||||
// Ubiquitous-utility-type modules
|
// Ubiquitous-utility-type modules
|
||||||
|
|
||||||
|
mod cmp;
|
||||||
mod either;
|
mod either;
|
||||||
mod iter;
|
mod iter;
|
||||||
mod logging;
|
mod logging;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import T = inst::T;
|
import T = inst::T;
|
||||||
|
import cmp::{eq, ord};
|
||||||
|
|
||||||
export min_value, max_value;
|
export min_value, max_value;
|
||||||
export min, max;
|
export min, max;
|
||||||
@@ -10,6 +11,7 @@ export range;
|
|||||||
export compl;
|
export compl;
|
||||||
export abs;
|
export abs;
|
||||||
export parse_buf, from_str, to_str, to_str_bytes, str;
|
export parse_buf, from_str, to_str, to_str_bytes, str;
|
||||||
|
export ord, eq;
|
||||||
|
|
||||||
const min_value: T = -1 as T << (inst::bits - 1 as T);
|
const min_value: T = -1 as T << (inst::bits - 1 as T);
|
||||||
const max_value: T = min_value - 1 as T;
|
const max_value: T = min_value - 1 as T;
|
||||||
@@ -108,6 +110,18 @@ fn to_str_bytes<U>(n: T, radix: uint, f: fn([u8]/&) -> U) -> U {
|
|||||||
#[doc = "Convert to a string"]
|
#[doc = "Convert to a string"]
|
||||||
fn str(i: T) -> str { ret to_str(i, 10u); }
|
fn str(i: T) -> str { ret to_str(i, 10u); }
|
||||||
|
|
||||||
|
impl ord of ord for T {
|
||||||
|
fn lt(&&other: T) -> bool {
|
||||||
|
ret self < other;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eq of eq for T {
|
||||||
|
fn eq(&&other: T) -> bool {
|
||||||
|
ret self == other;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// FIXME: Has alignment issues on windows and 32-bit linux
|
// FIXME: Has alignment issues on windows and 32-bit linux
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import T = inst::T;
|
import T = inst::T;
|
||||||
|
import cmp::{eq, ord};
|
||||||
|
|
||||||
export min_value, max_value;
|
export min_value, max_value;
|
||||||
export min, max;
|
export min, max;
|
||||||
@@ -10,6 +11,7 @@ export range;
|
|||||||
export compl;
|
export compl;
|
||||||
export to_str, to_str_bytes;
|
export to_str, to_str_bytes;
|
||||||
export from_str, from_str_radix, str, parse_buf;
|
export from_str, from_str_radix, str, parse_buf;
|
||||||
|
export ord, eq;
|
||||||
|
|
||||||
const min_value: T = 0 as T;
|
const min_value: T = 0 as T;
|
||||||
const max_value: T = 0 as T - 1 as T;
|
const max_value: T = 0 as T - 1 as T;
|
||||||
@@ -49,6 +51,18 @@ pure fn compl(i: T) -> T {
|
|||||||
max_value ^ i
|
max_value ^ i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ord of ord for T {
|
||||||
|
fn lt(&&other: T) -> bool {
|
||||||
|
ret self < other;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eq of eq for T {
|
||||||
|
fn eq(&&other: T) -> bool {
|
||||||
|
ret self == other;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
Parse a buffer of bytes
|
Parse a buffer of bytes
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#[doc = "Sorting methods"];
|
#[doc = "Sorting methods"];
|
||||||
import vec::len;
|
import vec::len;
|
||||||
|
import int::{eq, ord};
|
||||||
|
|
||||||
export le;
|
export le;
|
||||||
export merge_sort;
|
export merge_sort;
|
||||||
@@ -141,7 +142,6 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
|
|||||||
qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
|
qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: This should take lt and eq types (#2348)
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
Fancy quicksort. Sorts a mut vector in place.
|
Fancy quicksort. Sorts a mut vector in place.
|
||||||
|
|
||||||
@@ -152,10 +152,9 @@ According to these slides this is the algorithm of choice for
|
|||||||
|
|
||||||
This is an unstable sort.
|
This is an unstable sort.
|
||||||
"]
|
"]
|
||||||
fn quick_sort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
|
fn quick_sort3<T: copy ord eq>(arr: [mut T]) {
|
||||||
arr: [mut T]) {
|
|
||||||
if len::<T>(arr) == 0u { ret; }
|
if len::<T>(arr) == 0u { ret; }
|
||||||
qsort3::<T>(compare_func_lt, compare_func_eq, arr, 0,
|
qsort3::<T>({ |x, y| x.lt(y) }, { |x, y| x.eq(y) }, arr, 0,
|
||||||
(len::<T>(arr) as int) - 1);
|
(len::<T>(arr) as int) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,11 +162,7 @@ fn quick_sort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
|
|||||||
mod test_qsort3 {
|
mod test_qsort3 {
|
||||||
fn check_sort(v1: [mut int], v2: [mut int]) {
|
fn check_sort(v1: [mut int], v2: [mut int]) {
|
||||||
let len = vec::len::<int>(v1);
|
let len = vec::len::<int>(v1);
|
||||||
fn lt(&&a: int, &&b: int) -> bool { ret a < b; }
|
quick_sort3::<int>(v1);
|
||||||
fn equal(&&a: int, &&b: int) -> bool { ret a == b; }
|
|
||||||
let f1 = lt;
|
|
||||||
let f2 = equal;
|
|
||||||
quick_sort3::<int>(f1, f2, v1);
|
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
while i < len {
|
while i < len {
|
||||||
log(debug, v2[i]);
|
log(debug, v2[i]);
|
||||||
|
|||||||
Reference in New Issue
Block a user