From 8a959d1a1baab9e04aad57e65db53ec9f0fdf164 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 5 Feb 2017 11:55:27 +0100 Subject: [PATCH] Code cleanup + bump dmsort version to 0.1.2 --- Cargo.lock | 2 +- Cargo.toml | 3 ++- docs/dmsort/fn.sort.html | 2 +- docs/dmsort/fn.sort_by.html | 2 +- docs/dmsort/fn.sort_by_key.html | 2 +- docs/dmsort/fn.sort_copy.html | 2 +- docs/src/dmsort/dmsort.rs.html | 44 +++++++++------------------------ src/dmsort.rs | 34 +++++++++---------------- tests/benchmark.rs | 2 +- 9 files changed, 32 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6297e36..640ec7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "dmsort" -version = "0.1.1" +version = "0.1.2" dependencies = [ "gnuplot 0.0.22 (registry+https://github.com/rust-lang/crates.io-index)", "pbr 1.0.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 20ae712..9e59a8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dmsort" -version = "0.1.1" +version = "0.1.2" authors = ["Emil Ernerfeldt "] description = """ Fast adaptive sorting for when most of your data is already in order. @@ -10,6 +10,7 @@ documentation = "https://emilk.github.io/drop-merge-sort/dmsort/index.html" repository = "https://github.com/emilk/drop-merge-sort" readme = "README.md" keywords = ["sorting", "sort", "adaptive"] +categories = ["algorithms"] license = "MIT" include = [ "**/*.rs", "Cargo.toml", ] diff --git a/docs/dmsort/fn.sort.html b/docs/dmsort/fn.sort.html index c78c282..47ced44 100644 --- a/docs/dmsort/fn.sort.html +++ b/docs/dmsort/fn.sort.html @@ -47,7 +47,7 @@

Function dmsort:: [] - [src]

+ [src]
pub fn sort<T: Ord>(slice: &mut [T])

Sorts the elements using the Ord trait.

Examples

diff --git a/docs/dmsort/fn.sort_by.html b/docs/dmsort/fn.sort_by.html index 8835c45..e950234 100644 --- a/docs/dmsort/fn.sort_by.html +++ b/docs/dmsort/fn.sort_by.html @@ -47,7 +47,7 @@

Function dmsort:: [] - [src]

+ [src]
pub fn sort_by<T, F>(slice: &mut [T], compare: F) where F: FnMut(&T, &T) -> Ordering

Sorts the elements using the given compare function.

Examples

diff --git a/docs/dmsort/fn.sort_by_key.html b/docs/dmsort/fn.sort_by_key.html index 83a9806..d388ef1 100644 --- a/docs/dmsort/fn.sort_by_key.html +++ b/docs/dmsort/fn.sort_by_key.html @@ -47,7 +47,7 @@

Function dmsort:: [] - [src]

+ [src]
pub fn sort_by_key<T, K, F>(slice: &mut [T], key: F) where K: Ord, F: FnMut(&T) -> K

Sorts the elements using the given key function.

Examples

diff --git a/docs/dmsort/fn.sort_copy.html b/docs/dmsort/fn.sort_copy.html index 232c37b..418eebf 100644 --- a/docs/dmsort/fn.sort_copy.html +++ b/docs/dmsort/fn.sort_copy.html @@ -47,7 +47,7 @@

Function dmsort:: [] - [src]

+ [src]
pub fn sort_copy<T: Copy + Ord>(slice: &mut [T]) -> usize

UNSTABLE! FOR INTERNAL USE ONLY.

diff --git a/docs/src/dmsort/dmsort.rs.html b/docs/src/dmsort/dmsort.rs.html index 308ce35..a4de5bf 100644 --- a/docs/src/dmsort/dmsort.rs.html +++ b/docs/src/dmsort/dmsort.rs.html @@ -427,16 +427,6 @@ 383 384 385 -386 -387 -388 -389 -390 -391 -392 -393 -394 -395
 // Copyright (c) 2017 Emil Ernerfeldt
 
@@ -470,24 +460,6 @@
 
 // ----------------------------------------------------------------------------
 
-/// Helper function.
-#[inline(always)]
-fn max_in_slice<T, F>(slice: &[T], mut compare: F) -> &T
-	where F: FnMut(&T, &T) -> Ordering
-{
-	// slice.iter().max_by(|a, b| compare(a, b)).unwrap() // Not yet stable :(
-	debug_assert!(!slice.is_empty());
-	let mut max = &slice[0];
-	for value in slice.iter().skip(1) {
-		if compare(max, value) == Ordering::Less {
-			max = value;
-		}
-	}
-	max
-}
-
-// ----------------------------------------------------------------------------
-
 /// This is the readable reference implementation that only works for Copy types.
 /// Returns the number of dropped elements for diagnostic purposes.
 fn sort_copy_by<T, F>(slice: &mut [T], mut compare: F) -> usize
@@ -507,10 +479,13 @@
 	let mut num_dropped_in_row = 0;
 	let mut write = 0; // Index of where to write the next element to keep.
 	let mut read  = 0; // Index of the input stream.
+	let mut iteration = 0;
+	let ealy_out_stop = slice.len() / EARLY_OUT_TEST_AT;
 
 	while read < slice.len() {
+		iteration += 1;
 		if EARLY_OUT
-			&& read == slice.len() / EARLY_OUT_TEST_AT
+			&& iteration == ealy_out_stop
 			&& dropped.len() as f32 > read as f32 * EARLY_OUT_DISORDER_FRACTION {
 			// We have seen a lot of the elements and dropped a lot of them.
 			// This doesn't look good. Abort.
@@ -584,7 +559,8 @@
 
 				if FAST_BACKTRACKING {
 					// Back-track until we can accept at least one of the recently dropped elements:
-					let max_of_dropped = max_in_slice(&slice[read..(read + num_dropped_in_row + 1)], |a, b| compare(a, b));
+					let max_of_dropped = slice[read..(read + num_dropped_in_row + 1)]
+						.iter().max_by(|a, b| compare(a, b)).unwrap();
 
 					while 1 <= write && compare(max_of_dropped, &slice[write - 1]) == Ordering::Less {
 						num_backtracked += 1;
@@ -696,10 +672,13 @@
 
 	let mut num_dropped_in_row = 0;
 	let mut read = 0;
+	let mut iteration = 0;
+	let ealy_out_stop = s.slice.len() / EARLY_OUT_TEST_AT;
 
 	while read < s.slice.len() {
+		iteration += 1;
 		if EARLY_OUT
-			&& read == s.slice.len() / EARLY_OUT_TEST_AT
+			&& iteration == ealy_out_stop
 			&& s.dropped.len() as f32 > read as f32 * EARLY_OUT_DISORDER_FRACTION {
 			// We have seen a lot of the elements and dropped a lot of them.
 			// This doesn't look good. Abort.
@@ -744,7 +723,8 @@
 
 				if FAST_BACKTRACKING {
 					// Back-track until we can accept at least one of the recently dropped elements:
-					let max_of_dropped = max_in_slice(&s.slice[read..(read + num_dropped_in_row + 1)], |a, b| compare(a, b));
+					let max_of_dropped = s.slice[read..(read + num_dropped_in_row + 1)]
+						.iter().max_by(|a, b| compare(a, b)).unwrap();
 
 					while 1 <= s.write && compare(max_of_dropped, s.slice.get_unchecked(s.write - 1)) == Ordering::Less {
 						num_backtracked += 1;
diff --git a/src/dmsort.rs b/src/dmsort.rs
index 882f329..6aba5dc 100644
--- a/src/dmsort.rs
+++ b/src/dmsort.rs
@@ -30,24 +30,6 @@ const EARLY_OUT_DISORDER_FRACTION: f32 = 0.60;
 
 // ----------------------------------------------------------------------------
 
-/// Helper function.
-#[inline(always)]
-fn max_in_slice(slice: &[T], mut compare: F) -> &T
-	where F: FnMut(&T, &T) -> Ordering
-{
-	// slice.iter().max_by(|a, b| compare(a, b)).unwrap() // Not yet stable :(
-	debug_assert!(!slice.is_empty());
-	let mut max = &slice[0];
-	for value in slice.iter().skip(1) {
-		if compare(max, value) == Ordering::Less {
-			max = value;
-		}
-	}
-	max
-}
-
-// ----------------------------------------------------------------------------
-
 /// This is the readable reference implementation that only works for Copy types.
 /// Returns the number of dropped elements for diagnostic purposes.
 fn sort_copy_by(slice: &mut [T], mut compare: F) -> usize
@@ -67,10 +49,13 @@ fn sort_copy_by(slice: &mut [T], mut compare: F) -> usize
 	let mut num_dropped_in_row = 0;
 	let mut write = 0; // Index of where to write the next element to keep.
 	let mut read  = 0; // Index of the input stream.
+	let mut iteration = 0;
+	let ealy_out_stop = slice.len() / EARLY_OUT_TEST_AT;
 
 	while read < slice.len() {
+		iteration += 1;
 		if EARLY_OUT
-			&& read == slice.len() / EARLY_OUT_TEST_AT
+			&& iteration == ealy_out_stop
 			&& dropped.len() as f32 > read as f32 * EARLY_OUT_DISORDER_FRACTION {
 			// We have seen a lot of the elements and dropped a lot of them.
 			// This doesn't look good. Abort.
@@ -144,7 +129,8 @@ fn sort_copy_by(slice: &mut [T], mut compare: F) -> usize
 
 				if FAST_BACKTRACKING {
 					// Back-track until we can accept at least one of the recently dropped elements:
-					let max_of_dropped = max_in_slice(&slice[read..(read + num_dropped_in_row + 1)], |a, b| compare(a, b));
+					let max_of_dropped = slice[read..(read + num_dropped_in_row + 1)]
+						.iter().max_by(|a, b| compare(a, b)).unwrap();
 
 					while 1 <= write && compare(max_of_dropped, &slice[write - 1]) == Ordering::Less {
 						num_backtracked += 1;
@@ -256,10 +242,13 @@ fn sort_move_by(slice: &mut [T], mut compare: F)
 
 	let mut num_dropped_in_row = 0;
 	let mut read = 0;
+	let mut iteration = 0;
+	let ealy_out_stop = s.slice.len() / EARLY_OUT_TEST_AT;
 
 	while read < s.slice.len() {
+		iteration += 1;
 		if EARLY_OUT
-			&& read == s.slice.len() / EARLY_OUT_TEST_AT
+			&& iteration == ealy_out_stop
 			&& s.dropped.len() as f32 > read as f32 * EARLY_OUT_DISORDER_FRACTION {
 			// We have seen a lot of the elements and dropped a lot of them.
 			// This doesn't look good. Abort.
@@ -304,7 +293,8 @@ fn sort_move_by(slice: &mut [T], mut compare: F)
 
 				if FAST_BACKTRACKING {
 					// Back-track until we can accept at least one of the recently dropped elements:
-					let max_of_dropped = max_in_slice(&s.slice[read..(read + num_dropped_in_row + 1)], |a, b| compare(a, b));
+					let max_of_dropped = s.slice[read..(read + num_dropped_in_row + 1)]
+						.iter().max_by(|a, b| compare(a, b)).unwrap();
 
 					while 1 <= s.write && compare(max_of_dropped, s.slice.get_unchecked(s.write - 1)) == Ordering::Less {
 						num_backtracked += 1;
diff --git a/tests/benchmark.rs b/tests/benchmark.rs
index fb293dc..950aa3b 100644
--- a/tests/benchmark.rs
+++ b/tests/benchmark.rs
@@ -81,7 +81,7 @@ fn benchmark_and_plot(rng: &mut rand::StdRng,
 	pb.message(&format!("Benchmarking {} {}: ", length_str, element_type_long));
 
 	let mut std_ms_list         = vec![];
-	let mut pdq_ms_list     = vec![];
+	let mut pdq_ms_list         = vec![];
 	let mut quicker_ms_list     = vec![];
 	let mut dmsort_ms_list      = vec![];
 	let mut dmsort_speedup_list = vec![];