Skip to content

Commit 79b5f68

Browse files
committed
Implement + for @-vectors.
1 parent f110e8f commit 79b5f68

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/libcore/at_vec.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ pure fn build<A>(builder: fn(push: pure fn(+A))) -> @[A] {
7878
build_sized(4, builder)
7979
}
8080

81+
// Appending
82+
#[inline(always)]
83+
pure fn append<T: copy>(lhs: @[T], rhs: &[const T]) -> @[T] {
84+
do build_sized(lhs.len() + rhs.len()) |push| {
85+
for vec::each(lhs) |x| { push(x); }
86+
for uint::range(0, rhs.len()) |i| { push(rhs[i]); }
87+
}
88+
}
89+
90+
8191
/// Apply a function to each element of a vector and return the results
8292
pure fn map<T, U>(v: &[T], f: fn(T) -> U) -> @[U] {
8393
do build_sized(v.len()) |push| {
@@ -113,6 +123,21 @@ pure fn from_elem<T: copy>(n_elts: uint, t: T) -> @[T] {
113123
}
114124
}
115125

126+
impl extensions<T: copy> of vec_concat<T> for @[T] {
127+
#[inline(always)]
128+
pure fn +(rhs: &[const T]) -> @[T] {
129+
append(self, rhs)
130+
}
131+
}
132+
133+
#[cfg(notest)]
134+
impl extensions<T: copy> of add<&[const T],@[T]> for @[T] {
135+
#[inline(always)]
136+
pure fn add(rhs: &[const T]) -> @[T] {
137+
append(self, rhs)
138+
}
139+
}
140+
116141

117142
mod unsafe {
118143
type vec_repr = vec::unsafe::vec_repr;
@@ -213,4 +238,10 @@ fn test() {
213238
assert seq_range(10, 15) == @[10, 11, 12, 13, 14];
214239
assert from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5];
215240
assert from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14];
216-
}
241+
}
242+
243+
#[test]
244+
fn append_test() {
245+
assert @[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6];
246+
}
247+

src/libcore/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ pure fn riteri<T>(v: &[T], f: fn(uint, T)) {
12111211
* The total number of permutations produced is `len(v)!`. If `v` contains
12121212
* repeated elements, then some permutations are repeated.
12131213
*/
1214-
pure fn permute<T: copy>(v: &[T], put: fn(~[T])) {
1214+
pure fn permute<T: copy>(v: &[const T], put: fn(~[T])) {
12151215
let ln = len(v);
12161216
if ln == 0u {
12171217
put(~[]);
@@ -1221,7 +1221,7 @@ pure fn permute<T: copy>(v: &[T], put: fn(~[T])) {
12211221
let elt = v[i];
12221222
let mut rest = slice(v, 0u, i);
12231223
unchecked {
1224-
push_all(rest, view(v, i+1u, ln));
1224+
push_all(rest, const_view(v, i+1u, ln));
12251225
permute(rest, |permutation| {
12261226
put(append(~[elt], permutation))
12271227
})

0 commit comments

Comments
 (0)