Skip to content

Commit da24301

Browse files
committed
loose constrain on generic arg
1 parent 5698741 commit da24301

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

benches/sort.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn eq_data_merge_v2(b: &mut Bencher) {
107107
fn small_merge_v3(b: &mut Bencher) {
108108
b.iter(|| {
109109
let mut numbs = [1, 2, 4, 8, 9, 9, 13, 17, 22];
110-
let f = |x: i32, y: i32| x > y;
110+
let f = |x: &i32, y: &i32| x > y;
111111
sort::merge::v3::sort(&mut numbs, &f);
112112
});
113113
}
@@ -117,7 +117,7 @@ fn large_merge_v3(b: &mut Bencher) {
117117
let data = util::random_data(util::DATA_LEN);
118118
b.iter(|| {
119119
let mut numbs = data.clone();
120-
let f = |x: i32, y: i32| x > y;
120+
let f = |x: &i32, y: &i32| x > y;
121121
sort::merge::v3::sort(&mut numbs, &f);
122122
});
123123
}
@@ -127,7 +127,7 @@ fn large_sorted_asc_merge_v3(b: &mut Bencher) {
127127
let data = util::sorted_data_asc(util::DATA_LEN);
128128
b.iter(|| {
129129
let mut numbs = data.clone();
130-
let f = |x: i32, y: i32| x > y;
130+
let f = |x: &i32, y: &i32| x > y;
131131
sort::merge::v3::sort(&mut numbs, &f);
132132
});
133133
}
@@ -137,7 +137,7 @@ fn large_sorted_desc_merge_v3(b: &mut Bencher) {
137137
let data = util::sorted_data_desc(util::DATA_LEN);
138138
b.iter(|| {
139139
let mut numbs = data.clone();
140-
let f = |x: i32, y: i32| x > y;
140+
let f = |x: &i32, y: &i32| x > y;
141141
sort::merge::v3::sort(&mut numbs, &f);
142142
});
143143
}
@@ -147,7 +147,7 @@ fn eq_data_merge_v3(b: &mut Bencher) {
147147
let data = util::eq_data(util::DATA_LEN);
148148
b.iter(|| {
149149
let mut numbs = data.clone();
150-
let f = |x: i32, y: i32| x > y;
150+
let f = |x: &i32, y: &i32| x > y;
151151
sort::merge::v3::sort(&mut numbs, &f);
152152
});
153153
}

src/sort/insert.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
55
pub fn sort<T, F>(a: &mut [T], compare: F)
66
where
7-
T: Copy,
8-
F: Fn(T, T) -> bool,
7+
F: Fn(&T, &T) -> bool,
98
{
109
let len = a.len();
1110
// 注意起始索引
1211
for i in 1..len {
1312
// 将a[i]插入到a[i-1],a[i-2],a[i-3]……之中
1413
let mut j = i;
15-
while j > 0 && compare(a[j], a[j - 1]) {
14+
while j > 0 && compare(&a[j], &a[j - 1]) {
1615
a.swap(j, j - 1);
1716
j -= 1;
1817
}

src/sort/merge.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ pub mod v1 {
7272
}
7373

7474
pub mod v2 {
75-
use std::fmt::Debug;
7675
use std::ptr;
7776

7877
//ws为辅助space
@@ -124,7 +123,7 @@ pub mod v2 {
124123

125124
pub fn sort<T, F>(a: &mut [T], compare: &F)
126125
where
127-
T: Copy + Default + Debug,
126+
T: Copy + Default,
128127
F: Fn(T, T) -> bool,
129128
{
130129
let len = a.len();
@@ -148,11 +147,10 @@ pub mod v3 {
148147
compare: &F,
149148
mut w: usize,
150149
) where
151-
T: Copy,
152-
F: Fn(T, T) -> bool,
150+
F: Fn(&T, &T) -> bool,
153151
{
154152
while i < m && j < n {
155-
if compare(xs[i], xs[j]) {
153+
if compare(&xs[i], &xs[j]) {
156154
xs.swap(w, i);
157155
i += 1;
158156
} else {
@@ -179,8 +177,7 @@ pub mod v3 {
179177
/// constraint, len(w) == u - l
180178
fn wsort<T, F>(xs: &mut [T], mut l: usize, u: usize, compare: &F, mut w: usize)
181179
where
182-
T: Copy,
183-
F: Fn(T, T) -> bool,
180+
F: Fn(&T, &T) -> bool,
184181
{
185182
if u - l > 1 {
186183
let m = (u + l) / 2;
@@ -198,8 +195,7 @@ pub mod v3 {
198195

199196
fn do_sort<T, F>(a: &mut [T], l: usize, u: usize, compare: &F)
200197
where
201-
T: Copy,
202-
F: Fn(T, T) -> bool,
198+
F: Fn(&T, &T) -> bool,
203199
{
204200
if u - l > 1 {
205201
let mut m = (u + l) / 2;
@@ -218,7 +214,7 @@ pub mod v3 {
218214
let mut n = w;
219215
while n > l {
220216
m = n;
221-
while m < u && compare(a[m], a[m - 1]) {
217+
while m < u && compare(&a[m], &a[m - 1]) {
222218
a.swap(m, m - 1);
223219
m += 1;
224220
}
@@ -229,8 +225,7 @@ pub mod v3 {
229225

230226
pub fn sort<T, F>(a: &mut [T], compare: &F)
231227
where
232-
T: Copy,
233-
F: Fn(T, T) -> bool,
228+
F: Fn(&T, &T) -> bool,
234229
{
235230
let len = a.len();
236231
do_sort(a, 0, len, compare);

src/sort/selection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
pub fn sort<T>(a: &mut [T])
99
where
10-
T: Copy + std::cmp::PartialOrd,
10+
T: std::cmp::PartialOrd,
1111
{
1212
let len = a.len();
1313
for i in 0..len {
@@ -27,7 +27,7 @@ where
2727
/// 最大值放到末尾
2828
pub fn sort_cocktail<T>(a: &mut [T])
2929
where
30-
T: Copy + std::cmp::PartialOrd,
30+
T: std::cmp::PartialOrd,
3131
{
3232
let n = a.len();
3333
let semi_n = n / 2;

src/sort/shell.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
//! 是直接插入排序算法的一种更高效的改进版本。希尔排序是
55
//! 非稳定排序算法。该方法因 D.L.Shell 于 1959 年提出而得名。
66
7-
use std::fmt::Debug;
8-
97
pub fn sort<T, F>(a: &mut [T], compare: F)
108
where
11-
T: Copy + Default + Debug,
9+
T: Copy,
1210
F: Fn(T, T) -> bool,
1311
{
1412
let len = a.len();

tests/test_sort.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn bubble() {
1313

1414
#[test]
1515
fn insert() {
16-
let test = |x: i32, y: i32| x < y;
16+
let test = |x: &i32, y: &i32| x < y;
1717
let data = sort::util::plan_data();
1818
for (t, expect) in data {
1919
let mut tt = t.clone();
@@ -75,7 +75,7 @@ fn merge_v2() {
7575

7676
#[test]
7777
fn merge_v3() {
78-
let test = |x: i32, y: i32| x < y;
78+
let test = |x: &i32, y: &i32| x < y;
7979
let data = sort::util::plan_data();
8080
for (t, expect) in data {
8181
let mut tt = t.clone();

0 commit comments

Comments
 (0)