Skip to content

Commit dcbeebc

Browse files
committed
Make vec::zip not require T:copy (close #3254)
1 parent 79266c6 commit dcbeebc

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/libcore/tuple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<A: copy, B: copy> (&[A], &[B]): ExtendedTupleOps<A,B> {
4141

4242
fn zip() -> ~[(A, B)] {
4343
let (a, b) = self;
44-
vec::zip(a, b)
44+
vec::zip_slice(a, b)
4545
}
4646

4747
fn map<C>(f: fn(A, B) -> C) -> ~[C] {

src/libcore/vec.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export position_elem;
7070
export rposition;
7171
export rposition_between;
7272
export unzip;
73-
export zip;
73+
export zip, zip_slice;
7474
export swap;
7575
export reverse;
7676
export reversed;
@@ -1019,14 +1019,9 @@ pure fn rposition_between<T>(v: &[T], start: uint, end: uint,
10191019
// return a nominal record with a constraint saying that, instead of
10201020
// returning a tuple (contingent on issue #869)
10211021
/**
1022-
* Convert a vector of pairs into a pair of vectors
1023-
*
1024-
* Returns a tuple containing two vectors where the i-th element of the first
1025-
* vector contains the first element of the i-th tuple of the input vector,
1026-
* and the i-th element of the second vector contains the second element
1027-
* of the i-th tuple of the input vector.
1022+
* Convert a vector of pairs into a pair of vectors, by reference. As unzip().
10281023
*/
1029-
pure fn unzip<T: copy, U: copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
1024+
pure fn unzip_slice<T: copy, U: copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
10301025
let mut as = ~[], bs = ~[];
10311026
for each(v) |p| {
10321027
let (a, b) = p;
@@ -1039,12 +1034,30 @@ pure fn unzip<T: copy, U: copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
10391034
}
10401035

10411036
/**
1042-
* Convert two vectors to a vector of pairs
1037+
* Convert a vector of pairs into a pair of vectors.
10431038
*
1044-
* Returns a vector of tuples, where the i-th tuple contains contains the
1045-
* i-th elements from each of the input vectors.
1039+
* Returns a tuple containing two vectors where the i-th element of the first
1040+
* vector contains the first element of the i-th tuple of the input vector,
1041+
* and the i-th element of the second vector contains the second element
1042+
* of the i-th tuple of the input vector.
10461043
*/
1047-
pure fn zip<T: copy, U: copy>(v: &[const T], u: &[const U]) -> ~[(T, U)] {
1044+
pure fn unzip<T,U>(+v: ~[(T, U)]) -> (~[T], ~[U]) {
1045+
let mut ts = ~[], us = ~[];
1046+
unchecked {
1047+
do consume(v) |_i, p| {
1048+
let (a,b) = p;
1049+
push(ts, a);
1050+
push(us, b);
1051+
}
1052+
}
1053+
(ts, us)
1054+
}
1055+
1056+
/**
1057+
* Convert two vectors to a vector of pairs, by reference. As zip().
1058+
*/
1059+
pure fn zip_slice<T: copy, U: copy>(v: &[const T], u: &[const U])
1060+
-> ~[(T, U)] {
10481061
let mut zipped = ~[];
10491062
let sz = len(v);
10501063
let mut i = 0u;
@@ -1053,6 +1066,24 @@ pure fn zip<T: copy, U: copy>(v: &[const T], u: &[const U]) -> ~[(T, U)] {
10531066
return zipped;
10541067
}
10551068

1069+
/**
1070+
* Convert two vectors to a vector of pairs.
1071+
*
1072+
* Returns a vector of tuples, where the i-th tuple contains contains the
1073+
* i-th elements from each of the input vectors.
1074+
*/
1075+
pure fn zip<T, U>(+v: ~[const T], +u: ~[const U]) -> ~[(T, U)] {
1076+
let mut v = v, u = u, i = len(v);
1077+
assert i == len(u);
1078+
let mut w = ~[mut];
1079+
while i > 0 {
1080+
unchecked { push(w, (pop(v),pop(u))); }
1081+
i -= 1;
1082+
}
1083+
unchecked { reverse(w); }
1084+
from_mut(w)
1085+
}
1086+
10561087
/**
10571088
* Swaps two elements in a vector
10581089
*

0 commit comments

Comments
 (0)