Added vec::view, for creating subslices.

This commit is contained in:
Eric Holk 2012-05-18 16:55:22 -07:00
parent 0eed37da29
commit c568cf6099

View file

@ -22,6 +22,7 @@ export init;
export last;
export last_opt;
export slice;
export view;
export split;
export splitn;
export rsplit;
@ -254,6 +255,18 @@ fn slice<T: copy>(v: [const T], start: uint, end: uint) -> [T] {
ret result;
}
#[doc = "Return a slice that points into another slice."]
fn view<T: copy>(v: [const T]/&, start: uint, end: uint) -> [T]/&a {
assert (start <= end);
assert (end <= len(v));
unpack_slice(v) {|p, _len|
unsafe {
::unsafe::reinterpret_cast(
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
}
}
}
#[doc = "
Split the vector `v` by applying each element against the predicate `f`.
"]
@ -2029,6 +2042,15 @@ mod tests {
reserve(v, 10u);
assert capacity(v) == 10u;
}
#[test]
fn test_view() {
let v = [1, 2, 3, 4, 5];
let v = view(v, 1u, 3u);
assert(len(v) == 2u);
assert(v[0] == 2);
assert(v[1] == 3);
}
}
// Local Variables: