@@ -70,7 +70,7 @@ export position_elem;
70
70
export rposition;
71
71
export rposition_between;
72
72
export unzip;
73
- export zip;
73
+ export zip, zip_slice ;
74
74
export swap;
75
75
export reverse;
76
76
export reversed;
@@ -1019,14 +1019,9 @@ pure fn rposition_between<T>(v: &[T], start: uint, end: uint,
1019
1019
// return a nominal record with a constraint saying that, instead of
1020
1020
// returning a tuple (contingent on issue #869)
1021
1021
/**
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().
1028
1023
*/
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 ] ) {
1030
1025
let mut as = ~[ ] , bs = ~[ ] ;
1031
1026
for each( v) |p| {
1032
1027
let ( a, b) = p;
@@ -1039,12 +1034,30 @@ pure fn unzip<T: copy, U: copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
1039
1034
}
1040
1035
1041
1036
/**
1042
- * Convert two vectors to a vector of pairs
1037
+ * Convert a vector of pairs into a pair of vectors.
1043
1038
*
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.
1046
1043
*/
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 ) ] {
1048
1061
let mut zipped = ~[ ] ;
1049
1062
let sz = len ( v) ;
1050
1063
let mut i = 0 u;
@@ -1053,6 +1066,24 @@ pure fn zip<T: copy, U: copy>(v: &[const T], u: &[const U]) -> ~[(T, U)] {
1053
1066
return zipped;
1054
1067
}
1055
1068
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
+
1056
1087
/**
1057
1088
* Swaps two elements in a vector
1058
1089
*
0 commit comments