diff --git a/src/libcore/at_vec.rs b/src/libcore/at_vec.rs index b5d6e655e4a..90a68df211a 100644 --- a/src/libcore/at_vec.rs +++ b/src/libcore/at_vec.rs @@ -78,6 +78,16 @@ pure fn build(builder: fn(push: pure fn(+A))) -> @[A] { build_sized(4, builder) } +// Appending +#[inline(always)] +pure fn append(lhs: @[T], rhs: &[const T]) -> @[T] { + do build_sized(lhs.len() + rhs.len()) |push| { + for vec::each(lhs) |x| { push(x); } + for uint::range(0, rhs.len()) |i| { push(rhs[i]); } + } +} + + /// Apply a function to each element of a vector and return the results pure fn map(v: &[T], f: fn(T) -> U) -> @[U] { do build_sized(v.len()) |push| { @@ -113,6 +123,21 @@ pure fn from_elem(n_elts: uint, t: T) -> @[T] { } } +impl extensions of vec_concat for @[T] { + #[inline(always)] + pure fn +(rhs: &[const T]) -> @[T] { + append(self, rhs) + } +} + +#[cfg(notest)] +impl extensions of add<&[const T],@[T]> for @[T] { + #[inline(always)] + pure fn add(rhs: &[const T]) -> @[T] { + append(self, rhs) + } +} + mod unsafe { type vec_repr = vec::unsafe::vec_repr; @@ -213,4 +238,10 @@ fn test() { assert seq_range(10, 15) == @[10, 11, 12, 13, 14]; assert from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5]; assert from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14]; -} \ No newline at end of file +} + +#[test] +fn append_test() { + assert @[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6]; +} + diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index fcfbb3ea135..54b78f2a482 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1211,7 +1211,7 @@ pure fn riteri(v: &[T], f: fn(uint, T)) { * The total number of permutations produced is `len(v)!`. If `v` contains * repeated elements, then some permutations are repeated. */ -pure fn permute(v: &[T], put: fn(~[T])) { +pure fn permute(v: &[const T], put: fn(~[T])) { let ln = len(v); if ln == 0u { put(~[]); @@ -1221,7 +1221,7 @@ pure fn permute(v: &[T], put: fn(~[T])) { let elt = v[i]; let mut rest = slice(v, 0u, i); unchecked { - push_all(rest, view(v, i+1u, ln)); + push_all(rest, const_view(v, i+1u, ln)); permute(rest, |permutation| { put(append(~[elt], permutation)) })