Skip to content

Commit 2e8c522

Browse files
committed
std::vec: make the sorting closure use Ordering rather than just being
(implicitly) less_eq.
1 parent 1b1e4ca commit 2e8c522

File tree

15 files changed

+118
-140
lines changed

15 files changed

+118
-140
lines changed

src/libextra/glob.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl Iterator<Path> for GlobIterator {
148148
fn list_dir_sorted(path: &Path) -> ~[Path] {
149149
match io::result(|| fs::readdir(path)) {
150150
Ok(mut children) => {
151-
children.sort_by(|p1, p2| p2.filename() <= p1.filename());
151+
children.sort_by(|p1, p2| p2.filename().cmp(&p1.filename()));
152152
children
153153
}
154154
Err(..) => ~[]

src/libextra/priority_queue.rs

-4
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,7 @@ mod tests {
231231
fn test_top_and_pop() {
232232
let data = ~[2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
233233
let mut sorted = data.clone();
234-
<<<<<<< HEAD
235-
sorted.sort(|x, y| x.le(y));
236-
=======
237234
sorted.sort();
238-
>>>>>>> 9ceda35... std::vec: add a sugary .sort() method for plain Ord sorting.
239235
let mut heap = PriorityQueue::from_vec(data);
240236
while !heap.is_empty() {
241237
assert_eq!(heap.top(), sorted.last());

src/libextra/stats.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ use std::util;
1919
// NB: this can probably be rewritten in terms of num::Num
2020
// to be less f64-specific.
2121

22+
fn f64_cmp(x: f64, y: f64) -> Ordering {
23+
// arbitrarily decide that NaNs are larger than everything.
24+
if y.is_nan() {
25+
Less
26+
} else if x.is_nan() {
27+
Greater
28+
} else if x < y {
29+
Less
30+
} else if x == y {
31+
Equal
32+
} else {
33+
Greater
34+
}
35+
}
36+
37+
fn f64_sort(v: &mut [f64]) {
38+
v.sort_by(|x: &f64, y: &f64| f64_cmp(*x, *y));
39+
}
40+
2241
/// Trait that provides simple descriptive statistics on a univariate set of numeric samples.
2342
pub trait Stats {
2443

@@ -239,13 +258,13 @@ impl<'a> Stats for &'a [f64] {
239258

240259
fn percentile(self, pct: f64) -> f64 {
241260
let mut tmp = self.to_owned();
242-
tmp.sort();
261+
f64_sort(tmp);
243262
percentile_of_sorted(tmp, pct)
244263
}
245264

246265
fn quartiles(self) -> (f64,f64,f64) {
247266
let mut tmp = self.to_owned();
248-
tmp.sort();
267+
f64_sort(tmp);
249268
let a = percentile_of_sorted(tmp, 25.0);
250269
let b = percentile_of_sorted(tmp, 50.0);
251270
let c = percentile_of_sorted(tmp, 75.0);
@@ -290,7 +309,7 @@ fn percentile_of_sorted(sorted_samples: &[f64],
290309
/// See: http://en.wikipedia.org/wiki/Winsorising
291310
pub fn winsorize(samples: &mut [f64], pct: f64) {
292311
let mut tmp = samples.to_owned();
293-
tmp.sort();
312+
f64_sort(tmp);
294313
let lo = percentile_of_sorted(tmp, pct);
295314
let hi = percentile_of_sorted(tmp, 100.0-pct);
296315
for samp in samples.mut_iter() {

src/libextra/test.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,7 @@ pub fn filter_tests(
837837
};
838838

839839
// Sort the tests alphabetically
840-
fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
841-
t1.desc.name.to_str() <= t2.desc.name.to_str()
842-
}
843-
filtered.sort_by(lteq);
840+
filtered.sort_by(|t1, t2| t1.desc.name.to_str().cmp(&t2.desc.name.to_str()));
844841

845842
// Shard the remaining tests, if sharding requested.
846843
match opts.test_shard {

src/librustc/metadata/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn find_extern_mod_stmt_cnum(cstore: &CStore,
167167
cstore.extern_mod_crate_map.find(&emod_id).map(|x| *x)
168168
}
169169

170-
#[deriving(Clone)]
170+
#[deriving(Clone, TotalEq, TotalOrd)]
171171
struct crate_hash {
172172
name: @str,
173173
vers: @str,

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ fn encode_crate_deps(ecx: &EncodeContext,
15311531
});
15321532

15331533
// Sort by cnum
1534-
deps.sort_by(|kv1, kv2| kv1.cnum <= kv2.cnum);
1534+
deps.sort_by(|kv1, kv2| kv1.cnum.cmp(&kv2.cnum));
15351535

15361536
// Sanity-check the crate numbers
15371537
let mut expected_cnum = 1;

src/librustc/middle/check_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ fn missing_ctor(cx: &MatchCheckCtxt,
466466
// those with a destructured slice come first.
467467
vec_pat_lens.sort_by(|&(len1, slice1), &(len2, slice2)| {
468468
if len1 == len2 {
469-
slice1 > slice2
469+
slice2.cmp(&slice1)
470470
} else {
471-
len1 <= len2
471+
len1.cmp(&len2)
472472
}
473473
});
474474
vec_pat_lens.dedup();

src/librustc/middle/lint.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use syntax::{ast, ast_util, visit};
6363
use syntax::ast_util::IdVisitingOperation;
6464
use syntax::visit::Visitor;
6565

66-
#[deriving(Clone, Eq)]
66+
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
6767
pub enum lint {
6868
ctypes,
6969
unused_imports,
@@ -110,20 +110,16 @@ pub fn level_to_str(lv: level) -> &'static str {
110110
}
111111
}
112112

113-
#[deriving(Clone, Eq, Ord)]
113+
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
114114
pub enum level {
115115
allow, warn, deny, forbid
116116
}
117117

118-
#[deriving(Clone, Eq)]
118+
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
119119
pub struct LintSpec {
120+
default: level,
120121
lint: lint,
121122
desc: &'static str,
122-
default: level
123-
}
124-
125-
impl Ord for LintSpec {
126-
fn lt(&self, other: &LintSpec) -> bool { self.default < other.default }
127123
}
128124

129125
pub type LintDict = HashMap<&'static str, LintSpec>;

src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3163,7 +3163,7 @@ pub fn trans_crate(sess: session::Session,
31633163
println!("n_closures: {}", ccx.stats.n_closures);
31643164
println("fn stats:");
31653165

3166-
ccx.stats.fn_stats.sort_by(|&(_, _, insns_a), &(_, _, insns_b)| insns_a >= insns_b);
3166+
ccx.stats.fn_stats.sort_by(|&(_, _, insns_a), &(_, _, insns_b)| insns_b.cmp(&insns_a));
31673167

31683168
for tuple in ccx.stats.fn_stats.iter() {
31693169
match *tuple {

src/librustdoc/html/render.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -898,44 +898,44 @@ fn item_module(w: &mut Writer, cx: &Context,
898898
debug!("{:?}", items);
899899
let mut indices = vec::from_fn(items.len(), |i| i);
900900

901-
fn le(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> bool {
901+
fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
902902
if shortty(i1) == shortty(i2) {
903-
return i1.name <= i2.name;
903+
return i1.name.cmp(&i2.name);
904904
}
905905
match (&i1.inner, &i2.inner) {
906906
(&clean::ViewItemItem(ref a), &clean::ViewItemItem(ref b)) => {
907907
match (&a.inner, &b.inner) {
908-
(&clean::ExternMod(..), _) => true,
909-
(_, &clean::ExternMod(..)) => false,
910-
_ => idx1 <= idx2,
908+
(&clean::ExternMod(..), _) => Less,
909+
(_, &clean::ExternMod(..)) => Greater,
910+
_ => idx1.cmp(&idx2),
911911
}
912912
}
913-
(&clean::ViewItemItem(..), _) => true,
914-
(_, &clean::ViewItemItem(..)) => false,
915-
(&clean::ModuleItem(..), _) => true,
916-
(_, &clean::ModuleItem(..)) => false,
917-
(&clean::StructItem(..), _) => true,
918-
(_, &clean::StructItem(..)) => false,
919-
(&clean::EnumItem(..), _) => true,
920-
(_, &clean::EnumItem(..)) => false,
921-
(&clean::StaticItem(..), _) => true,
922-
(_, &clean::StaticItem(..)) => false,
923-
(&clean::ForeignFunctionItem(..), _) => true,
924-
(_, &clean::ForeignFunctionItem(..)) => false,
925-
(&clean::ForeignStaticItem(..), _) => true,
926-
(_, &clean::ForeignStaticItem(..)) => false,
927-
(&clean::TraitItem(..), _) => true,
928-
(_, &clean::TraitItem(..)) => false,
929-
(&clean::FunctionItem(..), _) => true,
930-
(_, &clean::FunctionItem(..)) => false,
931-
(&clean::TypedefItem(..), _) => true,
932-
(_, &clean::TypedefItem(..)) => false,
933-
_ => idx1 <= idx2,
913+
(&clean::ViewItemItem(..), _) => Less,
914+
(_, &clean::ViewItemItem(..)) => Greater,
915+
(&clean::ModuleItem(..), _) => Less,
916+
(_, &clean::ModuleItem(..)) => Greater,
917+
(&clean::StructItem(..), _) => Less,
918+
(_, &clean::StructItem(..)) => Greater,
919+
(&clean::EnumItem(..), _) => Less,
920+
(_, &clean::EnumItem(..)) => Greater,
921+
(&clean::StaticItem(..), _) => Less,
922+
(_, &clean::StaticItem(..)) => Greater,
923+
(&clean::ForeignFunctionItem(..), _) => Less,
924+
(_, &clean::ForeignFunctionItem(..)) => Greater,
925+
(&clean::ForeignStaticItem(..), _) => Less,
926+
(_, &clean::ForeignStaticItem(..)) => Greater,
927+
(&clean::TraitItem(..), _) => Less,
928+
(_, &clean::TraitItem(..)) => Greater,
929+
(&clean::FunctionItem(..), _) => Less,
930+
(_, &clean::FunctionItem(..)) => Greater,
931+
(&clean::TypedefItem(..), _) => Less,
932+
(_, &clean::TypedefItem(..)) => Greater,
933+
_ => idx1.cmp(&idx2),
934934
}
935935
}
936936

937937
debug!("{:?}", indices);
938-
indices.sort_by(|&i1, &i2| le(&items[i1], &items[i2], i1, i2));
938+
indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2));
939939

940940
debug!("{:?}", indices);
941941
let mut curty = "";
@@ -1530,7 +1530,7 @@ fn build_sidebar(m: &clean::Module) -> HashMap<~str, ~[~str]> {
15301530
}
15311531

15321532
for (_, items) in map.mut_iter() {
1533-
items.sort(|i1, i2| i1 <= i2);
1533+
items.sort();
15341534
}
15351535
return map;
15361536
}

src/libstd/prelude.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
7979
pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};
8080
pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12};
8181
pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
82-
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector, MutableOrdVector};
82+
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector};
83+
pub use vec::{MutableVector, MutableTotalOrdVector};
8384
pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
8485

8586
// Reexported runtime types

0 commit comments

Comments
 (0)