mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-08 07:57:40 +00:00
Implement + for @-vectors.
This commit is contained in:
parent
f110e8f21c
commit
79b5f68176
@ -78,6 +78,16 @@ pure fn build<A>(builder: fn(push: pure fn(+A))) -> @[A] {
|
|||||||
build_sized(4, builder)
|
build_sized(4, builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Appending
|
||||||
|
#[inline(always)]
|
||||||
|
pure fn append<T: copy>(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
|
/// Apply a function to each element of a vector and return the results
|
||||||
pure fn map<T, U>(v: &[T], f: fn(T) -> U) -> @[U] {
|
pure fn map<T, U>(v: &[T], f: fn(T) -> U) -> @[U] {
|
||||||
do build_sized(v.len()) |push| {
|
do build_sized(v.len()) |push| {
|
||||||
@ -113,6 +123,21 @@ pure fn from_elem<T: copy>(n_elts: uint, t: T) -> @[T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl extensions<T: copy> of vec_concat<T> for @[T] {
|
||||||
|
#[inline(always)]
|
||||||
|
pure fn +(rhs: &[const T]) -> @[T] {
|
||||||
|
append(self, rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(notest)]
|
||||||
|
impl extensions<T: copy> of add<&[const T],@[T]> for @[T] {
|
||||||
|
#[inline(always)]
|
||||||
|
pure fn add(rhs: &[const T]) -> @[T] {
|
||||||
|
append(self, rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
mod unsafe {
|
mod unsafe {
|
||||||
type vec_repr = vec::unsafe::vec_repr;
|
type vec_repr = vec::unsafe::vec_repr;
|
||||||
@ -214,3 +239,9 @@ fn test() {
|
|||||||
assert from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5];
|
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];
|
assert from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn append_test() {
|
||||||
|
assert @[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6];
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1211,7 +1211,7 @@ pure fn riteri<T>(v: &[T], f: fn(uint, T)) {
|
|||||||
* The total number of permutations produced is `len(v)!`. If `v` contains
|
* The total number of permutations produced is `len(v)!`. If `v` contains
|
||||||
* repeated elements, then some permutations are repeated.
|
* repeated elements, then some permutations are repeated.
|
||||||
*/
|
*/
|
||||||
pure fn permute<T: copy>(v: &[T], put: fn(~[T])) {
|
pure fn permute<T: copy>(v: &[const T], put: fn(~[T])) {
|
||||||
let ln = len(v);
|
let ln = len(v);
|
||||||
if ln == 0u {
|
if ln == 0u {
|
||||||
put(~[]);
|
put(~[]);
|
||||||
@ -1221,7 +1221,7 @@ pure fn permute<T: copy>(v: &[T], put: fn(~[T])) {
|
|||||||
let elt = v[i];
|
let elt = v[i];
|
||||||
let mut rest = slice(v, 0u, i);
|
let mut rest = slice(v, 0u, i);
|
||||||
unchecked {
|
unchecked {
|
||||||
push_all(rest, view(v, i+1u, ln));
|
push_all(rest, const_view(v, i+1u, ln));
|
||||||
permute(rest, |permutation| {
|
permute(rest, |permutation| {
|
||||||
put(append(~[elt], permutation))
|
put(append(~[elt], permutation))
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user