Skip to content

Commit 76f177e

Browse files
committed
---
yaml --- r: 23439 b: refs/heads/master c: dcbeebc h: refs/heads/master i: 23437: 75a7b2c 23435: 63655ab 23431: 460013a 23423: b499a73 v: v3
1 parent eff4cd6 commit 76f177e

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 79266c614d7fc9f3bee68f13f0ef3fbee3fda9b1
2+
refs/heads/master: dcbeebc801e071394874843e0e4c7509a0fb46c1
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/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] {

trunk/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)