libcore: add vec::{mut_view, const_view}.

This commit is contained in:
Erick Tryzelaar 2012-07-20 19:19:44 -07:00 committed by Graydon Hoare
parent a762c725b5
commit 9d4aab80a7

View File

@ -27,7 +27,7 @@
export last;
export last_opt;
export slice;
export view;
export view, mut_view, const_view;
export split;
export splitn;
export rsplit;
@ -325,6 +325,30 @@ fn reserve_at_least<T>(&v: ~[const T], n: uint) {
}
}
/// Return a slice that points into another slice.
pure fn mut_view<T>(v: &[mut T], start: uint, end: uint) -> &[mut T] {
assert (start <= end);
assert (end <= len(v));
do unpack_slice(v) |p, _len| {
unsafe {
::unsafe::reinterpret_cast(
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
}
}
}
/// Return a slice that points into another slice.
pure fn const_view<T>(v: &[const T], start: uint, end: uint) -> &[const T] {
assert (start <= end);
assert (end <= len(v));
do unpack_slice(v) |p, _len| {
unsafe {
::unsafe::reinterpret_cast(
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
}
}
}
/// Split the vector `v` by applying each element against the predicate `f`.
fn split<T: copy>(v: &[T], f: fn(T) -> bool) -> ~[~[T]] {
let ln = len(v);