Skip to content

Commit 55cbef6

Browse files
committed
auto merge of #11064 : huonw/rust/vec-sort, r=alexcrichton
This uses quite a bit of unsafe code for speed and failure safety, and allocates `2*n` temporary storage. [Performance](https://gist.github.com/huonw/5547f2478380288a28c2): | n | new | priority_queue | quick3 | |-------:|---------:|---------------:|---------:| | 5 | 200 | 155 | 106 | | 100 | 6490 | 8750 | 5810 | | 10000 | 1300000 | 1790000 | 1060000 | | 100000 | 16700000 | 23600000 | 12700000 | | sorted | 520000 | 1380000 | 53900000 | | trend | 1310000 | 1690000 | 1100000 | (The times are in nanoseconds, having subtracted the set-up time (i.e. the `just_generate` bench target).) I imagine that there is still significant room for improvement, particularly because both priority_queue and quick3 are doing a static call via `Ord` or `TotalOrd` for the comparisons, while this is using a (boxed) closure. Also, this code does not `clone`, unlike `quick_sort3`; and is stable, unlike both of the others.
2 parents cd13f4d + 645fff4 commit 55cbef6

File tree

19 files changed

+493
-1303
lines changed

19 files changed

+493
-1303
lines changed

src/libextra/glob.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ use std::io;
2828
use std::io::fs;
2929
use std::path::is_sep;
3030

31-
use sort;
32-
3331
/**
3432
* An iterator that yields Paths from the filesystem that match a particular
3533
* pattern - see the `glob` function for more details.
@@ -149,9 +147,8 @@ impl Iterator<Path> for GlobIterator {
149147

150148
fn list_dir_sorted(path: &Path) -> ~[Path] {
151149
match io::result(|| fs::readdir(path)) {
152-
Ok(children) => {
153-
let mut children = children;
154-
sort::quick_sort(children, |p1, p2| p2.filename() <= p1.filename());
150+
Ok(mut children) => {
151+
children.sort_by(|p1, p2| p2.filename().cmp(&p1.filename()));
155152
children
156153
}
157154
Err(..) => ~[]
@@ -771,4 +768,3 @@ mod test {
771768
assert!(Pattern::new("a/b").matches_path(&Path::new("a/b")));
772769
}
773770
}
774-

src/libextra/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ pub mod ringbuf;
6161
pub mod priority_queue;
6262
pub mod smallintmap;
6363

64-
pub mod sort;
65-
6664
pub mod dlist;
6765
pub mod treemap;
6866
pub mod btree;

src/libextra/priority_queue.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ impl<T: Ord> Extendable<T> for PriorityQueue<T> {
213213

214214
#[cfg(test)]
215215
mod tests {
216-
use sort::merge_sort;
217216
use priority_queue::PriorityQueue;
218217

219218
#[test]
@@ -231,7 +230,8 @@ mod tests {
231230
#[test]
232231
fn test_top_and_pop() {
233232
let data = ~[2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
234-
let mut sorted = merge_sort(data, |x, y| x.le(y));
233+
let mut sorted = data.clone();
234+
sorted.sort();
235235
let mut heap = PriorityQueue::from_vec(data);
236236
while !heap.is_empty() {
237237
assert_eq!(heap.top(), sorted.last());
@@ -311,11 +311,14 @@ mod tests {
311311
assert_eq!(heap.len(), 5);
312312
}
313313

314-
fn check_to_vec(data: ~[int]) {
314+
fn check_to_vec(mut data: ~[int]) {
315315
let heap = PriorityQueue::from_vec(data.clone());
316-
assert_eq!(merge_sort(heap.clone().to_vec(), |x, y| x.le(y)),
317-
merge_sort(data, |x, y| x.le(y)));
318-
assert_eq!(heap.to_sorted_vec(), merge_sort(data, |x, y| x.le(y)));
316+
let mut v = heap.clone().to_vec();
317+
v.sort();
318+
data.sort();
319+
320+
assert_eq!(v, data);
321+
assert_eq!(heap.to_sorted_vec(), data);
319322
}
320323

321324
#[test]

0 commit comments

Comments
 (0)