Skip to content

Commit 9e91b6e

Browse files
committed
---
yaml --- r: 30039 b: refs/heads/incoming c: dcbeebc h: refs/heads/master i: 30037: 28914d5 30035: 2541dae 30031: 29ae7aa v: v3
1 parent e5b5f00 commit 9e91b6e

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
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: 79266c614d7fc9f3bee68f13f0ef3fbee3fda9b1
9+
refs/heads/incoming: dcbeebc801e071394874843e0e4c7509a0fb46c1
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/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] {

branches/incoming/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)