about summary refs log tree commit diff
path: root/src/libstd/sort.rs
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2013-03-15 15:24:24 -0400
committerNiko Matsakis <niko@alum.mit.edu>2013-04-30 06:59:32 -0400
commita896440ca1b93cc5dc6edd827ea2ae89602bfa9e (patch)
treec6945d51bf84faeb9be6ac32247c8ffa2cd39226 /src/libstd/sort.rs
parentb5a7e8b35322869b1cf51bd1b35a86e9e721da54 (diff)
downloadrust-a896440ca1b93cc5dc6edd827ea2ae89602bfa9e.tar.gz
rust-a896440ca1b93cc5dc6edd827ea2ae89602bfa9e.zip
new borrow checker (mass squash)
Diffstat (limited to 'src/libstd/sort.rs')
-rw-r--r--src/libstd/sort.rs70
1 files changed, 49 insertions, 21 deletions
diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs
index 3e6011e80df..c153d7f22c0 100644
--- a/src/libstd/sort.rs
+++ b/src/libstd/sort.rs
@@ -61,6 +61,7 @@ pub fn merge_sort<T:Copy>(v: &const [T], le: Le<T>) -> ~[T] {
     }
 }
 
+#[cfg(stage0)]
 fn part<T>(arr: &mut [T], left: uint,
            right: uint, pivot: uint, compare_func: Le<T>) -> uint {
     arr[pivot] <-> arr[right];
@@ -79,6 +80,23 @@ fn part<T>(arr: &mut [T], left: uint,
     return storage_index;
 }
 
+#[cfg(not(stage0))]
+fn part<T>(arr: &mut [T], left: uint,
+           right: uint, pivot: uint, compare_func: Le<T>) -> uint {
+    arr[pivot] <-> arr[right];
+    let mut storage_index: uint = left;
+    let mut i: uint = left;
+    while i < right {
+        if compare_func(&arr[i], &arr[right]) {
+            arr[i] <-> arr[storage_index];
+            storage_index += 1;
+        }
+        i += 1;
+    }
+    arr[storage_index] <-> arr[right];
+    return storage_index;
+}
+
 fn qsort<T>(arr: &mut [T], left: uint,
             right: uint, compare_func: Le<T>) {
     if right > left {
@@ -162,7 +180,8 @@ fn qsort3<T:Copy + Ord + Eq>(arr: &mut [T], left: int, right: int) {
  */
 pub fn quick_sort3<T:Copy + Ord + Eq>(arr: &mut [T]) {
     if arr.len() <= 1 { return; }
-    qsort3(arr, 0, (arr.len() - 1) as int);
+    let len = arr.len() - 1; // FIXME(#5074) nested calls
+    qsort3(arr, 0, (len - 1) as int);
 }
 
 pub trait Sort {
@@ -195,15 +214,20 @@ pub fn tim_sort<T:Copy + Ord>(array: &mut [T]) {
     let mut idx = 0;
     let mut remaining = size;
     loop {
-        let arr = vec::mut_slice(array, idx, size);
-        let mut run_len: uint = count_run_ascending(arr);
-
-        if run_len < min_run {
-            let force = if remaining <= min_run {remaining} else {min_run};
-            let slice = vec::mut_slice(arr, 0, force);
-            binarysort(slice, run_len);
-            run_len = force;
-        }
+        let run_len: uint = {
+            // This scope contains the slice `arr` here:
+            let arr = vec::mut_slice(array, idx, size);
+            let mut run_len: uint = count_run_ascending(arr);
+
+            if run_len < min_run {
+                let force = if remaining <= min_run {remaining} else {min_run};
+                let slice = vec::mut_slice(arr, 0, force);
+                binarysort(slice, run_len);
+                run_len = force;
+            }
+
+            run_len
+        };
 
         ms.push_run(idx, run_len);
         ms.merge_collapse(array);
@@ -250,7 +274,7 @@ fn binarysort<T:Copy + Ord>(array: &mut [T], start: uint) {
 fn reverse_slice<T>(v: &mut [T], start: uint, end:uint) {
     let mut i = start;
     while i < end / 2 {
-        util::swap(&mut v[i], &mut v[end - i - 1]);
+        v[i] <-> v[end - i - 1];
         i += 1;
     }
 }
@@ -433,14 +457,17 @@ impl<T:Copy + Ord> MergeState<T> {
             self.runs[n+1].len = self.runs[n+2].len;
         }
 
-        let slice = vec::mut_slice(array, b1, b1+l1);
-        let k = gallop_right(&const array[b2], slice, 0);
+        let k = { // constrain lifetime of slice below
+            let slice = vec::mut_slice(array, b1, b1+l1);
+            gallop_right(&const array[b2], slice, 0)
+        };
         b1 += k;
         l1 -= k;
         if l1 != 0 {
-            let slice = vec::mut_slice(array, b2, b2+l2);
-            let l2 = gallop_left(
-                &const array[b1+l1-1],slice,l2-1);
+            let l2 = { // constrain lifetime of slice below
+                let slice = vec::mut_slice(array, b2, b2+l2);
+                gallop_left(&const array[b1+l1-1],slice,l2-1)
+            };
             if l2 > 0 {
                 if l1 <= l2 {
                     self.merge_lo(array, b1, l1, b2, l2);
@@ -621,9 +648,11 @@ impl<T:Copy + Ord> MergeState<T> {
             loop {
                 assert!(len2 > 1 && len1 != 0);
 
-                let tmp_view = vec::mut_slice(array, base1, base1+len1);
-                count1 = len1 - gallop_right(
-                    &const tmp[c2], tmp_view, len1-1);
+                { // constrain scope of tmp_view:
+                    let tmp_view = vec::mut_slice (array, base1, base1+len1);
+                    count1 = len1 - gallop_right(
+                        &const tmp[c2], tmp_view, len1-1);
+                }
 
                 if count1 != 0 {
                     dest -= count1; c1 -= count1; len1 -= count1;
@@ -636,12 +665,11 @@ impl<T:Copy + Ord> MergeState<T> {
                 if len2 == 1 { break_outer = true; break; }
 
                 let count2;
-                {
+                { // constrain scope of tmp_view
                     let tmp_view = vec::mut_slice(tmp, 0, len2);
                     count2 = len2 - gallop_left(&const array[c1],
                                                 tmp_view,
                                                 len2-1);
-                    // Make tmp_view go out of scope to appease borrowck.
                 }
 
                 if count2 != 0 {