From ad66d72e6c0f77d7c6a094cf3fc5799b65824c87 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 28 Oct 2011 13:56:01 -0700 Subject: [PATCH] stdlib: Make merge_sort take [mutable? T] --- src/lib/sort.rs | 5 +++-- src/test/stdtest/sort.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib/sort.rs b/src/lib/sort.rs index 6d4ead4c429..a4fe4cf3f8b 100644 --- a/src/lib/sort.rs +++ b/src/lib/sort.rs @@ -20,7 +20,7 @@ Merge sort. Returns a new vector containing the sorted list. Has worst case O(n log n) performance, best case O(n), but is not space efficient. This is a stable sort. */ -fn merge_sort(le: lteq, v: [T]) -> [T] { +fn merge_sort(le: lteq, v: [mutable? T]) -> [T] { fn merge(le: lteq, a: [T], b: [T]) -> [T] { let rs: [T] = []; let a_len: uint = len::(a); @@ -38,7 +38,8 @@ fn merge_sort(le: lteq, v: [T]) -> [T] { ret rs; } let v_len: uint = len::(v); - if v_len <= 1u { ret v; } + if v_len == 0u { ret []; } + if v_len == 1u { ret [v[0]]; } let mid: uint = v_len / 2u; let a: [T] = slice::(v, 0u, mid); let b: [T] = slice::(v, mid, v_len); diff --git a/src/test/stdtest/sort.rs b/src/test/stdtest/sort.rs index ff86042e6b1..9e362f0c988 100644 --- a/src/test/stdtest/sort.rs +++ b/src/test/stdtest/sort.rs @@ -26,3 +26,11 @@ fn test() { check_sort(v1, v2); } } + +#[test] +fn test_merge_sort_mutable() { + fn lteq(&&a: int, &&b: int) -> bool { ret a <= b; } + let v1 = [mutable 3, 2, 1]; + let v2 = std::sort::merge_sort(lteq, v1); + assert v2 == [1, 2, 3]; +} \ No newline at end of file