Skip to content

Commit 3df3732

Browse files
lilyballemberian
authored andcommitted
Add methods .move_from() and .copy_from() to vec
Add method .move_from() to MutableVector, which consumes another vector and moves elements into the receiver. Add new trait MutableCloneableVector with one method .copy_from(), which clones elements from another vector into the receiver.
1 parent 524a92c commit 3df3732

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/libstd/vec.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use cast::transmute;
1616
use cast;
1717
use container::{Container, Mutable};
18+
use cmp;
1819
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
1920
use clone::Clone;
2021
use iterator::{FromIterator, Iterator, IteratorUtil};
@@ -2059,6 +2060,21 @@ pub trait MutableVector<'self, T> {
20592060
fn mut_iter(self) -> VecMutIterator<'self, T>;
20602061
fn mut_rev_iter(self) -> VecMutRevIterator<'self, T>;
20612062

2063+
/**
2064+
* Consumes `src` and moves as many elements as it can into `self`
2065+
* from the range [start,end).
2066+
*
2067+
* Returns the number of elements copied (the shorter of self.len()
2068+
* and end - start).
2069+
*
2070+
* # Arguments
2071+
*
2072+
* * src - A mutable vector of `T`
2073+
* * start - The index into `src` to start copying from
2074+
* * end - The index into `str` to stop copying from
2075+
*/
2076+
fn move_from(self, src: ~[T], start: uint, end: uint) -> uint;
2077+
20622078
unsafe fn unsafe_mut_ref(&self, index: uint) -> *mut T;
20632079
unsafe fn unsafe_set(&self, index: uint, val: T);
20642080
}
@@ -2087,6 +2103,14 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
20872103
}
20882104
}
20892105

2106+
#[inline]
2107+
fn move_from(self, mut src: ~[T], start: uint, end: uint) -> uint {
2108+
for self.mut_iter().zip(src.mut_slice(start, end).mut_iter()).advance |(a, b)| {
2109+
util::swap(a, b);
2110+
}
2111+
cmp::min(self.len(), end-start)
2112+
}
2113+
20902114
#[inline]
20912115
unsafe fn unsafe_mut_ref(&self, index: uint) -> *mut T {
20922116
let pair_ptr: &(*mut T, uint) = transmute(self);
@@ -2100,6 +2124,23 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
21002124
}
21012125
}
21022126

2127+
/// Trait for ~[T] where T is Cloneable
2128+
pub trait MutableCloneableVector<T> {
2129+
/// Copies as many elements from `src` as it can into `self`
2130+
/// (the shorter of self.len() and src.len()). Returns the number of elements copied.
2131+
fn copy_from(self, &[T]) -> uint;
2132+
}
2133+
2134+
impl<'self, T:Clone> MutableCloneableVector<T> for &'self mut [T] {
2135+
#[inline]
2136+
fn copy_from(self, src: &[T]) -> uint {
2137+
for self.mut_iter().zip(src.iter()).advance |(a, b)| {
2138+
*a = b.clone();
2139+
}
2140+
cmp::min(self.len(), src.len())
2141+
}
2142+
}
2143+
21032144
/**
21042145
* Constructs a vector from an unsafe pointer to a buffer
21052146
*
@@ -3895,6 +3936,38 @@ mod tests {
38953936
assert_eq!(xs, [5, 5, 5, 5, 5])
38963937
}
38973938

3939+
#[test]
3940+
fn test_move_from() {
3941+
let mut a = [1,2,3,4,5];
3942+
let b = ~[6,7,8];
3943+
assert_eq!(a.move_from(b, 0, 3), 3);
3944+
assert_eq!(a, [6,7,8,4,5]);
3945+
let mut a = [7,2,8,1];
3946+
let b = ~[3,1,4,1,5,9];
3947+
assert_eq!(a.move_from(b, 0, 6), 4);
3948+
assert_eq!(a, [3,1,4,1]);
3949+
let mut a = [1,2,3,4];
3950+
let b = ~[5,6,7,8,9,0];
3951+
assert_eq!(a.move_from(b, 2, 3), 1);
3952+
assert_eq!(a, [7,2,3,4]);
3953+
let mut a = [1,2,3,4,5];
3954+
let b = ~[5,6,7,8,9,0];
3955+
assert_eq!(a.mut_slice(2,4).move_from(b,1,6), 2);
3956+
assert_eq!(a, [1,2,6,7,5]);
3957+
}
3958+
3959+
#[test]
3960+
fn test_copy_from() {
3961+
let mut a = [1,2,3,4,5];
3962+
let b = [6,7,8];
3963+
assert_eq!(a.copy_from(b), 3);
3964+
assert_eq!(a, [6,7,8,4,5]);
3965+
let mut c = [7,2,8,1];
3966+
let d = [3,1,4,1,5,9];
3967+
assert_eq!(c.copy_from(d), 4);
3968+
assert_eq!(c, [3,1,4,1]);
3969+
}
3970+
38983971
#[test]
38993972
fn test_reverse_part() {
39003973
let mut values = [1,2,3,4,5];

0 commit comments

Comments
 (0)