diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2013-05-01 08:49:48 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2013-05-01 08:49:48 -0400 |
| commit | 5ab33a297521c2d5885422bc1744f6d9dab8f3f7 (patch) | |
| tree | 7cfc8d31f53e598e33747835b17e81b19244e3c4 /src/libstd | |
| parent | f236b850c0dc4c1b925e33d1173f359605801307 (diff) | |
| download | rust-5ab33a297521c2d5885422bc1744f6d9dab8f3f7.tar.gz rust-5ab33a297521c2d5885422bc1744f6d9dab8f3f7.zip | |
correct incorrect handling of overloaded operators, exposing various other bits of rot
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/arc.rs | 8 | ||||
| -rw-r--r-- | src/libstd/rope.rs | 28 | ||||
| -rw-r--r-- | src/libstd/sort.rs | 97 | ||||
| -rw-r--r-- | src/libstd/std.rc | 7 |
4 files changed, 78 insertions, 62 deletions
diff --git a/src/libstd/arc.rs b/src/libstd/arc.rs index 027bf93b481..98d7a01b928 100644 --- a/src/libstd/arc.rs +++ b/src/libstd/arc.rs @@ -598,8 +598,8 @@ mod tests { let arc = ~RWARC(1); let arc2 = (*arc).clone(); do task::try || { - do arc2.write_downgrade |write_mode| { - do (&write_mode).write |one| { + do arc2.write_downgrade |mut write_mode| { + do write_mode.write |one| { assert!(*one == 2); } } @@ -733,8 +733,8 @@ mod tests { } // Downgrader (us) - do arc.write_downgrade |write_mode| { - do (&write_mode).write_cond |state, cond| { + do arc.write_downgrade |mut write_mode| { + do write_mode.write_cond |state, cond| { wc1.send(()); // send to another writer who will wake us up while *state == 0 { cond.wait(); diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs index 1292d2a8585..93364f8a319 100644 --- a/src/libstd/rope.rs +++ b/src/libstd/rope.rs @@ -1256,22 +1256,24 @@ mod tests { match (r) { node::Empty => return ~"", node::Content(x) => { - let str = @mut ~""; - fn aux(str: @mut ~str, node: @node::Node) { + let mut str = ~""; + fn aux(str: &mut ~str, node: @node::Node) { match (*node) { - node::Leaf(x) => { - *str += str::slice( - *x.content, x.byte_offset, - x.byte_offset + x.byte_len).to_owned(); - } - node::Concat(ref x) => { - aux(str, x.left); - aux(str, x.right); - } + node::Leaf(x) => { + str::push_str( + str, + str::slice( + *x.content, x.byte_offset, + x.byte_offset + x.byte_len)); + } + node::Concat(ref x) => { + aux(str, x.left); + aux(str, x.right); + } } } - aux(str, x); - return *str + aux(&mut str, x); + return str } } } diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index f3d30ecd5cd..119b47c904e 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -22,12 +22,12 @@ type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool; * Has worst case O(n log n) performance, best case O(n), but * is not space efficient. This is a stable sort. */ -pub fn merge_sort<T:Copy>(v: &const [T], le: Le<T>) -> ~[T] { +pub fn merge_sort<T:Copy>(v: &[T], le: Le<T>) -> ~[T] { type Slice = (uint, uint); return merge_sort_(v, (0u, len(v)), le); - fn merge_sort_<T:Copy>(v: &const [T], slice: Slice, le: Le<T>) + fn merge_sort_<T:Copy>(v: &[T], slice: Slice, le: Le<T>) -> ~[T] { let begin = slice.first(); let end = slice.second(); @@ -179,7 +179,7 @@ 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; } - let len = arr.len() - 1; // FIXME(#5074) nested calls + let len = arr.len(); // FIXME(#5074) nested calls qsort3(arr, 0, (len - 1) as int); } @@ -263,7 +263,7 @@ fn binarysort<T:Copy + Ord>(array: &mut [T], start: uint) { assert!(left == right); let n = start-left; - copy_vec(array, left+1, array, left, n); + shift_vec(array, left+1, left, n); array[left] = pivot; start += 1; } @@ -309,8 +309,8 @@ fn count_run_ascending<T:Copy + Ord>(array: &mut [T]) -> uint { return run; } -fn gallop_left<T:Copy + Ord>(key: &const T, - array: &const [T], +fn gallop_left<T:Copy + Ord>(key: &T, + array: &[T], hint: uint) -> uint { let size = array.len(); @@ -360,8 +360,8 @@ fn gallop_left<T:Copy + Ord>(key: &const T, return ofs; } -fn gallop_right<T:Copy + Ord>(key: &const T, - array: &const [T], +fn gallop_right<T:Copy + Ord>(key: &T, + array: &[T], hint: uint) -> uint { let size = array.len(); @@ -457,15 +457,15 @@ impl<T:Copy + Ord> MergeState<T> { } let k = { // constrain lifetime of slice below - let slice = vec::mut_slice(array, b1, b1+l1); - gallop_right(&const array[b2], slice, 0) + let slice = vec::slice(array, b1, b1+l1); + gallop_right(&array[b2], slice, 0) }; b1 += k; l1 -= k; if l1 != 0 { 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) + let slice = vec::slice(array, b2, b2+l2); + gallop_left(&array[b1+l1-1],slice,l2-1) }; if l2 > 0 { if l1 <= l2 { @@ -497,11 +497,11 @@ impl<T:Copy + Ord> MergeState<T> { dest += 1; c2 += 1; len2 -= 1; if len2 == 0 { - copy_vec(array, dest, tmp, 0, len1); + copy_vec(array, dest, tmp.slice(0, len1)); return; } if len1 == 1 { - copy_vec(array, dest, array, c2, len2); + shift_vec(array, dest, c2, len2); array[dest+len2] <-> tmp[c1]; return; } @@ -539,10 +539,12 @@ impl<T:Copy + Ord> MergeState<T> { loop { assert!(len1 > 1 && len2 != 0); - let tmp_view = vec::const_slice(tmp, c1, c1+len1); - count1 = gallop_right(&const array[c2], tmp_view, 0); + count1 = { + let tmp_view = vec::slice(tmp, c1, c1+len1); + gallop_right(&array[c2], tmp_view, 0) + }; if count1 != 0 { - copy_vec(array, dest, tmp, c1, count1); + copy_vec(array, dest, tmp.slice(c1, c1+count1)); dest += count1; c1 += count1; len1 -= count1; if len1 <= 1 { break_outer = true; break; } } @@ -550,10 +552,12 @@ impl<T:Copy + Ord> MergeState<T> { dest += 1; c2 += 1; len2 -= 1; if len2 == 0 { break_outer = true; break; } - let tmp_view = vec::const_slice(array, c2, c2+len2); - count2 = gallop_left(&const tmp[c1], tmp_view, 0); + count2 = { + let tmp_view = vec::slice(array, c2, c2+len2); + gallop_left(&tmp[c1], tmp_view, 0) + }; if count2 != 0 { - copy_vec(array, dest, array, c2, count2); + shift_vec(array, dest, c2, count2); dest += count2; c2 += count2; len2 -= count2; if len2 == 0 { break_outer = true; break; } } @@ -573,14 +577,14 @@ impl<T:Copy + Ord> MergeState<T> { if len1 == 1 { assert!(len2 > 0); - copy_vec(array, dest, array, c2, len2); + shift_vec(array, dest, c2, len2); array[dest+len2] <-> tmp[c1]; } else if len1 == 0 { fail!(~"Comparison violates its contract!"); } else { assert!(len2 == 0); assert!(len1 > 1); - copy_vec(array, dest, tmp, c1, len1); + copy_vec(array, dest, tmp.slice(c1, c1+len1)); } } @@ -603,13 +607,13 @@ impl<T:Copy + Ord> MergeState<T> { dest -= 1; c1 -= 1; len1 -= 1; if len1 == 0 { - copy_vec(array, dest-(len2-1), tmp, 0, len2); + copy_vec(array, dest-(len2-1), tmp.slice(0, len2)); return; } if len2 == 1 { dest -= len1; c1 -= len1; - copy_vec(array, dest+1, array, c1+1, len1); + shift_vec(array, dest+1, c1+1, len1); array[dest] <-> tmp[c2]; return; } @@ -650,12 +654,12 @@ impl<T:Copy + Ord> MergeState<T> { { // 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); + &tmp[c2], tmp_view, len1-1); } if count1 != 0 { dest -= count1; c1 -= count1; len1 -= count1; - copy_vec(array, dest+1, array, c1+1, count1); + shift_vec(array, dest+1, c1+1, count1); if len1 == 0 { break_outer = true; break; } } @@ -666,14 +670,14 @@ impl<T:Copy + Ord> MergeState<T> { let count2; { // constrain scope of tmp_view let tmp_view = vec::mut_slice(tmp, 0, len2); - count2 = len2 - gallop_left(&const array[c1], + count2 = len2 - gallop_left(&array[c1], tmp_view, len2-1); } if count2 != 0 { dest -= count2; c2 -= count2; len2 -= count2; - copy_vec(array, dest+1, tmp, c2+1, count2); + copy_vec(array, dest+1, tmp.slice(c2+1, c2+1+count2)); if len2 <= 1 { break_outer = true; break; } } array[dest] <-> array[c1]; @@ -695,14 +699,14 @@ impl<T:Copy + Ord> MergeState<T> { assert!(len1 > 0); dest -= len1; c1 -= len1; - copy_vec(array, dest+1, array, c1+1, len1); + shift_vec(array, dest+1, c1+1, len1); array[dest] <-> tmp[c2]; } else if len2 == 0 { fail!(~"Comparison violates its contract!"); } else { assert!(len1 == 0); assert!(len2 != 0); - copy_vec(array, dest-(len2-1), tmp, 0, len2); + copy_vec(array, dest-(len2-1), tmp.slice(0, len2)); } } @@ -738,21 +742,25 @@ impl<T:Copy + Ord> MergeState<T> { #[inline(always)] fn copy_vec<T:Copy>(dest: &mut [T], s1: uint, - from: &const [T], - s2: uint, - len: uint) { - assert!(s1+len <= dest.len() && s2+len <= from.len()); - - let mut slice = ~[]; - for uint::range(s2, s2+len) |i| { - slice.push(from[i]); - } + from: &[T]) { + assert!(s1+from.len() <= dest.len()); - for slice.eachi |i, v| { + for from.eachi |i, v| { dest[s1+i] = *v; } } +#[inline(always)] +fn shift_vec<T:Copy>(dest: &mut [T], + s1: uint, + s2: uint, + len: uint) { + assert!(s1+len <= dest.len()); + + let tmp = dest.slice(s2, s2+len).to_vec(); + copy_vec(dest, s1, tmp); +} + #[cfg(test)] mod test_qsort3 { use sort::*; @@ -764,8 +772,7 @@ mod test_qsort3 { quick_sort3::<int>(v1); let mut i = 0; while i < len { - // debug!(v2[i]); - assert!((v2[i] == v1[i])); + assert_eq!(v2[i], v1[i]); i += 1; } } @@ -1036,7 +1043,7 @@ mod big_tests { tabulate_managed(low, high); } - fn multiplyVec<T:Copy>(arr: &const [T], num: uint) -> ~[T] { + fn multiplyVec<T:Copy>(arr: &[T], num: uint) -> ~[T] { let size = arr.len(); let res = do vec::from_fn(num) |i| { arr[i % size] @@ -1052,7 +1059,7 @@ mod big_tests { } fn tabulate_unique(lo: uint, hi: uint) { - fn isSorted<T:Ord>(arr: &const [T]) { + fn isSorted<T:Ord>(arr: &[T]) { for uint::range(0, arr.len()-1) |i| { if arr[i] > arr[i+1] { fail!(~"Array not sorted"); @@ -1123,7 +1130,7 @@ mod big_tests { } fn tabulate_managed(lo: uint, hi: uint) { - fn isSorted<T:Ord>(arr: &const [@T]) { + fn isSorted<T:Ord>(arr: &[@T]) { for uint::range(0, arr.len()-1) |i| { if arr[i] > arr[i+1] { fail!(~"Array not sorted"); diff --git a/src/libstd/std.rc b/src/libstd/std.rc index d9af8b111bb..9d1ddb8ec54 100644 --- a/src/libstd/std.rc +++ b/src/libstd/std.rc @@ -69,7 +69,14 @@ pub mod list; pub mod priority_queue; pub mod rope; pub mod smallintmap; + +#[cfg(stage0)] +#[path="sort_stage0.rs"] +pub mod sort; + +#[cfg(not(stage0))] pub mod sort; + pub mod dlist; pub mod treemap; |
