Skip to content

Commit d9cddde

Browse files
committed
stdlib: Introduce ord and eq interfaces. Make std::sort::quick_sort3 use them. i=#2348
1 parent 49c6dac commit d9cddde

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

src/libcore/cmp.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[doc="Interfaces used for comparison."]
2+
3+
iface ord {
4+
fn lt(&&other: self) -> bool;
5+
}
6+
7+
iface eq {
8+
fn eq(&&other: self) -> bool;
9+
}
10+

src/libcore/core.rc

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export extfmt;
4444
export tuple;
4545
export to_str;
4646
export dvec, dvec_iter;
47+
export cmp;
4748

4849
// NDM seems to be necessary for resolve to work
4950
export option_iter;
@@ -152,6 +153,7 @@ mod tuple;
152153

153154
// Ubiquitous-utility-type modules
154155

156+
mod cmp;
155157
mod either;
156158
mod iter;
157159
mod logging;

src/libcore/int-template.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import T = inst::T;
2+
import cmp::{eq, ord};
23

34
export min_value, max_value;
45
export min, max;
@@ -10,6 +11,7 @@ export range;
1011
export compl;
1112
export abs;
1213
export parse_buf, from_str, to_str, to_str_bytes, str;
14+
export ord, eq;
1315

1416
const min_value: T = -1 as T << (inst::bits - 1 as T);
1517
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 {
108110
#[doc = "Convert to a string"]
109111
fn str(i: T) -> str { ret to_str(i, 10u); }
110112

113+
impl ord of ord for T {
114+
fn lt(&&other: T) -> bool {
115+
ret self < other;
116+
}
117+
}
118+
119+
impl eq of eq for T {
120+
fn eq(&&other: T) -> bool {
121+
ret self == other;
122+
}
123+
}
124+
111125

112126
// FIXME: Has alignment issues on windows and 32-bit linux
113127
#[test]

src/libcore/uint-template.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import T = inst::T;
2+
import cmp::{eq, ord};
23

34
export min_value, max_value;
45
export min, max;
@@ -10,6 +11,7 @@ export range;
1011
export compl;
1112
export to_str, to_str_bytes;
1213
export from_str, from_str_radix, str, parse_buf;
14+
export ord, eq;
1315

1416
const min_value: T = 0 as T;
1517
const max_value: T = 0 as T - 1 as T;
@@ -49,6 +51,18 @@ pure fn compl(i: T) -> T {
4951
max_value ^ i
5052
}
5153

54+
impl ord of ord for T {
55+
fn lt(&&other: T) -> bool {
56+
ret self < other;
57+
}
58+
}
59+
60+
impl eq of eq for T {
61+
fn eq(&&other: T) -> bool {
62+
ret self == other;
63+
}
64+
}
65+
5266
#[doc = "
5367
Parse a buffer of bytes
5468

src/libstd/sort.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[doc = "Sorting methods"];
22
import vec::len;
3+
import int::{eq, ord};
34

45
export le;
56
export merge_sort;
@@ -141,7 +142,6 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
141142
qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
142143
}
143144

144-
// FIXME: This should take lt and eq types (#2348)
145145
#[doc = "
146146
Fancy quicksort. Sorts a mut vector in place.
147147
@@ -152,22 +152,17 @@ According to these slides this is the algorithm of choice for
152152
153153
This is an unstable sort.
154154
"]
155-
fn quick_sort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
156-
arr: [mut T]) {
155+
fn quick_sort3<T: copy ord eq>(arr: [mut T]) {
157156
if len::<T>(arr) == 0u { ret; }
158-
qsort3::<T>(compare_func_lt, compare_func_eq, arr, 0,
157+
qsort3::<T>({ |x, y| x.lt(y) }, { |x, y| x.eq(y) }, arr, 0,
159158
(len::<T>(arr) as int) - 1);
160159
}
161160

162161
#[cfg(test)]
163162
mod test_qsort3 {
164163
fn check_sort(v1: [mut int], v2: [mut int]) {
165164
let len = vec::len::<int>(v1);
166-
fn lt(&&a: int, &&b: int) -> bool { ret a < b; }
167-
fn equal(&&a: int, &&b: int) -> bool { ret a == b; }
168-
let f1 = lt;
169-
let f2 = equal;
170-
quick_sort3::<int>(f1, f2, v1);
165+
quick_sort3::<int>(v1);
171166
let mut i = 0u;
172167
while i < len {
173168
log(debug, v2[i]);

0 commit comments

Comments
 (0)