mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 15:54:15 +00:00
Convert vec::{reverse, swap} to methods.
This commit is contained in:
parent
a890c2cbf1
commit
9e83b2fe55
@ -107,7 +107,7 @@ impl<T:Ord> PriorityQueue<T> {
|
||||
let mut end = q.len();
|
||||
while end > 1 {
|
||||
end -= 1;
|
||||
vec::swap(q.data, 0, end);
|
||||
q.data.swap(0, end);
|
||||
q.siftdown_range(0, end)
|
||||
}
|
||||
q.to_vec()
|
||||
|
@ -65,17 +65,17 @@ pub fn merge_sort<T:Copy>(v: &[T], le: Le<T>) -> ~[T] {
|
||||
|
||||
fn part<T>(arr: &mut [T], left: uint,
|
||||
right: uint, pivot: uint, compare_func: Le<T>) -> uint {
|
||||
vec::swap(arr, pivot, right);
|
||||
arr.swap(pivot, right);
|
||||
let mut storage_index: uint = left;
|
||||
let mut i: uint = left;
|
||||
while i < right {
|
||||
if compare_func(&arr[i], &arr[right]) {
|
||||
vec::swap(arr, i, storage_index);
|
||||
arr.swap(i, storage_index);
|
||||
storage_index += 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
vec::swap(arr, storage_index, right);
|
||||
arr.swap(storage_index, right);
|
||||
return storage_index;
|
||||
}
|
||||
|
||||
@ -120,29 +120,29 @@ fn qsort3<T:Copy + Ord + Eq>(arr: &mut [T], left: int, right: int) {
|
||||
j -= 1;
|
||||
}
|
||||
if i >= j { break; }
|
||||
vec::swap(arr, i as uint, j as uint);
|
||||
arr.swap(i as uint, j as uint);
|
||||
if arr[i] == v {
|
||||
p += 1;
|
||||
vec::swap(arr, p as uint, i as uint);
|
||||
arr.swap(p as uint, i as uint);
|
||||
}
|
||||
if v == arr[j] {
|
||||
q -= 1;
|
||||
vec::swap(arr, j as uint, q as uint);
|
||||
arr.swap(j as uint, q as uint);
|
||||
}
|
||||
}
|
||||
vec::swap(arr, i as uint, right as uint);
|
||||
arr.swap(i as uint, right as uint);
|
||||
j = i - 1;
|
||||
i += 1;
|
||||
let mut k: int = left;
|
||||
while k < p {
|
||||
vec::swap(arr, k as uint, j as uint);
|
||||
arr.swap(k as uint, j as uint);
|
||||
k += 1;
|
||||
j -= 1;
|
||||
if k == arr.len() as int { break; }
|
||||
}
|
||||
k = right - 1;
|
||||
while k > q {
|
||||
vec::swap(arr, i as uint, k as uint);
|
||||
arr.swap(i as uint, k as uint);
|
||||
k -= 1;
|
||||
i += 1;
|
||||
if k == 0 { break; }
|
||||
@ -259,7 +259,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 {
|
||||
vec::swap(v, i, end - i - 1);
|
||||
v.swap(i, end - i - 1);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
@ -479,7 +479,7 @@ impl<T:Copy + Ord> MergeState<T> {
|
||||
let mut len1 = len1;
|
||||
let mut len2 = len2;
|
||||
|
||||
vec::swap(array, dest, c2);
|
||||
array.swap(dest, c2);
|
||||
dest += 1; c2 += 1; len2 -= 1;
|
||||
|
||||
if len2 == 0 {
|
||||
@ -501,7 +501,7 @@ impl<T:Copy + Ord> MergeState<T> {
|
||||
loop {
|
||||
assert!(len1 > 1 && len2 != 0);
|
||||
if array[c2] < tmp[c1] {
|
||||
vec::swap(array, dest, c2);
|
||||
array.swap(dest, c2);
|
||||
dest += 1; c2 += 1; len2 -= 1;
|
||||
count2 += 1; count1 = 0;
|
||||
if len2 == 0 {
|
||||
@ -534,7 +534,7 @@ impl<T:Copy + Ord> MergeState<T> {
|
||||
dest += count1; c1 += count1; len1 -= count1;
|
||||
if len1 <= 1 { break_outer = true; break; }
|
||||
}
|
||||
vec::swap(array, dest, c2);
|
||||
array.swap(dest, c2);
|
||||
dest += 1; c2 += 1; len2 -= 1;
|
||||
if len2 == 0 { break_outer = true; break; }
|
||||
|
||||
@ -589,7 +589,7 @@ impl<T:Copy + Ord> MergeState<T> {
|
||||
let mut len1 = len1;
|
||||
let mut len2 = len2;
|
||||
|
||||
vec::swap(array, dest, c1);
|
||||
array.swap(dest, c1);
|
||||
dest -= 1; c1 -= 1; len1 -= 1;
|
||||
|
||||
if len1 == 0 {
|
||||
@ -613,7 +613,7 @@ impl<T:Copy + Ord> MergeState<T> {
|
||||
loop {
|
||||
assert!(len1 != 0 && len2 > 1);
|
||||
if tmp[c2] < array[c1] {
|
||||
vec::swap(array, dest, c1);
|
||||
array.swap(dest, c1);
|
||||
dest -= 1; c1 -= 1; len1 -= 1;
|
||||
count1 += 1; count2 = 0;
|
||||
if len1 == 0 {
|
||||
@ -666,7 +666,7 @@ impl<T:Copy + Ord> MergeState<T> {
|
||||
copy_vec(array, dest+1, tmp.slice(c2+1, c2+1+count2));
|
||||
if len2 <= 1 { break_outer = true; break; }
|
||||
}
|
||||
vec::swap(array, dest, c1);
|
||||
array.swap(dest, c1);
|
||||
dest -= 1; c1 -= 1; len1 -= 1;
|
||||
if len1 == 0 { break_outer = true; break; }
|
||||
min_gallop -= 1;
|
||||
@ -1049,7 +1049,7 @@ mod big_tests {
|
||||
fn makeRange(n: uint) -> ~[uint] {
|
||||
let one = do vec::from_fn(n) |i| { i };
|
||||
let mut two = copy one;
|
||||
vec::reverse(two);
|
||||
two.reverse();
|
||||
vec::append(two, one)
|
||||
}
|
||||
|
||||
@ -1073,7 +1073,7 @@ mod big_tests {
|
||||
tim_sort(arr); // *sort
|
||||
isSorted(arr);
|
||||
|
||||
vec::reverse(arr);
|
||||
arr.reverse();
|
||||
tim_sort(arr); // \sort
|
||||
isSorted(arr);
|
||||
|
||||
@ -1083,7 +1083,7 @@ mod big_tests {
|
||||
for 3.times {
|
||||
let i1 = rng.gen_uint_range(0, n);
|
||||
let i2 = rng.gen_uint_range(0, n);
|
||||
vec::swap(arr, i1, i2);
|
||||
arr.swap(i1, i2);
|
||||
}
|
||||
tim_sort(arr); // 3sort
|
||||
isSorted(arr);
|
||||
@ -1145,7 +1145,7 @@ mod big_tests {
|
||||
tim_sort(arr); // *sort
|
||||
isSorted(arr);
|
||||
|
||||
vec::reverse(arr);
|
||||
arr.reverse();
|
||||
tim_sort(arr); // \sort
|
||||
isSorted(arr);
|
||||
|
||||
@ -1155,7 +1155,7 @@ mod big_tests {
|
||||
for 3.times {
|
||||
let i1 = rng.gen_uint_range(0, n);
|
||||
let i2 = rng.gen_uint_range(0, n);
|
||||
vec::swap(arr, i1, i2);
|
||||
arr.swap(i1, i2);
|
||||
}
|
||||
tim_sort(arr); // 3sort
|
||||
isSorted(arr);
|
||||
|
@ -449,7 +449,7 @@ fn run_tests(opts: &TestOpts,
|
||||
debug!("using %u test tasks", concurrency);
|
||||
|
||||
let mut remaining = filtered_tests;
|
||||
vec::reverse(remaining);
|
||||
remaining.reverse();
|
||||
let mut pending = 0;
|
||||
|
||||
let (p, ch) = stream();
|
||||
|
@ -18,8 +18,7 @@ use char;
|
||||
use str;
|
||||
use str::{StrSlice};
|
||||
use kinds::Copy;
|
||||
use vec;
|
||||
use vec::{CopyableVector, ImmutableVector};
|
||||
use vec::{CopyableVector, ImmutableVector, MutableVector};
|
||||
use vec::OwnedVector;
|
||||
use num::{NumCast, Zero, One, cast, pow_with_uint};
|
||||
use f64;
|
||||
@ -257,7 +256,7 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
|
||||
_ => ()
|
||||
}
|
||||
|
||||
vec::reverse(buf);
|
||||
buf.reverse();
|
||||
|
||||
// Remember start of the fractional digits.
|
||||
// Points one beyond end of buf if none get generated,
|
||||
|
@ -590,7 +590,7 @@ impl<R: Rng> RngUtil for R {
|
||||
// invariant: elements with index >= i have been locked in place.
|
||||
i -= 1u;
|
||||
// lock element i in place.
|
||||
vec::swap(values, i, self.gen_uint_range(0u, i + 1u));
|
||||
values.swap(i, self.gen_uint_range(0u, i + 1u));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ pub fn rsplit<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] {
|
||||
}
|
||||
}
|
||||
result.push(v.slice(0u, end).to_owned());
|
||||
reverse(result);
|
||||
result.reverse();
|
||||
result
|
||||
}
|
||||
|
||||
@ -278,7 +278,7 @@ pub fn rsplitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
|
||||
}
|
||||
}
|
||||
result.push(v.slice(0u, end).to_owned());
|
||||
reverse(result);
|
||||
result.reverse();
|
||||
result
|
||||
}
|
||||
|
||||
@ -751,40 +751,10 @@ pub fn zip<T, U>(mut v: ~[T], mut u: ~[U]) -> ~[(T, U)] {
|
||||
w.push((v.pop(),u.pop()));
|
||||
i -= 1;
|
||||
}
|
||||
reverse(w);
|
||||
w.reverse();
|
||||
w
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps two elements in a vector
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * v The input vector
|
||||
* * a - The index of the first element
|
||||
* * b - The index of the second element
|
||||
*/
|
||||
#[inline]
|
||||
pub fn swap<T>(v: &mut [T], a: uint, b: uint) {
|
||||
unsafe {
|
||||
// Can't take two mutable loans from one vector, so instead just cast
|
||||
// them to their raw pointers to do the swap
|
||||
let pa: *mut T = &mut v[a];
|
||||
let pb: *mut T = &mut v[b];
|
||||
ptr::swap_ptr(pa, pb);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverse the order of elements in a vector, in place
|
||||
pub fn reverse<T>(v: &mut [T]) {
|
||||
let mut i: uint = 0;
|
||||
let ln = v.len();
|
||||
while i < ln / 2 {
|
||||
swap(v, i, ln - i - 1);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a vector with the order of elements reversed
|
||||
pub fn reversed<T:Copy>(v: &[T]) -> ~[T] {
|
||||
let mut rs: ~[T] = ~[];
|
||||
@ -840,8 +810,8 @@ pub fn each_permutation<T:Copy>(values: &[T], fun: &fn(perm : &[T]) -> bool) ->
|
||||
}
|
||||
// swap indices[k] and indices[l]; sort indices[k+1..]
|
||||
// (they're just reversed)
|
||||
vec::swap(indices, k, l);
|
||||
reverse(indices.mut_slice(k+1, length));
|
||||
indices.swap(k, l);
|
||||
indices.mut_slice(k+1, length).reverse();
|
||||
// fixup permutation based on indices
|
||||
for uint::range(k, length) |i| {
|
||||
permutation[i] = copy values[indices[i]];
|
||||
@ -1598,7 +1568,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
||||
self.push(x);
|
||||
let mut j = len;
|
||||
while j > i {
|
||||
swap(*self, j, j - 1);
|
||||
self.swap(j, j - 1);
|
||||
j -= 1;
|
||||
}
|
||||
}
|
||||
@ -1611,7 +1581,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
||||
|
||||
let mut j = i;
|
||||
while j < len - 1 {
|
||||
swap(*self, j, j + 1);
|
||||
self.swap(j, j + 1);
|
||||
j += 1;
|
||||
}
|
||||
self.pop()
|
||||
@ -1629,7 +1599,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
||||
fail!("vec::swap_remove - index %u >= length %u", index, ln);
|
||||
}
|
||||
if index < ln - 1 {
|
||||
swap(*self, index, ln - 1);
|
||||
self.swap(index, ln - 1);
|
||||
}
|
||||
self.pop()
|
||||
}
|
||||
@ -1660,7 +1630,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
||||
if !f(&self[i]) {
|
||||
deleted += 1;
|
||||
} else if deleted > 0 {
|
||||
swap(*self, i - deleted, i);
|
||||
self.swap(i - deleted, i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1772,6 +1742,10 @@ pub trait MutableVector<'self, T> {
|
||||
fn mut_iter(self) -> VecMutIterator<'self, T>;
|
||||
fn mut_rev_iter(self) -> VecMutRevIterator<'self, T>;
|
||||
|
||||
fn swap(self, a: uint, b: uint);
|
||||
|
||||
fn reverse(self);
|
||||
|
||||
/**
|
||||
* Consumes `src` and moves as many elements as it can into `self`
|
||||
* from the range [start,end).
|
||||
@ -1823,6 +1797,34 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps two elements in a vector
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * a - The index of the first element
|
||||
* * b - The index of the second element
|
||||
*/
|
||||
fn swap(self, a: uint, b: uint) {
|
||||
unsafe {
|
||||
// Can't take two mutable loans from one vector, so instead just cast
|
||||
// them to their raw pointers to do the swap
|
||||
let pa: *mut T = &mut self[a];
|
||||
let pb: *mut T = &mut self[b];
|
||||
ptr::swap_ptr(pa, pb);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverse the order of elements in a vector, in place
|
||||
fn reverse(self) {
|
||||
let mut i: uint = 0;
|
||||
let ln = self.len();
|
||||
while i < ln / 2 {
|
||||
self.swap(i, ln - i - 1);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn move_from(self, mut src: ~[T], start: uint, end: uint) -> uint {
|
||||
for self.mut_iter().zip(src.mut_slice(start, end).mut_iter()).advance |(a, b)| {
|
||||
@ -2887,7 +2889,7 @@ mod tests {
|
||||
let mut v: ~[int] = ~[10, 20];
|
||||
assert_eq!(v[0], 10);
|
||||
assert_eq!(v[1], 20);
|
||||
reverse(v);
|
||||
v.reverse();
|
||||
assert_eq!(v[0], 20);
|
||||
assert_eq!(v[1], 10);
|
||||
let v2 = reversed::<int>([10, 20]);
|
||||
@ -2900,7 +2902,7 @@ mod tests {
|
||||
let v4 = reversed::<int>([]);
|
||||
assert_eq!(v4, ~[]);
|
||||
let mut v3: ~[int] = ~[];
|
||||
reverse::<int>(v3);
|
||||
v3.reverse();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -3549,7 +3551,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_reverse_part() {
|
||||
let mut values = [1,2,3,4,5];
|
||||
reverse(values.mut_slice(1, 4));
|
||||
values.mut_slice(1, 4).reverse();
|
||||
assert_eq!(values, [1,4,3,2,5]);
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
// Tests that references to move-by-default values trigger moves when
|
||||
// they occur as part of various kinds of expressions.
|
||||
|
||||
use std::vec;
|
||||
|
||||
struct Foo<A> { f: A }
|
||||
fn guard(_s: ~str) -> bool {fail!()}
|
||||
fn touch<A>(_a: &A) {}
|
||||
@ -92,7 +90,7 @@ fn f110() {
|
||||
|
||||
fn f120() {
|
||||
let mut x = ~[~"hi", ~"ho"];
|
||||
vec::swap(x, 0, 1);
|
||||
x.swap(0, 1);
|
||||
touch(&x[0]);
|
||||
touch(&x[1]);
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
use std::vec;
|
||||
|
||||
trait Reverser {
|
||||
fn reverse(&self);
|
||||
fn reverse(v: &mut [uint]) {
|
||||
v.reverse();
|
||||
}
|
||||
|
||||
fn bar(v: &mut [uint]) {
|
||||
vec::reverse(v);
|
||||
vec::reverse(v);
|
||||
vec::reverse(v);
|
||||
reverse(v);
|
||||
reverse(v);
|
||||
reverse(v);
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
|
@ -1,15 +1,3 @@
|
||||
use std::vec;
|
||||
|
||||
trait Reverser {
|
||||
fn reverse(self);
|
||||
}
|
||||
|
||||
impl<'self> Reverser for &'self mut [uint] {
|
||||
fn reverse(self) {
|
||||
vec::reverse(self);
|
||||
}
|
||||
}
|
||||
|
||||
fn bar(v: &mut [uint]) {
|
||||
v.reverse();
|
||||
v.reverse();
|
||||
|
@ -9,11 +9,10 @@
|
||||
// except according to those terms.
|
||||
|
||||
use std::util;
|
||||
use std::vec;
|
||||
|
||||
pub fn main() {
|
||||
let mut a: ~[int] = ~[0, 1, 2, 3, 4, 5, 6];
|
||||
vec::swap(a, 2, 4);
|
||||
a.swap(2, 4);
|
||||
assert_eq!(a[2], 4);
|
||||
assert_eq!(a[4], 2);
|
||||
let mut n = 42;
|
||||
|
Loading…
Reference in New Issue
Block a user